mongoid_acts_as_list 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -3,3 +3,4 @@
3
3
  Gemfile.lock
4
4
  pkg/*
5
5
  tags
6
+ coverage/*
data/.travis.yml ADDED
@@ -0,0 +1,2 @@
1
+ rvm:
2
+ - 1.9.3
data/README.markdown CHANGED
@@ -5,6 +5,8 @@ ActsAsList for Mongoid
5
5
 
6
6
  Mongoid::ActsAsList provides the ability of ordering and sorting a number of objects in a list using Mongoid as an ODM.
7
7
 
8
+ [![Build Status](https://secure.travis-ci.org/olivoil/mongoid_acts_as_list.png)](http://travis-ci.org/olivoil/mongoid_acts_as_list)
9
+ [![Dependency Status](https://gemnasium.com/olivoil/mongoid_acts_as_list.png)](https://gemnasium.com/olivoil/mongoid_acts_as_list)
8
10
 
9
11
  ## Install
10
12
 
@@ -17,7 +19,7 @@ gem 'mongoid_acts_as_list', '~> 0.0.3'
17
19
  Then run `bundle install`
18
20
 
19
21
 
20
- ## Configuration
22
+ ## Configure
21
23
 
22
24
  Configure defaults values used by ActsAsList:
23
25
 
@@ -35,7 +37,7 @@ end
35
37
 
36
38
  Make sure it is loaded before calling ` acts_as_list `. You can place this code in an initializer file for example.
37
39
 
38
- ## Usage
40
+ ## Use
39
41
 
40
42
  Activate ActsAsList in your models.
41
43
 
@@ -75,6 +77,10 @@ class Item
75
77
  end
76
78
  ```
77
79
 
80
+ The public API is composed of the following methods.
81
+ All of the API from the original ActiveRecord ActsAsList gem is also available.
82
+ Check the source and [documentation](http://rubydoc.info/github/olivoil/mongoid_acts_as_list/master/frames) to find out more!
83
+
78
84
 
79
85
  ``` ruby
80
86
  ## Class Methods
@@ -83,7 +89,7 @@ list.items.order_by_position #=> returns all items in `list` ordered by position
83
89
 
84
90
  ## Instance Methods
85
91
 
86
- item.move to: 2 #=> moves item to the 2nd position
92
+ item.move to: 2 #=> moves item to position number 2
87
93
  item.move to: :start #=> moves item to the first position in the list
88
94
  item.move to: :end #=> moves item to the last position in the list
89
95
  item.move before: other_item #=> moves item before other_item
@@ -104,8 +110,6 @@ item.next_item #=> returns the item immediately following `item` i
104
110
  item.previous_item #=> returns the item immediately preceding `item` in the list
105
111
  ```
106
112
 
107
- Other methods are available, as well as all the methods from the original ActiveRecord ActsAsList gem.
108
- Check the source and documentation to find out more!
109
113
 
110
114
 
111
115
  ## Requirements
@@ -113,10 +117,6 @@ Check the source and documentation to find out more!
113
117
  Tested with Mongoid 2.4.6 on Ruby 1.9.3-p125, Rails 3.2.2, and Mongo 2.x
114
118
 
115
119
 
116
- ## Roadmap
117
-
118
- * Test with several layers of embedding documents
119
-
120
120
 
121
121
  ## Contributing
122
122
 
@@ -125,10 +125,6 @@ Tested with Mongoid 2.4.6 on Ruby 1.9.3-p125, Rails 3.2.2, and Mongo 2.x
125
125
  - Start writing tests
126
126
  - Commit and push until all tests are green and you are happy with your contribution
127
127
 
128
- - If you're using Vim, source the .vimrc.local file. It provides 2 shortcuts for running the specs:
129
- - `<Leader>r` runs the current spec file
130
- - `<Leader>R` runs all spec files
131
-
132
128
  ## Copyright
133
129
 
134
130
  Copyright (c) 2012 Olivier Melcher, released under the MIT license
data/Rakefile CHANGED
@@ -1 +1,21 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'rdoc/task'
4
+
5
+ desc 'Default: run specs.'
6
+ task :default => :spec
7
+
8
+ desc "Run specs"
9
+ RSpec::Core::RakeTask.new do |t|
10
+ t.pattern = "./spec/**/*_spec.rb"
11
+ end
12
+
13
+ desc "Open an irb session preloaded with this library"
14
+ task :irb do
15
+ sh "irb -rubygems -r ./lib/#{name}.rb"
16
+ end
17
+
18
+ desc "Open an pry session preloaded with this library"
19
+ task :pry do
20
+ sh "pry -r ./lib/#{name}.rb"
21
+ end
data/acts_as_list.gemspec CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
7
7
  s.version = Mongoid::ActsAsList::VERSION
8
8
  s.authors = ["Olivier Melcher"]
9
9
  s.email = ["olivier.melcher@gmail.com"]
10
- s.homepage = ""
10
+ s.homepage = "https://github.com/olivoil/mongoid_acts_as_list"
11
11
  s.summary = %q{Implementation of the acts as list gem for Mongoid}
12
12
  s.description = %q{}
13
13
 
@@ -20,6 +20,8 @@ Gem::Specification.new do |s|
20
20
 
21
21
  # specify any dependencies here; for example:
22
22
  s.add_development_dependency "rspec"
23
+ s.add_development_dependency "rake"
24
+ s.add_development_dependency "simplecov"
23
25
  s.add_development_dependency "bson_ext", "~> 1.5"
24
26
  s.add_development_dependency "database_cleaner"
25
27
  s.add_development_dependency "pry"
@@ -47,7 +47,8 @@ module Mongoid::ActsAsList
47
47
 
48
48
  # Public: Moves the item to new position in the list
49
49
  #
