acts_as_restful_list 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.rdoc +5 -0
- data/VERSION +1 -1
- data/acts_as_restful_list.gemspec +2 -2
- data/lib/acts_as_restful_list.rb +17 -1
- data/spec/acts_as_restful_list_spec.rb +19 -0
- data/spec/spec_helper.rb +4 -4
- metadata +4 -4
data/History.rdoc
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.5.0
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{acts_as_restful_list}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.5.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["'Trey Bean'"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2011-02-08}
|
13
13
|
s.description = %q{Just like acts_as_list, but allows updating through standard restful methods.}
|
14
14
|
s.email = %q{trey@12spokes.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/acts_as_restful_list.rb
CHANGED
@@ -36,6 +36,19 @@ module ActsAsRestfulList
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
+
define_method 'scope_condition_was' do
|
40
|
+
if configuration[:scope].nil?
|
41
|
+
nil
|
42
|
+
else
|
43
|
+
scopes = Array(configuration[:scope]).collect do |scope|
|
44
|
+
column = self.class.column_names.include?(scope.to_s) ? scope.to_s : "#{scope}_id"
|
45
|
+
value = self.send("#{column}_was")
|
46
|
+
value.nil? ? "#{column} IS NULL" : "#{column} = #{value.is_a?(String) ? "'#{value}'" : value}"
|
47
|
+
end
|
48
|
+
scopes.join(' AND ')
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
39
52
|
define_method 'optimistic_locking_update' do
|
40
53
|
self.class.column_names.include?("lock_version") ? ", lock_version = (lock_version + 1)" : ""
|
41
54
|
end
|
@@ -49,7 +62,10 @@ module ActsAsRestfulList
|
|
49
62
|
end
|
50
63
|
|
51
64
|
def reset_order_after_update
|
52
|
-
if
|
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?" )
|
53
69
|
if self.send( "#{position_column}_was" ) > self.send( position_column )
|
54
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 '))
|
55
71
|
else
|
@@ -333,6 +333,25 @@ describe "ActsAsRestfulList" do
|
|
333
333
|
Mixin.all(:conditions => { :parent_id => 1 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
|
334
334
|
Mixin.all(:conditions => { :parent_id => 2 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4,5,6]
|
335
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
|
336
355
|
end
|
337
356
|
|
338
357
|
it 'should automatically reorder the list scoped by parent if the record is deleted' do
|
data/spec/spec_helper.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
require 'rubygems'
|
2
|
-
gem 'activerecord', '>=
|
2
|
+
gem 'activerecord', '>= 3'
|
3
3
|
require 'active_record'
|
4
4
|
|
5
5
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
6
6
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
7
7
|
require 'acts_as_restful_list'
|
8
|
-
require '
|
9
|
-
require '
|
8
|
+
require 'rspec'
|
9
|
+
require 'rspec/autorun'
|
10
10
|
|
11
11
|
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
12
12
|
|
13
|
-
|
13
|
+
RSpec.configure do |config|
|
14
14
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_restful_list
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 11
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 5
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.5.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- "'Trey Bean'"
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-02-08 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|