mongoid_orderable 4.0.0 → 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- Njk2ODJiZDJmM2RiNmY2MTkzNDljNjkzYmI1ZGIzMzI5YzlhNjI1ZQ==
4
+ YThjMDBhYzQ5YmQ4NzJlZWRjMjdhZTIyNTdmMTkwMmE0YmZlZWI5NQ==
5
5
  data.tar.gz: !binary |-
6
- NjU4ZWExYTdjN2MzOGY0YWQzN2RiMTk1NzI1ODM1NzUyNTdjMDkyNw==
6
+ ZjBmZDk1OTM1MmE1ZGNiMmFiMDU1Y2IxYTAwMzZlZTkzZWFiMjhlYQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OWMyZGZkOTliN2MzNmVhOTExMWJkZDI5OTg0MzY0MTcwMjcyY2Q4MzMzZDdh
10
- OTFkM2Q2NWNkMDQwZGQ0Y2FhZDQ4OTliN2ViMDM2MGJiNGY2M2FkMzI1NWRl
11
- MjQ1MTM3NjkyNDZmOWYzZTYxYzY2NjI3ODI4OTQwYWJmZmMwNzA=
9
+ NDc5NWY5NTlkMDYzNWMzZGI3OGRiNjc0MDEzNjdhYWJiMDA5MTkzOTE5OGMx
10
+ YzA4NWZmNmZmZTJmNTZmNTdlNDFjZDk2NmJkOGQyNDMyMTM2Mjg2ZjU5NGE1
11
+ MmI0YzU2MmVhODdmMWY1MGI5ZDhhZTVkNGQ0NjRlNzNlMzI4ZDU=
12
12
  data.tar.gz: !binary |-
13
- ODFjOGIwNDhhMjdjNjk4M2E5MzY2M2JmZDdiMWI2MDgzNWI2M2Y0NDA0YzAz
14
- Mjg1YmU1OTM3YzgyNDVjZGU2ODdhMWU0YTcxYTc4YzQxYzE0MTRkYWIxYWNk
15
- ZTU2NThlOGZlMmRjMDc3NGQ4MmVkYjQxYzM4YzY0MzhkZmY1N2E=
13
+ NTYyN2E1ZTVlNDQ5YzZhNDY5Y2UyNDMzZWVhZWQ4YWZmMTJkNTI0ZDNhNzIy
14
+ ZDg0MTkyZjY3YWVkNzdmNWM5ZDY3ZWJiNDMxYzk2NDM0ZWUxOTFiMmMxMWEy
15
+ ZjE2MmRiZDcyY2U5NTg1MjU3ODkwOGIwMDIxMDY0NDMyMmExMzQ=
data/.travis.yml CHANGED
@@ -1,9 +1,9 @@
1
1
  rvm:
2
2
  - 1.8.7
3
- - 1.9.2
4
3
  - 1.9.3
5
- - ruby-head
6
- #- rbx-19mode
4
+ - 2.0.0
5
+ - 2.1.1
6
+ # - rbx
7
7
 
8
8
  env:
9
9
  - MONGOID_VERSION=2
@@ -16,9 +16,5 @@ matrix:
16
16
  env: MONGOID_VERSION=3
17
17
  - rvm: 1.8.7
18
18
  env: MONGOID_VERSION=4
19
- - rvm: 1.9.2
20
- env: MONGOID_VERSION=3
21
- - rvm: 1.9.2
22
- env: MONGOID_VERSION=4
23
19
 
24
20
  services: mongodb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
+ # master
2
+
3
+ # 4.1.0
4
+
5
+ * Resolving scope foreign key form passed relation name (@dsci)
6
+ * Fix: relation metadata quering (@pjkelly)
7
+ * `previous_items` and `next_items` methods (@mrjlynch)
8
+
1
9
  # 4.0.0
2
10
 
3
11
  * Semantic versioning
4
12
  * Added Mongoid 4 support (@dblock)
5
- * Fixes by @johnnyshields and @zhengjia
13
+ * Fixes by @johnnyshields and @zhengjia
data/README.md CHANGED
@@ -7,8 +7,8 @@ Mongoid::Orderable is a ordered list implementation for your mongoid models.
7
7
  # Why?