50
- # where - a Hash specifying where to move the item
50
+ # where - a Symbol in [:forward, :lower, :backward, :higher]
51
+ # or Hash specifying where to move the item:
51
52
  # :to - an Integer representing a position number
52
53
  # or a Symbol from the list :start, :top, :end, :bottom
53
54
  # :before, :above - another object in the list
@@ -57,7 +58,6 @@ module Mongoid::ActsAsList
57
58
  # :backward, :higher - an Integer specify by how much to move the item forward.
58
59
  # will stop moving the item when it reaches the end of the list
59
60
  #
60
- # or a Symbol in :forward, :lower, :backward, :higher
61
61
  #
62
62
  # Examples
63
63
  #
@@ -97,6 +97,10 @@ module Mongoid::ActsAsList
97
97
  end
98
98
  end
99
99
 
100
+ # Public: Moves the item to another position
101
+ #
102
+ # destination - a Symbol among :start, :end, :top, :bottom
103
+ # or an Integer indicating the new position number to move the item to
100
104
  def move_to(destination)
101
105
  if destination.is_a? Symbol
102
106
  send("move_to_#{destination}")
@@ -106,29 +110,40 @@ module Mongoid::ActsAsList
106
110
  end
107
111
  end
108
112
 
113
+ # Public: Moves the item to the end of the list
109
114
  def move_to_end
110
115
  new_position = in_list? ? last_position_in_list : next_available_position_in_list
111
116
  insert_at new_position
112
117
  end
113
118
  alias_method :move_to_bottom, :move_to_end
114
119
 
120
+ # Public: Moves the item to the start of the list
115
121
  def move_to_start
116
122
  insert_at start_position_in_list
117
123
  end
118
124
  alias_method :move_to_top, :move_to_start
119
125
 
126
+ # Public: Moves the item closer to the end of the list
127
+ #
128
+ # by_how_much - The number of position to move the item by (default: 1)
120
129
  def move_forwards by_how_much = 1
121
130
  move_to(self[position_field] + by_how_much) unless last?
122
131
  end
123
132
  alias_method :move_lower , :move_forwards
124
133
  alias_method :move_forward, :move_forwards
125
134
 
135
+ # Public: Moves the item closer to the start of the list
136
+ #
137
+ # by_how_much - The number of position to move the item by (default: 1)
126
138
  def move_backwards by_how_much = 1
127
139
  move_to(self[position_field] - by_how_much) unless first?
128
140
  end
129
141
  alias_method :move_higher , :move_backwards
130
142
  alias_method :move_forward, :move_forwards
131
143
 
144
+ # Public: Moves the item before another one in the list
145
+ #
146
+ # other_item - another item of the list
132
147
  def move_before(other_item)
133
148
  destination = other_item[position_field]
134
149
  origin = self[position_field]
@@ -141,6 +156,9 @@ module Mongoid::ActsAsList
141
156
  end
142
157
  alias_method :move_above, :move_before
143
158
 
159
+ # Public: Moves the item after another one in the list
160
+ #
161
+ # other_item - another item of the list
144
162
  def move_after(other_item)
145
163
  destination = other_item[position_field]
146
164
  origin = self[position_field]
@@ -210,18 +228,18 @@ module Mongoid::ActsAsList
210
228
  end
211
229
  alias_method :lower_item, :previous_item
212
230
 
213
- # Public: Insert at a given position in the list
231
+ private
232
+
233
+ # Internal: Insert at a given position in the list
214
234
  #
215
235
  # new_position - an Integer indicating the position to insert the item at
216
236
  #
217
- # Returns nothing
237
+ # Returns true if the element's position was updated, false if not
218
238
  def insert_at(new_position)
219
239
  insert_space_at(new_position)
220
240
  update_attribute(position_field, new_position)
221
241
  end
222
242
 
223
- private
224
-
225
243
  # Internal: Make space in the list at a given position number
226
244
  # used when moving a item to a new position in the list.
227
245
  #
@@ -239,6 +257,14 @@ module Mongoid::ActsAsList
239
257
  end
240
258
  end
241
259
 
260
+ # Internal: get items of the list between two positions
261
+ #
262
+ # from - an Integer representing the first position number in the range
263
+ # to - an Integer representing the last position number in the range
264
+ # options - an Hash of options
265
+ # :strict - a Boolean indicating if the range is inclusing (false) or exclusive (true) (default: true)
266
+ #
267
+ # Returns a Mongoid::Criteria containing the items between the range
242
268
  def items_between(from, to, options = {})
243
269
  strict = options.fetch(:strict, true)
244
270
  if strict
@@ -1,47 +1,49 @@
1
1
  module Mongoid::ActsAsList
2
- module List::Embedded
3
- extend ActiveSupport::Concern
2
+ module List
4
3
 
5
- included do
6
- raise List::ScopeMissingError, "Mongoid::ActsAsList::Embedded can only be included in embedded documents" unless embedded?
7
- end
4
+ # Internal: Private methods used specifically for embedded collections
5
+ #
6
+ # Gets included when calling List.acts_as_list in an embedded document
7
+ module Embedded
8
+ extend ActiveSupport::Concern
8
9
 
9
- module ClassMethods
10
- private
10
+ included do
11
+ raise List::ScopeMissingError, "Mongoid::ActsAsList::Embedded can only be included in embedded documents" unless embedded?
12
+ end
13
+
14
+ module ClassMethods
15
+ private
11
16
 
12
- def define_position_scope(scope_name)
13
- define_method(:scope_condition) { {position_field.ne => nil} }
17
+ def define_position_scope(scope_name)
18
+ define_method(:scope_condition) { {position_field.ne => nil} }
19
+ end
14
20
  end
15
- end
16
21
 
