Sha256: 4484d0f47099c3daa0c6feea4b2f7568c4754d75a09fb4deb359623f466fe95d
Contents?: true
Size: 1.81 KB
Versions: 3
Compression:
Stored size: 1.81 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, options={}) @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 options[:order] ||= sns_items_class.new.position_column if sns_items_class.method_defined?(:position_column) 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 => options[:order], :dependent => :destroy end end module InstanceMethods def self_and_descendants_with_sns_items self_and_descendants.includes(self.class.sns_items_type).collect { |category| [category, category.sns_items] }.flatten 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 end end ActiveRecord::Base.send :include, SortableNestedSet
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
sortable_nested_set-0.4.2 | lib/acts_as_sortable_nested_set.rb |
sortable_nested_set-0.4.1 | lib/acts_as_sortable_nested_set.rb |
sortable_nested_set-0.4.0 | lib/acts_as_sortable_nested_set.rb |