acts_as_restful_list 0.5.0 → 0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,9 @@
1
+ === Version 0.6.0 / 2011-07-18
2
+
3
+ * enhancements
4
+ * Automatically initialize the order of the list under conditions that would
5
+ otherwise break things. Thanks evizitei!!
6
+
1
7
  === Version 0.5.0 / 2011-02-08
2
8
 
3
9
  * enhancements
@@ -21,4 +27,4 @@
21
27
  === Version 0.0.3 / 2010-02-14
22
28
 
23
29
  * enhancements
24
- * can set the position column
30
+ * can set the position column
@@ -57,19 +57,24 @@ module ActsAsRestfulList
57
57
 
58
58
  module InstanceMethods
59
59
  def set_position
60
- last_record = self.class.last( :conditions => scope_condition, :order => "#{position_column} ASC" )
61
- self.send( "#{position_column}=", ( last_record.nil? ? 1 : last_record.send(position_column) + 1 ) )
60
+ initialize_order if !last_record.nil? and last_record_position.nil?
61
+
62
+ self.send( "#{position_column}=", last_record.nil? ? 1 : last_record_position + 1)
62
63
  end
63
64
 
64
65
  def reset_order_after_update
65
- if scope_condition != scope_condition_was
66
- self.class.update_all("#{position_column} = (#{position_column} - 1) #{optimistic_locking_update}", [scope_condition_was, "#{position_column} > #{self.send( "#{position_column}_was" )}", "id != #{id}"].compact.join(' AND '))
67
- self.class.update_all("#{position_column} = (#{position_column} + 1) #{optimistic_locking_update}", [scope_condition, "#{position_column} >= #{self.send( position_column )}", "id != #{id}"].compact.join(' AND '))
68
- elsif self.send( "#{position_column}_changed?" )
69
- if self.send( "#{position_column}_was" ) > self.send( position_column )
70
- self.class.update_all("#{position_column} = (#{position_column} + 1) #{optimistic_locking_update}", [scope_condition, "#{position_column} >= #{self.send( position_column )}", "id != #{id}", "#{position_column} < #{self.send( "#{position_column}_was" )}"].compact.join(' AND '))
71
- else
72
- self.class.update_all("#{position_column} = (#{position_column} - 1) #{optimistic_locking_update}", [scope_condition, "#{position_column} <= #{self.send( position_column )}", "#{position_column} >= #{self.send( "#{position_column}_was" )}", "id != #{id}"].compact.join(' AND '))
66
+ if previous_position.nil?
67
+ initialize_order
68
+ else
69
+ if scope_condition != scope_condition_was
70
+ self.class.update_all( decrement_position_sql, [scope_condition_was, "#{position_column} > #{previous_position}", "id != #{id}"].compact.join(' AND '))
71
+ self.class.update_all( increment_position_sql, [scope_condition, "#{position_column} >= #{current_position}", "id != #{id}"].compact.join(' AND '))
72
+ elsif self.send( "#{position_column}_changed?" )
73
+ if previous_position > current_position
74
+ self.class.update_all( increment_position_sql, [scope_condition, "#{position_column} >= #{current_position}", "id != #{id}", "#{position_column} < #{previous_position}"].compact.join(' AND '))
75
+ else
76
+ self.class.update_all( decrement_position_sql, [scope_condition, "#{position_column} <= #{current_position}", "#{position_column} >= #{previous_position}", "id != #{id}"].compact.join(' AND '))
77
+ end
73
78
  end
74
79
  end
75
80
  end
@@ -77,6 +82,42 @@ module ActsAsRestfulList
77
82
  def reset_order_after_destroy
78
83
  self.class.update_all("#{position_column} = (#{position_column} - 1) #{optimistic_locking_update}", [scope_condition, "#{position_column} > #{self.send( position_column )}"].compact.join(' AND '))
79
84
  end
85
+
86
+ def initialize_order
87
+ initial_set = self.class.find(:all,:conditions=>scope_condition,:select=>"id",:order=>"created_at ASC")
88
+
89
+ initial_set.each_with_index do |item,idx|
90
+ ActiveRecord::Base.connection.execute("update #{self.class.table_name} set position = #{idx + 1} where id = #{item.id};")
91
+ end
92
+ end
93
+
94
+ def last_record
95
+ self.class.last( :conditions => scope_condition, :order => "#{position_column} ASC" )
96
+ end
97
+
98
+ def last_record_position
99
+ last_record.send(position_column)
100
+ end
101
+
102
+ def current_position
103
+ self.send( position_column )
104
+ end
105
+
106
+ def current_position=(value)
107
+ self.send( "#{position_column}=", value )
108
+ end
109
+
110
+ def previous_position
111
+ self.send( "#{position_column}_was" )
112
+ end
113
+
114
+ def increment_position_sql
115
+ "#{position_column} = (#{position_column} + 1) #{optimistic_locking_update}"
116
+ end
117
+
118
+ def decrement_position_sql
119
+ "#{position_column} = (#{position_column} - 1) #{optimistic_locking_update}"
120
+ end
80
121
  end
81
122
  end
82
123
 
metadata CHANGED
@@ -1,39 +1,51 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_restful_list
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 5
9
- - 0
10
- version: 0.5.0
4
+ prerelease:
5
+ version: "0.6"
11
6
  platform: ruby
12
7
  authors:
13
- - "'Trey Bean'"
8
+ - Trey Bean
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-02-08 00:00:00 -07:00
13
+ date: 2011-07-18 00:00:00 -06:00
19
14
  default_executable:
20
15
  dependencies:
21
16
  - !ruby/object:Gem::Dependency
22
17
  name: rspec
23
- prerelease: false
24
18
  requirement: &id001 !ruby/object:Gem::Requirement
25
19
  none: false
26
20
  requirements:
27
21
  - - ">="
28
22
  - !ruby/object:Gem::Version
29
- hash: 13
30
- segments:
31
- - 1
32
- - 2
33
- - 9
34
- version: 1.2.9
23
+ version: "0"
35
24
  type: :development
25
+ prerelease: false
36
26
  version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: sqlite3
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
37
49
  description: Just like acts_as_list, but allows updating through standard restful methods.
38
50
  email: trey@12spokes.com
39
51
  executables: []
@@ -44,19 +56,13 @@ extra_rdoc_files:
44
56
  - LICENSE
45
57
  - README.rdoc
46
58
  files:
47
- - .document
59
+ - lib/acts_as_restful_list.rb
48
60
  - .gitignore
49
61
  - History.rdoc
50
62
  - LICENSE
51
63
  - README.rdoc
52
- - Rakefile
53
64
  - Todo.rdoc
54
65
  - VERSION
55
- - acts_as_restful_list.gemspec
56
- - lib/acts_as_restful_list.rb
57
- - spec/acts_as_restful_list_spec.rb
58
- - spec/spec.opts
59
- - spec/spec_helper.rb
60
66
  has_rdoc: true
61
67
  homepage: http://github.com/12spokes/acts_as_restful_list
62
68
  licenses: []
@@ -71,7 +77,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
71
77
  requirements:
72
78
  - - ">="
73
79
  - !ruby/object:Gem::Version
74
- hash: 3
80
+ hash: -2292716820751671520
75
81
  segments:
76
82
  - 0
77
83
  version: "0"
@@ -80,17 +86,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
86
  requirements:
81
87
  - - ">="
82
88
  - !ruby/object:Gem::Version
83
- hash: 3
84
- segments:
85
- - 0
86
89
  version: "0"
87
90
  requirements: []
88
91
 
89
92
  rubyforge_project:
90
- rubygems_version: 1.3.7
93
+ rubygems_version: 1.6.2
91
94
  signing_key:
92
95
  specification_version: 3
93
96
  summary: Restful acts_as_list
94
- test_files:
95
- - spec/acts_as_restful_list_spec.rb
96
- - spec/spec_helper.rb
97
+ test_files: []
98
+
data/.document DELETED
@@ -1,5 +0,0 @@
1
- README.rdoc
2
- lib/**/*.rb
3
- bin/*
4
- features/**/*.feature
5
- LICENSE
data/Rakefile DELETED
@@ -1,45 +0,0 @@
1
- require 'rubygems'
2
- require 'rake'
3
-
4
- begin
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |gem|
7
- gem.name = "acts_as_restful_list"
8
- gem.summary = %Q{Restful acts_as_list}
9
- gem.description = %Q{Just like acts_as_list, but allows updating through standard restful methods.}
10
- gem.email = "trey@12spokes.com"
11
- gem.homepage = "http://github.com/12spokes/acts_as_restful_list"
12
- gem.authors = ["'Trey Bean'"]
13
- gem.add_development_dependency "rspec", ">= 1.2.9"
14
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
- end
16
- Jeweler::GemcutterTasks.new
17
- rescue LoadError
18
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
- end
20
-
21
- require 'spec/rake/spectask'
22
- Spec::Rake::SpecTask.new(:spec) do |spec|
23
- spec.libs << 'lib' << 'spec'
24
- spec.spec_files = FileList['spec/**/*_spec.rb']
25
- end
26
-
27
- Spec::Rake::SpecTask.new(:rcov) do |spec|
28
- spec.libs << 'lib' << 'spec'
29
- spec.pattern = 'spec/**/*_spec.rb'
30
- spec.rcov = true
31
- end
32
-
33
- task :spec => :check_dependencies
34
-
35
- task :default => :spec
36
-
37
- require 'rake/rdoctask'
38
- Rake::RDocTask.new do |rdoc|
39
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
-
41
- rdoc.rdoc_dir = 'rdoc'
42
- rdoc.title = "acts_as_restful_list #{version}"
43
- rdoc.rdoc_files.include('README*')
44
- rdoc.rdoc_files.include('lib/**/*.rb')
45
- end
@@ -1,57 +0,0 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
- # -*- encoding: utf-8 -*-
5
-
6
- Gem::Specification.new do |s|
7
- s.name = %q{acts_as_restful_list}
8
- s.version = "0.5.0"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["'Trey Bean'"]
12
- s.date = %q{2011-02-08}
13
- s.description = %q{Just like acts_as_list, but allows updating through standard restful methods.}
14
- s.email = %q{trey@12spokes.com}
15
- s.extra_rdoc_files = [
16
- "LICENSE",
17
- "README.rdoc"
18
- ]
19
- s.files = [
20
- ".document",
21
- ".gitignore",
22
- "History.rdoc",
23
- "LICENSE",
24
- "README.rdoc",
25
- "Rakefile",
26
- "Todo.rdoc",
27
- "VERSION",
28
- "acts_as_restful_list.gemspec",
29
- "lib/acts_as_restful_list.rb",
30
- "spec/acts_as_restful_list_spec.rb",
31
- "spec/spec.opts",
32
- "spec/spec_helper.rb"
33
- ]
34
- s.homepage = %q{http://github.com/12spokes/acts_as_restful_list}
35
- s.rdoc_options = ["--charset=UTF-8"]
36
- s.require_paths = ["lib"]
37
- s.rubygems_version = %q{1.3.7}
38
- s.summary = %q{Restful acts_as_list}
39
- s.test_files = [
40
- "spec/acts_as_restful_list_spec.rb",
41
- "spec/spec_helper.rb"
42
- ]
43
-
44
- if s.respond_to? :specification_version then
45
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
- s.specification_version = 3
47
-
48
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
- s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
50
- else
51
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
52
- end
53
- else
54
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
55
- end
56
- end
57
-
@@ -1,524 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe "ActsAsRestfulList" do
4
- after(:each) do
5
- ActiveRecord::Base.connection.execute("DELETE FROM mixins")
6
- ActiveRecord::Base.connection.execute("DELETE FROM sqlite_sequence where name='mixins'")
7
- end
8
-
9
- describe 'standard declaration with no options' do
10
- before(:all) do
11
- ActiveRecord::Schema.define(:version => 1) do
12
- create_table :mixins do |t|
13
- t.column :position, :integer
14
- t.column :parent_id, :integer
15
- t.column :created_at, :datetime
16
- t.column :updated_at, :datetime
17
- end
18
- end
19
-
20
- class Mixin < ActiveRecord::Base
21
- acts_as_restful_list
22
- end
23
- end
24
-
25
- after(:all) do
26
- Object.send(:remove_const, :Mixin)
27
-
28
- ActiveRecord::Base.connection.tables.each do |table|
29
- ActiveRecord::Base.connection.drop_table(table)
30
- end
31
- end
32
-
33
- it "should return position as it's position column" do
34
- Mixin.new.position_column.should == 'position'
35
- end
36
-
37
- it 'should set the position before creating the record' do
38
- mixin = Mixin.new
39
- mixin.should_receive(:set_position).and_return(true)
40
- mixin.save!
41
- end
42
-
43
- it 'should save the first record with position 1' do
44
- Mixin.create!.position.should == 1
45
- end
46
-
47
- it 'should put each new record at the end of the list' do
48
- (1..4).each do |n|
49
- Mixin.create!.position.should == n
50
- end
51
- end
52
-
53
- describe 'reordering on update' do
54
- before(:each) do
55
- (1..4).each{ Mixin.create! }
56
- end
57
-
58
- it 'should reset order after updating a record' do
59
- mixin = Mixin.create
60
- mixin.should_receive(:reset_order_after_update).and_return(true)
61
- mixin.save!
62
- end
63
-
64
- it 'should automatically reorder the list if a record is updated with a lower position' do
65
- fourth_mixin = Mixin.first( :conditions => { :position => 4 } )
66
- fourth_mixin.position = 2
67
- fourth_mixin.save!
68
- fourth_mixin.reload.position.should == 2
69
- Mixin.all(:order => 'position ASC').collect(&:position).should == [1,2,3,4]
70
- end
71
-
72
- it 'should reorder the list correctly if a record in the middle is updated with a lower position' do
73
- third_mixin = Mixin.first( :conditions => { :position => 3 } )
74
- third_mixin.position = 2
75
- third_mixin.save!
76
- third_mixin.reload.position.should == 2
77
- Mixin.all(:order => 'position ASC').collect(&:position).should == [1,2,3,4]
78
- end
79
-
80
- it 'should automatically reorder the list if a record is updated with a higher position' do
81
- second_mixin = Mixin.first( :conditions => { :position => 2 } )
82
- second_mixin.position = 4
83
- second_mixin.save!
84
- second_mixin.reload.position.should == 4
85
- Mixin.all(:order => 'position ASC').collect(&:position).should == [1,2,3,4]
86
- end
87
- end
88
-
89
- describe 'reordering on deletion' do
90
- it 'should reset the order after deleting a record' do
91
- mixin = Mixin.create
92
- mixin.should_receive(:reset_order_after_destroy).and_return(true)
93
- mixin.destroy
94
- end
95
-
96
- it 'should automatically reorder the list if the record is deleted' do
97
- (1..4).each{ Mixin.create! }
98
- second_mixin = Mixin.first( :conditions => { :position => 2 } )
99
- second_mixin.destroy
100
- Mixin.all(:order => 'position ASC').collect(&:position).should == [1,2,3]
101
- end
102
- end
103
-
104
- it 'should return nil for scope_condition since it was not set' do
105
- Mixin.new.scope_condition.should be_nil
106
- end
107
- end
108
-
109
- describe 'optimistic locking' do
110
- before(:all) do
111
- ActiveRecord::Schema.define(:version => 1) do
112
- create_table :mixins do |t|
113
- t.column :position, :integer
114
- t.column :parent_id, :integer
115
- t.column :lock_version, :integer, :default => 0
116
- t.column :created_at, :datetime
117
- t.column :updated_at, :datetime
118
- end
119
- end
120
-
121
- class Mixin < ActiveRecord::Base
122
- acts_as_restful_list
123
- end
124
- end
125
-
126
- after(:all) do
127
- Object.send(:remove_const, :Mixin)
128
-
129
- ActiveRecord::Base.connection.tables.each do |table|
130
- ActiveRecord::Base.connection.drop_table(table)
131
- end
132
- end
133
-
134
- before(:each) do
135
- (1..4).each{ Mixin.create! }
136
- end
137
-
138
- describe 'reordering on destroy' do
139
- it 'should raise an error for stale objects' do
140
- second_mixin = Mixin.first( :conditions => { :position => 2 } )
141
- third_mixin = Mixin.first( :conditions => { :position => 3 } )
142
- second_mixin.destroy
143
- lambda {
144
- third_mixin.destroy
145
- }.should raise_error(ActiveRecord::StaleObjectError)
146
- end
147
-
148
- it 'should NOT raise an error if update did not affect existing position' do
149
- second_mixin = Mixin.first( :conditions => { :position => 2 } )
150
- third_mixin = Mixin.first( :conditions => { :position => 3 } )
151
- third_mixin.destroy
152
- lambda {
153
- second_mixin.destroy
154
- }.should_not raise_error(ActiveRecord::StaleObjectError)
155
- Mixin.all(:order => 'position ASC').collect(&:position).should == [1,2]
156
- end
157
- end
158
-
159
- describe 'reordering on update' do
160
- it 'should raise an error for stale objects' do
161
- first_mixin = Mixin.first( :conditions => { :position => 1 } )
162
- fourth_mixin = Mixin.first( :conditions => { :position => 4 } )
163
- fourth_mixin.update_attributes(:position => 1)
164
- lambda {
165
- first_mixin.update_attributes(:position => 2)
166
- }.should raise_error(ActiveRecord::StaleObjectError)
167
- end
168
-
169
- it 'should NOT raise an error if update did not affect existing position' do
170
- first_mixin = Mixin.first( :conditions => { :position => 1 } )
171
- fourth_mixin = Mixin.first( :conditions => { :position => 4 } )
172
- fourth_mixin.update_attributes(:position => 2)
173
- lambda {
174
- first_mixin.update_attributes(:position => 3)
175
- }.should_not raise_error(ActiveRecord::StaleObjectError)
176
- Mixin.all(:order => 'position ASC').collect(&:position).should == [1,2,3,4]
177
- end
178
- end
179
-
180
- end
181
-
182
- describe 'declaring acts_as_restful_list and setting the column' do
183
- before(:all) do
184
- ActiveRecord::Schema.define(:version => 1) do
185
- create_table :mixins do |t|
186
- t.column :pos, :integer
187
- t.column :parent_id, :integer
188
- t.column :created_at, :datetime
189
- t.column :updated_at, :datetime
190
- end
191
- end
192
-
193
- class Mixin < ActiveRecord::Base
194
- acts_as_restful_list :column => :pos
195
- end
196
- end
197
-
198
- after(:all) do
199
- Object.send(:remove_const, :Mixin)
200
-
201
- ActiveRecord::Base.connection.tables.each do |table|
202
- ActiveRecord::Base.connection.drop_table(table)
203
- end
204
- end
205
-
206
- it "should return pos as it's position column" do
207
- Mixin.new.position_column.should == 'pos'
208
- end
209
-
210
- it 'should set the position before creating the record' do
211
- mixin = Mixin.new
212
- mixin.should_receive(:set_position).and_return(true)
213
- mixin.save!
214
- end
215
-
216
- it 'should save the first record with position 1' do
217
- Mixin.create!.pos.should == 1
218
- end
219
-
220
- it 'should put each new record at the end of the list' do
221
- (1..4).each do |n|
222
- Mixin.create!.pos.should == n
223
- end
224
- end
225
-
226
- describe 'reordering on update' do
227
- before(:each) do
228
- (1..4).each{ Mixin.create! }
229
- end
230
-
231
- it 'should reset order after updating a record' do
232
- mixin = Mixin.create
233
- mixin.should_receive(:reset_order_after_update).and_return(true)
234
- mixin.save!
235
- end
236
-
237
- it 'should automatically reorder the list if a record is updated with a lower position' do
238
- fourth_mixin = Mixin.first( :conditions => { :pos => 4 } )
239
- fourth_mixin.pos = 2
240
- fourth_mixin.save!
241
- fourth_mixin.reload.pos.should == 2
242
- Mixin.all(:order => 'pos ASC').collect(&:pos).should == [1,2,3,4]
243
- end
244
-
245
- it 'should automatically reorder the list if a record is updated with a higher position' do
246
- second_mixin = Mixin.first( :conditions => { :pos => 2 } )
247
- second_mixin.pos = 4
248
- second_mixin.save!
249
- second_mixin.reload.pos.should == 4
250
- Mixin.all(:order => 'pos ASC').collect(&:pos).should == [1,2,3,4]
251
- end
252
- end
253
-
254
- describe 'reordering on deletion' do
255
- it 'should reset the order after deleting a record' do
256
- mixin = Mixin.create
257
- mixin.should_receive(:reset_order_after_destroy).and_return(true)
258
- mixin.destroy
259
- end
260
-
261
- it 'should automatically reorder the list if the record is deleted' do
262
- (1..4).each{ Mixin.create! }
263
- second_mixin = Mixin.first( :conditions => { :pos => 2 } )
264
- second_mixin.destroy
265
- Mixin.all(:order => 'pos ASC').collect(&:pos).should == [1,2,3]
266
- end
267
- end
268
- end
269
-
270
- describe 'declaring acts_as_restful_list and setting the scope' do
271
- before(:all) do
272
- ActiveRecord::Schema.define(:version => 1) do
273
- create_table :mixins do |t|
274
- t.column :position, :integer
275
- t.column :parent_id, :integer
276
- t.column :created_at, :datetime
277
- t.column :updated_at, :datetime
278
- end
279
- end
280
-
281
- class Mixin < ActiveRecord::Base
282
- acts_as_restful_list :scope => :parent_id
283
- end
284
- end
285
-
286
- after(:all) do
287
- Object.send(:remove_const, :Mixin)
288
-
289
- ActiveRecord::Base.connection.tables.each do |table|
290
- ActiveRecord::Base.connection.drop_table(table)
291
- end
292
- end
293
-
294
- it 'should define scope_condition as an instance method' do
295
- Mixin.new.should respond_to(:scope_condition)
296
- end
297
-
298
- it 'should return a scope condition that limits based on the parent_id' do
299
- Mixin.new(:parent_id => 3).scope_condition.should == "parent_id = 3"
300
- end
301
-
302
- it 'should return a scope limiting based parent_id being NULL if parent_id is nil' do
303
- Mixin.new.scope_condition.should == "parent_id IS NULL"
304
- end
305
-
306
- it 'should set the position based on the scope list when adding a new item' do
307
- Mixin.create!.position.should == 1
308
- Mixin.create!(:parent_id => 1).position.should == 1
309
- Mixin.create!(:parent_id => 1).position.should == 2
310
- Mixin.create!(:parent_id => 2).position.should == 1
311
- end
312
-
313
- describe 'reordering on update' do
314
- before(:each) do
315
- (1..4).each{ Mixin.create!(:parent_id => 1) }
316
- (1..6).each{ Mixin.create!(:parent_id => 2) }
317
- end
318
-
319
- it 'should automatically reorder the list if a record is updated with a lower position' do
320
- fourth_mixin = Mixin.first( :conditions => { :position => 4, :parent_id => 1 } )
321
- fourth_mixin.position = 2
322
- fourth_mixin.save!
323
- fourth_mixin.reload.position.should == 2
324
- Mixin.all(:conditions => { :parent_id => 1 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
325
- Mixin.all(:conditions => { :parent_id => 2 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4,5,6]
326
- end
327
-
328
- it 'should automatically reorder the list if a record is updated with a higher position' do
329
- second_mixin = Mixin.first( :conditions => { :position => 2, :parent_id => 1 } )
330
- second_mixin.position = 4
331
- second_mixin.save!
332
- second_mixin.reload.position.should == 4
333
- Mixin.all(:conditions => { :parent_id => 1 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
334
- Mixin.all(:conditions => { :parent_id => 2 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4,5,6]
335
- end
336
-
337
- it 'should report the old and new scope correctly' do
338
- second_mixin = Mixin.first( :conditions => { :position => 2, :parent_id => 1 } )
339
- second_mixin.parent_id = 2
340
- second_mixin.position = 4
341
- second_mixin.scope_condition_was.should == 'parent_id = 1'
342
- second_mixin.scope_condition.should == 'parent_id = 2'
343
- end
344
-
345
- it 'should automatically reorder both lists if a record is moved between them' do
346
- second_mixin = Mixin.first( :conditions => { :position => 2, :parent_id => 1 } )
347
- second_mixin.parent_id = 2
348
- second_mixin.position = 4
349
- second_mixin.save!
350
- second_mixin.reload.parent_id.should == 2
351
- second_mixin.reload.position.should == 4
352
- Mixin.all(:conditions => { :parent_id => 1 }, :order => 'position ASC').collect(&:position).should == [1,2,3]
353
- Mixin.all(:conditions => { :parent_id => 2 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4,5,6,7]
354
- end
355
- end
356
-
357
- it 'should automatically reorder the list scoped by parent if the record is deleted' do
358
- (1..4).each{ Mixin.create!(:parent_id => 1) }
359
- (1..6).each{ Mixin.create!(:parent_id => 2) }
360
- second_mixin = Mixin.first( :conditions => { :position => 2, :parent_id => 1 } )
361
- second_mixin.destroy
362
- Mixin.all(:conditions => { :parent_id => 1 }, :order => 'position ASC').collect(&:position).should == [1,2,3]
363
- Mixin.all(:conditions => { :parent_id => 2 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4,5,6]
364
- end
365
- end
366
-
367
- describe 'declaring acts_as_restful_list and setting the scope without the _id' do
368
- before(:all) do
369
- ActiveRecord::Schema.define(:version => 1) do
370
- create_table :mixins do |t|
371
- t.column :position, :integer
372
- t.column :parent_id, :integer
373
- t.column :created_at, :datetime
374
- t.column :updated_at, :datetime
375
- end
376
- end
377
-
378
- class Mixin < ActiveRecord::Base
379
- acts_as_restful_list :scope => :parent
380
- end
381
- end
382
-
383
- after(:all) do
384
- Object.send(:remove_const, :Mixin)
385
-
386
- ActiveRecord::Base.connection.tables.each do |table|
387
- ActiveRecord::Base.connection.drop_table(table)
388
- end
389
- end
390
-
391
- it 'should define scope_condition as an instance method' do
392
- Mixin.new.should respond_to(:scope_condition)
393
- end
394
-
395
- it 'should return a scope condition that limits based on the parent_id' do
396
- Mixin.new(:parent_id => 3).scope_condition.should == "parent_id = 3"
397
- end
398
- end
399
-
400
-
401
- describe 'declaring acts_as_restful_list and setting the scope on a column that is not an _id column' do
402
- before(:all) do
403
- ActiveRecord::Schema.define(:version => 1) do
404
- create_table :mixins do |t|
405
- t.column :position, :integer
406
- t.column :parent_name, :string
407
- t.column :created_at, :datetime
408
- t.column :updated_at, :datetime
409
- end
410
- end
411
-
412
- class Mixin < ActiveRecord::Base
413
- acts_as_restful_list :scope => :parent_name
414
- end
415
- end
416
-
417
- after(:all) do
418
- Object.send(:remove_const, :Mixin)
419
-
420
- ActiveRecord::Base.connection.tables.each do |table|
421
- ActiveRecord::Base.connection.drop_table(table)
422
- end
423
- end
424
-
425
- it 'should define scope_condition as an instance method' do
426
- Mixin.new.should respond_to(:scope_condition)
427
- end
428
-
429
- it 'should return a scope condition that limits based on the parent_id' do
430
- Mixin.new(:parent_name => 'Brandy').scope_condition.should == "parent_name = 'Brandy'"
431
- end
432
- end
433
-
434
-
435
- describe 'declaring acts_as_restful_list and setting the scope to multiple columns' do
436
- before(:all) do
437
- ActiveRecord::Schema.define(:version => 1) do
438
- create_table :mixins do |t|
439
- t.column :position, :integer
440
- t.column :user_id, :integer
441
- t.column :parent_id, :integer
442
- t.column :created_at, :datetime
443
- t.column :updated_at, :datetime
444
- end
445
- end
446
-
447
- class Mixin < ActiveRecord::Base
448
- acts_as_restful_list :scope => [:parent, :user]
449
- end
450
- end
451
-
452
- after(:all) do
453
- Object.send(:remove_const, :Mixin)
454
-
455
- ActiveRecord::Base.connection.tables.each do |table|
456
- ActiveRecord::Base.connection.drop_table(table)
457
- end
458
- end
459
-
460
- it 'should define scope_condition as an instance method' do
461
- Mixin.new.should respond_to(:scope_condition)
462
- end
463
-
464
- it 'should return a scope condition that limits based on the parent_id' do
465
- Mixin.new(:user_id => 4, :parent_id => 3).scope_condition.should == "parent_id = 3 AND user_id = 4"
466
- end
467
-
468
- describe 'reordering on update' do
469
- before(:each) do
470
- (1..4).each{ Mixin.create!(:parent_id => 1, :user_id => 5) }
471
- (1..4).each{ Mixin.create!(:parent_id => 2, :user_id => 5) }
472
- (1..4).each{ Mixin.create!(:parent_id => 1, :user_id => 7) }
473
- (1..4).each{ Mixin.create!(:parent_id => 2, :user_id => 7) }
474
- end
475
-
476
- it 'should automatically reorder the list if a record is updated with a lower position' do
477
- user5_parent1_fourth_mixin = Mixin.first( :conditions => { :position => 4, :parent_id => 1, :user_id => 5 } )
478
- user5_parent1_fourth_mixin.position = 2
479
- user5_parent1_fourth_mixin.save!
480
- user5_parent1_fourth_mixin.reload.position.should == 2
481
- Mixin.all(:conditions => { :parent_id => 1, :user_id => 5 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
482
- Mixin.all(:conditions => { :parent_id => 1, :user_id => 5 }, :order => 'position ASC').collect(&:id).should == [1,4,2,3]
483
- Mixin.all(:conditions => { :parent_id => 2, :user_id => 5 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
484
- Mixin.all(:conditions => { :parent_id => 2, :user_id => 5 }, :order => 'position ASC').collect(&:id).should == [5,6,7,8]
485
- Mixin.all(:conditions => { :parent_id => 1, :user_id => 7 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
486
- Mixin.all(:conditions => { :parent_id => 1, :user_id => 7 }, :order => 'position ASC').collect(&:id).should == [9,10,11,12]
487
- Mixin.all(:conditions => { :parent_id => 2, :user_id => 7 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
488
- Mixin.all(:conditions => { :parent_id => 2, :user_id => 7 }, :order => 'position ASC').collect(&:id).should == [13,14,15,16]
489
- end
490
-
491
- it 'should automatically reorder the list if a record is updated with a higher position' do
492
- second_mixin = Mixin.first( :conditions => { :position => 2, :parent_id => 1, :user_id => 5 } )
493
- second_mixin.position = 4
494
- second_mixin.save!
495
- second_mixin.reload.position.should == 4
496
- Mixin.all(:conditions => { :parent_id => 1, :user_id => 5 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
497
- Mixin.all(:conditions => { :parent_id => 1, :user_id => 5 }, :order => 'position ASC').collect(&:id).should == [1,3,4,2]
498
- Mixin.all(:conditions => { :parent_id => 2, :user_id => 5 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
499
- Mixin.all(:conditions => { :parent_id => 2, :user_id => 5 }, :order => 'position ASC').collect(&:id).should == [5,6,7,8]
500
- Mixin.all(:conditions => { :parent_id => 1, :user_id => 7 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
501
- Mixin.all(:conditions => { :parent_id => 1, :user_id => 7 }, :order => 'position ASC').collect(&:id).should == [9,10,11,12]
502
- Mixin.all(:conditions => { :parent_id => 2, :user_id => 7 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
503
- Mixin.all(:conditions => { :parent_id => 2, :user_id => 7 }, :order => 'position ASC').collect(&:id).should == [13,14,15,16]
504
- end
505
- end
506
-
507
- it 'should automatically reorder if the record is deleted' do
508
- (1..4).each{ Mixin.create!(:parent_id => 1, :user_id => 5) }
509
- (1..4).each{ Mixin.create!(:parent_id => 2, :user_id => 5) }
510
- (1..4).each{ Mixin.create!(:parent_id => 1, :user_id => 7) }
511
- (1..4).each{ Mixin.create!(:parent_id => 2, :user_id => 7) }
512
- second_mixin = Mixin.first( :conditions => { :position => 2, :parent_id => 1, :user_id => 5 } )
513
- second_mixin.destroy
514
- Mixin.all(:conditions => { :parent_id => 1, :user_id => 5 }, :order => 'position ASC').collect(&:position).should == [1,2,3]
515
- Mixin.all(:conditions => { :parent_id => 1, :user_id => 5 }, :order => 'position ASC').collect(&:id).should == [1,3,4]
516
- Mixin.all(:conditions => { :parent_id => 2, :user_id => 5 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
517
- Mixin.all(:conditions => { :parent_id => 2, :user_id => 5 }, :order => 'position ASC').collect(&:id).should == [5,6,7,8]
518
- Mixin.all(:conditions => { :parent_id => 1, :user_id => 7 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
519
- Mixin.all(:conditions => { :parent_id => 1, :user_id => 7 }, :order => 'position ASC').collect(&:id).should == [9,10,11,12]
520
- Mixin.all(:conditions => { :parent_id => 2, :user_id => 7 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
521
- Mixin.all(:conditions => { :parent_id => 2, :user_id => 7 }, :order => 'position ASC').collect(&:id).should == [13,14,15,16]
522
- end
523
- end
524
- end
@@ -1 +0,0 @@
1
- --color
@@ -1,14 +0,0 @@
1
- require 'rubygems'
2
- gem 'activerecord', '>= 3'
3
- require 'active_record'
4
-
5
- $LOAD_PATH.unshift(File.dirname(__FILE__))
6
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
- require 'acts_as_restful_list'
8
- require 'rspec'
9
- require 'rspec/autorun'
10
-
11
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
12
-
13
- RSpec.configure do |config|
14
- end