10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/acts_as_sortable_nested_set.rb', line 10
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
|