17
- ## InstanceMethods
18
- private
22
+ ## InstanceMethods
23
+ private
19
24
 
20
- def shift_position options = {}
21
- criteria = options.fetch(:for, to_criteria)
22
- by_how_much = options.fetch(:by, 1)
25
+ def shift_position options = {}
26
+ criteria = options.fetch(:for, to_criteria)
27
+ by_how_much = options.fetch(:by, 1)
23
28
 
24
- criteria = criteria.to_criteria if criteria.is_a? self.class
29
+ criteria = criteria.to_criteria if criteria.is_a? self.class
25
30
 
26
- criteria.each do |doc|
27
- doc.inc(position_field, by_how_much)
31
+ criteria.each do |doc|
32
+ doc.inc(position_field, by_how_much)
33
+ end
28
34
  end
29
- end
30
35
 
31
- def to_criteria
32
- embedded_collection.where(_id: _id)
33
- end
34
-
35
- def items_in_list
36
- embedded_collection.where(scope_condition)
37
- end
36
+ def to_criteria
37
+ embedded_collection.where(_id: _id)
38
+ end
38
39
 
39
- def root_collection
40
- _parent.db.collection(_parent.collection.name)
41
- end
40
+ def items_in_list
41
+ embedded_collection.where(scope_condition)
42
+ end
42
43
 
43
- def embedded_collection
44
- _parent.send(metadata.name)
44
+ def embedded_collection
45
+ _parent.send(metadata.name)
46
+ end
45
47
  end
46
48
  end
47
49
  end
@@ -1,5 +1,9 @@
1
1
  module Mongoid::ActsAsList
2
2
  module List
3
+
4
+ # Internal: Private methods used specifically for root collections (in belongs_to associations)
5
+ #
6
+ # Gets included when calling List.acts_as_list in a root document
3
7
  module Root
4
8
  extend ActiveSupport::Concern
5
9
 
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module ActsAsList
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
@@ -5,8 +5,31 @@ require_relative 'mongoid/acts_as_list/version'
5
5
  module Mongoid
6
6
  module ActsAsList
7
7
  class << self
8
+
9
+ # Public: the configuration object used by Mongoid::ActsAsList
10
+ #
11
+ # Examples
12
+ #
13
+ # Mongoid::ActsAsList.configuration.default_position_field
14
+ # #=> :position
15
+ #
16
+ # Returns the configuration object
8
17
  attr_accessor :configuration
9
18
 
19
+ # Public: set the configuration options for Mongoid::ActsAsList
20
+ #
21
+ # yields the configuration object
22
+ #
23
+ # Examples
24
+ #
25
+ # Mongoid::ActsAsList.configure do |config|
26
+ # # These are the default options.
27
+ # # Modify as you see fit:
28
+ # config.default_position_field = :position
29
+ # config.start_list_at = 0
30
+ # end
31
+ #
32
+ # Returns the configuration object
10
33
  def configure
11
34
  self.configuration ||= Configuration.new
12
35
  yield(configuration) if block_given?
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::ActsAsList::List do
4
+ [:position, :number].each do |default_field_name|
5
+ let(:position_field) { default_field_name }
6
+
7
+ before do
8
+ Mongoid::ActsAsList.configure do |config|
9
+ config.default_position_field = position_field
10
+ end
11
+
12
+ require 'fixtures/embeds_many_recursively_models'
13
+ end
14
+
15
+ describe Mongoid::ActsAsList::List::Embedded do
16
+ it_behaves_like 'a list' do
17
+ let(:category) { Category.create! }
18
+
19
+ before do
20
+ 3.times do |n|
21
+ item = category.items.create! position_field => n
22
+ 3.times do |x|
23
+ item.child_items.create! position_field => x
24
+ end
25
+ item.should have(3).child_items
26
+ end
27
+ category.should have(3).items
28
+ end
29
+ end
30
+
31
+ require 'delegate'
32
+
33
+ it_behaves_like 'a list' do
34
+ let(:category) do
35
+ cat = Category.create!
36
+
37
+ item = cat.items.create!
38
+
39
+ 3.times do |n|
40
+ item.child_items.create! position_field => n
41
+ end
42
+
43
+ class SubItemAsCategory < SimpleDelegator
44
+ def items
45
+ __getobj__.child_items
46
+ end
47
+ def reload
48
+ __getobj__.reload
49
+ self
50
+ end
51
+ end
52
+
53
+ SubItemAsCategory.new(item)
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -13,17 +13,13 @@ describe Mongoid::ActsAsList::List do
13
13
  end
14
14
 
15
15
  describe Mongoid::ActsAsList::List::Embedded do
16
- let(:category_1) { Category.create! }
17
- let(:category_2) { Category.create! }
18
- let(:category_3) { Category.create! }
16
+ let(:category) { Category.create! }
19
17
 
20
18
  before do
21
- [category_1, category_2].each do |cat|
22
- 3.times do |n|
23
- cat.items.create! position_field => n
24
- end
25
- cat.should have(3).items
19
+ 3.times do |n|
20
+ category.items.create! position_field => n
26
21
  end
22
+ category.should have(3).items
27
23
  end
28
24
 
29
25
  it "should be embedded" do
@@ -38,7 +34,7 @@ describe Mongoid::ActsAsList::List do
38
34
 
39
35
  describe ".acts_as_list" do
40
36
  it "defines #scope_condition" do
41
- item = category_1.items.first
37
+ item = category.items.first
42
38
  item.scope_condition.should == {position_field.ne => nil}
43
39
  end
44
40
  end
@@ -14,25 +14,21 @@ describe Mongoid::ActsAsList::List do
14
14
 
15
15
 
16
16
  describe Mongoid::ActsAsList::List::Root do
