acts_as_restful_list 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -5,14 +5,23 @@ non-standard method calls like insert_at, acts_as_restful_list makes managing li
5
5
  simple. You update the position attribute just like you would update anything else
6
6
  and the rest is taken care of for you.
7
7
 
8
- For example:
8
+ == Installation
9
+ #environment.rb
10
+ config.gem 'acts_as_restful_list'
11
+
12
+ # in Terminal
13
+ rake gems:install
14
+
15
+ == Usage
16
+
17
+ === In your model:
9
18
 
10
19
  class Item < ActiveRecord::Base
11
20
  belongs_to :parent
12
21
  acts_as_restful_list :scope => :parent
13
22
  end
14
23
 
15
- Now, simply CRUD away:
24
+ === Now, simply CRUD away:
16
25
 
17
26
  item = Item.create
18
27
  item.position # 1
@@ -37,6 +46,8 @@ Now, simply CRUD away:
37
46
 
38
47
  And that's that.
39
48
 
49
+ Report issues at http://github.com/12spokes/acts_as_restful_list/issues
50
+
40
51
  == Note on Patches/Pull Requests
41
52
 
42
53
  * Fork the project.
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ begin
8
8
  gem.summary = %Q{Restful acts_as_list}
9
9
  gem.description = %Q{Just like acts_as_list, but allows updating through standard restful methods.}
10
10
  gem.email = "trey@12spokes.com"
11
- gem.homepage = "http://github.com/treybean/acts_as_restful_list"
11
+ gem.homepage = "http://github.com/12spokes/acts_as_restful_list"
12
12
  gem.authors = ["'Trey Bean'"]
13
13
  gem.add_development_dependency "rspec", ">= 1.2.9"
14
14
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -0,0 +1,57 @@
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.2.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{2010-02-19}
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.5}
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::RubyGemsVersion) >= 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
+
@@ -27,9 +27,12 @@ module ActsAsRestfulList
27
27
  if configuration[:scope].nil?
28
28
  nil
29
29
  else
30
- column = configuration[:scope].to_s.match(/_id$/) ? configuration[:scope].to_s : "#{configuration[:scope]}_id"
31
- value = self.send(column)
32
- value.nil? ? "#{column} IS NULL" : "#{column} = #{value}"
30
+ scopes = Array(configuration[:scope]).collect do |scope|
31
+ column = scope.to_s.match(/_id$/) ? scope.to_s : "#{scope}_id"
32
+ value = self.send(column)
33
+ value.nil? ? "#{column} IS NULL" : "#{column} = #{value}"
34
+ end
35
+ scopes.join(' AND ')
33
36
  end
34
37
  end
35
38
  end
@@ -85,7 +85,7 @@ describe "ActsAsRestfulList" do
85
85
  mixin.destroy
86
86
  end
87
87
 
88
- it 'should automatically reorder the list if the record id deleted' do
88
+ it 'should automatically reorder the list if the record is deleted' do
89
89
  (1..4).each{ Mixin.create! }
90
90
  second_mixin = Mixin.first( :conditions => { :position => 2 } )
91
91
  second_mixin.destroy
@@ -178,7 +178,7 @@ describe "ActsAsRestfulList" do
178
178
  mixin.destroy
179
179
  end
180
180
 
181
- it 'should automatically reorder the list if the record id deleted' do
181
+ it 'should automatically reorder the list if the record is deleted' do
182
182
  (1..4).each{ Mixin.create! }
183
183
  second_mixin = Mixin.first( :conditions => { :pos => 2 } )
184
184
  second_mixin.destroy
@@ -255,7 +255,7 @@ describe "ActsAsRestfulList" do
255
255
  end
256
256
  end
257
257
 
258
- it 'should automatically reorder the list scoped by parent if the record id deleted' do
258
+ it 'should automatically reorder the list scoped by parent if the record is deleted' do
259
259
  (1..4).each{ Mixin.create!(:parent_id => 1) }
260
260
  (1..6).each{ Mixin.create!(:parent_id => 2) }
261
261
  second_mixin = Mixin.first( :conditions => { :position => 2, :parent_id => 1 } )
@@ -297,4 +297,95 @@ describe "ActsAsRestfulList" do
297
297
  Mixin.new(:parent_id => 3).scope_condition.should == "parent_id = 3"
298
298
  end
299
299
  end
