Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Half-done parent column option work. I decided it was too much effort…

… for now, but may return to it later.
  • Loading branch information...
commit fbe9b7242803fd2a6f5e46603009757703af2607 1 parent 0a9333f
@jonleighton jonleighton authored
View
34 lib/even_better_nested_set.rb
@@ -18,7 +18,7 @@ def self.included(base)
module ClassMethods
def find_last_root
- find(:first, :order => "#{nested_set_column(:right)} DESC", :conditions => { :parent_id => nil })
+ find(:first, :order => "#{nested_set_column(:right)} DESC", :conditions => { nested_set_options[:parent_column] => nil })
end
def find_boundaries(id)
@@ -58,11 +58,10 @@ def sort_nodes_to_nested_set(nodes)
end
return roots
end
-
- # In the future perhaps this can be extended to allow for custom column names specified
- # as options
+
def nested_set_column(name)
- connection.quote_column_name(name)
+ column_name = nested_set_options["#{name}_column".to_sym]
+ connection.quote_column_name(column_name)
end
end
@@ -182,7 +181,7 @@ def remove_node
def append_node
boundary = 1
- if parent_id?
+ if !send(nested_set_options[:parent_column]).nil?
transaction do
boundary = parent(true).right
shift! 2, boundary
@@ -251,7 +250,7 @@ def base_class
end
def validate_parent_is_within_scope
- if nested_set_options[:scope] && parent_id
+ if nested_set_options[:scope] && send(nested_set_options[:parent_column])
parent.reload # Make sure we are testing the record corresponding to the parent_id
if self.send(nested_set_options[:scope]) != parent.send(nested_set_options[:scope])
errors.add(:parent_id, "cannot be a record with a different #{nested_set_options[:scope]} to this record")
@@ -263,7 +262,20 @@ def validate_parent_is_within_scope
module ClassMethods
def acts_as_nested_set(options = {})
- options[:scope] = "#{options[:scope]}_id" if options[:scope]
+ options = {
+ :parent_column => "parent_id"
+ }.merge(options)
+
+ if !options[:scope].nil? && options[:scope].to_s !~ /_id$/
+ options[:scope] = "#{options[:scope]}_id"
+ end
+
+ (class << self; self; end).class_eval do
+ define_method :nested_set_options do
+ options
+ end
+ end
+ delegate :nested_set_options, :to => "self.class"
include NestedSet
@@ -278,12 +290,6 @@ def acts_as_nested_set(options = {})
validate_on_update :illegal_nesting
validate :validate_parent_is_within_scope
- class_eval do
- define_method :nested_set_options do
- options
- end
- end
-
delegate :nested_set_column, :to => "self.class"
end
View
19 spec/custom_named_directory_spec.rb
@@ -0,0 +1,19 @@
+require File.dirname(__FILE__) + '/spec_helper'
+require File.dirname(__FILE__) + '/nested_set_behavior'
+
+class CustomNamedDirectory < ActiveRecord::Base
+ acts_as_nested_set :parent_column => :baz_id
+
+ validates_presence_of :name
+end
+
+describe CustomNamedDirectory do
+ include AttributeHelper
+
+ before do
+ @model = CustomNamedDirectory
+ end
+
+ it_should_behave_like "all nested set models"
+
+end
View
12 spec/directory_spec.rb
@@ -8,15 +8,7 @@ class Directory < ActiveRecord::Base
end
describe Directory do
-
- def invalid_attributes(options = {})
- return { }.merge(options)
- end
-
- def valid_attributes(options = {})
- $directory_no = $directory_no ? $directory_no + 1 : 0
- return { :name => "directory#{$directory_no}" }.merge(options)
- end
+ include AttributeHelper
before do
@model = Directory
@@ -24,4 +16,4 @@ def valid_attributes(options = {})
it_should_behave_like "all nested set models"
-end
+end
View
19 spec/spec_helper.rb
@@ -56,11 +56,19 @@ def self.up
t.column :name, :string
t.column :company_id, :integer
end
+
+ create_table :custom_named_directories, :force => true do |t|
+ t.column :left, :integer
+ t.column :right, :integer
+ t.column :baz_id, :integer # parent_id
+ t.column :name, :string
+ end
end
def self.down
drop_table :directories
drop_table :employees
+ drop_table :custom_named_directories
rescue
nil
end
@@ -75,3 +83,14 @@ def without_changing_the_database
TestMigration.down
TestMigration.up
+
+module AttributeHelper
+ def invalid_attributes(options = {})
+ return { }.merge(options)
+ end
+
+ def valid_attributes(options = {})
+ $count = $count ? $count + 1 : 0
+ return { :name => "item#{$count}" }.merge(options)
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.