ordered_tree 0.1.9 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc CHANGED
@@ -1 +1 @@
1
- rvm @ordered_tree
1
+ rvm use ruby-1.9.2-p290@ordered_tree
data/Gemfile CHANGED
@@ -1,10 +1,7 @@
1
1
  source "http://rubygems.org"
2
- # Add dependencies required to use your gem here.
3
- # Example:
4
- gem "activerecord", ">= 3.0.0"
5
2
 
6
- # Add dependencies to develop your gem here.
7
- # Include everything needed to run rake, tests, features, etc.
3
+ gem "activerecord", ">= 3.1.1"
4
+
8
5
  group :development do
9
6
  gem "rspec", "~> 2.6.0"
10
7
  gem "bundler", "~> 1.0.0"
data/Gemfile.lock CHANGED
@@ -1,52 +1,54 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- activemodel (3.0.7)
5
- activesupport (= 3.0.7)
6
- builder (~> 2.1.2)
7
- i18n (~> 0.5.0)
8
- activerecord (3.0.7)
9
- activemodel (= 3.0.7)
10
- activesupport (= 3.0.7)
11
- arel (~> 2.0.2)
12
- tzinfo (~> 0.3.23)
13
- activesupport (3.0.7)
14
- arel (2.0.10)
15
- builder (2.1.2)
16
- diff-lcs (1.1.2)
17
- ffi (1.0.9)
4
+ activemodel (3.1.1)
5
+ activesupport (= 3.1.1)
6
+ builder (~> 3.0.0)
7
+ i18n (~> 0.6)
8
+ activerecord (3.1.1)
9
+ activemodel (= 3.1.1)
10
+ activesupport (= 3.1.1)
11
+ arel (~> 2.2.1)
12
+ tzinfo (~> 0.3.29)
13
+ activesupport (3.1.1)
14
+ multi_json (~> 1.0)
15
+ arel (2.2.1)
16
+ builder (3.0.0)
17
+ diff-lcs (1.1.3)
18
+ ffi (1.0.10)
18
19
  git (1.2.5)
19
- guard (0.3.4)
20
+ guard (0.8.8)
20
21
  thor (~> 0.14.6)
21
- guard-rspec (0.3.1)
22
- guard (>= 0.2.2)
23
- i18n (0.5.0)
24
- jeweler (1.6.2)
22
+ guard-rspec (0.5.3)
23
+ guard (>= 0.8.4)
24
+ i18n (0.6.0)
25
+ jeweler (1.6.4)
25
26
  bundler (~> 1.0)
26
27
  git (>= 1.2.5)
27
28
  rake
28
- libnotify (0.5.5)
29
- rake (0.9.1)
30
- rb-inotify (0.8.5)
29
+ libnotify (0.5.9)
30
+ multi_json (1.0.3)
31
+ rake (0.9.2.2)
32
+ rb-inotify (0.8.8)
31
33
  ffi (>= 0.5.0)
32
- rcov (0.9.9)
34
+ rcov (0.9.11)
33
35
  rspec (2.6.0)
34
36
  rspec-core (~> 2.6.0)
35
37
  rspec-expectations (~> 2.6.0)
36
38
  rspec-mocks (~> 2.6.0)
37
- rspec-core (2.6.3)
39
+ rspec-core (2.6.4)
38
40
  rspec-expectations (2.6.0)
39
41
  diff-lcs (~> 1.1.2)
40
42
  rspec-mocks (2.6.0)
41
- sqlite3 (1.3.3)
43
+ sqlite3 (1.3.4)
42
44
  thor (0.14.6)
43
- tzinfo (0.3.27)
45
+ tzinfo (0.3.31)
44
46
 
45
47
  PLATFORMS
46
48
  ruby
47
49
 
48
50
  DEPENDENCIES
49
- activerecord (>= 3.0.0)
51
+ activerecord (>= 3.1.1)
50
52
  bundler (~> 1.0.0)
51
53
  guard-rspec
52
54
  jeweler (~> 1.6.2)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.9
1
+ 0.2.1
data/lib/ordered_tree.rb CHANGED
@@ -28,19 +28,22 @@ module OrderedTree #:nodoc:
28
28
  self.ordered_tree_config.update(options) if options.is_a?(Hash)
