Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multipart constant names like "Admin::PageHierarchy" #47

Merged
merged 2 commits into from
Mar 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/closure_tree/acts_as_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def acts_as_tree(options = {})
# Auto-inject the hierarchy table
# See https://github.com/patshaughnessy/class_factory/blob/master/lib/class_factory/class_factory.rb
class_attribute :hierarchy_class
self.hierarchy_class = Object.const_set hierarchy_class_name, Class.new(ActiveRecord::Base)
self.hierarchy_class = ct_class.parent.const_set short_hierarchy_class_name, Class.new(ActiveRecord::Base)

self.hierarchy_class.class_eval <<-RUBY
belongs_to :ancestor, :class_name => "#{ct_class.to_s}"
Expand All @@ -50,5 +50,6 @@ def ==(comparison_object)
include ClosureTree::DeterministicNumericOrdering if order_is_numeric
end
end

end
end
end
13 changes: 13 additions & 0 deletions lib/closure_tree/columns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ def hierarchy_class_name
closure_tree_options[:hierarchy_class_name] || ct_class.to_s + "Hierarchy"
end


#
# Returns the constant name of the hierarchy_class
#
# @return [String] the constant name
#
# @example
# Namespace::Model.hierarchy_class_name # => "Namespace::ModelHierarchy"
# Namespace::Model.short_hierarchy_class_name # => "ModelHierarchy"
def short_hierarchy_class_name
hierarchy_class_name.split('::').last
end

def quoted_hierarchy_table_name
connection.quote_table_name hierarchy_table_name
end
Expand Down
11 changes: 11 additions & 0 deletions spec/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,15 @@ def force_add_index(table_name, columns, options = {})
t.integer "descendant_id", :null => false
t.integer "generations", :null => false
end

create_table "namespace_types", :force => true do |t|
t.string "name"
t.integer "parent_id"
end

create_table "namespace_type_hierarchies", :id => false, :force => true do |t|
t.integer "ancestor_id", :null => false
t.integer "descendant_id", :null => false
t.integer "generations", :null => false
end
end
13 changes: 13 additions & 0 deletions spec/namespace_type_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'spec_helper'

describe Namespace::Type do

context "class injection" do
it "should build hierarchy classname correctly" do
Namespace::Type.hierarchy_class.to_s.should == "Namespace::TypeHierarchy"
Namespace::Type.hierarchy_class_name.should == "Namespace::TypeHierarchy"
Namespace::Type.short_hierarchy_class_name.should == "TypeHierarchy"
end
end

end
7 changes: 7 additions & 0 deletions spec/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,10 @@ class DirectoryLabel < Label
class CuisineType < ActiveRecord::Base
acts_as_tree
end

module Namespace
class Type < ActiveRecord::Base
acts_as_tree :dependent => :destroy
attr_accessible :name
end
end
1 change: 1 addition & 0 deletions spec/tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ def nuke_db
it "should build hierarchy classname correctly" do
Tag.hierarchy_class.to_s.should == "TagHierarchy"
Tag.hierarchy_class_name.should == "TagHierarchy"
Tag.short_hierarchy_class_name.should == "TagHierarchy"
end

it "should have a correct parent column name" do
Expand Down