300
+
301
+
302
+ describe 'declaring acts_as_restful_list and setting the scope to multiple columns' do
303
+ before(:all) do
304
+ ActiveRecord::Schema.define(:version => 1) do
305
+ create_table :mixins do |t|
306
+ t.column :position, :integer
307
+ t.column :user_id, :integer
308
+ t.column :parent_id, :integer
309
+ t.column :created_at, :datetime
310
+ t.column :updated_at, :datetime
311
+ end
312
+ end
313
+
314
+ class Mixin < ActiveRecord::Base
315
+ acts_as_restful_list :scope => [:parent, :user]
316
+ end
317
+ end
318
+
319
+ after(:all) do
320
+ Object.send(:remove_const, :Mixin)
321
+
322
+ ActiveRecord::Base.connection.tables.each do |table|
323
+ ActiveRecord::Base.connection.drop_table(table)
324
+ end
325
+ end
326
+
327
+ it 'should define scope_condition as an instance method' do
328
+ Mixin.new.should respond_to(:scope_condition)
329
+ end
330
+
331
+ it 'should return a scope condition that limits based on the parent_id' do
332
+ Mixin.new(:user_id => 4, :parent_id => 3).scope_condition.should == "parent_id = 3 AND user_id = 4"
333
+ end
334
+
335
+ describe 'reordering on update' do
336
+ before(:each) do
337
+ (1..4).each{ Mixin.create!(:parent_id => 1, :user_id => 5) }
338
+ (1..4).each{ Mixin.create!(:parent_id => 2, :user_id => 5) }
339
+ (1..4).each{ Mixin.create!(:parent_id => 1, :user_id => 7) }
340
+ (1..4).each{ Mixin.create!(:parent_id => 2, :user_id => 7) }
341
+ end
342
+
343
+ it 'should automatically reorder the list if a record is updated with a lower position' do
344
+ user5_parent1_fourth_mixin = Mixin.first( :conditions => { :position => 4, :parent_id => 1, :user_id => 5 } )
345
+ user5_parent1_fourth_mixin.position = 2
346
+ user5_parent1_fourth_mixin.save!
347
+ user5_parent1_fourth_mixin.reload.position.should == 2
348
+ Mixin.all(:conditions => { :parent_id => 1, :user_id => 5 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
349
+ Mixin.all(:conditions => { :parent_id => 1, :user_id => 5 }, :order => 'position ASC').collect(&:id).should == [1,4,2,3]
350
+ Mixin.all(:conditions => { :parent_id => 2, :user_id => 5 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
351
+ Mixin.all(:conditions => { :parent_id => 2, :user_id => 5 }, :order => 'position ASC').collect(&:id).should == [5,6,7,8]
352
+ Mixin.all(:conditions => { :parent_id => 1, :user_id => 7 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
353
+ Mixin.all(:conditions => { :parent_id => 1, :user_id => 7 }, :order => 'position ASC').collect(&:id).should == [9,10,11,12]
354
+ Mixin.all(:conditions => { :parent_id => 2, :user_id => 7 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
355
+ Mixin.all(:conditions => { :parent_id => 2, :user_id => 7 }, :order => 'position ASC').collect(&:id).should == [13,14,15,16]
356
+ end
357
+
358
+ it 'should automatically reorder the list if a record is updated with a higher position' do
359
+ second_mixin = Mixin.first( :conditions => { :position => 2, :parent_id => 1, :user_id => 5 } )
360
+ second_mixin.position = 4
361
+ second_mixin.save!
362
+ second_mixin.reload.position.should == 4
363
+ Mixin.all(:conditions => { :parent_id => 1, :user_id => 5 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
364
+ Mixin.all(:conditions => { :parent_id => 1, :user_id => 5 }, :order => 'position ASC').collect(&:id).should == [1,3,4,2]
365
+ Mixin.all(:conditions => { :parent_id => 2, :user_id => 5 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
366
+ Mixin.all(:conditions => { :parent_id => 2, :user_id => 5 }, :order => 'position ASC').collect(&:id).should == [5,6,7,8]
367
+ Mixin.all(:conditions => { :parent_id => 1, :user_id => 7 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
368
+ Mixin.all(:conditions => { :parent_id => 1, :user_id => 7 }, :order => 'position ASC').collect(&:id).should == [9,10,11,12]
369
+ Mixin.all(:conditions => { :parent_id => 2, :user_id => 7 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
370
+ Mixin.all(:conditions => { :parent_id => 2, :user_id => 7 }, :order => 'position ASC').collect(&:id).should == [13,14,15,16]
371
+ end
372
+ end
373
+
374
+ it 'should automatically reorder if the record is deleted' do
375
+ (1..4).each{ Mixin.create!(:parent_id => 1, :user_id => 5) }
376
+ (1..4).each{ Mixin.create!(:parent_id => 2, :user_id => 5) }
377
+ (1..4).each{ Mixin.create!(:parent_id => 1, :user_id => 7) }
378
+ (1..4).each{ Mixin.create!(:parent_id => 2, :user_id => 7) }
379
+ second_mixin = Mixin.first( :conditions => { :position => 2, :parent_id => 1, :user_id => 5 } )
380
+ second_mixin.destroy
381
+ Mixin.all(:conditions => { :parent_id => 1, :user_id => 5 }, :order => 'position ASC').collect(&:position).should == [1,2,3]
382
+ Mixin.all(:conditions => { :parent_id => 1, :user_id => 5 }, :order => 'position ASC').collect(&:id).should == [1,3,4]
383
+ Mixin.all(:conditions => { :parent_id => 2, :user_id => 5 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
384
+ Mixin.all(:conditions => { :parent_id => 2, :user_id => 5 }, :order => 'position ASC').collect(&:id).should == [5,6,7,8]
385
+ Mixin.all(:conditions => { :parent_id => 1, :user_id => 7 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
386
+ Mixin.all(:conditions => { :parent_id => 1, :user_id => 7 }, :order => 'position ASC').collect(&:id).should == [9,10,11,12]
387
+ Mixin.all(:conditions => { :parent_id => 2, :user_id => 7 }, :order => 'position ASC').collect(&:position).should == [1,2,3,4]
388
+ Mixin.all(:conditions => { :parent_id => 2, :user_id => 7 }, :order => 'position ASC').collect(&:id).should == [13,14,15,16]
389
+ end
390
+ end
300
391
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_restful_list
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "'Trey Bean'"
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-17 00:00:00 -07:00
12
+ date: 2010-02-19 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -40,12 +40,13 @@ files:
40
40
  - Rakefile
41
41
  - Todo.rdoc
42
42
  - VERSION
43
+ - acts_as_restful_list.gemspec
43
44
  - lib/acts_as_restful_list.rb
44
45
  - spec/acts_as_restful_list_spec.rb
45
46
  - spec/spec.opts
46
47
  - spec/spec_helper.rb
47
48
  has_rdoc: true
48
- homepage: http://github.com/treybean/acts_as_restful_list
49
+ homepage: http://github.com/12spokes/acts_as_restful_list
49
50
  licenses: []
50
51
 
51
52
  post_install_message: