Sha256: 6e2e69d70acb4fce71fa0e01647bfc898ab18614966b39a18fa9477eaf6a33ae

Contents?: true

Size: 1.14 KB

Versions: 8

Compression:

Stored size: 1.14 KB

Contents

module ActsAsNestedInterval
  module ClassMethods
    
    # Rebuild the intervals tree
    def rebuild_nested_interval_tree!
      # temporary changes
      skip_callback :update, :before, :update_nested_interval
      old_default_scopes = default_scopes # save to revert later
      default_scope where("#{quoted_table_name}.lftq > 0") # use lft1 > 0 as a "migrated?" flag
          
      # zero all intervals
      update_hash = {lftp: 0, lftq: 0}
      update_hash[:rgtp] = 0 if columns_hash["rgtp"]
      update_hash[:rgtq] = 0 if columns_hash["rgtq"]
      update_hash[:lft]  = 0 if columns_hash["lft"]
      update_hash[:rgt]  = 0 if columns_hash["rgt"]
      update_all update_hash
          
      # recompute intervals with a recursive lambda
      clear_cache!
      update_subtree = ->(node){
        node.create_nested_interval
        node.save
        node.class.unscoped.where(nested_interval_foreign_key => node.id).find_each &update_subtree
      }
      unscoped.roots.find_each &update_subtree

      # revert changes
      set_callback :update, :before, :update_nested_interval
      self.default_scopes = old_default_scopes
    end
    
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
acts_as_nested_interval-0.1.1 lib/acts_as_nested_interval/class_methods.rb
acts_as_nested_interval-0.1.0 lib/acts_as_nested_interval/class_methods.rb
acts_as_nested_interval-0.0.10 lib/acts_as_nested_interval/class_methods.rb
acts_as_nested_interval-0.0.9 lib/acts_as_nested_interval/class_methods.rb
acts_as_nested_interval-0.0.8 lib/acts_as_nested_interval/class_methods.rb
acts_as_nested_interval-0.0.7 lib/acts_as_nested_interval/class_methods.rb
acts_as_nested_interval-0.0.6 lib/acts_as_nested_interval/class_methods.rb
acts_as_nested_interval-0.0.5 lib/acts_as_nested_interval/class_methods.rb