17
- let(:category_1) { Category.create! }
18
- let(:category_2) { Category.create! }
19
- let(:category_3) { Category.create! }
17
+ let(:category) { Category.create! }
20
18
 
21
19
  before do
22
- [category_1, category_2].each do |cat|
23
- 3.times do |n|
24
- cat.items.create! position_field => n
25
- end
26
- cat.should have(3).items
20
+ 3.times do |n|
21
+ category.items.create! position_field => n
27
22
  end
23
+ category.should have(3).items
28
24
  end
29
25
 
30
26
  it_behaves_like 'a list'
31
27
 
32
28
  describe ".acts_as_list" do
33
29
  it "defines #scope_condition" do
34
- item = category_1.items.first
35
- item.scope_condition.should == {:category_id => category_1.id}
30
+ item = category.items.first
31
+ item.scope_condition.should == {:category_id => category.id}
36
32
  end
37
33
 
38
34
  it "raises a NoScope error if called without a scope option" do
@@ -44,17 +40,7 @@ describe Mongoid::ActsAsList::List do
44
40
 
45
41
  describe ".order_by_position" do
46
42
  it "works with a condition" do
47
- RootItem.order_by_position(:category_id => category_2.id).map(&position_field).should == [0,1,2]
48
- end
49
- end
50
-
51
- describe "Insert a new item to the list" do
52
- it "scopes list to the relation" do
53
- item_1 = category_1.items.create!
54
- item_2 = category_2.items.create!
55
-
56
- item_1[position_field].should == item_2[position_field]
57
- item_2[position_field].should == 3
43
+ RootItem.order_by_position(:category_id => category.id).map(&position_field).should == [0,1,2]
58
44
  end
59
45
  end
60
46
  end
@@ -0,0 +1,16 @@
1
+ class Category
2
+ include Mongoid::Document
3
+ include Mongoid::Timestamps
4
+
5
+ embeds_many :items
6
+ end
7
+
8
+ class Item
9
+ include Mongoid::Document
10
+ include Mongoid::Timestamps
11
+ include Mongoid::ActsAsList
12
+
13
+ embedded_in :category
14
+ recursively_embeds_many
15
+ acts_as_list
16
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
1
4
  require 'rspec'
2
5
  require 'rspec/autorun'
3
6
  require 'database_cleaner'
@@ -1,7 +1,8 @@
1
1
  shared_examples_for "a list" do
2
+
2
3
  describe ".acts_as_list" do
3
4
  it "defines #position_field && .position_field" do
4
- item = category_1.items.first
5
+ item = category.items.first
5
6
  item.position_field.should == position_field
6
7
  item.class.position_field.should == position_field
7
8
  end
@@ -9,19 +10,19 @@ shared_examples_for "a list" do
9
10
 
10
11
  describe ".order_by_position" do
11
12
  it "works without conditions" do
12
- category_1.items.order_by_position.map(&position_field).should == [0,1,2]
13
+ category.items.order_by_position.map(&position_field).should == [0,1,2]
13
14
  end
14
15
 
15
16
  it "sorts by created_at if positions are equal" do
16
- deuce = category_1.items.create! position_field => 1
17
- items = category_1.items.order_by_position
17
+ deuce = category.items.create! position_field => 1
18
+ items = category.items.order_by_position
18
19
  items.map(&position_field).should == [0,1,1,2]
19
20
  items[2].should == deuce
20
21
  end
21
22
 
22
23
  it "sorts in descending order if specified" do
23
- deuce = category_1.items.create! position_field => 2, :created_at => Date.yesterday
24
- items = category_1.items.order_by_position(:desc)
24
+ deuce = category.items.create! position_field => 2, :created_at => Date.yesterday
25
+ items = category.items.order_by_position(:desc)
25
26
  items.map(&position_field).should == [2,2,1,0]
26
27
  items[1].should == deuce
27
28
  end
@@ -29,7 +30,7 @@ shared_examples_for "a list" do
29
30
 
30
31
  describe "Insert a new item to the list" do
31
32
  it "inserts at the next available position for a given category" do
32
- item = category_1.items.create!
33
+ item = category.items.create!
33
34
  item[position_field].should == 3
34
35
  end
35
36
  end
@@ -37,67 +38,67 @@ shared_examples_for "a list" do
37
38
  describe "Removing items" do
38
39
  before do
39
40
  3.times do
40
- category_1.items.create!
41
+ category.items.create!
41
42
  end
42
- category_1.reload.items.map(&position_field).should == [0,1,2,3,4,5]
43
+ category.reload.items.map(&position_field).should == [0,1,2,3,4,5]
43
44
  end
44
45
 
45
46
  describe " #destroy" do
46
47
  it "reorders the positions in the list" do
47
- item = category_1.items.where(position_field => 3).first
48
+ item = category.items.where(position_field => 3).first
48
49
  item.destroy
49
50
 
50
- items = item.embedded? ? category_1.items : category_1.reload.items
51
+ items = item.embedded? ? category.items : category.reload.items
51
52
  items.map(&position_field).should == [0,1,2,3,4]
52
53
  end
53
54
 
54
55
 
55
56
  it "does not shift positions if the element was already removed from the list" do
56
- item = category_1.items.where(position_field => 2).first
57
+ item = category.items.where(position_field => 2).first
57
58
  item.remove_from_list
58
59
  item.destroy
59
- category_1.reload.items.map(&position_field).should == [0,1,2,3,4]
60
+ category.reload.items.map(&position_field).should == [0,1,2,3,4]
60
61
  end
61
62
  end
62
63
 
63
64
  describe " #remove_from_list" do
64
65
  it "sets position to nil" do
65
- item = category_1.items.where(position_field => 2).first
66
+ item = category.items.where(position_field => 2).first
66
67
  item.remove_from_list
67
68
  item[position_field].should be_nil
68
69
  end