8
8
 
9
9
  * It uses native mongo batch increment feature
10
- * It supports assignable api
11
- * It proper assingns position while moving document between scopes
10
+ * It supports mutators api
11
+ * It correctly assigns the position while moving document between scopes
12
12
  * It supports mongoid 2, 3 and 4
13
13
 
14
14
  # How?
@@ -25,12 +25,16 @@ class Item
25
25
  include Mongoid::Orderable
26
26
 
27
27
  # belongs_to :group
28
+ # belongs_to :drawer, class_name: "Office::Drawer",
29
+ # foreign_key: "legacy_drawer_key_id"
28
30
 
29
31
  # orderable
30
- # orderable :scope => :group, :column => :pos
31
- # orderable :scope => lambda { |document| where(:group_id => document.group_id) }
32
- # orderable :index => false # this one if you want specify indexes manually
33
- # orderable :base => 0 # count position from zero as the top-most value (1 is the default value)
32
+ # orderable scope: :group, column: :pos
33
+ # orderable scope: :drawer, column: :pos # resolves scope foreign key from relation
34
+ # orderable scope: 'drawer', column: :pos # but if you pass a string - it will use it as is, as the column name for scope
35
+ # orderable scope: lambda { |document| where(group_id: document.group_id) }
36
+ # orderable index: false # this one if you want specify indexes manually
37
+ # orderable base: 0 # count position from zero as the top-most value (1 is the default value)
34
38
  end
35
39
  ```
36
40
 
@@ -52,6 +56,12 @@ item.move_to_top
52
56
  item.move_to_bottom
53
57
  item.move_higher
54
58
  item.move_lower
59
+
60
+ item.next_items # return a collection of items higher on the list
61
+ item.previous_items # return a collection of items lower on the list
62
+
63
+ item.next_item # returns the next item in the list
64
+ item.previous_item # returns the previous item in the list
55
65
  ```
56
66
 
57
67
  # Contributing
@@ -11,7 +11,17 @@ module Mongoid::Orderable
11
11
  }
12
12
 
13
13
  configuration.merge! options if options.is_a?(Hash)
14
- configuration[:scope] = "#{configuration[:scope]}_id".to_sym if configuration[:scope].is_a?(Symbol) && configuration[:scope].to_s !~ /_id$/
14
+
15
+ if configuration[:scope].is_a?(Symbol) && configuration[:scope].to_s !~ /_id$/
16
+ scope_relation = self.relations[configuration[:scope].to_s]
17
+ if scope_relation
18
+ configuration[:scope] = scope_relation.key.to_sym
19
+ else
20
+ configuration[:scope] = "#{configuration[:scope]}_id".to_sym
21
+ end
22
+ elsif configuration[:scope].is_a?(String)
23
+ configuration[:scope] = configuration[:scope].to_sym
24
+ end
15
25
 
16
26
  field configuration[:column], orderable_field_opts(configuration)
17
27
  if configuration[:index]
@@ -58,6 +68,42 @@ module Mongoid::Orderable
58
68
  end
59
69
  end
60
70
 
71
+ ##
72
+ # Returns items above the current document.
73
+ # Items with a position lower than this document's position.
74
+ def previous_items
75
+ orderable_scoped.where(orderable_column.lt => self.position)
76
+ end
77
+ alias_method :prev_items, :previous_items
78
+
79
+ ##
80
+ # Returns items below the current document.
81
+ # Items with a position greater than this document's position.
82
+ def next_items
83
+ orderable_scoped.where(orderable_column.gt => self.position)
84
+ end
85
+
86
+ # returns the previous item in the list
87
+ def previous_item
88
+ if previous_items.present?
89
+ previous_position = self.position - 1
90
+ orderable_scoped.where(:position => previous_position).first
91
+ else
92
+ nil
93
+ end
94
+ end
95
+ alias_method :prev_item, :previous_item
96
+
97
+ # returns the next item in the list
98
+ def next_item
99
+ if next_items.present?
100
+ next_position = self.position + 1
101
+ orderable_scoped.where(:position => next_position).first
102
+ else
103
+ nil
104
+ end
105
+ end
106
+
61
107
  def move_to! target_position
