Sha256: f06b00c8383639a32978eee20046cd4012a0ebef290810bad132d0da980e5d8f

Contents?: true

Size: 1.26 KB

Versions: 6

Compression:

Stored size: 1.26 KB

Contents

class Folder < ActiveRecord::Base
  has_many :templates, :dependent => :destroy
  
  validates :basename, :presence => true, :uniqueness => { :scope => :parent_id }
  
  acts_as_nested_set :depth_column => 'depth', :counter_cache => :children_count
  attr_protected :lft, :rgt  
  
  before_validation :update_path
  after_save :update_children,  :if => Proc.new { |folder| folder.pathname_changed? or folder.basename_changed? }
  after_save :update_templates, :if => Proc.new { |folder| folder.pathname_changed? or folder.basename_changed? }
  
  default_scope order(:lft)  
  
  def to_s
    self.fullname
    # self.tree_label
  end
  
  def tree_label
    "#{'&#160;&#160;&#160;&#160;' * self.level} |--#{self.basename}".html_safe
  end
  
  def fullname
    if self.basename == "/"
      "/"
    else
      "#{self.pathname}#{self.basename}/"
    end
  end 
  
  def update_path
    if self.parent
      self.pathname = self.parent.fullname
    end
  end
  
  def update_path!
    self.update_path
    self.save
  end
  
  def update_children
    self.children.each do |folder|
      folder.update_path!
    end
  end
  
  def update_templates
    self.templates.each do |template|
      template.update_pathname!
    end
  end
  
  def self.root_folders
    self.roots
  end  
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ecm_cms_core-0.0.8 lib/generators/ecm/cms_core/models/templates/folder.rb
ecm_cms_core-0.0.7 lib/generators/ecm/cms_core/models/templates/folder.rb
ecm_cms_core-0.0.6 lib/generators/ecm/cms_core/models/templates/folder.rb
ecm_cms_core-0.0.5 lib/generators/ecm/cms_core/models/templates/folder.rb
ecm_cms_core-0.0.3 lib/generators/ecm/cms_core/models/templates/folder.rb
ecm_cms_core-0.0.2 lib/generators/ecm/cms_core/models/templates/folder.rb