closure_tree 3.3.2 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -88,25 +88,31 @@ Child nodes are created by appending to the children collection:
88
88
  parent = grandparent.children.create(:name => 'Parent')
89
89
  ```
90
90
 
91
- You can also append to the children collection:
91
+ Or by giving the parent to the constructor:
92
92
 
93
93
  ```ruby
94
- child1 = Tag.create(:name => 'First Child')
95
- parent.children << child1
94
+ child1 = Tag.create(:name => 'First Child', :parent => parent)
96
95
  ```
97
96
 
98
- Or call the "add_child" method:
97
+ Or by appending to the children collection:
99
98
 
100
99
  ```ruby
101
- child2 = Tag.create(:name => 'Second Child')
102
- parent.add_child child2
100
+ child2 = Tag.new(:name => 'Second Child')
101
+ parent.children << child2
102
+ ```
103
+
104
+ Or by calling the "add_child" method:
105
+
106
+ ```ruby
107
+ child3 = Tag.new(:name => 'Third Child')
108
+ parent.add_child child3
103
109
  ```
104
110
 
105
111
  Then:
106
112
 
107
113
  ```ruby
108
114
  grandparent.self_and_descendants.collect(&:name)
109
- => ["Grandparent", "Parent", "First Child", "Second Child"]
115
+ => ["Grandparent", "Parent", "First Child", "Second Child", "Third Child"]
110
116
 
111
117
  child1.ancestry_path
112
118
  => ["Grandparent", "Parent", "First Child"]
@@ -116,9 +122,9 @@ child1.ancestry_path
116
122
 
117
123
  We can do all the node creation and add_child calls with one method call:
118
124
 
119
- ```ruby
120
- child = Tag.find_or_create_by_path(["grandparent", "parent", "child"])
121
- ```
125
+ ```ruby
126
+ child = Tag.find_or_create_by_path(["grandparent", "parent", "child"])
127
+ ```
122
128
 
123
129
  You can ```find``` as well as ```find_or_create``` by "ancestry paths".
124
130
  Ancestry paths may be built using any column in your model. The default
