Module ActsAsFileSystem::ClassMethods
In: lib/acts_as_filesystem.rb

Methods

Included Modules

ActsAsFileSystem::InstanceMethods

Public Instance methods

Declares the ActiveRecord model to acts like a filesystem: objects are arranged in a tree (liks acts_as_tree), and . The underlying table must have the following fields:

  • name (+:string+) - the title of the object
  • slug (+:string+)- the title turned in a URL-friendly string (downcased, non-ascii chars transliterated into ascii, all sequences of non-alphanumericd characters changed into dashed)
  • path (+:text+)- stores the full path of the object (the full path of the parent, a "/" and the slug of the object)
  • children_count - a cache of the number of children elements.

[Source]

    # File lib/acts_as_filesystem.rb, line 16
16:     def acts_as_filesystem
17: 
18:       include ActsAsFileSystem::InstanceMethods
19: 
20:       # a filesystem is a tree
21:       acts_as_tree :order => 'name', :counter_cache => :children_count
22: 
23:       # calculate the right path
24:       before_create do |record|
25:         if record.path == record.slug && (! record.top_level?)
26:           record.path = record.calculate_path
27:         end
28:         true
29:       end
30: 
31:       # when renaming a category, all children categories must have their paths
32:       # recalculated
33:       after_update do |record|
34:         if record.recalculate_path
35:           record.children.each do |item|
36:             item.path = item.calculate_path
37:             item.recalculate_path = true
38:             item.save!
39:           end
40:         end
41:         record.recalculate_path = false
42:         true
43:       end
44: 
45:     end

[Validate]