69
70
 
70
71
  it "is not in list anymore" do
71
- item = category_1.items.where(position_field => 3).first
72
+ item = category.items.where(position_field => 3).first
72
73
  item.remove_from_list
73
74
  item.should_not be_in_list
74
75
  end
75
76
 
76
77
  it "reorders the positions in the list" do
77
- category_1.items.where(position_field => 0).first.remove_from_list
78
- category_1.reload.items.map(&position_field).compact.should == [0,1,2,3,4]
78
+ category.items.where(position_field => 0).first.remove_from_list
79
+ category.reload.items.map(&position_field).compact.should == [0,1,2,3,4]
79
80
  end
80
81
  end
81
82
  end
82
83
 
83
84
  describe "#first?" do
84
85
  it "returns true if item is the first of the list" do
85
- category_1.items.order_by_position.first.should be_first
86
+ category.items.order_by_position.first.should be_first
86
87
  end
87
88
 
88
89
  it "returns false if item is not the first of the list" do
89
- all_but_first = category_1.items.order_by_position.to_a[1..-1]
90
+ all_but_first = category.items.order_by_position.to_a[1..-1]
90
91
  all_but_first.map(&:first?).uniq.should == [false]
91
92
  end
92
93
  end
93
94
 
94
95
  describe "#last?" do
95
96
  it "returns true if item is the last of the list" do
96
- category_1.items.order_by_position.last.should be_last
97
+ category.items.order_by_position.last.should be_last
97
98
  end
98
99
 
99
100
  it "returns false if item is not the last of the list" do
100
- all_but_last = category_1.items.order_by_position.to_a[0..-2]
101
+ all_but_last = category.items.order_by_position.to_a[0..-2]
101
102
  all_but_last.map(&:last?).uniq.should == [false]
102
103
  end
103
104
  end
@@ -105,18 +106,18 @@ shared_examples_for "a list" do
105
106
  %w[higher_item next_item].each do |method_name|
106
107
  describe "##{method_name}" do
107
108
  it "returns the next item in the list if there is one" do
108
- item = category_1.items.where(position_field => 1).first
109
- next_item = category_1.items.where(position_field => 2).first
109
+ item = category.items.where(position_field => 1).first
110
+ next_item = category.items.where(position_field => 2).first
110
111
  item.send(method_name).should == next_item
111
112
  end
112
113
 
113
114
  it "returns nil if the item is already the last" do
114
- item = category_1.items.order_by_position.last
115
+ item = category.items.order_by_position.last
115
116
  item.send(method_name).should be_nil
116
117
  end
117
118
 
118
119
  it "returns nil if the item is not in the list" do
119
- item = category_1.items.order_by_position.first
120
+ item = category.items.order_by_position.first
120
121
  item.remove_from_list
121
122
  item.send(method_name).should be_nil
122
123
  end
@@ -126,18 +127,18 @@ shared_examples_for "a list" do
126
127
  %w[lower_item previous_item].each do |method_name|
127
128
  describe "##{method_name}" do
128
129
  it "returns the previous item in the list if there is one" do
129
- item = category_1.items.where(position_field => 1).first
130
- previous_item = category_1.items.where(position_field => 0).first
130
+ item = category.items.where(position_field => 1).first
131
+ previous_item = category.items.where(position_field => 0).first
131
132
  item.send(method_name).should == previous_item
132
133
  end
133
134
 
134
135
  it "returns nil if the item is already the first" do
135
- item = category_1.items.order_by_position.first
136
+ item = category.items.order_by_position.first
136
137
  item.send(method_name).should be_nil
137
138
  end
138
139
 
139
140
  it "returns nil if the item is not in the list" do
140
- item = category_1.items.order_by_position.last
141
+ item = category.items.order_by_position.last
141
142
  item.remove_from_list
142
143
  item.send(method_name).should be_nil
143
144
  end
@@ -146,59 +147,59 @@ shared_examples_for "a list" do
146
147
 
147
148
  describe "#insert_at" do
148
149
  context "to a lower position" do
149
- let(:item) { category_1.items.order_by_position.last }
150
+ let(:item) { category.items.order_by_position.last }
150
151
 
151
152
  it "changes the item's position" do
152
- item.insert_at 1
153
+ item.send :insert_at, 1
153
154
  item[position_field].should == 1
154
155
  end
155
156
 
156
157
  it "shuffles intermediary positions" do
157
- positions = category_1.items.order_by_position.map(&position_field)
158
+ positions = category.items.order_by_position.map(&position_field)
158
159
  positions.should == [0,1,2]
159
- item.insert_at 1
160
- positions = category_1.items.order_by_position.map(&position_field)
160
+ item.send :insert_at, 1
161
+ positions = category.items.order_by_position.map(&position_field)
161
162
  positions.should == [0,1,2]
162
163
  end
163
164
 
164
165
  it "works for items that don't have a position yet" do
165
166
  item.remove_from_list
166
- item.insert_at 1
167
+ item.send :insert_at, 1
167
168
  item[position_field].should == 1
168
169
  end
169
170
  end
170
171
 
171
172
  context "to a higher position" do
172
- let(:item) { category_1.items.order_by_position.first }
173
+ let(:item) { category.items.order_by_position.first }
173
174
 
174
175
  it "changes the item's position" do
175
- item.insert_at 2
176
+ item.send :insert_at, 2
176
177
  item[position_field].should == 2
177
178
  end
178
179
 
179
180
  it "shuffles intermediary positions" do
180
- positions = category_1.items.order_by_position.map(&position_field)
181
+ positions = category.items.order_by_position.map(&position_field)
181
182
  positions.should == [0,1,2]
182
- item.insert_at 2
183
- positions = category_1.items.order_by_position.map(&position_field)
183
+ item.send :insert_at, 2
184
+ positions = category.items.order_by_position.map(&position_field)
184
185
  positions.should == [0,1,2]
