Ticket #10: sti-fix.patch
| File sti-fix.patch, 16.0 kB (added by tolsen@limewire.com, 2 years ago) |
|---|
-
vendor/plugins/better_nested_set/lib/better_nested_set.rb
old new 86 86 :left_column => (options[:left_column] || 'lft'), 87 87 :right_column => (options[:right_column] || 'rgt'), 88 88 :scope => (options[:scope] || '1 = 1'), 89 :text_column => (options[:text_column] || columns.collect{|c| (c.type == :string) ? c.name : nil }.compact.first) 89 :text_column => (options[:text_column] || columns.collect{|c| (c.type == :string) ? c.name : nil }.compact.first), 90 :class => self 90 91 } ) 91 92 92 93 class_inheritable_reader :acts_as_nested_set_options … … 112 113 extend SymetrieCom::Acts::NestedSet::ClassMethods 113 114 114 115 # adds the helper for the class 115 # ActionView::Base.send(:define_method, "#{Inflector.underscore( self.class)}_options_for_select") { special=nil116 # "#{acts_as_nested_set_options[:text_column]} || "#{ self.class} id #{id}"116 # ActionView::Base.send(:define_method, "#{Inflector.underscore(acts_as_nested_set_options[:class])}_options_for_select") { special=nil 117 # "#{acts_as_nested_set_options[:text_column]} || "#{acts_as_nested_set_options[:class]} id #{id}" 117 118 # } 118 119 119 120 end … … 136 137 137 138 # on creation, set automatically lft and rgt to the end of the tree 138 139 def before_create 139 maxright = self.class.maximum(acts_as_nested_set_options[:right_column], :conditions => acts_as_nested_set_options[:scope]) || 0140 maxright = acts_as_nested_set_options[:class].maximum(acts_as_nested_set_options[:right_column], :conditions => acts_as_nested_set_options[:scope]) || 0 140 141 # adds the new node to the right of all existing nodes 141 142 self[acts_as_nested_set_options[:left_column]] = maxright+1 142 143 self[acts_as_nested_set_options[:right_column]] = maxright+2 … … 198 199 child[acts_as_nested_set_options[:left_column]] = right_bound 199 200 child[acts_as_nested_set_options[:right_column]] = right_bound + 1 200 201 self[acts_as_nested_set_options[:right_column]] += 2 201 self.class.transaction {202 self.class.update_all( "#{acts_as_nested_set_options[:left_column]} = (#{acts_as_nested_set_options[:left_column]} + 2)", "#{acts_as_nested_set_options[:scope]} AND #{acts_as_nested_set_options[:left_column]} >= #{right_bound}" )203 self.class.update_all( "#{acts_as_nested_set_options[:right_column]} = (#{acts_as_nested_set_options[:right_column]} + 2)", "#{acts_as_nested_set_options[:scope]} AND #{acts_as_nested_set_options[:right_column]} >= #{right_bound}" )202 acts_as_nested_set_options[:class].transaction { 203 acts_as_nested_set_options[:class].update_all( "#{acts_as_nested_set_options[:left_column]} = (#{acts_as_nested_set_options[:left_column]} + 2)", "#{acts_as_nested_set_options[:scope]} AND #{acts_as_nested_set_options[:left_column]} >= #{right_bound}" ) 204 acts_as_nested_set_options[:class].update_all( "#{acts_as_nested_set_options[:right_column]} = (#{acts_as_nested_set_options[:right_column]} + 2)", "#{acts_as_nested_set_options[:scope]} AND #{acts_as_nested_set_options[:right_column]} >= #{right_bound}" ) 204 205 self.save 205 206 child.save 206 207 } … … 210 211 211 212 # Returns root 212 213 def root 213 self.class.find(:first, :conditions => "#{acts_as_nested_set_options[:scope]} AND (#{acts_as_nested_set_options[:parent_column]} IS NULL)")214 acts_as_nested_set_options[:class].find(:first, :conditions => "#{acts_as_nested_set_options[:scope]} AND (#{acts_as_nested_set_options[:parent_column]} IS NULL)") 214 215 end 215 216 216 217 # Returns roots when multiple roots (or virtual root, which is the same) 217 218 def roots 218 self.class.find(:all, :conditions => "#{acts_as_nested_set_options[:scope]} AND (#{acts_as_nested_set_options[:parent_column]} IS NULL)", :order => "#{acts_as_nested_set_options[:left_column]}")219 acts_as_nested_set_options[:class].find(:all, :conditions => "#{acts_as_nested_set_options[:scope]} AND (#{acts_as_nested_set_options[:parent_column]} IS NULL)", :order => "#{acts_as_nested_set_options[:left_column]}") 219 220 end 220 221 221 222 # Returns the parent 222 223 def parent 223 self.class.find(self[acts_as_nested_set_options[:parent_column]]) if self[acts_as_nested_set_options[:parent_column]]224 acts_as_nested_set_options[:class].find(self[acts_as_nested_set_options[:parent_column]]) if self[acts_as_nested_set_options[:parent_column]] 224 225 end 225 226 226 227 # Returns an array of all parents 227 228 # Maybe 'full_outline' would be a better name, but we prefer to mimic the Tree class 228 229 def ancestors 229 self.class.find(:all, :conditions => "#{acts_as_nested_set_options[:scope]} AND (#{acts_as_nested_set_options[:left_column]} < #{self[acts_as_nested_set_options[:left_column]]} and #{acts_as_nested_set_options[:right_column]} > #{self[acts_as_nested_set_options[:right_column]]})", :order => acts_as_nested_set_options[:left_column] )230 acts_as_nested_set_options[:class].find(:all, :conditions => "#{acts_as_nested_set_options[:scope]} AND (#{acts_as_nested_set_options[:left_column]} < #{self[acts_as_nested_set_options[:left_column]]} and #{acts_as_nested_set_options[:right_column]} > #{self[acts_as_nested_set_options[:right_column]]})", :order => acts_as_nested_set_options[:left_column] ) 230 231 end 231 232 232 233 # Returns the array of all parents and self … … 244 245 if self[acts_as_nested_set_options[:parent_column]].nil? || self[acts_as_nested_set_options[:parent_column]].zero? 245 246 [self] 246 247 else 247 self.class.find(:all, :conditions => "#{acts_as_nested_set_options[:scope]} and #{acts_as_nested_set_options[:parent_column]} = #{self[acts_as_nested_set_options[:parent_column]]}", :order => acts_as_nested_set_options[:left_column])248 acts_as_nested_set_options[:class].find(:all, :conditions => "#{acts_as_nested_set_options[:scope]} and #{acts_as_nested_set_options[:parent_column]} = #{self[acts_as_nested_set_options[:parent_column]]}", :order => acts_as_nested_set_options[:left_column]) 248 249 end 249 250 end 250 251 … … 252 253 # root level is 0 253 254 def level 254 255 return 0 if self[acts_as_nested_set_options[:parent_column]].nil? 255 self.class.count("#{acts_as_nested_set_options[:scope]} AND (#{acts_as_nested_set_options[:left_column]} < #{self[acts_as_nested_set_options[:left_column]]} and #{acts_as_nested_set_options[:right_column]} > #{self[acts_as_nested_set_options[:right_column]]})")256 acts_as_nested_set_options[:class].count("#{acts_as_nested_set_options[:scope]} AND (#{acts_as_nested_set_options[:left_column]} < #{self[acts_as_nested_set_options[:left_column]]} and #{acts_as_nested_set_options[:right_column]} > #{self[acts_as_nested_set_options[:right_column]]})") 256 257 end 257 258 258 259 # Returns the number of nested children of this object. … … 264 265 # Pass :exclude => item, or id, or [items or id] to exclude some parts of the tree 265 266 def full_set(special=nil) 266 267 return [self] if new_record? or self[acts_as_nested_set_options[:right_column]]-self[acts_as_nested_set_options[:left_column]] == 1 267 # self.class.find(:all, :conditions => "#{acts_as_nested_set_options[:scope]} AND (#{acts_as_nested_set_options[:left_column]} BETWEEN #{self[acts_as_nested_set_options[:left_column]]} and #{self[acts_as_nested_set_options[:right_column]]})", :order => acts_as_nested_set_options[:left_column])268 # acts_as_nested_set_options[:class].find(:all, :conditions => "#{acts_as_nested_set_options[:scope]} AND (#{acts_as_nested_set_options[:left_column]} BETWEEN #{self[acts_as_nested_set_options[:left_column]]} and #{self[acts_as_nested_set_options[:right_column]]})", :order => acts_as_nested_set_options[:left_column]) 268 269 [self] + all_children(special) 269 270 end 270 271 … … 276 277 # exclude some items and all their children 277 278 special[:exclude] = [special[:exclude]] if !special[:exclude].is_a?(Array) 278 279 # get objects for ids 279 special[:exclude].collect! {|s| s.is_a?( self.class) ? s : self.class.find(s)}280 special[:exclude].collect! {|s| s.is_a?(acts_as_nested_set_options[:class]) ? s : acts_as_nested_set_options[:class].find(s)} 280 281 # get all subtrees and flatten the list 281 282 exclude_list = special[:exclude].map{|e| e.full_set.map{|ee| ee.id}}.flatten.uniq.join(',') 282 283 if exclude_list.blank? 283 self.class.find(:all, :conditions => "#{acts_as_nested_set_options[:scope]} AND (#{acts_as_nested_set_options[:left_column]} > #{self[acts_as_nested_set_options[:left_column]]}) and (#{acts_as_nested_set_options[:right_column]} < #{self[acts_as_nested_set_options[:right_column]]})", :order => acts_as_nested_set_options[:left_column])284 acts_as_nested_set_options[:class].find(:all, :conditions => "#{acts_as_nested_set_options[:scope]} AND (#{acts_as_nested_set_options[:left_column]} > #{self[acts_as_nested_set_options[:left_column]]}) and (#{acts_as_nested_set_options[:right_column]} < #{self[acts_as_nested_set_options[:right_column]]})", :order => acts_as_nested_set_options[:left_column]) 284 285 else 285 self.class.find(:all, :conditions => "#{acts_as_nested_set_options[:scope]} AND id NOT IN (#{exclude_list}) AND (#{acts_as_nested_set_options[:left_column]} > #{self[acts_as_nested_set_options[:left_column]]}) and (#{acts_as_nested_set_options[:right_column]} < #{self[acts_as_nested_set_options[:right_column]]})", :order => acts_as_nested_set_options[:left_column])286 acts_as_nested_set_options[:class].find(:all, :conditions => "#{acts_as_nested_set_options[:scope]} AND id NOT IN (#{exclude_list}) AND (#{acts_as_nested_set_options[:left_column]} > #{self[acts_as_nested_set_options[:left_column]]}) and (#{acts_as_nested_set_options[:right_column]} < #{self[acts_as_nested_set_options[:right_column]]})", :order => acts_as_nested_set_options[:left_column]) 286 287 end 287 288 end 288 289 else 289 self.class.find(:all, :conditions => "#{acts_as_nested_set_options[:scope]} AND (#{acts_as_nested_set_options[:left_column]} > #{self[acts_as_nested_set_options[:left_column]]}) and (#{acts_as_nested_set_options[:right_column]} < #{self[acts_as_nested_set_options[:right_column]]})", :order => acts_as_nested_set_options[:left_column])290 acts_as_nested_set_options[:class].find(:all, :conditions => "#{acts_as_nested_set_options[:scope]} AND (#{acts_as_nested_set_options[:left_column]} > #{self[acts_as_nested_set_options[:left_column]]}) and (#{acts_as_nested_set_options[:right_column]} < #{self[acts_as_nested_set_options[:right_column]]})", :order => acts_as_nested_set_options[:left_column]) 290 291 end 291 292 end 292 293 293 294 # Returns a set of only this entry's immediate children 294 295 def children 295 self.class.find(:all, :conditions => "#{acts_as_nested_set_options[:scope]} AND #{acts_as_nested_set_options[:parent_column]} = #{self.id}", :order => acts_as_nested_set_options[:left_column])296 acts_as_nested_set_options[:class].find(:all, :conditions => "#{acts_as_nested_set_options[:scope]} AND #{acts_as_nested_set_options[:parent_column]} = #{self.id}", :order => acts_as_nested_set_options[:left_column]) 296 297 end 297 298 298 299 # Prunes a branch off of the tree, shifting all of the elements on the right … … 301 302 return if self[acts_as_nested_set_options[:right_column]].nil? || self[acts_as_nested_set_options[:left_column]].nil? 302 303 dif = self[acts_as_nested_set_options[:right_column]] - self[acts_as_nested_set_options[:left_column]] + 1 303 304 304 self.class.transaction {305 self.class.delete_all( "#{acts_as_nested_set_options[:scope]} AND #{acts_as_nested_set_options[:left_column]} > #{self[acts_as_nested_set_options[:left_column]]} and #{acts_as_nested_set_options[:right_column]} < #{self[acts_as_nested_set_options[:right_column]]}" )306 self.class.update_all( "#{acts_as_nested_set_options[:left_column]} = (#{acts_as_nested_set_options[:left_column]} - #{dif})", "#{acts_as_nested_set_options[:scope]} AND #{acts_as_nested_set_options[:left_column]} >= #{self[acts_as_nested_set_options[:right_column]]}" )307 self.class.update_all( "#{acts_as_nested_set_options[:right_column]} = (#{acts_as_nested_set_options[:right_column]} - #{dif} )", "#{acts_as_nested_set_options[:scope]} AND #{acts_as_nested_set_options[:right_column]} >= #{self[acts_as_nested_set_options[:right_column]]}" )305 acts_as_nested_set_options[:class].transaction { 306 acts_as_nested_set_options[:class].delete_all( "#{acts_as_nested_set_options[:scope]} AND #{acts_as_nested_set_options[:left_column]} > #{self[acts_as_nested_set_options[:left_column]]} and #{acts_as_nested_set_options[:right_column]} < #{self[acts_as_nested_set_options[:right_column]]}" ) 307 acts_as_nested_set_options[:class].update_all( "#{acts_as_nested_set_options[:left_column]} = (#{acts_as_nested_set_options[:left_column]} - #{dif})", "#{acts_as_nested_set_options[:scope]} AND #{acts_as_nested_set_options[:left_column]} >= #{self[acts_as_nested_set_options[:right_column]]}" ) 308 acts_as_nested_set_options[:class].update_all( "#{acts_as_nested_set_options[:right_column]} = (#{acts_as_nested_set_options[:right_column]} - #{dif} )", "#{acts_as_nested_set_options[:scope]} AND #{acts_as_nested_set_options[:right_column]} >= #{self[acts_as_nested_set_options[:right_column]]}" ) 308 309 } 309 310 end 310 311 … … 334 335 extent = cur_right - cur_left + 1 335 336 336 337 # load object if node is not an object 337 if !( self.class === target)338 target = self.class.find(target)338 if !(target.kind_of? acts_as_nested_set_options[:class]) 339 target = acts_as_nested_set_options[:class].find(target) 339 340 end 340 341 target_left, target_right = target[acts_as_nested_set_options[:left_column]], target[acts_as_nested_set_options[:right_column]] 341 342 … … 390 391 end 391 392 392 393 # update and that rules 393 self.class.update_all( "#{acts_as_nested_set_options[:left_column]} = CASE \394 acts_as_nested_set_options[:class].update_all( "#{acts_as_nested_set_options[:left_column]} = CASE \ 394 395 WHEN #{acts_as_nested_set_options[:left_column]} BETWEEN #{cur_left} AND #{cur_right} \ 395 396 THEN #{acts_as_nested_set_options[:left_column]} + #{shift} \ 396 397 WHEN #{acts_as_nested_set_options[:left_column]} BETWEEN #{b_left} AND #{b_right} \ … … 403 404 THEN #{acts_as_nested_set_options[:right_column]} + #{updown} \ 404 405 ELSE #{acts_as_nested_set_options[:right_column]} END, \ 405 406 #{acts_as_nested_set_options[:parent_column]} = CASE \ 406 WHEN #{ self.class.primary_key} = #{self.id} \407 WHEN #{acts_as_nested_set_options[:class].primary_key} = #{self.id} \ 407 408 THEN #{new_parent} \ 408 409 ELSE #{acts_as_nested_set_options[:parent_column]} END", 409 410 acts_as_nested_set_options[:scope] )