62
108
  @move_to = target_position
63
109
  save
@@ -126,7 +172,7 @@ private
126
172
 
127
173
  def orderable_scoped
128
174
  if embedded?
129
- send(metadata.inverse).send(metadata.name).orderable_scope(self)
175
+ send(MongoidOrderable.metadata(self).inverse).send(MongoidOrderable.metadata(self).name).orderable_scope(self)
130
176
  else
131
177
  (orderable_inherited_class || self.class).orderable_scope(self)
132
178
  end
@@ -13,6 +13,14 @@ module MongoidOrderable
13
13
  instance.inc(attribute => value)
14
14
  end
15
15
  end
16
+
17
+ def self.metadata instance
18
+ if MongoidOrderable.mongoid2? || MongoidOrderable.mongoid3?
19
+ instance.metadata
20
+ else
21
+ instance.relation_metadata
22
+ end
23
+ end
16
24
  end
17
25
 
18
26
  require 'mongoid'
@@ -1,3 +1,3 @@
1
1
  module MongoidOrderable
2
- VERSION = '4.0.0'
2
+ VERSION = '4.1.0'
3
3
  end
@@ -25,6 +25,15 @@ describe Mongoid::Orderable do
25
25
  orderable :scope => :group
26
26
  end
27
27
 
28
+ class StringScopedOrderable
29
+ include Mongoid::Document
30
+ include Mongoid::Orderable
31
+
32
+ field :some_scope, :type => Integer
33
+
34
+ orderable :scope => 'some_scope'
35
+ end
36
+
28
37
  class EmbedsOrderable
29
38
  include Mongoid::Document
30
39
 
@@ -74,6 +83,16 @@ describe Mongoid::Orderable do
74
83
  class Orange < Fruit
75
84
  end
76
85
 
86
+ class ForeignKeyDiffersOrderable
87
+ include Mongoid::Document
88
+ include Mongoid::Orderable
89
+
90
+ belongs_to :different_scope, :class_name => "ForeignKeyDiffersOrderable",
91
+ :foreign_key => "different_orderable_id"
92
+
93
+ orderable :scope => :different_scope
94
+ end
95
+
77
96
  describe SimpleOrderable do
78
97
 
79
98
  before :each do
@@ -200,6 +219,21 @@ describe Mongoid::Orderable do
200
219
  record.reload.position.should == 3
201
220
  end
202
221
  end
222
+
223
+ describe 'utiity methods' do
224
+
225
+ it "should return a collection of items lower/higher on the list for next_items/previous_items" do
226
+ record_1 = SimpleOrderable.where(:position => 1).first
227
+ record_2 = SimpleOrderable.where(:position => 2).first
228
+ record_3 = SimpleOrderable.where(:position => 3).first
229
+ record_4 = SimpleOrderable.where(:position => 4).first
230
+ record_5 = SimpleOrderable.where(:position => 5).first
231
+ expect(record_1.next_items.to_a).to eq([record_2, record_3, record_4, record_5])
232
+ expect(record_5.previous_items.to_a).to eq([record_1, record_2, record_3, record_4])
233
+ expect(record_3.previous_items.to_a).to eq([record_1, record_2])
234
+ expect(record_3.next_items.to_a).to eq([record_4, record_5])
235
+ end
236
+ end
203
237
  end
204
238
 
205
239
  describe ScopedOrderable do
@@ -321,6 +355,38 @@ describe Mongoid::Orderable do
321
355
  end
322
356
  end
323
357
  end