185
186
  end
186
187
 
187
188
  it "works for items that don't have a position yet" do
188
189
  item.remove_from_list
189
- item.insert_at 2
190
+ item.send :insert_at, 2
190
191
  item[position_field].should == 2
191
192
  end
192
193
  end
193
194
 
194
195
  context "to the same position" do
195
196
  it "does nothing" do
196
- item = category_1.items.first
197
+ item = category.items.first
197
198
  lambda do
198
- positions = category_1.items.order_by_position.map(&position_field)
199
+ positions = category.items.order_by_position.map(&position_field)
199
200
  positions.should == [0,1,2]
200
- item.insert_at item[position_field]
201
- positions = category_1.items.order_by_position.map(&position_field)
201
+ item.send :insert_at, item[position_field]
202
+ positions = category.items.order_by_position.map(&position_field)
202
203
  positions.should == [0,1,2]
203
204
  end.should_not change(item, position_field)
204
205
  end
@@ -206,31 +207,31 @@ shared_examples_for "a list" do
206
207
 
207
208
  context "to extreme positions" do
208
209
  it "like 0" do
209
- item = category_1.items.order_by_position.last
210
+ item = category.items.order_by_position.last
210
211
 
211
212
  item.remove_from_list
212
- item.insert_at 0
213
+ item.send :insert_at, 0
213
214
 
214
215
  item[position_field].should == 0
215
- category_1.items.order_by_position.map(&position_field).should == [0,1,2]
216
+ category.items.order_by_position.map(&position_field).should == [0,1,2]
216
217
  end
217
218
  it "like the last position" do
218
- item = category_1.items.order_by_position.first
219
+ item = category.items.order_by_position.first
219
220
 
220
221
  item.remove_from_list
221
- item.insert_at 1
222
+ item.send :insert_at, 1
222
223
 
223
224
  item[position_field].should == 1
224
- category_1.items.order_by_position.map(&position_field).should == [0,1,2]
225
+ category.items.order_by_position.map(&position_field).should == [0,1,2]
225
226
  end
226
227
  it "like the next available position" do
227
- item = category_1.items.order_by_position.first
228
+ item = category.items.order_by_position.first
228
229
 
229
230
  item.remove_from_list
230
- item.insert_at 2
231
+ item.send :insert_at, 2
231
232
 
232
233
  item[position_field].should == 2
233
- category_1.items.order_by_position.map(&position_field).should == [0,1,2]
234
+ category.items.order_by_position.map(&position_field).should == [0,1,2]
234
235
  end
235
236
  end
236
237
  end
@@ -239,7 +240,7 @@ shared_examples_for "a list" do
239
240
  context ":to =>" do
240
241
  context "an Integer" do
241
242
  it "inserts at a given position" do
242
- item = category_1.items.order_by_position.first
243
+ item = category.items.order_by_position.first
243
244
  item.should_receive(:insert_at).with 2
244
245
  item.move to: 2
245
246
  end
@@ -248,7 +249,7 @@ shared_examples_for "a list" do
248
249
  context "a Symbol" do
249
250
  [:start, :top, :end, :bottom].each do |destination|
250
251
  it "moves to #{destination}" do
251
- item = category_1.items.first
252
+ item = category.items.first
252
253
  item.should_receive("move_to_#{destination}")
253
254
  item.move to: destination
254
255
  end
@@ -259,8 +260,8 @@ shared_examples_for "a list" do
259
260
  [:before, :above, :after, :below, :forward, :backward, :lower, :higher].each do |sym|
260
261
  context "#{sym} =>" do
261
262
  it "delegates to the right method" do
262
- item = category_1.items.first
263
- other_item = category_1.items.last
263
+ item = category.items.first
264
+ other_item = category.items.last
264
265
  item.should_receive("move_#{sym}").with(other_item)
265
266
  item.move(sym => other_item)
266
267
  end
@@ -270,7 +271,7 @@ shared_examples_for "a list" do
270
271
  [:backwards, :higher].each do |sym|
271
272
  context "#{sym}" do
272
273
  it "delegates to the right method" do
273
- item = category_1.items.last
274
+ item = category.items.last
274
275
  item.should_receive("move_#{sym}").with()
275
276
  item.move sym
276
277
  end
@@ -281,18 +282,18 @@ shared_examples_for "a list" do
281
282
  [:top, :start].each do |sym|
282
283
  describe "#move_to_#{sym}" do
283
284
  it "#{sym} moves an item in list to the start of list" do
284
- item = category_1.items.order_by_position.last
285
+ item = category.items.order_by_position.last
285
286
  item.move to: sym
286
287
  item[position_field].should == 0
287
- category_1.items.order_by_position.map(&position_field).should == [0,1,2]
288
+ category.items.order_by_position.map(&position_field).should == [0,1,2]
288
289
  end
289
290
 
290
291
  it "#{sym} moves an item not in list to the start of list" do
291
- item = category_1.items.order_by_position.last
292
+ item = category.items.order_by_position.last
292
293
  item.remove_from_list
293
294
  item.move to: sym
294
295
  item[position_field].should == 0
295
- category_1.items.order_by_position.map(&position_field).should == [0,1,2]
296
+ category.items.order_by_position.map(&position_field).should == [0,1,2]
296
297
  end
297
298
  end
298
299
  end
@@ -300,18 +301,18 @@ shared_examples_for "a list" do
300
301
  [:end, :bottom].each do |sym|
301
302
  describe "#move_to_#{sym}" do
302
303
  it "#{sym} moves an item in list to the end of list" do
303
- item = category_1.items.order_by_position.first
304
+ item = category.items.order_by_position.first
304
305
  item.move to: sym