29
29
 
30
30
  belongs_to :parent_node,
31
- :class_name => self.name,
31
+ :class_name => self.name,
32
32
  :foreign_key => ordered_tree_config[:foreign_key],
33
33
  :primary_key => ordered_tree_config[:primary_key],
34
- :conditions => proc {scope_condition}
34
+ :conditions => proc {scope_condition}
35
35
  has_many :child_nodes,
36
- :class_name => self.name,
36
+ :class_name => self.name,
37
37
  :foreign_key => ordered_tree_config[:foreign_key],
38
38
  :primary_key => ordered_tree_config[:primary_key],
39
- :conditions => proc {scope_condition},
40
- :order => ordered_tree_config[:order]
39
+ :conditions => proc {scope_condition},
40
+ :order => ordered_tree_config[:order]
41
41
  scope :roots, lambda { |*args|
42
+ column = "#{self.table_name}.#{self.ordered_tree_config[:foreign_key].to_sym}"
42
43
  scope_condition = args[0]
43
- where(scope_condition).where(self.ordered_tree_config[:foreign_key].to_sym => 0).order(self.ordered_tree_config[:order])
44
+ where(scope_condition).
45
+ where("#{column} = 0 OR #{column} IS NULL").
46
+ order(self.ordered_tree_config[:order])
44
47
  }
45
48
 
46
49
  include OrderedTree::ClassMethods
data/ordered_tree.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ordered_tree}
8
- s.version = "0.1.9"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ramon Tayag"]
12
- s.date = %q{2011-08-07}
12
+ s.date = %q{2011-11-13}
13
13
  s.description = %q{Uses parent_id and position to create an ordered tree.}
14
14
  s.email = %q{ramon@tayag.net}
