diff --git a/lib/closure_tree/acts_as_tree.rb b/lib/closure_tree/acts_as_tree.rb index 7fc0829c..585ab1af 100644 --- a/lib/closure_tree/acts_as_tree.rb +++ b/lib/closure_tree/acts_as_tree.rb @@ -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}" @@ -50,5 +50,6 @@ def ==(comparison_object) include ClosureTree::DeterministicNumericOrdering if order_is_numeric end end + end -end \ No newline at end of file +end diff --git a/lib/closure_tree/columns.rb b/lib/closure_tree/columns.rb index 4f24d366..b6af38fb 100644 --- a/lib/closure_tree/columns.rb +++ b/lib/closure_tree/columns.rb @@ -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 diff --git a/spec/db/schema.rb b/spec/db/schema.rb index c6b3edb1..b9e782d9 100644 --- a/spec/db/schema.rb +++ b/spec/db/schema.rb @@ -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 diff --git a/spec/namespace_type_spec.rb b/spec/namespace_type_spec.rb new file mode 100644 index 00000000..85edf7ac --- /dev/null +++ b/spec/namespace_type_spec.rb @@ -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 diff --git a/spec/support/models.rb b/spec/support/models.rb index 8bff5f1c..83cb1e52 100644 --- a/spec/support/models.rb +++ b/spec/support/models.rb @@ -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 diff --git a/spec/tag_spec.rb b/spec/tag_spec.rb index ec5f8679..35f4d986 100644 --- a/spec/tag_spec.rb +++ b/spec/tag_spec.rb @@ -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