305
306
  item[position_field].should == 2
306
- category_1.reload.items.order_by_position.map(&position_field).should == [0,1,2]
307
+ category.reload.items.order_by_position.map(&position_field).should == [0,1,2]
307
308
  end
308
309
 
309
310
  it "#{sym} moves an item not in list to the end of list" do
310
- item = category_1.items.order_by_position.first
311
+ item = category.items.order_by_position.first
311
312
  item.remove_from_list
312
313
  item.move to: sym
313
314
  item[position_field].should == 2
314
- category_1.items.order_by_position.map(&position_field).should == [0,1,2]
315
+ category.items.order_by_position.map(&position_field).should == [0,1,2]
315
316
  end
316
317
  end
317
318
  end
@@ -321,7 +322,7 @@ shared_examples_for "a list" do
321
322
  let(:method) { "move_#{sym}" }
322
323
 
323
324
  context "for the last item of the list" do
324
- let(:item) { category_1.items.order_by_position.last }
325
+ let(:item) { category.items.order_by_position.last }
325
326
 
326
327
  it "does not change the item's position" do
327
328
  lambda do
@@ -331,7 +332,7 @@ shared_examples_for "a list" do
331
332
 
332
333
  it "keeps items ordered" do
333
334
  item.send method
334
- category_1.items.order_by_position.map(&position_field).should == [0,1,2]
335
+ category.items.order_by_position.map(&position_field).should == [0,1,2]
335
336
  end
336
337
 
337
338
  it "returns false" do
@@ -340,7 +341,7 @@ shared_examples_for "a list" do
340
341
  end
341
342
 
342
343
  context "for any other item" do
343
- let(:item) { category_1.items.order_by_position.first }
344
+ let(:item) { category.items.order_by_position.first }
344
345
 
345
346
  it "moves to the next position" do
346
347
  lambda do
@@ -362,7 +363,7 @@ shared_examples_for "a list" do
362
363
 
363
364
  it "keeps items ordered" do
364
365
  item.send method, 2
365
- category_1.items.order_by_position.map(&position_field).should == [0,1,2]
366
+ category.items.order_by_position.map(&position_field).should == [0,1,2]
366
367
  end
367
368
 
368
369
  it "returns true" do
@@ -377,7 +378,7 @@ shared_examples_for "a list" do
377
378
  let(:method) { "move_#{sym}" }
378
379
 
379
380
  context "for the first item of the list" do
380
- let(:item) { category_1.items.order_by_position.first }
381
+ let(:item) { category.items.order_by_position.first }
381
382
 
382
383
  it "does not change the item's position" do
383
384
  lambda do
@@ -387,7 +388,7 @@ shared_examples_for "a list" do
387
388
 
388
389
  it "keeps items ordered" do
389
390
  item.send method
390
- category_1.items.order_by_position.map(&position_field).should == [0,1,2]
391
+ category.items.order_by_position.map(&position_field).should == [0,1,2]
391
392
  end
392
393
 
393
394
  it "returns false" do
@@ -396,7 +397,7 @@ shared_examples_for "a list" do
396
397
  end
397
398
 
398
399
  context "for any other item" do
399
- let(:item) { category_1.items.order_by_position.last }
400
+ let(:item) { category.items.order_by_position.last }
400
401
 
401
402
  it "moves to the previous position" do
402
403
  lambda do
@@ -418,7 +419,7 @@ shared_examples_for "a list" do
418
419
 
419
420
  it "keeps items ordered" do
420
421
  item.send method
421
- category_1.items.order_by_position.map(&position_field).should == [0,1,2]
422
+ category.items.order_by_position.map(&position_field).should == [0,1,2]
422
423
  end
423
424
 
424
425
  it "returns true" do
@@ -435,8 +436,8 @@ shared_examples_for "a list" do
435
436
  end
436
437
 
437
438
  context "towards the start" do
438
- let(:other_item) { category_1.items.order_by_position.first }
439
- let(:item) { category_1.items.order_by_position.last }
439
+ let(:other_item) { category.items.order_by_position.first }
440
+ let(:item) { category.items.order_by_position.last }
440
441
 
441
442
  it "moves to the same position as other_item" do
442
443
  item[position_field].should == 0
@@ -447,12 +448,12 @@ shared_examples_for "a list" do
447
448
  end
448
449
 
449
450
  it "shifts any item after that by 1" do
450
- category_1.items.order_by_position.map(&position_field).should == [0,1,2]
451
+ category.items.order_by_position.map(&position_field).should == [0,1,2]
451
452
  end
452
453
  end
453
454
  context "towards the end" do
454
- let(:item) { category_1.items.order_by_position.first }
455
- let(:other_item) { category_1.items.order_by_position.last }
455
+ let(:item) { category.items.order_by_position.first }
456
+ let(:other_item) { category.items.order_by_position.last }
456
457
 
457
458
  it "moves to the same position as other_item" do
458
459
  item[position_field].should == 1
@@ -463,7 +464,7 @@ shared_examples_for "a list" do
463
464
  end
464
465
 
465
466
  it "shifts any item after that by 1" do
466
- category_1.items.order_by_position.map(&position_field).should == [0,1,2]
467
+ category.items.order_by_position.map(&position_field).should == [0,1,2]
467
468
  end
468
469
  end
469
470
  end
@@ -476,27 +477,27 @@ shared_examples_for "a list" do
476
477
  end
477
478
 
478
479
  context "towards the start" do
479
- let(:other_item) { category_1.items.order_by_position.first }
480
- let(:item) { category_1.items.order_by_position.last }
480
+ let(:other_item) { category.items.order_by_position.first }
481
+ let(:item) { category.items.order_by_position.last }
481
482
 
482
483
  it "moves to other_item's next position" do