15
15
  s.extra_rdoc_files = [
@@ -50,7 +50,7 @@ Gem::Specification.new do |s|
50
50
  s.specification_version = 3
51
51
 
52
52
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
53
- s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0"])
53
+ s.add_runtime_dependency(%q<activerecord>, [">= 3.1.1"])
54
54
  s.add_development_dependency(%q<rspec>, ["~> 2.6.0"])
55
55
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
56
56
  s.add_development_dependency(%q<jeweler>, ["~> 1.6.2"])
@@ -60,7 +60,7 @@ Gem::Specification.new do |s|
60
60
  s.add_development_dependency(%q<libnotify>, [">= 0"])
61
61
  s.add_development_dependency(%q<rb-inotify>, [">= 0"])
62
62
  else
63
- s.add_dependency(%q<activerecord>, [">= 3.0.0"])
63
+ s.add_dependency(%q<activerecord>, [">= 3.1.1"])
64
64
  s.add_dependency(%q<rspec>, ["~> 2.6.0"])
65
65
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
66
66
  s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
@@ -71,7 +71,7 @@ Gem::Specification.new do |s|
71
71
  s.add_dependency(%q<rb-inotify>, [">= 0"])
72
72
  end
73
73
  else
74
- s.add_dependency(%q<activerecord>, [">= 3.0.0"])
74
+ s.add_dependency(%q<activerecord>, [">= 3.1.1"])
75
75
  s.add_dependency(%q<rspec>, ["~> 2.6.0"])
76
76
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
77
77
  s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
@@ -29,20 +29,30 @@ describe OrderedTree do
29
29
  # This is especially important for working with root items that may belong to different accounts
30
30
  it "should only work within that scope" do
31
31
  # when the scope is an association
32
- ordered_tree Page, :scope => :person do
33
- Page.create(:person => @people[0]).position.should == 1
34
- Page.create(:person => @people[2]).position.should == 1
35
- Page.create(:person => @people[0]).position.should == 2
36
- Page.create(:person => @people[1]).position.should == 1
32
+ Object.send :remove_const, 'Page'
33
+ class Page < ActiveRecord::Base
34
+ belongs_to :person
35
+ ordered_tree :scope => :person
37
36
  end
38
37
 
38
+ Page.create(:person => @people[0]).position.should == 1
39
+ Page.create(:person => @people[2]).position.should == 1
40
+ Page.create(:person => @people[0]).position.should == 2
41
+ Page.create(:person => @people[1]).position.should == 1
42
+
39
43
  # when the scope is an association id
40
- ordered_tree Page, :scope => :person_id do
41
- Page.create(:person => @people[0]).position.should == 3
42
- Page.create(:person => @people[2]).position.should == 2
43
- Page.create(:person => @people[0]).position.should == 4
44
- Page.create(:person => @people[1]).position.should == 2
44
+ Object.send :remove_const, 'Page'
45
+ class Page < ActiveRecord::Base
46
+ belongs_to :person
47
+ ordered_tree :scope => :person_id
45
48
  end
49
+ Page.create(:person => @people[0]).position.should == 3
50
+ Page.create(:person => @people[2]).position.should == 2
51
+ Page.create(:person => @people[0]).position.should == 4
52
+ Page.create(:person => @people[1]).position.should == 2
53
+
54
+ Object.send :remove_const, 'Page'
55
+ load 'fixtures/page.rb'
46
56
  end
47
57
 
48
58
  it "should only work within that scope_condition overridden method" do
@@ -73,7 +83,7 @@ describe OrderedTree do
73
83
  page_2.position.should == 1
74
84
  page_2.parent.should == nil
75
85
  page_3.position.should == 1
76
- page_3.parent.should == page_1
86
+ page_3.parent.id.should == page_1.id
77
87
  page_4.position.should == 2
78
88
  page_4.parent.should == nil
79
89
  page_5.position.should == 1
@@ -110,6 +120,15 @@ describe OrderedTree do
110
120
  @people[0].reload
111
121
  @people[2].parent != @people[7]
112
122
  end
123
+
124
+ context "and a scope and primary key are supplied" do
125
+ it "should not allow assigning a parent as one of its descendants" do
126
+ c1 = Category.create(:person_id => 1, :alt_id => 10)
127
+ c1a = Category.create(:parent_id => 10, :person_id => 1, :alt_id => 30)
128
+ (c1a.children << c1).should be_false
129
+ c1.errors[:base].should include("is an ancestor of the new parent.")
130
+ end
131
+ end
113
132
  end
114
133
 
115
134
  describe "when validate :on => :update is called," do
@@ -160,9 +179,14 @@ describe OrderedTree do
160
179
  end
161
180
 
162
181
  describe "class#roots" do
163
- it "should return all the roots" do
182
+ it "should return all people with parent_id of 0" do
164
183
  Person.roots.should == [@people[0], @people[11]]
165
184
  end
185
+
186
+ it "should return all people with parent_id of nil" do
187
+ page = Page.create :person => @people[0]
188
+ Page.roots.should include(page)
189
+ end
166
190
  end
167
191
 
168
192
  it "should reorder the list when stuff are destroyed" do
data/spec/spec_helper.rb CHANGED
@@ -4,9 +4,9 @@ require 'rubygems'
4
4
  require 'active_record'
5
5
  require 'rspec'
6
6
  require 'ordered_tree'
7
- require 'spec/fixtures/person'
8
- require 'spec/fixtures/page'
9
- require 'spec/fixtures/category'
7
+ require 'fixtures/person'
8
+ require 'fixtures/page'
9
+ require 'fixtures/category'
10
10
 
11
11
  #Allow to connect to SQLite
12
12
  ActiveRecord::Base.establish_connection(
@@ -32,19 +32,25 @@ def reset_database
32
32
  #add_index :people, [:parent_id], :name => "index_people_on_parent_id"
33
33
  end
34
34
  ActiveRecord::Base.connection.create_table(:pages) do |t|
35
- t.integer :parent_id, :null => false, :default => 0
35
+ t.integer :parent_id
36
36
  t.integer :position
37
37
  t.string :name
38
38
  t.integer :person_id
39
39
  end
40
40
  ActiveRecord::Base.connection.create_table(:categories) do |t|
41
- t.integer :parent_id, :null => false, :default => 0
41
+ t.integer :parent_id
42
42
  t.integer :position
43
43
  t.integer :alt_id
44
44
  t.integer :person_id
45
45
  end
46
46
  end
47
47
 
48
+ # Using this doesn't seem to work when >= Rails 3.1
49
+ # http://stackoverflow.com/q/7446358/61018
50
+ #
51
+ # I haven't found a real solution but will just make separate classes
52
+ # to have different configurations, so we don't have to open
53
+ # things up in the spec.
48
54
  def ordered_tree(klass, *opts)
49
55
  klass.ordered_tree *opts
50
56
  yield
metadata CHANGED
@@ -1,167 +1,124 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: ordered_tree
3
- version: !ruby/object:Gem::Version
4
- hash: 9
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 9
10
- version: 0.1.9
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Ramon Tayag
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-08-07 00:00:00 +08:00
12
+ date: 2011-11-13 00:00:00.000000000 +08:00
19
13
  default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
22
16
  name: activerecord
23
- type: :runtime
24
- version_requirements: &id001 !ruby/object:Gem::Requirement
17
+ requirement: &87969330 !ruby/object:Gem::Requirement
25
18
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 7
30
- segments:
31
- - 3
32
- - 0
33
- - 0
34
- version: 3.0.0
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 3.1.1
23
+ type: :runtime
35
24
  prerelease: false
36
- requirement: *id001
37
- - !ruby/object:Gem::Dependency
25
+ version_requirements: *87969330
26
+ - !ruby/object:Gem::Dependency
38
27
  name: rspec
39
- type: :development
40
- version_requirements: &id002 !ruby/object:Gem::Requirement
28
+ requirement: &87897880 !ruby/object:Gem::Requirement
41
29
  none: false
42
- requirements:
30
+ requirements:
43
31
  - - ~>
44
- - !ruby/object:Gem::Version
45
- hash: 23
46
- segments:
47
- - 2
48
- - 6
49
- - 0
32
+ - !ruby/object:Gem::Version
50
33
  version: 2.6.0
34
+ type: :development
51
35
  prerelease: false
52
- requirement: *id002
53
- - !ruby/object:Gem::Dependency
36
+ version_requirements: *87897880
37
+ - !ruby/object:Gem::Dependency
54
38
  name: bundler
55
- type: :development
56
- version_requirements: &id003 !ruby/object:Gem::Requirement
39
+ requirement: &87897640 !ruby/object:Gem::Requirement
57
40
  none: false
58
- requirements:
41
+ requirements:
59
42
  - - ~>
60
- - !ruby/object:Gem::Version
61
- hash: 23
62
- segments:
63
- - 1
64
- - 0
65
- - 0
43
+ - !ruby/object:Gem::Version
66
44
  version: 1.0.0
45
+ type: :development
67
46
  prerelease: false
68
- requirement: *id003
69
- - !ruby/object:Gem::Dependency
47
+ version_requirements: *87897640
48
+ - !ruby/object:Gem::Dependency
70
49
  name: jeweler
71
- type: :development
72
- version_requirements: &id004 !ruby/object:Gem::Requirement
50
+ requirement: &87897400 !ruby/object:Gem::Requirement
73
51
  none: false
74
- requirements:
52
+ requirements:
75
53
  - - ~>
76
- - !ruby/object:Gem::Version
77
- hash: 11
78
- segments:
79
- - 1
80
- - 6
81
- - 2
54
+ - !ruby/object:Gem::Version
82
55
  version: 1.6.2
56
+ type: :development
83
57
  prerelease: false
84
- requirement: *id004
85
- - !ruby/object:Gem::Dependency
58
+ version_requirements: *87897400
59
+ - !ruby/object:Gem::Dependency
86
60
  name: rcov
87
- type: :development
88
- version_requirements: &id005 !ruby/object:Gem::Requirement
61
+ requirement: &87897160 !ruby/object:Gem::Requirement
89
62
  none: false
90
- requirements:
91
- - - ">="
92
- - !ruby/object:Gem::Version
93
- hash: 3
94
- segments:
95
- - 0
96
- version: "0"
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ type: :development
97
68
  prerelease: false
98
- requirement: *id005
99
- - !ruby/object:Gem::Dependency
69
+ version_requirements: *87897160
70
+ - !ruby/object:Gem::Dependency
100
71
  name: sqlite3
101
- type: :development
102
- version_requirements: &id006 !ruby/object:Gem::Requirement
72
+ requirement: &87896910 !ruby/object:Gem::Requirement
103
73
  none: false
104
- requirements:
105
- - - ">="
106
- - !ruby/object:Gem::Version
107
- hash: 3
108
- segments:
109
- - 0
110
- version: "0"
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ type: :development
111
79
  prerelease: false
112
- requirement: *id006
113
- - !ruby/object:Gem::Dependency
80
+ version_requirements: *87896910
81
+ - !ruby/object:Gem::Dependency
114
82
  name: guard-rspec
115
- type: :development
116
- version_requirements: &id007 !ruby/object:Gem::Requirement
83
+ requirement: &87896610 !ruby/object:Gem::Requirement
117
84
  none: false
118
- requirements:
119
- - - ">="
120
- - !ruby/object:Gem::Version
121
- hash: 3
122
- segments:
123
- - 0
124
- version: "0"
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ type: :development
125
90
  prerelease: false
126
- requirement: *id007
127
- - !ruby/object:Gem::Dependency
91
+ version_requirements: *87896610
92
+ - !ruby/object:Gem::Dependency
128
93
  name: libnotify
129
- type: :development
130
- version_requirements: &id008 !ruby/object:Gem::Requirement
94
+ requirement: &87896310 !ruby/object:Gem::Requirement
131
95
  none: false
132
- requirements:
133
- - - ">="
134
- - !ruby/object:Gem::Version
135
- hash: 3
136
- segments:
137
- - 0
138
- version: "0"
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ type: :development
139
101
  prerelease: false
140
- requirement: *id008
141
- - !ruby/object:Gem::Dependency
102
+ version_requirements: *87896310
103
+ - !ruby/object:Gem::Dependency
142
104
  name: rb-inotify
143
- type: :development
144
- version_requirements: &id009 !ruby/object:Gem::Requirement
105
+ requirement: &87896010 !ruby/object:Gem::Requirement
145
106
  none: false
146
- requirements:
147
- - - ">="
148
- - !ruby/object:Gem::Version
149
- hash: 3
150
- segments:
151
- - 0
152
- version: "0"
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ type: :development
153
112
  prerelease: false
154
- requirement: *id009
113
+ version_requirements: *87896010
155
114
  description: Uses parent_id and position to create an ordered tree.
156
115
  email: ramon@tayag.net
157
116
  executables: []
158
-
159
117
  extensions: []
160
-
161
- extra_rdoc_files:
118
+ extra_rdoc_files:
162
119
  - LICENSE.txt
163
120
  - README.textile
164
- files:
121
+ files:
165
122
  - .rvmrc
166
123
  - CHANGELOG
167
124
  - Gemfile
@@ -186,37 +143,31 @@ files:
186
143
  - spec/spec_helper.rb
187
144
  has_rdoc: true
188
145
  homepage: http://github.com/ramontayag/ordered_tree
189
- licenses:
146
+ licenses:
190
147
  - MIT
191
148
  post_install_message:
192
149
  rdoc_options: []
193
-
194
- require_paths:
150
+ require_paths:
195
151
  - lib
196
- required_ruby_version: !ruby/object:Gem::Requirement
152
+ required_ruby_version: !ruby/object:Gem::Requirement
197
153
  none: false
198
- requirements:
199
- - - ">="
200
- - !ruby/object:Gem::Version
201
- hash: 3
202
- segments:
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ segments:
203
159
  - 0
204
- version: "0"
205
- required_rubygems_version: !ruby/object:Gem::Requirement
160
+ hash: 539117903
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
206
162
  none: false
207
- requirements:
208
- - - ">="
209
- - !ruby/object:Gem::Version
210
- hash: 3
211
- segments:
212
- - 0
213
- version: "0"
163
+ requirements:
164
+ - - ! '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
214
167
  requirements: []
215
-
216
168
  rubyforge_project:
217
169
  rubygems_version: 1.6.2
218
170
  signing_key:
219
171
  specification_version: 3
220
172
  summary: Gem version of Wizard's ActsAsTree
221
173
  test_files: []
222
-