Skip to content

Commit

Permalink
Half-done parent column option work. I decided it was too much effort…
Browse files Browse the repository at this point in the history
… for now, but may return to it later.
  • Loading branch information
jonleighton committed Dec 23, 2008
1 parent 0a9333f commit fbe9b72
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 24 deletions.
34 changes: 20 additions & 14 deletions lib/even_better_nested_set.rb
Expand Up @@ -18,7 +18,7 @@ def self.included(base)
module ClassMethods module ClassMethods


def find_last_root 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 end


def find_boundaries(id) def find_boundaries(id)
Expand Down Expand Up @@ -58,11 +58,10 @@ def sort_nodes_to_nested_set(nodes)
end end
return roots return roots
end end


# In the future perhaps this can be extended to allow for custom column names specified
# as options
def nested_set_column(name) 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


end end
Expand Down Expand Up @@ -182,7 +181,7 @@ def remove_node
def append_node def append_node
boundary = 1 boundary = 1


if parent_id? if !send(nested_set_options[:parent_column]).nil?
transaction do transaction do
boundary = parent(true).right boundary = parent(true).right
shift! 2, boundary shift! 2, boundary
Expand Down Expand Up @@ -251,7 +250,7 @@ def base_class
end end


def validate_parent_is_within_scope 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 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]) 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") errors.add(:parent_id, "cannot be a record with a different #{nested_set_options[:scope]} to this record")
Expand All @@ -263,7 +262,20 @@ def validate_parent_is_within_scope
module ClassMethods module ClassMethods


def acts_as_nested_set(options = {}) 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 include NestedSet


Expand All @@ -278,12 +290,6 @@ def acts_as_nested_set(options = {})
validate_on_update :illegal_nesting validate_on_update :illegal_nesting
validate :validate_parent_is_within_scope 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" delegate :nested_set_column, :to => "self.class"
end end


Expand Down
19 changes: 19 additions & 0 deletions 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
12 changes: 2 additions & 10 deletions spec/directory_spec.rb
Expand Up @@ -8,20 +8,12 @@ class Directory < ActiveRecord::Base
end end


describe Directory do describe Directory do

include AttributeHelper
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


before do before do
@model = Directory @model = Directory
end end


it_should_behave_like "all nested set models" it_should_behave_like "all nested set models"


end end
19 changes: 19 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -56,11 +56,19 @@ def self.up
t.column :name, :string t.column :name, :string
t.column :company_id, :integer t.column :company_id, :integer
end 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 end


def self.down def self.down
drop_table :directories drop_table :directories
drop_table :employees drop_table :employees
drop_table :custom_named_directories
rescue rescue
nil nil
end end
Expand All @@ -75,3 +83,14 @@ def without_changing_the_database


TestMigration.down TestMigration.down
TestMigration.up 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

0 comments on commit fbe9b72

Please sign in to comment.