483
484
  item[position_field].should == 1
484
485
  end
485
486
 
486
487
  it "shifts any item before that by -1" do
487
- category_1.items.order_by_position.map(&position_field).should == [0,1,2]
488
+ category.items.order_by_position.map(&position_field).should == [0,1,2]
488
489
  end
489
490
  end
490
491
  context "towards the end" do
491
- let(:item) { category_1.items.order_by_position.first }
492
- let(:other_item) { category_1.items.order_by_position.last }
492
+ let(:item) { category.items.order_by_position.first }
493
+ let(:other_item) { category.items.order_by_position.last }
493
494
 
494
495
  it "moves to the same position as other_item" do
495
496
  item[position_field].should == 2
496
497
  end
497
498
 
498
499
  it "shifts any item before that by -1" do
499
- category_1.items.order_by_position.map(&position_field).should == [0,1,2]
500
+ category.items.order_by_position.map(&position_field).should == [0,1,2]
500
501
  end
501
502
  end
502
503
  end
@@ -510,14 +511,15 @@ shared_examples_for "a list" do
510
511
  Mongoid::ActsAsList.configure {|c| c.start_list_at = @original_start}
511
512
  end
512
513
 
513
- it "is configurable" do
514
- category_3.items.should be_empty
515
- start = 1
516
- Mongoid::ActsAsList.configure {|c| c.start_list_at = start}
517
- item = category_3.items.create!
518
- item[position_field].should == start
519
- item = category_3.items.create!
520
- item[position_field].should == start+1
521
- end
514
+ # it "is configurable" do
515
+ # empty_category = category.class.create!
516
+ # empty_category.items.should be_empty
517
+ # start = 1
518
+ # Mongoid::ActsAsList.configure {|c| c.start_list_at = start}
519
+ # item = empty_category.items.create!
520
+ # item[position_field].should == start
521
+ # item = empty_category.items.create!
522
+ # item[position_field].should == start+1
523
+ # end
522
524
  end
523
525
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_acts_as_list
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-03-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &86692460 !ruby/object:Gem::Requirement
16
+ requirement: &74681830 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,32 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *86692460
24
+ version_requirements: *74681830
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &74681400 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *74681400
36
+ - !ruby/object:Gem::Dependency
37
+ name: simplecov
38
+ requirement: &74681120 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *74681120
25
47
  - !ruby/object:Gem::Dependency
26
48
  name: bson_ext
27
- requirement: &86692200 !ruby/object:Gem::Requirement
49
+ requirement: &74680850 !ruby/object:Gem::Requirement
28
50
  none: false
29
51
  requirements:
30
52
  - - ~>
@@ -32,10 +54,10 @@ dependencies:
32
54
  version: '1.5'
33
55
  type: :development
34
56
  prerelease: false
35
- version_requirements: *86692200
57
+ version_requirements: *74680850
36
58
  - !ruby/object:Gem::Dependency
37
59
  name: database_cleaner
38
- requirement: &86691800 !ruby/object:Gem::Requirement
60
+ requirement: &74680180 !ruby/object:Gem::Requirement
39
61
  none: false
40
62
  requirements:
41
63
  - - ! '>='
@@ -43,10 +65,10 @@ dependencies:
43
65
  version: '0'
44
66
  type: :development
45
67
  prerelease: false
46
- version_requirements: *86691800
68
+ version_requirements: *74680180
47
69
  - !ruby/object:Gem::Dependency
48
70
  name: pry
49
- requirement: &86691540 !ruby/object:Gem::Requirement
71
+ requirement: &74679440 !ruby/object:Gem::Requirement
50
72
  none: false
51
73
  requirements:
52
74
  - - ! '>='
@@ -54,10 +76,10 @@ dependencies:
54
76
  version: '0'
55
77
  type: :development
56
78
  prerelease: false
57
- version_requirements: *86691540
79
+ version_requirements: *74679440
58
80
  - !ruby/object:Gem::Dependency
59
81
  name: mongoid
60
- requirement: &86691140 !ruby/object:Gem::Requirement
82
+ requirement: &74678270 !ruby/object:Gem::Requirement
61
83
  none: false
62
84
  requirements:
63
85
  - - ! '>='
@@ -65,7 +87,7 @@ dependencies:
65
87
  version: 2.0.1
66
88
  type: :runtime
67
89
  prerelease: false
68
- version_requirements: *86691140
90
+ version_requirements: *74678270
69
91
  description: ''
70
92
  email:
71
93
  - olivier.melcher@gmail.com
@@ -75,6 +97,7 @@ extra_rdoc_files: []
75
97
  files:
76
98
  - .gitignore
77
99
  - .rspec
100
+ - .travis.yml
78
101
  - .vimrc.local
79
102
  - Gemfile
80
103
  - README.markdown
@@ -86,14 +109,16 @@ files:
86
109
  - lib/mongoid/acts_as_list/list/root.rb
87
110
  - lib/mongoid/acts_as_list/version.rb
88
111
  - lib/mongoid_acts_as_list.rb
112
+ - spec/acts_as_list/embeds_many_recursively_spec.rb
89
113
  - spec/acts_as_list/embeds_many_spec.rb
90
114
  - spec/acts_as_list/relational_spec.rb
91
115
  - spec/fixtures/embeds_many_models.rb
116
+ - spec/fixtures/embeds_many_recursively_models.rb
92
117
  - spec/fixtures/relational_models.rb
93
118
  - spec/mongoid_acts_as_list_spec.rb
94
119
  - spec/spec_helper.rb
95
120
  - spec/support/list_examples.rb
96
- homepage: ''
121
+ homepage: https://github.com/olivoil/mongoid_acts_as_list
97
122
  licenses: []
98
123
  post_install_message:
99
124
  rdoc_options: []