Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d543e16

Browse files
committedNov 14, 2014
Merge pull request #130 from eturino/has_closure_tree
Prevent name collision with other gems: allowing the use of `has_closure_tree`method besides `acts_as_tree`
2 parents b64847b + fbc8653 commit d543e16

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed
 

‎README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ for a description of different tree storage algorithms.
4343
## Table of Contents
4444

4545
- [Installation](#installation)
46+
- [Warning](#warning)
4647
- [Usage](#usage)
4748
- [Accessing Data](#accessing-data)
4849
- [Polymorphic hierarchies with STI](#polymorphic-hierarchies-with-sti)
@@ -60,19 +61,25 @@ Note that closure_tree only supports Rails 3.2 and later, and has test coverage
6061

6162
2. Run `bundle install`
6263

63-
3. Add `acts_as_tree` to your hierarchical model:
64+
3. Add `acts_as_tree` or `has_closure_tree` (alias of the same method) to your hierarchical model:
6465

6566
```ruby
6667
class Tag < ActiveRecord::Base
6768
acts_as_tree
6869
end
70+
71+
class AnotherTag < ActiveRecord::Base
72+
has_closure_tree
73+
end
6974
```
7075

7176
Make sure you check out the [large number options](#available-options) that `acts_as_tree` accepts.
7277

7378
Make sure you add `acts_as_tree` **after** `attr_accessible` and
7479
`self.table_name =` lines in your model.
7580

81+
If you have other hierarchical gems in your stack, `acts_as_tree` method may not be safe to use. See the [Warning](#warning) section bellow.
82+
7683
4. Add a migration to add a `parent_id` column to the hierarchical model.
7784
You may want to also [add a column for deterministic ordering of children](#sort_order), but that's optional.
7885

@@ -101,6 +108,12 @@ Note that closure_tree only supports Rails 3.2 and later, and has test coverage
101108
102109
If you're starting from scratch you don't need to call `rebuild!`.
103110
111+
## Warning
112+
113+
The preferred method is `acts_as_tree`. However, other gems (like [ancestry](https://github.com/stefankroes/ancestry) or [acts_as_tree](https://github.com/amerine/acts_as_tree)) may use that method name too. If you have those gems as dependencies, use the alternative `has_closure_tree` method.
114+
115+
Bear in mind that using multiple hierarchy gems in the same model may not be safe. In fact, it's a safe bet to assume that it will cause all sorts of pain, suffering, and havoc. Avoid it.
116+
104117
## Usage
105118

106119
### Creation

‎lib/closure_tree/acts_as_tree.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module ClosureTree
44
module ActsAsTree
5-
def acts_as_tree(options = {})
5+
def has_closure_tree(options = {})
66
options.assert_valid_keys(
77
:parent_column_name,
88
:dependent,
@@ -35,5 +35,7 @@ def acts_as_tree(options = {})
3535
# Support Heroku's database-less assets:precompile pre-deploy step:
3636
raise e unless ENV['DATABASE_URL'].to_s.include?('//user:pass@127.0.0.1/') && ENV['RAILS_GROUPS'] == 'assets'
3737
end
38+
39+
alias_method :acts_as_tree, :has_closure_tree
3840
end
3941
end

‎spec/db/models.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'uuidtools'
22

33
class Tag < ActiveRecord::Base
4-
acts_as_tree :dependent => :destroy, :order => :name
4+
has_closure_tree :dependent => :destroy, :order => :name
55
before_destroy :add_destroyed_tag
66
attr_accessible :name, :title if _ct.use_attr_accessible?
77

0 commit comments

Comments
 (0)
Failed to load comments.