Sha256: b14703e1fd1c7df63587a1f50d676c0367e77a45e02b1fa3bdf208f7a80a8d25

Contents?: true

Size: 1.58 KB

Versions: 6

Compression:

Stored size: 1.58 KB

Contents

module Ecm
  module CmsCore
    class Folder < ActiveRecord::Base
      self.table_name = 'ecm_cms_core_folders'
      
      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
  end
end    
    

Version data entries

6 entries across 6 versions & 1 rubygems

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