Commit 4d1b6e6745061d6853885d933bdc4c01a4acf0e8
1 parent
9cd86716
Exists in
master
and in
29 other branches
Move path methods into PathMethods
Showing
1 changed file
with
80 additions
and
67 deletions
Show diff stats
lib/acts_as_filesystem.rb
| ... | ... | @@ -17,12 +17,15 @@ module ActsAsFileSystem |
| 17 | 17 | # a filesystem is a tree |
| 18 | 18 | acts_as_tree :counter_cache => :children_count |
| 19 | 19 | |
| 20 | - include InstanceMethods | |
| 21 | 20 | extend ClassMethods |
| 21 | + include InstanceMethods | |
| 22 | + if self.has_path? | |
| 23 | + after_update :update_children_path | |
| 24 | + before_create :set_path | |
| 25 | + include InstanceMethods::PathMethods | |
| 26 | + end | |
| 22 | 27 | |
| 23 | - before_create :set_path | |
| 24 | 28 | before_save :set_ancestry |
| 25 | - after_update :update_children_path | |
| 26 | 29 | end |
| 27 | 30 | |
| 28 | 31 | end |
| ... | ... | @@ -43,26 +46,14 @@ module ActsAsFileSystem |
| 43 | 46 | #raise "Couldn't reach and set ancestry on every record" if self.base_class.count(:conditions => ['ancestry is null']) != 0 |
| 44 | 47 | end |
| 45 | 48 | |
| 49 | + def has_path? | |
| 50 | + (['name', 'slug', 'path'] - self.column_names).blank? | |
| 51 | + end | |
| 52 | + | |
| 46 | 53 | end |
| 47 | 54 | |
| 48 | 55 | module InstanceMethods |
| 49 | 56 | |
| 50 | - # used to know when to trigger batch renaming | |
| 51 | - attr_accessor :recalculate_path | |
| 52 | - | |
| 53 | - # calculates the full path to this record using parent's path. | |
| 54 | - def calculate_path | |
| 55 | - self.hierarchy.map{ |obj| obj.slug }.join('/') | |
| 56 | - end | |
| 57 | - def set_path | |
| 58 | - if self.path == self.slug && !self.top_level? | |
| 59 | - self.path = self.calculate_path | |
| 60 | - end | |
| 61 | - end | |
| 62 | - def explode_path | |
| 63 | - path.split(/\//) | |
| 64 | - end | |
| 65 | - | |
| 66 | 57 | def ancestry_column |
| 67 | 58 | 'ancestry' |
| 68 | 59 | end |
| ... | ... | @@ -95,17 +86,6 @@ module ActsAsFileSystem |
| 95 | 86 | end |
| 96 | 87 | end |
| 97 | 88 | |
| 98 | - def update_children_path | |
| 99 | - if self.recalculate_path | |
| 100 | - self.children.each do |child| | |
| 101 | - child.path = child.calculate_path | |
| 102 | - child.recalculate_path = true | |
| 103 | - child.save! | |
| 104 | - end | |
| 105 | - end | |
| 106 | - self.recalculate_path = false | |
| 107 | - end | |
| 108 | - | |
| 109 | 89 | # calculates the level of the record in the records hierarchy. Top-level |
| 110 | 90 | # records have level 0; the children of the top-level records have |
| 111 | 91 | # level 1; the children of records with level 1 have level 2, and so on. |
| ... | ... | @@ -196,51 +176,84 @@ module ActsAsFileSystem |
| 196 | 176 | res |
| 197 | 177 | end |
| 198 | 178 | |
| 199 | - # calculates the full name of a record by accessing the name of all its | |
| 200 | - # ancestors. | |
| 201 | - # | |
| 202 | - # If you have this record hierarchy: | |
| 203 | - # Record "A" | |
| 204 | - # Record "B" | |
| 205 | - # Record "C" | |
| 206 | - # | |
| 207 | - # Then Record "C" will have "A/B/C" as its full name. | |
| 208 | - def full_name(sep = '/') | |
| 209 | - self.hierarchy.map {|item| item.name || '?' }.join(sep) | |
| 210 | - end | |
| 179 | + ##### | |
| 180 | + # Path methods | |
| 181 | + # These methods are used when _path_, _name_ and _slug_ attributes exist | |
| 182 | + # and should be calculated based on the tree | |
| 183 | + ##### | |
| 184 | + module PathMethods | |
| 185 | + # used to know when to trigger batch renaming | |
| 186 | + attr_accessor :recalculate_path | |
| 187 | + | |
| 188 | + # calculates the full path to this record using parent's path. | |
| 189 | + def calculate_path | |
| 190 | + self.hierarchy.map{ |obj| obj.slug }.join('/') | |
| 191 | + end | |
| 192 | + def set_path | |
| 193 | + if self.path == self.slug && !self.top_level? | |
| 194 | + self.path = self.calculate_path | |
| 195 | + end | |
| 196 | + end | |
| 197 | + def explode_path | |
| 198 | + path.split(/\//) | |
| 199 | + end | |
| 211 | 200 | |
| 212 | - # gets the name without leading parents. Useful when dividing records | |
| 213 | - # in top-level groups and full names must not include the top-level | |
| 214 | - # record which is already a emphasized label | |
| 215 | - def full_name_without_leading(count, sep = '/') | |
| 216 | - parts = self.full_name(sep).split(sep) | |
| 217 | - count.times { parts.shift } | |
| 218 | - parts.join(sep) | |
| 219 | - end | |
| 201 | + def update_children_path | |
| 202 | + if self.recalculate_path | |
| 203 | + self.children.each do |child| | |
| 204 | + child.path = child.calculate_path | |
| 205 | + child.recalculate_path = true | |
| 206 | + child.save! | |
| 207 | + end | |
| 208 | + end | |
| 209 | + self.recalculate_path = false | |
| 210 | + end | |
| 220 | 211 | |
| 221 | - def set_name(value) | |
| 222 | - if self.name != value | |
| 223 | - self.recalculate_path = true | |
| 212 | + # calculates the full name of a record by accessing the name of all its | |
| 213 | + # ancestors. | |
| 214 | + # | |
| 215 | + # If you have this record hierarchy: | |
| 216 | + # Record "A" | |
| 217 | + # Record "B" | |
| 218 | + # Record "C" | |
| 219 | + # | |
| 220 | + # Then Record "C" will have "A/B/C" as its full name. | |
| 221 | + def full_name(sep = '/') | |
| 222 | + self.hierarchy.map {|item| item.name || '?' }.join(sep) | |
| 224 | 223 | end |
| 225 | - self[:name] = value | |
| 226 | - end | |
| 227 | 224 | |
| 228 | - # sets the name of the record. Also sets #slug accordingly. | |
| 229 | - def name=(value) | |
| 230 | - self.set_name(value) | |
| 231 | - unless self.name.blank? | |
| 232 | - self.slug = self.name.to_slug | |
| 225 | + # gets the name without leading parents. Useful when dividing records | |
| 226 | + # in top-level groups and full names must not include the top-level | |
| 227 | + # record which is already a emphasized label | |
| 228 | + def full_name_without_leading(count, sep = '/') | |
| 229 | + parts = self.full_name(sep).split(sep) | |
| 230 | + count.times { parts.shift } | |
| 231 | + parts.join(sep) | |
| 233 | 232 | end |
| 234 | - end | |
| 235 | 233 | |
| 236 | - # sets the slug of the record. Also sets the path with the new slug value. | |
| 237 | - def slug=(value) | |
| 238 | - self[:slug] = value | |
| 239 | - unless self.slug.blank? | |
| 240 | - self.path = self.calculate_path | |
| 234 | + def set_name(value) | |
| 235 | + if self.name != value | |
| 236 | + self.recalculate_path = true | |
| 237 | + end | |
| 238 | + self[:name] = value | |
| 241 | 239 | end |
| 242 | - end | |
| 243 | 240 | |
| 241 | + # sets the name of the record. Also sets #slug accordingly. | |
| 242 | + def name=(value) | |
| 243 | + self.set_name(value) | |
| 244 | + unless self.name.blank? | |
| 245 | + self.slug = self.name.to_slug | |
| 246 | + end | |
| 247 | + end | |
| 248 | + | |
| 249 | + # sets the slug of the record. Also sets the path with the new slug value. | |
| 250 | + def slug=(value) | |
| 251 | + self[:slug] = value | |
| 252 | + unless self.slug.blank? | |
| 253 | + self.path = self.calculate_path | |
| 254 | + end | |
| 255 | + end | |
| 256 | + end | |
| 244 | 257 | end |
| 245 | 258 | end |
| 246 | 259 | ... | ... |