Skip to content
This repository
Browse code

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
@@ -14,7 +14,7 @@ env:
14 14 - DB=sqlite3
15 15 - DB=mysql
16 16 - DB=postgresql
17   -script: bundle exec rake
  17 +script: bundle exec rake specs_with_db_ixes
18 18
19 19 before_script:
20 20 - mysql -e 'create database closure_tree_test'
10 README.md
Source Rendered
@@ -198,7 +198,8 @@ HT: [ancestry](https://github.com/stefankroes/ancestry#arrangement) and [elhoyos
198 198 When you include ```acts_as_tree``` in your model, you can provide a hash to override the following defaults:
199 199
200 200 * ```:parent_column_name``` to override the column name of the parent foreign key in the model's table. This defaults to "parent_id".
201   -* ```:hierarchy_table_name``` to override the hierarchy table name. This defaults to the singular name of the model + "_hierarchies".
  201 +* ```:hierarchy_table_name``` to override the hierarchy class name. This defaults to the singular name of the model + "Hierarchy", like ```TagHierarchy```.
  202 +* ```:hierarchy_table_name``` to override the hierarchy table name. This defaults to the singular name of the model + "_hierarchies", like ```tag_hierarchies```.
202 203 * ```:dependent``` determines what happens when a node is destroyed. Defaults to ```nullify```.
203 204 * ```:nullify``` will simply set the parent column to null. Each child node will be considered a "root" node. This is the default.
204 205 * ```: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
351 352
352 353 ## Change log
353 354
  355 +### 3.6.0
  356 +
  357 +* Added support for
  358 + * ```:hierarchy_class_name``` as an option
  359 + * ActiveRecord::Base.table_name_prefix
  360 + * ActiveRecord::Base.table_name_suffix
  361 +
354 362 ### 3.5.2
355 363
356 364 * Added ```find_all_by_generation```
8 Rakefile
@@ -16,5 +16,11 @@ RSpec::Core::RakeTask.new(:spec)
16 16
17 17 task :default => :spec
18 18
  19 +task :specs_with_db_ixes do
  20 + [["", ""], ["db_prefix_", ""], ["", "_db_suffix"], ["abc_", "_123"]].each do |prefix, suffix|
  21 + fail unless system("rake spec DB_PREFIX=#{prefix} DB_SUFFIX=#{suffix}")
  22 + end
  23 +end
  24 +
19 25 # Run the specs using all the different database engines:
20   -# for DB in sqlite3 mysql postgresql ; do rake ; done
  26 +# for DB in sqlite3 mysql postgresql ; do rake ; done
24 lib/closure_tree/acts_as_tree.rb
@@ -31,6 +31,8 @@ def ==(comparison_object)
31 31 alias :eql? :==
32 32 RUBY
33 33
  34 + self.hierarchy_class.table_name = hierarchy_table_name
  35 +
34 36 unless order_option.nil?
35 37 include ClosureTree::DeterministicOrdering
36 38 include ClosureTree::DeterministicNumericOrdering if order_is_numeric
@@ -388,19 +390,17 @@ def name_sym
388 390 end
389 391
390 392 def hierarchy_table_name
391   - # We need to use the table_name, not ct_class.to_s.demodulize, because they may have overridden the table name
392   - # We also need to escape prefix/suffix before singularizing, and then re-add afterwards
393   - closure_tree_options[:hierarchy_table_name] ||= begin
394   - ActiveRecord::Base.table_name_prefix +
395   - remove_prefix_and_suffix(ct_table_name).singularize +
396   - "_hierarchies" +
397   - ActiveRecord::Base.table_name_suffix
398   - end
  393 + # We need to use the table_name, not something like ct_class.to_s.demodulize + "_hierarchies",
  394 + # because they may have overridden the table name, which is what we want to be consistent with
  395 + # in order for the schema to make sense.
  396 + tablename = closure_tree_options[:hierarchy_table_name] ||
  397 + remove_prefix_and_suffix(ct_table_name).singularize + "_hierarchies"
  398 +
  399 + ActiveRecord::Base.table_name_prefix + tablename + ActiveRecord::Base.table_name_suffix
399 400 end
400 401
401 402 def hierarchy_class_name
402   - # We need to strip prefix and suffix from table to generate the class name
403   - remove_prefix_and_suffix(hierarchy_table_name).singularize.camelize
  403 + closure_tree_options[:hierarchy_class_name] || ct_class.to_s + "Hierarchy"
404 404 end
405 405
406 406 def quoted_hierarchy_table_name
@@ -453,10 +453,10 @@ def quoted_table_name
453 453 connection.quote_column_name ct_table_name
454 454 end
455 455
456   - def remove_prefix_and_suffix(table)
  456 + def remove_prefix_and_suffix(table_name)
457 457 prefix = Regexp.escape(ActiveRecord::Base.table_name_prefix)
458 458 suffix = Regexp.escape(ActiveRecord::Base.table_name_suffix)
459   - table.gsub(/^#{prefix}(.+)#{suffix}$/, "\\2")
  459 + table_name.gsub(/^#{prefix}(.+)#{suffix}$/, "\\1")
460 460 end
461 461 end
462 462
2  lib/closure_tree/version.rb
... ... @@ -1,3 +1,3 @@
1 1 module ClosureTree
2   - VERSION = "3.5.2" unless defined?(::ClosureTree::VERSION)
  2 + VERSION = "3.6.0" unless defined?(::ClosureTree::VERSION)
3 3 end
12 spec/db/schema.rb
@@ -20,8 +20,8 @@
20 20 t.string "name"
21 21 end
22 22
23   - add_index :tag_hierarchies, [:ancestor_id, :descendant_id], :unique => true
24   - add_index :tag_hierarchies, [:descendant_id]
  23 + add_index "tag_hierarchies", [:ancestor_id, :descendant_id], :unique => true, :name => "tag_anc_desc_idx"
  24 + add_index "tag_hierarchies", [:descendant_id], :name => "tag_desc_idx"
25 25
26 26 create_table "users", :force => true do |t|
27 27 t.string "email"
@@ -40,8 +40,8 @@
40 40 t.integer "generations", :null => false
41 41 end
42 42
43   - add_index :referral_hierarchies, [:ancestor_id, :descendant_id], :unique => true
44   - add_index :referral_hierarchies, [:descendant_id]
  43 + add_index "referral_hierarchies", [:ancestor_id, :descendant_id], :unique => true, :name => "ref_anc_desc_idx"
  44 + add_index "referral_hierarchies", [:descendant_id], :name => "ref_desc_idx"
45 45
46 46 create_table "labels", :force => true do |t|
47 47 t.string "name"
@@ -56,8 +56,8 @@
56 56 t.integer "generations", :null => false
57 57 end
58 58
59   - add_index :label_hierarchies, [:ancestor_id, :descendant_id], :unique => true
60   - add_index :label_hierarchies, [:descendant_id]
  59 + add_index "label_hierarchies", [:ancestor_id, :descendant_id], :unique => true, :name => "lh_anc_desc_idx"
  60 + add_index "label_hierarchies", [:descendant_id], :name => "lh_desc_idx"
61 61
62 62 create_table "cuisine_types", :force => true do |t|
63 63 t.string "name"
16 spec/spec_helper.rb
@@ -14,23 +14,19 @@
14 14
15 15 require 'closure_tree'
16 16
17   -log = Logger.new(plugin_test_dir + "/debug.log")
18   -log.sev_threshold = Logger::DEBUG
19   -log.datetime_format = "%Y-%m-%d %H:%M:%S"
20   -log.formatter = Logger::Formatter.new
21   -
22   -ActiveRecord::Base.logger = log
  17 +#log = Logger.new(STDOUT)
  18 +#log.sev_threshold = Logger::DEBUG
  19 +#ActiveRecord::Base.logger = log
23 20
24 21 require 'yaml'
25 22 require 'erb'
26 23 ENV["DB"] ||= "sqlite3mem"
27   -ActiveRecord::Base.table_name_prefix = "p_"
28   -ActiveRecord::Base.table_name_suffix = "_s"
  24 +ActiveRecord::Base.table_name_prefix = ENV['DB_PREFIX'].to_s
  25 +ActiveRecord::Base.table_name_suffix = ENV['DB_SUFFIX'].to_s
29 26 ActiveRecord::Base.configurations = YAML::load(ERB.new(IO.read(plugin_test_dir + "/db/database.yml")).result)
30 27 ActiveRecord::Base.establish_connection(ENV["DB"])
31 28 ActiveRecord::Migration.verbose = false
32   -load(File.join(plugin_test_dir, "db", "schema.rb"))
33   -
  29 +require 'db/schema'
34 30 require 'support/models'
35 31 require 'rspec/rails' # TODO: clean this up-- I don't want to pull the elephant through the mouse hole just for fixture support
36 32
3  spec/support/models.rb
@@ -20,7 +20,8 @@ class DestroyedTag < ActiveRecord::Base
20 20 class User < ActiveRecord::Base
21 21 acts_as_tree :parent_column_name => "referrer_id",
22 22 :name_column => 'email',
23   - :hierarchy_table_name => 'p_referral_hierarchies_s'
  23 + :hierarchy_class_name => 'ReferralHierarchy',
  24 + :hierarchy_table_name => 'referral_hierarchies'
24 25
25 26 has_many :contracts
26 27

0 comments on commit 27a35b4

Please sign in to comment.
Something went wrong with that request. Please try again.