@@ -317,6 +323,12 @@ Closure tree is [tested under every combination](http://travis-ci.org/#!/mceache
317
323
 
318
324
  ## Change log
319
325
 
326
+ ### 3.4.0
327
+
328
+ Fixed [issue 15](https://github.com/mceachen/closure_tree/issues/15):
329
+ * "parent" is now attr_accessible, which adds support for constructor-provided parents.
330
+ * updated readme accordingly
331
+
320
332
  ### 3.3.2
321
333
 
322
334
  * Merged calebphillips' patch for a more efficient leaves query
@@ -47,6 +47,8 @@ module ClosureTree
47
47
  :class_name => ct_class.to_s,
48
48
  :foreign_key => parent_column_name
49
49
 
50
+ attr_accessible :parent
51
+
50
52
  has_many :children, with_order_option(
51
53
  :class_name => ct_class.to_s,
52
54
  :foreign_key => parent_column_name,
@@ -1,3 +1,3 @@
1
1
  module ClosureTree
2
- VERSION = "3.3.2" unless defined?(::ClosureTree::VERSION)
2
+ VERSION = "3.4.0" unless defined?(::ClosureTree::VERSION)
3
3
  end
@@ -1,18 +1,27 @@
1
1
  require 'spec_helper'
2
2
 
3
+ def assert_lineage(e, m)
4
+ m.parent.should == e
5
+ m.self_and_ancestors.should == [m, e]
6
+
7
+ # make sure reloading doesn't affect the self_and_ancestors:
8
+ m.reload
9
+ m.self_and_ancestors.should == [m, e]
10
+ end
11
+
3
12
  describe CuisineType do
4
- it "finds self and parents properly" do
13
+ it "finds self and parents when children << is used" do
5
14
  e = CuisineType.new(:name => "e")
6
15
  m = CuisineType.new(:name => "m")
7
16
  e.children << m
8
17
  e.save
18
+ assert_lineage(e, m)
19
+ end
9
20
 
10
- m.parent.should == e
11
- m.self_and_ancestors.should == [m, e]
12
-
13
- # make sure reloading doesn't affect the self_and_ancestors:
14
- m.reload
15
- m.self_and_ancestors.should == [m, e]
21
+ it "finds self and parents properly if the constructor is used" do
22
+ e = CuisineType.create(:name => "e")
23
+ m = CuisineType.create(:name => "m", :parent => e)
24
+ assert_lineage(e, m)
16
25
  end
17
26
 
18
27
  it "sets the table_name of the hierarchy class properly" do
@@ -40,8 +40,8 @@ class Contract < ActiveRecord::Base
40
40
  end
41
41
 
42
42
  class Label < ActiveRecord::Base
43
+ attr_accessible :name # < - make sure order doesn't matter
43
44
  acts_as_tree :order => "sort_order"
44
- attr_accessible :name
45
45
 
46
46
  def to_s
47
47
  "#{self.class}: #{name}"
@@ -1,6 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  shared_examples_for Tag do
4
+
5
+ it "has correct accessible_attributes" do
6
+ Tag.accessible_attributes.to_a.should =~ %w(parent name)
7
+ end
8
+
4
9
  describe "empty db" do
5
10
 
6
11
  def nuke_db
@@ -140,9 +145,31 @@ shared_examples_for Tag do
140
145
  b.hash_tree(:limit_depth => 3).should == b_tree
141
146
  b.hash_tree.should == b_tree
142
147
  end
148
+
149
+ it "performs as the readme says it does" do
150
+ grandparent = Tag.create(:name => 'Grandparent')
151
+ parent = grandparent.children.create(:name => 'Parent')
152
+ child1 = Tag.create(:name => 'First Child', :parent => parent)
153
+ child2 = Tag.new(:name => 'Second Child')
154
+ parent.children << child2
155
+ child3 = Tag.new(:name => 'Third Child')
156
+ parent.add_child child3
157
+ grandparent.self_and_descendants.collect(&:name).should ==
158
+ ["Grandparent", "Parent", "First Child", "Second Child", "Third Child"]
159
+ child1.ancestry_path.should ==
160
+ ["Grandparent", "Parent", "First Child"]
161
+ child3.ancestry_path.should ==
162
+ ["Grandparent", "Parent", "Third Child"]
163
+ d = Tag.find_or_create_by_path %w(a b c d)
164
+ h = Tag.find_or_create_by_path %w(e f g h)
165
+ e = h.root
166
+ d.add_child(e) # "d.children << e" would work too, of course
167
+ h.ancestry_path.should == %w(a b c d e f g h)
168
+ end
169
+
143
170
  end
144
171
 
145
- describe Tag do
172
+ describe "Tag with fixtures" do
146
173
 
147
174
  fixtures :tags
148
175
 
@@ -353,24 +380,6 @@ shared_examples_for Tag do
353
380
  city.self_and_ancestors.should == [city, tags(:california), tags(:united_states), tags(:places)]
354
381
  end
355
382
 
356
- it "performs as the readme says it does" do
357
- grandparent = Tag.create(:name => 'Grandparent')
358
- parent = grandparent.children.create(:name => 'Parent')
359
- child1 = Tag.create(:name => 'First Child')
360
- parent.children << child1
361
- child2 = Tag.create(:name => 'Second Child')
362
- parent.add_child child2
363
- grandparent.self_and_descendants.collect(&:name).should ==
364
- ["Grandparent", "Parent", "First Child", "Second Child"]
365
- child1.ancestry_path.should ==
366
- ["Grandparent", "Parent", "First Child"]
367
- d = Tag.find_or_create_by_path %w(a b c d)
368
- h = Tag.find_or_create_by_path %w(e f g h)
369
- e = h.root
370
- d.add_child(e) # "d.children << e" would work too, of course
371
- h.ancestry_path.should == %w(a b c d e f g h)
372
- end
373
-
374
383
  end
375
384
  end
376
385
 
@@ -386,3 +395,4 @@ describe "Tag with AR whitelisted attributes enabled" do
386
395
  it_behaves_like Tag
387
396
  end
388
397
 
398
+
@@ -113,7 +113,7 @@ describe "empty db" do
113
113
 
114
114
  a.save
115
115
  User.roots.should == [a]
116
- User.leaves.should == [b1, c1, d]
116
+ User.leaves.should =~ [b1, c1, d]
117
117
  d.ancestry_path.should == %w(a b2 c2 d)
118
118
  end
119
119
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: closure_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.2
4
+ version: 3.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-27 00:00:00.000000000 Z
12
+ date: 2012-09-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -35,7 +35,7 @@ dependencies:
35
35
  - - ! '>='
36
36
  - !ruby/object:Gem::Version
37
37
  version: '0'
38
- type: :development
38
+ type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
@@ -51,7 +51,7 @@ dependencies:
51
51
  - - ! '>='
52
52
  - !ruby/object:Gem::Version
53
53
  version: '0'
54
- type: :development
54
+ type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  none: false
@@ -67,7 +67,7 @@ dependencies:
67
67
  - - ! '>='
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
- type: :development
70
+ type: :runtime
71
71
  prerelease: false
72
72
  version_requirements: !ruby/object:Gem::Requirement
73
73
  none: false
@@ -83,7 +83,7 @@ dependencies:
83
83
  - - ! '>='
84
84
  - !ruby/object:Gem::Version
85
85
  version: '0'
86
- type: :development
86
+ type: :runtime
87
87
  prerelease: false
88
88
  version_requirements: !ruby/object:Gem::Requirement
89
89
  none: false
@@ -99,7 +99,7 @@ dependencies:
99
99
  - - ! '>='
100
100
  - !ruby/object:Gem::Version
101
101
  version: '0'
102
- type: :development
102
+ type: :runtime
103
103
  prerelease: false
104
104
  version_requirements: !ruby/object:Gem::Requirement
105
105
  none: false
@@ -115,7 +115,7 @@ dependencies:
115
115
  - - ! '>='
116
116
  - !ruby/object:Gem::Version
117
117
  version: '0'
118
- type: :development
118
+ type: :runtime
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  none: false
@@ -131,7 +131,7 @@ dependencies:
131
131
  - - ! '>='
132
132
  - !ruby/object:Gem::Version
133
133
  version: '0'
134
- type: :development
134
+ type: :runtime
135
135
  prerelease: false
136
136
  version_requirements: !ruby/object:Gem::Requirement
137
137
  none: false
@@ -147,7 +147,7 @@ dependencies:
147
147
  - - ! '>='
148
148
  - !ruby/object:Gem::Version
149
149
  version: '0'
150
- type: :development
150
+ type: :runtime
151
151
  prerelease: false
152
152
  version_requirements: !ruby/object:Gem::Requirement
153
153
  none: false
@@ -155,9 +155,7 @@ dependencies:
155
155
  - - ! '>='
156
156
  - !ruby/object:Gem::Version
157
157
  version: '0'
158
- description: ! " A mostly-API-compatible replacement for the acts_as_tree and awesome_nested_set
159
- gems,\n but with much better mutation performance thanks to the Closure Tree
160
- storage algorithm\n"
158
+ description: Easily and efficiently make your ActiveRecord model support hierarchies
161
159
  email:
162
160
  - matthew-github@mceachen.org
163
161
  executables: []
@@ -194,7 +192,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
194
192
  version: '0'
195
193
  segments:
196
194
  - 0
197
- hash: 2505204839193156816
195
+ hash: 1501864790180733571
198
196
  required_rubygems_version: !ruby/object:Gem::Requirement
199
197
  none: false
200
198
  requirements:
@@ -203,13 +201,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
203
201
  version: '0'
204
202
  segments:
205
203
  - 0
206
- hash: 2505204839193156816
204
+ hash: 1501864790180733571
207
205
  requirements: []
208
206
  rubyforge_project:
209
207
  rubygems_version: 1.8.23
210
208
  signing_key:
211
209
  specification_version: 3
212
- summary: Hierarchies for ActiveRecord models using a Closure Tree storage algorithm
210
+ summary: Easily and efficiently make your ActiveRecord model support hierarchies
213
211
  test_files:
214
212
  - spec/cuisine_type_spec.rb
215
213
  - spec/db/database.yml