358
+
359
+ describe 'utiity methods' do
360
+
361
+ it "should return a collection of items lower/higher on the list for next_items/previous_items" do
362
+ record_1 = SimpleOrderable.where(:position => 1).first
363
+ record_2 = SimpleOrderable.where(:position => 2).first
364
+ record_3 = SimpleOrderable.where(:position => 3).first
365
+ record_4 = SimpleOrderable.where(:position => 4).first
366
+ record_5 = SimpleOrderable.where(:position => 5).first
367
+ expect(record_1.next_items.to_a).to eq([record_2, record_3, record_4, record_5])
368
+ expect(record_5.previous_items.to_a).to eq([record_1, record_2, record_3, record_4])
369
+ expect(record_3.previous_items.to_a).to eq([record_1, record_2])
370
+ expect(record_3.next_items.to_a).to eq([record_4, record_5])
371
+ # next_item & previous_item testing
372
+ expect(record_1.next_item).to eq(record_2)
373
+ expect(record_2.previous_item).to eq(record_1)
374
+ expect(record_1.previous_item).to eq(nil)
375
+ expect(record_5.next_item).to eq(nil)
376
+ end
377
+ end
378
+ end
379
+
380
+ describe StringScopedOrderable do
381
+
382
+ it 'uses the foreign key of the relationship as scope' do
383
+ orderable1 = StringScopedOrderable.create(:some_scope => 1)
384
+ orderable2 = StringScopedOrderable.create(:some_scope => 1)
385
+ orderable3 = StringScopedOrderable.create(:some_scope => 2)
386
+ expect(orderable1.position).to eq 1
387
+ expect(orderable2.position).to eq 2
388
+ expect(orderable3.position).to eq 1
389
+ end
324
390
  end
325
391
 
326
392
  describe EmbeddedOrderable do
@@ -492,6 +558,26 @@ describe Mongoid::Orderable do
492
558
  record.reload.position.should == 3
493
559
  end
494
560
  end
561
+
562
+ describe 'utiity methods' do
563
+
564
+ it "should return a collection of items lower/higher on the list for next_items/previous_items" do
565
+ record_1 = SimpleOrderable.where(:position => 1).first
566
+ record_2 = SimpleOrderable.where(:position => 2).first
567
+ record_3 = SimpleOrderable.where(:position => 3).first
568
+ record_4 = SimpleOrderable.where(:position => 4).first
569
+ record_5 = SimpleOrderable.where(:position => 5).first
570
+ expect(record_1.next_items.to_a).to eq([record_2, record_3, record_4, record_5])
571
+ expect(record_5.previous_items.to_a).to eq([record_1, record_2, record_3, record_4])
572
+ expect(record_3.previous_items.to_a).to eq([record_1, record_2])
573
+ expect(record_3.next_items.to_a).to eq([record_4, record_5])
574
+ # next_item & previous_item testing
575
+ expect(record_1.next_item).to eq(record_2)
576
+ expect(record_2.previous_item).to eq(record_1)
577
+ expect(record_1.previous_item).to eq(nil)
578
+ expect(record_5.next_item).to eq(nil)
579
+ end
580
+ end
495
581
  end
496
582
 
497
583
  describe Fruit do
@@ -503,4 +589,21 @@ describe Mongoid::Orderable do
503
589
  fruit2.position.should == 2
504
590
  end
505
591
  end
592
+
593
+ describe ForeignKeyDiffersOrderable do
594
+
595
+ it 'uses the foreign key of the relationship as scope' do
596
+ orderable1, orderable2, orderable3 = nil
597
+ parent_scope1 = ForeignKeyDiffersOrderable.create
598
+ parent_scope2 = ForeignKeyDiffersOrderable.create
599
+ expect do
600
+ orderable1 = ForeignKeyDiffersOrderable.create(:different_scope => parent_scope1)
601
+ orderable2 = ForeignKeyDiffersOrderable.create(:different_scope => parent_scope1)
602
+ orderable3 = ForeignKeyDiffersOrderable.create(:different_scope => parent_scope2)
603
+ end.to_not raise_error
604
+ expect(orderable1.position).to eq 1
605
+ expect(orderable2.position).to eq 2
606
+ expect(orderable3.position).to eq 1
607
+ end
608
+ end
506
609
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_orderable
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - pyromaniac
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-22 00:00:00.000000000 Z
11
+ date: 2014-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -96,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
96
  version: '0'
97
97
  requirements: []
98
98
  rubyforge_project: mongoid_orderable
99
- rubygems_version: 2.1.9
99
+ rubygems_version: 2.2.1
100
100
  signing_key:
101
101
  specification_version: 4
102
102
  summary: Acts as list mongoid implementation