Sha256: e228fa417bd5343bcdc36c48a02c290dfc7b11dcefb48f16357548ec3bb1d5f0
Contents?: true
Size: 1.86 KB
Versions: 1
Compression:
Stored size: 1.86 KB
Contents
require 'nested_set' module SortableNestedSet def self.included(base) base.extend ClassMethods base.send :include, InstanceMethods end module ClassMethods def acts_as_sortable_nested_set_of(items_type) @subcategories_type = "sub#{name.tableize}".to_sym @items_type = items_type @foreign_key = self.class.method_defined?(:parent) ? :parent_id : name.foreign_key instance_eval do def sns_subcategories_type ; @subcategories_type end def sns_items_type ; @items_type end def sns_items_class ; @items_type.to_s.singularize.classify.constantize end end class_eval do define_method(:sns_subcategories) { send(self.class.sns_subcategories_type) } define_method(:sns_items) { send(self.class.sns_items_type) } end acts_as_nested_set :parent_column => @foreign_key has_many @subcategories_type, :class_name => name, :foreign_key => @foreign_key, :dependent => :destroy has_many @items_type, :order => (sns_items_class.position_column if sns_items_class.method_defined?(:position_column)), :dependent => :destroy after_initialize :default_depth_to_zero end end module InstanceMethods def indented_name(indentation_character='.') indentation_character * depth + name end def move(parent, children) if children.size == 1 move_to_child_of(parent) else prev_sibling = nil for i in 0...children.size if children[i] == self.id prev_sibling ? move_to_right_of(prev_sibling) : move_to_left_of(children[i+1]) break end prev_sibling = children[i] end end end private def default_depth_to_zero self.depth ||= 0 if has_attribute?(:depth) end end end ActiveRecord::Base.send :include, SortableNestedSet
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
sortable_nested_set-0.3.0 | lib/acts_as_sortable_nested_set.rb |