acts_as_list_mongoid 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
File without changes
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :rubygems
2
+
3
+ gem 'mongoid', '>= 2.0.0.beta.14'
4
+ gem "mongoid_embedded_helper", '>= 0.2.5'
data/README.markdown CHANGED
@@ -7,7 +7,7 @@ If you do not specify custom position +column+ in the options, a key named +pos+
7
7
 
8
8
  ## Installation
9
9
 
10
- <code>gem install acts_a_list_mongoid</code>
10
+ <code>gem install acts_as_list_mongoid</code>
11
11
 
12
12
  ## Usage
13
13
 
data/Rakefile CHANGED
@@ -11,8 +11,8 @@ begin
11
11
  gem.email = "kmandrup@gmail.com"
12
12
  gem.homepage = "http://github.com/rails/acts_as_list"
13
13
  gem.authors = ["Kristian Mandrup"]
14
- gem.add_dependency "mongoid", ">= 2.0.0.beta7"
15
- gem.add_dependency "mongoid_embedded_helper", ">= 0.2.3"
14
+ gem.add_dependency "mongoid", ">= 2.0.0.beta.14"
15
+ gem.add_dependency "mongoid_embedded_helper", ">= 0.2.5"
16
16
  # gem.add_development_dependency "yard"
17
17
  end
18
18
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{acts_as_list_mongoid}
8
- s.version = "0.2.0"
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 = ["Kristian Mandrup"]
12
- s.date = %q{2010-07-06}
12
+ s.date = %q{2010-08-08}
13
13
  s.description = %q{Make your Mongoid model acts as a list. This acts_as extension provides the capabilities for sorting and reordering a number of objects in a list.
14
14
  The instances that take part in the list should have a +position+ field of type Integer.}
15
15
  s.email = %q{kmandrup@gmail.com}
@@ -19,6 +19,8 @@ Gem::Specification.new do |s|
19
19
  s.files = [
20
20
  ".DS_Store",
21
21
  ".gitignore",
22
+ ".rspec",
23
+ "Gemfile",
22
24
  "README.markdown",
23
25
  "Rakefile",
24
26
  "VERSION",
@@ -30,9 +32,10 @@ Gem::Specification.new do |s|
30
32
  "lib/init.rb",
31
33
  "lib/mongoid/acts_as_list.rb",
32
34
  "model/embedded_item.rb",
35
+ "model/referenced_category.rb",
33
36
  "spec/.rspec",
34
37
  "spec/acts_as_list/embedded_item_spec.rb",
35
- "spec/spec.opts",
38
+ "spec/acts_as_list/referenced_category_spec.rb",
36
39
  "spec/spec_helper.rb"
37
40
  ]
