mongoid_sortable_relation 0.0.6 → 0.0.7
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 56a84064bb8deeae2440de1dc56a6bf3197f6149
|
4
|
+
data.tar.gz: d6d084c4f728214902287b2193d8bb5e312ecb5c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 890a1e87c313fc7128e9b945199207d7b9e03e07b9c6705752167d8d23e81b9bc4ca90c9f77379cea4bcf05d85534ed74a81a7a40792190d474c165265c6fca1
|
7
|
+
data.tar.gz: c08ec34103b2d231055f011d6fcaaf0044ab375f630313c46a5bc9d494a358a0a90d2e5619c2ecca57450fa89c8bf55690d05d67b16c6f335b7e02e003ca66eb
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Relations
|
3
|
+
module Accessors
|
4
|
+
module ClassMethods
|
5
|
+
|
6
|
+
def setter(name, metadata)
|
7
|
+
re_define_method("#{name}=") do |object|
|
8
|
+
without_autobuild do
|
9
|
+
sort_related_items(object, name, metadata) if metadata.sortable?
|
10
|
+
if value = get_relation(name, metadata, object)
|
11
|
+
set_relation(name, value.substitute(object.substitutable))
|
12
|
+
else
|
13
|
+
__build__(name, object.substitutable, metadata)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
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
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -6,15 +6,15 @@ describe 'Many to many sortable relation' do
|
|
6
6
|
Post.create
|
7
7
|
end
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
9
|
+
let!(:tags) do
|
10
|
+
tag1 = Tag.create :name => 'Name1'
|
11
|
+
tag2 = Tag.create :name => 'Name2'
|
12
|
+
tag3 = Tag.create :name => 'Name3'
|
13
|
+
post.tags = [tag2, tag1, tag3]
|
14
|
+
post.save
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '.getter' do
|
18
18
|
|
19
19
|
it 'returns ordered relations' do
|
20
20
|
expect(post.tags.map(&:name)).to eq(%w(Name2 Name1 Name3))
|
@@ -22,4 +22,14 @@ describe 'Many to many sortable relation' do
|
|
22
22
|
|
23
23
|
end
|
24
24
|
|
25
|
+
describe '.setter' do
|
26
|
+
|
27
|
+
it 'updates order' do
|
28
|
+
tags = [Tag.find_by(:name => 'Name1'), Tag.find_by(:name => 'Name3'), Tag.find_by(:name => 'Name2')]
|
29
|
+
post.tags = tags
|
30
|
+
expect(post.tags.map(&:name)).to eq(tags.map(&:name))
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
25
35
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_sortable_relation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vladislav Melanitskiy
|
@@ -46,6 +46,7 @@ extensions: []
|
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
48
|
- lib/mongoid_sortable_relation.rb
|
49
|
+
- lib/mongoid_sortable_relation/accessors.rb
|
49
50
|
- lib/mongoid_sortable_relation/many/builder.rb
|
50
51
|
- lib/mongoid_sortable_relation/many/findable.rb
|
51
52
|
- lib/mongoid_sortable_relation/many/macros.rb
|