def acts_as_nested_set(options = {})
extend(SingletonMethods) unless respond_to?(:find_in_nestedset)
options[:scope] = "#{options[:scope]}_id".intern if options[:scope].is_a?(Symbol) && options[:scope].to_s !~ /_id$/
write_inheritable_attribute(:acts_as_nested_set_options,
{ :parent_column => (options[:parent_column] || 'parent_id'),
:left_column => (options[:left_column] || 'lft'),
:right_column => (options[:right_column] || 'rgt'),
:scope => (options[:scope] || '1 = 1'),
:text_column => (options[:text_column] || columns.collect{|c| (c.type == :string) ? c.name : nil }.compact.first),
:class => self,
:dependent => (options[:dependent] || :delete_all)
} )
class_inheritable_reader :acts_as_nested_set_options
base_set_class.class_inheritable_accessor :acts_as_nested_set_scope_enabled
base_set_class.acts_as_nested_set_scope_enabled = true
if acts_as_nested_set_options[:scope].is_a?(Symbol)
scope_condition_method = %(
def scope_condition
if #{acts_as_nested_set_options[:scope].to_s}.nil?
self.class.use_scope_condition? ? "#{table_name}.#{acts_as_nested_set_options[:scope].to_s} IS NULL" : "(1 = 1)"
else
self.class.use_scope_condition? ? "#{table_name}.#{acts_as_nested_set_options[:scope].to_s} = \#{#{acts_as_nested_set_options[:scope].to_s}}" : "(1 = 1)"
end
end
)
else
scope_condition_method = "def scope_condition(); self.class.use_scope_condition? ? \"#{acts_as_nested_set_options[:scope]}\" : \"(1 = 1)\"; end"
end
attr_accessor :skip_before_destroy
attr_protected acts_as_nested_set_options[:left_column].intern,
acts_as_nested_set_options[:right_column].intern,
acts_as_nested_set_options[:parent_column].intern
class_eval "before_create :set_left_right\nbefore_destroy :destroy_descendants\ninclude SymetrieCom::Acts::NestedSet::InstanceMethods\n\ndef \#{acts_as_nested_set_options[:left_column]}=(x)\nraise ActiveRecord::ActiveRecordError, \"Unauthorized assignment to \#{acts_as_nested_set_options[:left_column]}: it's an internal field handled by acts_as_nested_set code, use move_to_* methods instead.\"\nend\ndef \#{acts_as_nested_set_options[:right_column]}=(x)\nraise ActiveRecord::ActiveRecordError, \"Unauthorized assignment to \#{acts_as_nested_set_options[:right_column]}: it's an internal field handled by acts_as_nested_set code, use move_to_* methods instead.\"\nend\ndef \#{acts_as_nested_set_options[:parent_column]}=(x)\nraise ActiveRecord::ActiveRecordError, \"Unauthorized assignment to \#{acts_as_nested_set_options[:parent_column]}: it's an internal field handled by acts_as_nested_set code, use move_to_* methods instead.\"\nend\n\#{scope_condition_method}\n"
end