38
41
  s.homepage = %q{http://github.com/rails/acts_as_list}
@@ -42,6 +45,7 @@ Gem::Specification.new do |s|
42
45
  s.summary = %q{acts_as_list for Mongoid}
43
46
  s.test_files = [
44
47
  "spec/acts_as_list/embedded_item_spec.rb",
48
+ "spec/acts_as_list/referenced_category_spec.rb",
45
49
  "spec/spec_helper.rb"
46
50
  ]
47
51
 
@@ -50,15 +54,15 @@ Gem::Specification.new do |s|
50
54
  s.specification_version = 3
51
55
 
52
56
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
53
- s.add_runtime_dependency(%q<mongoid>, [">= 2.0.0.beta7"])
54
- s.add_runtime_dependency(%q<mongoid_embedded_helper>, [">= 0.2.3"])
57
+ s.add_runtime_dependency(%q<mongoid>, [">= 2.0.0.beta.14"])
58
+ s.add_runtime_dependency(%q<mongoid_embedded_helper>, [">= 0.2.5"])
55
59
  else
56
- s.add_dependency(%q<mongoid>, [">= 2.0.0.beta7"])
57
- s.add_dependency(%q<mongoid_embedded_helper>, [">= 0.2.3"])
60
+ s.add_dependency(%q<mongoid>, [">= 2.0.0.beta.14"])
61
+ s.add_dependency(%q<mongoid_embedded_helper>, [">= 0.2.5"])
58
62
  end
59
63
  else
60
- s.add_dependency(%q<mongoid>, [">= 2.0.0.beta7"])
61
- s.add_dependency(%q<mongoid_embedded_helper>, [">= 0.2.3"])
64
+ s.add_dependency(%q<mongoid>, [">= 2.0.0.beta.14"])
65
+ s.add_dependency(%q<mongoid_embedded_helper>, [">= 0.2.5"])
62
66
  end
63
67
  end
64
68
 
@@ -1,5 +1,6 @@
1
1
  require "mongoid"
2
2
  require 'mongoid_embedded_helper'
3
+ require 'mongoid_adjust'
3
4
 
4
5
  module ActsAsList
5
6
  module Mongoid
@@ -99,7 +100,7 @@ module ActsAsList
99
100
  sub_collection = if embedded?
100
101
  sub_collection.sort { |x,y| x.my_position <=> y.my_position }
101
102
  else
102
- sub_collection.order_by([position_key, :desc])
103
+ sub_collection.order_by(position_key.to_sym.asc)
103
104
  end
104
105
 
105
106
  if !extras.empty?
@@ -145,12 +146,12 @@ module ActsAsList
145
146
  end
146
147
 
147
148
  def move_below(object)
148
- new_pos = ( (self == object) or (object.my_position < self.my_position) ) ? self.my_position : object.my_position
149
+ new_pos = (self == object) ? self.my_position : ((object.my_position > self.my_position) ? object.my_position : object.my_position + 1)
149
150
  move_to(new_pos)
150
151
  end
151
152
 
152
153
  def move_above(object)
153
- new_pos = ( self == object or (object.my_position < self.my_position) ) ? self.my_position : object.my_position - 1
154
+ new_pos = ( self == object ) ? self.my_position : ((object.my_position > self.my_position) ? object.my_position - 1 : object.my_position)
154
155
  move_to(new_pos)
155
156
  end
156
157
 
@@ -340,7 +341,7 @@ module ActsAsList
340
341
  end
341
342
 
342
343
  def adjust_all! collection, number
343
- collection.adjust! position_key => number
344
+ collection.adjust!(position_key => number).each{|doc| doc.save}
344
345
  end
345
346
 
346
347
  def increase_all! collection
@@ -406,7 +407,7 @@ module ActsAsList
406
407
  def []=(key, value)
407
408
  if set_allowed?(key)
408
409
  @attributes[key.to_s] = value
409
- elsif write_allowed?(key)
410
+ else
410
411
  self.send("#{key}=", value)
411
412
  end
412
413
  save!
@@ -414,7 +415,7 @@ module ActsAsList
414
415
 
415
416
  def ==(other)
416
417
  return true if other.equal?(self)
417
- return true if other.instance_of?(self.class) and other._id == self._id
418
+ return true if other.instance_of?(self.class) and other.respond_to?('_id') and other._id == self._id
418
419
  false
419
420
  end
420
421
 
@@ -429,4 +430,4 @@ class Array
429
430
  def init_list!
430
431
  each {|i| i.init_list_item! }
431
432
  end
432
- end
433
+ end
@@ -0,0 +1,17 @@
1
+ class Category
2
+ include Mongoid::Document
3
+ include Mongoid::Timestamps
4
+ include ActsAsList::Mongoid
5
+
6
+ field :number, :type => Integer
7
+
8
+ field :pos, :type => Integer
9
+ acts_as_list :column => :pos
10
+
11
+ references_many :categories
12
+ referenced_in :category
13
+
14
+ def scope_condition
15
+ {:category_id => category.id, :pos.ne => nil}
16
+ end
17
+ end
@@ -134,6 +134,18 @@ describe 'ActsAsList for Mongoid' do
134
134
  get_positions(@list).should == [2, 1, 3, 4]
135
135
  end
136
136
 
137
+ it "items[3].move_below(item[1]) should move item 3 to position 2" do
138
+ item1 = @list.items.where(:number => 1).first
139
+ @list.items.where(:number => 3).first.move_below(item1)
140
+ get_positions(@list).should == [1, 3, 2, 4]
141
+ end
142
+
143
+ it "items[3].move_above(item[2]) should move item 3 to position 2" do
144
+ item2 = @list.items.where(:number => 2).first
145
+ @list.items.where(:number => 3).first.move_above(item2)
146
+ get_positions(@list).should == [1, 3, 2, 4]
147
+ end
148
+
137
149
  it "items[1].move_above(item[3]) should move item 1 to position 2" do
138
150
  item3 = @list.items.where(:number => 3).first
139
151
  @list.items.where(:number => 1).first.move_above(item3)
@@ -162,4 +174,4 @@ describe 'ActsAsList for Mongoid' do
162
174
  end
163
175
  end
164
176
  end
165
- end
177
+ end
@@ -0,0 +1,177 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ require 'acts_as_list_mongoid'
4
+ require 'referenced_category'
5
+
6
+ describe 'ActsAsList for Mongoid' do
7
+
8
+ before :each do
9
+ @category = Category.create!
10
+ @category.categories = []
11
+ (1..4).each do |counter|
12
+ @category.categories << Category.new(:number => counter)
13
+ end
14
+ @category.save!
15
+
16
+ @category.categories.init_list!
17
+ end
18
+
19
+ after :each do
20
+ Mongoid.database.collections.each do |coll|
21
+ coll.remove
22
+ end
23
+ end
24
+
25
+ def get_positions category
26
+ category.reload.categories.sort { |x,y| x.my_position <=> y.my_position }.map(&:number)
27
+ end
28
+
29
+ context "4 category categories (1,2,3,4) that have parent_id pointing to first category container" do
30
+ describe '# initial configuration' do
31
+ it "should category categories 1 to 4 in order" do
32
+ positions = get_positions @category
33
+ positions.should == [1, 2, 3, 4]
34
+ end
35
+ end
36
+
37
+ describe '#reordering' do
38
+ it "should move item 2 to position 3" do
39
+ @category.categories[1].increment_position
40
+ @category.categories[2].decrement_position
41
+ get_positions(@category).should == [1, 3, 2, 4]
42
+ end
43
+
44
+
45
+ it "should move item 2 to position 3 directly" do
46
+ Category.where(:number => 2).first.move_lower
47
+ get_positions(@category).should == [1, 3, 2, 4]
48
+ end
49
+
50
+ it "move :down should move item 2 to position 3" do
51
+ Category.where(:number => 2).first.move(:down)
52
+ get_positions(@category).should == [1, 3, 2, 4]
53
+ end
54
+
55
+ it "move :lower should move item 2 to position 3" do
56
+ Category.where(:number => 2).first.move(:lower)
57
+ get_positions(@category).should == [1, 3, 2, 4]
58
+ end
59
+
60
+ it "should move item 2 to position 1" do
61
+ Category.where(:number => 2).first.move_higher
62
+ get_positions(@category).should == [2, 1, 3, 4]
63
+ end
64
+
65
+ it "move :up should move item 2 to position 1" do
66
+ Category.where(:number => 2).first.move(:up)
67
+ get_positions(@category).should == [2, 1, 3, 4]
68
+ end
69
+
70
+ it "move :higher should move item 2 to position 1" do
71
+ Category.where(:number => 2).first.move(:higher)
72
+ get_positions(@category).should == [2, 1, 3, 4]
73
+ end
74
+
75
+ it "should move item 1 to bottom" do
76
+ Category.where(:number => 1).first.move_to_bottom
77
+ get_positions(@category).should == [2, 3, 4, 1]
78
+ end
79
+
80
+ it "move :lowest should move item 1 to bottom" do
81
+ Category.where(:number => 1).first.move(:lowest)
82
+ get_positions(@category).should == [2, 3, 4, 1]
83
+ end
84
+
85
+ it "should move item 1 to top" do
86
+ Category.where(:number => 1).first.move_to_top
87
+ get_positions(@category).should == [1, 2, 3, 4]
88
+ end
89
+
90
+ it "move :highest should move item 1 to top" do
91
+ Category.where(:number => 1).first.move(:highest)
92
+ get_positions(@category).should == [1, 2, 3, 4]
93
+ end
94
+
95
+ it "move :unknown should cause argument error" do
96
+ lambda {Category.where(:number => 1).first.move(:unknown)}.should raise_error
97
+ end
98
+
99
+
100
+ it "should move item 2 to bottom" do
101
+ Category.where(:number => 2).first.move_to_bottom
102
+ get_positions(@category).should == [1, 3, 4, 2]
103
+ end
104
+
105
+ it "should move item 4 to top" do
106
+ Category.where(:number => 4).first.move_to_top
107
+ get_positions(@category).should == [4, 1, 2, 3]
108
+ end
109
+
110
+ it "should move item 3 to bottom" do
111
+ Category.where(:number => 3).first.move_to_bottom
112
+ get_positions(@category).should == [1, 2, 4, 3]
113
+
114
+ end
115
+
116
+ it "categories[2].move_to(4) should move item 2 to position 4" do
117
+ Category.where(:number => 2).first.move_to(4)
118
+ get_positions(@category).should == [1, 3, 4, 2]
119
+ end
120
+
121
+ it "categories[2].insert_at(3) should move item 2 to position 3" do
122
+ Category.where(:number => 2).first.insert_at(3)
123
+ get_positions(@category).should == [1, 3, 2, 4]
124
+ end
125
+
126
+ it "categories[2].move(:to => 3) should move item 2 to position 3" do
127
+ Category.where(:number => 2).first.move(:to => 3)
128
+ get_positions(@category).should == [1, 3, 2, 4]
129
+ end
130
+
131
+ it "categories[1].move_below(item[2]) should move item 1 to position 2" do
132
+ item2 = Category.where(:number => 2).first
133
+ Category.where(:number => 1).first.move_below(item2)
134
+ get_positions(@category).should == [2, 1, 3, 4]
135
+ end
136
+
137
+ it "categories[3].move_below(item[1]) should move item 3 to position 2" do
138
+ item1 = Category.where(:number => 1).first
139
+ Category.where(:number => 3).first.move_below(item1)
140
+ get_positions(@category).should == [1, 3, 2, 4]
141
+ end
142
+
143
+ it "categories[3].move_above(item[2]) should move item 3 to position 2" do
144
+ item2 = Category.where(:number => 2).first
145
+ Category.where(:number => 3).first.move_above(item2)
146
+ get_positions(@category).should == [1, 3, 2, 4]
147
+ end
148
+
149
+ it "categories[1].move_above(item[3]) should move item 1 to position 2" do
150
+ item3 = Category.where(:number => 3).first
151
+ Category.where(:number => 1).first.move_above(item3)
152
+ get_positions(@category).should == [2, 1, 3, 4]
153
+ end
154
+
155
+ end
156
+
157
+ describe 'relative position queries' do
158
+ it "should find item 2 to be lower item of item 1" do
159
+ expected = Category.where(:pos => 2).first
160
+ Category.where(:pos => 1).first.lower_item.should == expected
161
+ end
162
+
163
+ it "should not find any item higher than nr 1" do
164
+ Category.where(:pos => 1).first.higher_item.should == nil
165
+ end
166
+
167
+ it "should find item 3 to be higher item of item 4" do
168
+ expected = Category.where(:pos => 3).first
169
+ Category.where(:pos => 4).first.higher_item.should == expected
170
+ end
171
+
172
+ it "should not find item lower than item 4" do
173
+ Category.where(:pos => 4).first.lower_item.should == nil
174
+ end
175
+ end
176
+ end
177
+ end
data/spec/spec_helper.rb CHANGED
@@ -2,6 +2,7 @@ require 'rspec'
2
2
  require 'rspec/autorun'
3
3
  require 'mongoid'
4
4
  require 'mongoid_embedded_helper'
5
+ require 'mongoid_adjust'
5
6
 
6
7
  $:.unshift "#{File.dirname(__FILE__)}/../model/"
7
8
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 1
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Mandrup
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-07-06 00:00:00 +02:00
17
+ date: 2010-08-08 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -29,8 +29,9 @@ dependencies:
29
29
  - 2
30
30
  - 0
31
31
  - 0
32
- - beta7
33
- version: 2.0.0.beta7
32
+ - beta
33
+ - 14
34
+ version: 2.0.0.beta.14
34
35
  type: :runtime
35
36
  version_requirements: *id001
36
37
  - !ruby/object:Gem::Dependency
@@ -44,8 +45,8 @@ dependencies:
44
45
  segments:
45
46
  - 0
46
47
  - 2
47
- - 3
48
- version: 0.2.3
48
+ - 5
49
+ version: 0.2.5
49
50
  type: :runtime
50
51
  version_requirements: *id002
51
52
  description: |-
@@ -61,6 +62,8 @@ extra_rdoc_files:
61
62
  files:
62
63
  - .DS_Store
63
64
  - .gitignore
65
+ - .rspec
66
+ - Gemfile
64
67
  - README.markdown
65
68
  - Rakefile
66
69
  - VERSION
@@ -72,9 +75,10 @@ files:
72
75
  - lib/init.rb
73
76
  - lib/mongoid/acts_as_list.rb
74
77
  - model/embedded_item.rb
78
+ - model/referenced_category.rb
75
79
  - spec/.rspec
76
80
  - spec/acts_as_list/embedded_item_spec.rb
77
- - spec/spec.opts
81
+ - spec/acts_as_list/referenced_category_spec.rb
78
82
  - spec/spec_helper.rb
79
83
  has_rdoc: true
80
84
  homepage: http://github.com/rails/acts_as_list
@@ -110,4 +114,5 @@ specification_version: 3
110
114
  summary: acts_as_list for Mongoid
111
115
  test_files:
112
116
  - spec/acts_as_list/embedded_item_spec.rb
117
+ - spec/acts_as_list/referenced_category_spec.rb
113
118
  - spec/spec_helper.rb