mongoid_sortable_relation 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 56a84064bb8deeae2440de1dc56a6bf3197f6149
4
- data.tar.gz: d6d084c4f728214902287b2193d8bb5e312ecb5c
3
+ metadata.gz: e118acaea75224925026e44e9dba970789285696
4
+ data.tar.gz: 6b599d7430a1c5b19607f94de1b127dc506f20b2
5
5
  SHA512:
6
- metadata.gz: 890a1e87c313fc7128e9b945199207d7b9e03e07b9c6705752167d8d23e81b9bc4ca90c9f77379cea4bcf05d85534ed74a81a7a40792190d474c165265c6fca1
7
- data.tar.gz: c08ec34103b2d231055f011d6fcaaf0044ab375f630313c46a5bc9d494a358a0a90d2e5619c2ecca57450fa89c8bf55690d05d67b16c6f335b7e02e003ca66eb
6
+ metadata.gz: 0208776b1b7b0ddb3c94c57caf8187d2e72ea933b9e7f34561f912e3e9b5e1da64f66ee8f35ac8b9d1c8618091ced24da9769b3b9e1f9c37f77790b062020a7f
7
+ data.tar.gz: 69bb477fc39c2772182b670ed5e590b76c958a5ca2bd5004657896b454c4aa9015a8345210afb7a2d30277a3ca0ee98378e912eb7061d34db7542a713a064483
@@ -12,6 +12,7 @@ require 'mongoid_sortable_relation/many/origin_optional'
12
12
  require 'mongoid_sortable_relation/many_to_many/referenced'
13
13
  require 'mongoid_sortable_relation/many_to_many/builder'
14
14
 
15
+ require 'mongoid_sortable_relation/accessors'
15
16
  require 'mongoid_sortable_relation/metadata'
16
17
 
17
18
  module MongoidSortableRelation
@@ -1,6 +1,29 @@
1
1
  module Mongoid
2
2
  module Relations
3
3
  module Accessors
4
+
5
+ private
6
+
7
+ def sort_related_items(object, name, metadata)
8
+ if metadata.relation.macro == :has_many
9
+ update_sortable_field(object, metadata)
10
+ else
11
+ nullify_related_items(send(name))
12
+ end
13
+ object.each &:reload
14
+ end
15
+
16
+ def nullify_related_items(related_items)
17
+ related_items.nullify unless related_items.empty?
18
+ end
19
+
20
+ def update_sortable_field(object, metadata)
21
+ object.each_with_index do |entity, index|
22
+ entity.send(:"#{metadata.sortable_field}=", index + 1)
23
+ entity.save
24
+ end
25
+ end
26
+
4
27
  module ClassMethods
5
28
 
6
29
  def setter(name, metadata)
@@ -17,26 +40,8 @@ module Mongoid
17
40
  self
18
41
  end
19
42
 
20
- private
21
-
22
- def sort_related_items(object, name, metadata)
23
- nullify_related_items(send(name))
24
- update_sortable_field(object, metadata) if metadata.relation.macro == :has_many
25
- object.each &:reload
26
- end
27
-
28
- def nullify_related_items(related_items)
29
- related_items.nullify unless related_items.empty?
30
- end
31
-
32
- def update_sortable_field(object, metadata)
33
- object.each_with_index do |entity, index|
34
- entity.send(:"#{metadata.sortable_field}=", index + 1)
35
- entity.save
36
- end
37
- end
38
-
39
43
  end
44
+
40
45
  end
41
46
  end
42
47
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module MongoidSortableRelation
3
- VERSION = '0.0.7'
3
+ VERSION = '0.0.8'
4
4
  end
@@ -6,13 +6,13 @@ describe 'One to many sortable relation' do
6
6
  User.create
7
7
  end
8
8
 
9
- describe 'get relations' do
9
+ let!(:posts) do
10
+ Post.create :user => user, :title => 'Title1', :user_position => 2
11
+ Post.create :user => user, :title => 'Title2', :user_position => 1
12
+ Post.create :user => user, :title => 'Title3', :user_position => 3
13
+ end
10
14
 
11
- let!(:posts) do
12
- Post.create :user => user, :title => 'Title1', :user_position => 2
13
- Post.create :user => user, :title => 'Title2', :user_position => 1
14
- Post.create :user => user, :title => 'Title3', :user_position => 3
15
- end
15
+ describe '.getter' do
16
16
 
17
17
  it 'returns ordered relations' do
18
18
  expect(user.posts.map(&:title)).to eq(%w(Title2 Title1 Title3))
@@ -20,4 +20,15 @@ describe 'One to many sortable relation' do
20
20
 
21
21
  end
22
22
 
23
+ describe '.setter' do
24
+
25
+ it 'updates order' do
26
+ posts = [Post.find_by(:title => 'Title1'), Post.find_by(:title => 'Title3'), Post.find_by(:title => 'Title2')]
27
+ user.posts = posts
28
+ user.save
29
+ expect(user.posts.map(&:title)).to eq(posts.map(&:title))
30
+ end
31
+
32
+ end
33
+
23
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_sortable_relation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladislav Melanitskiy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-30 00:00:00.000000000 Z
11
+ date: 2014-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid