Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

3.6.0: Added support for

  * ```:hierarchy_class_name``` as an option
  * ActiveRecord::Base.table_name_prefix
  * ActiveRecord::Base.table_name_suffix
  • Loading branch information...
commit 27a35b4d04d5970e83ea9bc73b6946a7687406b6 1 parent 5203217
Matthew McEachen authored
2  .travis.yml
View
@@ -14,7 +14,7 @@ env:
- DB=sqlite3
- DB=mysql
- DB=postgresql
-script: bundle exec rake
+script: bundle exec rake specs_with_db_ixes
before_script:
- mysql -e 'create database closure_tree_test'
10 README.md
View
@@ -198,7 +198,8 @@ HT: [ancestry](https://github.com/stefankroes/ancestry#arrangement) and [elhoyos
When you include ```acts_as_tree``` in your model, you can provide a hash to override the following defaults:
* ```:parent_column_name``` to override the column name of the parent foreign key in the model's table. This defaults to "parent_id".
-* ```:hierarchy_table_name``` to override the hierarchy table name. This defaults to the singular name of the model + "_hierarchies".
+* ```:hierarchy_table_name``` to override the hierarchy class name. This defaults to the singular name of the model + "Hierarchy", like ```TagHierarchy```.
+* ```:hierarchy_table_name``` to override the hierarchy table name. This defaults to the singular name of the model + "_hierarchies", like ```tag_hierarchies```.
* ```:dependent``` determines what happens when a node is destroyed. Defaults to ```nullify```.
* ```:nullify``` will simply set the parent column to null. Each child node will be considered a "root" node. This is the default.
* ```:delete_all``` will delete all descendant nodes (which circumvents the destroy hooks)
@@ -351,6 +352,13 @@ Closure tree is [tested under every combination](http://travis-ci.org/#!/mceache
## Change log
+### 3.6.0
+
+* Added support for
+ * ```:hierarchy_class_name``` as an option
+ * ActiveRecord::Base.table_name_prefix
+ * ActiveRecord::Base.table_name_suffix
+
### 3.5.2
* Added ```find_all_by_generation```
8 Rakefile
View
@@ -16,5 +16,11 @@ RSpec::Core::RakeTask.new(:spec)
task :default => :spec
+task :specs_with_db_ixes do
+ [["", ""], ["db_prefix_", ""], ["", "_db_suffix"], ["abc_", "_123"]].each do |prefix, suffix|
+ fail unless system("rake spec DB_PREFIX=#{prefix} DB_SUFFIX=#{suffix}")
+ end
+end
+
# Run the specs using all the different database engines:
-# for DB in sqlite3 mysql postgresql ; do rake ; done
+# for DB in sqlite3 mysql postgresql ; do rake ; done
24 lib/closure_tree/acts_as_tree.rb
View
@@ -31,6 +31,8 @@ def ==(comparison_object)
alias :eql? :==
RUBY
+ self.hierarchy_class.table_name = hierarchy_table_name
+
unless order_option.nil?
include ClosureTree::DeterministicOrdering
include ClosureTree::DeterministicNumericOrdering if order_is_numeric
@@ -388,19 +390,17 @@ def name_sym
end
def hierarchy_table_name
- # We need to use the table_name, not ct_class.to_s.demodulize, because they may have overridden the table name
- # We also need to escape prefix/suffix before singularizing, and then re-add afterwards
- closure_tree_options[:hierarchy_table_name] ||= begin
- ActiveRecord::Base.table_name_prefix +
- remove_prefix_and_suffix(ct_table_name).singularize +
- "_hierarchies" +
- ActiveRecord::Base.table_name_suffix
- end
+ # We need to use the table_name, not something like ct_class.to_s.demodulize + "_hierarchies",
+ # because they may have overridden the table name, which is what we want to be consistent with
+ # in order for the schema to make sense.
+ tablename = closure_tree_options[:hierarchy_table_name] ||
+ remove_prefix_and_suffix(ct_table_name).singularize + "_hierarchies"
+
+ ActiveRecord::Base.table_name_prefix + tablename + ActiveRecord::Base.table_name_suffix
end
def hierarchy_class_name
- # We need to strip prefix and suffix from table to generate the class name
- remove_prefix_and_suffix(hierarchy_table_name).singularize.camelize
+ closure_tree_options[:hierarchy_class_name] || ct_class.to_s + "Hierarchy"
end
def quoted_hierarchy_table_name
@@ -453,10 +453,10 @@ def quoted_table_name
connection.quote_column_name ct_table_name
end
- def remove_prefix_and_suffix(table)
+ def remove_prefix_and_suffix(table_name)
prefix = Regexp.escape(ActiveRecord::Base.table_name_prefix)
suffix = Regexp.escape(ActiveRecord::Base.table_name_suffix)
- table.gsub(/^#{prefix}(.+)#{suffix}$/, "\\2")
+ table_name.gsub(/^#{prefix}(.+)#{suffix}$/, "\\1")
end
end
2  lib/closure_tree/version.rb
View
@@ -1,3 +1,3 @@
module ClosureTree
- VERSION = "3.5.2" unless defined?(::ClosureTree::VERSION)
+ VERSION = "3.6.0" unless defined?(::ClosureTree::VERSION)
end
12 spec/db/schema.rb
View
@@ -20,8 +20,8 @@
t.string "name"
end
- add_index :tag_hierarchies, [:ancestor_id, :descendant_id], :unique => true
- add_index :tag_hierarchies, [:descendant_id]
+ add_index "tag_hierarchies", [:ancestor_id, :descendant_id], :unique => true, :name => "tag_anc_desc_idx"
+ add_index "tag_hierarchies", [:descendant_id], :name => "tag_desc_idx"
create_table "users", :force => true do |t|
t.string "email"
@@ -40,8 +40,8 @@
t.integer "generations", :null => false
end
- add_index :referral_hierarchies, [:ancestor_id, :descendant_id], :unique => true
- add_index :referral_hierarchies, [:descendant_id]
+ add_index "referral_hierarchies", [:ancestor_id, :descendant_id], :unique => true, :name => "ref_anc_desc_idx"
+ add_index "referral_hierarchies", [:descendant_id], :name => "ref_desc_idx"
create_table "labels", :force => true do |t|
t.string "name"
@@ -56,8 +56,8 @@
t.integer "generations", :null => false
end
- add_index :label_hierarchies, [:ancestor_id, :descendant_id], :unique => true
- add_index :label_hierarchies, [:descendant_id]
+ add_index "label_hierarchies", [:ancestor_id, :descendant_id], :unique => true, :name => "lh_anc_desc_idx"
+ add_index "label_hierarchies", [:descendant_id], :name => "lh_desc_idx"
create_table "cuisine_types", :force => true do |t|
t.string "name"
16 spec/spec_helper.rb
View
@@ -14,23 +14,19 @@
require 'closure_tree'
-log = Logger.new(plugin_test_dir + "/debug.log")
-log.sev_threshold = Logger::DEBUG
-log.datetime_format = "%Y-%m-%d %H:%M:%S"
-log.formatter = Logger::Formatter.new
-
-ActiveRecord::Base.logger = log
+#log = Logger.new(STDOUT)
+#log.sev_threshold = Logger::DEBUG
+#ActiveRecord::Base.logger = log
require 'yaml'
require 'erb'
ENV["DB"] ||= "sqlite3mem"
-ActiveRecord::Base.table_name_prefix = "p_"
-ActiveRecord::Base.table_name_suffix = "_s"
+ActiveRecord::Base.table_name_prefix = ENV['DB_PREFIX'].to_s
+ActiveRecord::Base.table_name_suffix = ENV['DB_SUFFIX'].to_s
ActiveRecord::Base.configurations = YAML::load(ERB.new(IO.read(plugin_test_dir + "/db/database.yml")).result)
ActiveRecord::Base.establish_connection(ENV["DB"])
ActiveRecord::Migration.verbose = false
-load(File.join(plugin_test_dir, "db", "schema.rb"))
-
+require 'db/schema'
require 'support/models'
require 'rspec/rails' # TODO: clean this up-- I don't want to pull the elephant through the mouse hole just for fixture support
3  spec/support/models.rb
View
@@ -20,7 +20,8 @@ class DestroyedTag < ActiveRecord::Base
class User < ActiveRecord::Base
acts_as_tree :parent_column_name => "referrer_id",
:name_column => 'email',
- :hierarchy_table_name => 'p_referral_hierarchies_s'
+ :hierarchy_class_name => 'ReferralHierarchy',
+ :hierarchy_table_name => 'referral_hierarchies'
has_many :contracts
Please sign in to comment.
Something went wrong with that request. Please try again.