Skip to content

Commit

Permalink
3.6.0: Added support for
Browse files Browse the repository at this point in the history
  * ```:hierarchy_class_name``` as an option
  * ActiveRecord::Base.table_name_prefix
  * ActiveRecord::Base.table_name_suffix
  • Loading branch information
mceachen committed Sep 17, 2012
1 parent 5203217 commit 27a35b4
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -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'
Expand Down
10 changes: 9 additions & 1 deletion README.md
Expand Up @@ -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)
Expand Down Expand Up @@ -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```
Expand Down
8 changes: 7 additions & 1 deletion Rakefile
Expand Up @@ -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 changes: 12 additions & 12 deletions lib/closure_tree/acts_as_tree.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/closure_tree/version.rb
@@ -1,3 +1,3 @@
module ClosureTree
VERSION = "3.5.2" unless defined?(::ClosureTree::VERSION)
VERSION = "3.6.0" unless defined?(::ClosureTree::VERSION)
end
12 changes: 6 additions & 6 deletions spec/db/schema.rb
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down
16 changes: 6 additions & 10 deletions spec/spec_helper.rb
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion spec/support/models.rb
Expand Up @@ -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

Expand Down

0 comments on commit 27a35b4

Please sign in to comment.