ransack_query 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -4
- data/lib/grouping.rb +5 -10
- data/lib/ransack_query/version.rb +1 -1
- data/lib/ransack_query.rb +17 -2
- data/spec/factories/condition.rb +6 -6
- data/spec/factories/grouping.rb +2 -2
- data/spec/factories/hash.rb +51 -55
- data/spec/models/ransack_query_spec.rb +14 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29866fedcac520d326da58852285d277c3c8a5cf
|
4
|
+
data.tar.gz: 4316ef41d3eb2d83f10b5dd6641dae14faaa1718
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4783eb8b35d2c9e286526346da0e0e75f8bec468681fe678919ea13e193ed7098eb5af264c4b8282bd51e5c64dda4ec470dbe41938d3b53f3ccbdeb57885e1f3
|
7
|
+
data.tar.gz: 2026a2f35455d90bc04654fe30f46ae042360510b779531c1c9570d44fb17638693b5f9360c3f8d8d5c50ddf85a540e88b9a23984dfc754dc1b2e7c7a623f967
|
data/README.md
CHANGED
@@ -42,7 +42,7 @@ and with arrays
|
|
42
42
|
end
|
43
43
|
and with blocks
|
44
44
|
|
45
|
-
RansackQuery.build do |grouping|
|
45
|
+
RansackQuery.build(prefix: 'q') do |grouping|
|
46
46
|
grouping.add_condition do |condition|
|
47
47
|
condition.attribute = 'first_name'
|
48
48
|
condition.value = 'Bob'
|
@@ -56,7 +56,7 @@ and with blocks
|
|
56
56
|
end
|
57
57
|
|
58
58
|
|
59
|
-
All Produce the following output
|
59
|
+
All Produce the following output (with different ids):
|
60
60
|
|
61
61
|
{
|
62
62
|
"g" => {
|
@@ -97,7 +97,7 @@ All Produce the following output in json (with different ids):
|
|
97
97
|
|
98
98
|
and passing the following options hash to build
|
99
99
|
|
100
|
-
RansackQuery.build(prefix: 'q')
|
100
|
+
RansackQuery.build(format: :json, prefix: 'q')
|
101
101
|
|
102
102
|
will produce the following in json
|
103
103
|
|
@@ -229,4 +229,4 @@ Will produce:
|
|
229
229
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
230
230
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
231
231
|
4. Push to the branch (`git push origin my-new-feature`)
|
232
|
-
5. Create a new Pull Request
|
232
|
+
5. Create a new Pull Request
|
data/lib/grouping.rb
CHANGED
@@ -42,10 +42,8 @@ class Grouping
|
|
42
42
|
|
43
43
|
def ransackify
|
44
44
|
ransack_hash = {
|
45
|
-
|
46
|
-
|
47
|
-
'm' => @combinator.to_s
|
48
|
-
}
|
45
|
+
@id => {
|
46
|
+
'm' => @combinator.to_s
|
49
47
|
}
|
50
48
|
}
|
51
49
|
ransackify_conditions(ransack_hash)
|
@@ -55,18 +53,15 @@ class Grouping
|
|
55
53
|
|
56
54
|
private
|
57
55
|
|
58
|
-
def group_id
|
59
|
-
"ransack_group_#{id}"
|
60
|
-
end
|
61
|
-
|
62
56
|
def ransackify_conditions(ransack_hash)
|
63
57
|
return if conditions.empty?
|
64
|
-
ransack_hash[
|
58
|
+
ransack_hash[@id].merge!({'c' => conditions.reduce({}) { |result, condition| result.merge! condition.ransackify }})
|
65
59
|
end
|
66
60
|
|
67
61
|
def ransackify_groupings(ransack_hash)
|
68
62
|
return if groupings.empty?
|
69
|
-
|
63
|
+
ransack_hash[@id].merge!({'g' => {}})
|
64
|
+
groupings.each { |grouping| ransack_hash[@id]['g'].merge!(grouping.ransackify) }
|
70
65
|
end
|
71
66
|
|
72
67
|
end
|
data/lib/ransack_query.rb
CHANGED
@@ -10,13 +10,28 @@ module RansackQuery
|
|
10
10
|
grouping = Grouping.new do |new_grouping|
|
11
11
|
yield new_grouping
|
12
12
|
end
|
13
|
-
ransack_hash = grouping.ransackify
|
13
|
+
ransack_hash = {'g' => grouping.ransackify}
|
14
14
|
ransack_hash = {options[:prefix] => ransack_hash} if options[:prefix]
|
15
|
-
ransack_hash.to_json
|
15
|
+
ransack_hash = ransack_hash.to_json if options[:format] == :json
|
16
|
+
ransack_hash
|
16
17
|
end
|
17
18
|
|
18
19
|
def self.generate_id
|
19
20
|
SecureRandom.hex
|
20
21
|
end
|
21
22
|
|
23
|
+
def self.sample
|
24
|
+
build do |grouping|
|
25
|
+
grouping.combinator = :or
|
26
|
+
grouping.add_grouping do |new_grouping|
|
27
|
+
new_grouping.add_condition(Condition.new(attribute: 'document_number', value: '111'))
|
28
|
+
new_grouping.add_condition(Condition.new(attribute: 'driver_name', value: '222'))
|
29
|
+
end
|
30
|
+
grouping.add_grouping do |new_grouping|
|
31
|
+
new_grouping.add_condition(Condition.new(attribute: 'document_number', value: '333'))
|
32
|
+
new_grouping.add_condition(Condition.new(attribute: 'driver_name', value: '444'))
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
22
37
|
end
|
data/spec/factories/condition.rb
CHANGED
@@ -6,16 +6,16 @@ FactoryGirl.define do
|
|
6
6
|
attribute 'attribute'
|
7
7
|
value 'value'
|
8
8
|
|
9
|
-
trait :
|
9
|
+
trait :document_number do
|
10
10
|
id 2
|
11
|
-
attribute '
|
12
|
-
value '
|
11
|
+
attribute 'document_number'
|
12
|
+
value '111'
|
13
13
|
end
|
14
14
|
|
15
|
-
trait :
|
15
|
+
trait :driver_name do
|
16
16
|
id 3
|
17
|
-
attribute '
|
18
|
-
value '
|
17
|
+
attribute 'driver_name'
|
18
|
+
value '222'
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
data/spec/factories/grouping.rb
CHANGED
@@ -19,8 +19,8 @@ FactoryGirl.define do
|
|
19
19
|
trait :with_conditions do
|
20
20
|
id 2
|
21
21
|
after(:build) do |obj|
|
22
|
-
obj.add_condition FactoryGirl.build(:condition, :
|
23
|
-
obj.add_condition FactoryGirl.build(:condition, :
|
22
|
+
obj.add_condition FactoryGirl.build(:condition, :document_number)
|
23
|
+
obj.add_condition FactoryGirl.build(:condition, :driver_name)
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
data/spec/factories/hash.rb
CHANGED
@@ -26,70 +26,66 @@ FactoryGirl.define do
|
|
26
26
|
skip_create
|
27
27
|
initialize_with {
|
28
28
|
{
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
'
|
38
|
-
'
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
'
|
44
|
-
'
|
45
|
-
|
46
|
-
|
29
|
+
1 => {
|
30
|
+
'm' => 'or',
|
31
|
+
'g' => {
|
32
|
+
2 => {
|
33
|
+
'm' => 'and',
|
34
|
+
'c' => {
|
35
|
+
2 => {
|
36
|
+
'a' => {
|
37
|
+
'0' => {
|
38
|
+
'name' => 'document_number'
|
39
|
+
}
|
40
|
+
},
|
41
|
+
'p' => 'eq',
|
42
|
+
'v' => {
|
43
|
+
'0' => {
|
44
|
+
'value' => '111'
|
45
|
+
}
|
46
|
+
}
|
47
|
+
},
|
48
|
+
3 => {
|
49
|
+
'a' => {
|
50
|
+
'0' => {
|
51
|
+
'name' => 'driver_name'
|
47
52
|
}
|
48
53
|
},
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
}
|
54
|
-
},
|
55
|
-
'p' => 'eq',
|
56
|
-
'v' => {
|
57
|
-
'0' => {
|
58
|
-
'value' => 'Smith'
|
59
|
-
}
|
54
|
+
'p' => 'eq',
|
55
|
+
'v' => {
|
56
|
+
'0' => {
|
57
|
+
'value' => '222'
|
60
58
|
}
|
61
59
|
}
|
62
60
|
}
|
63
61
|
}
|
64
62
|
},
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
'
|
71
|
-
'
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
'
|
77
|
-
'
|
78
|
-
|
79
|
-
|
63
|
+
3 => {
|
64
|
+
'm' => 'and',
|
65
|
+
'c' => {
|
66
|
+
2 => {
|
67
|
+
'a' => {
|
68
|
+
'0' => {
|
69
|
+
'name' => 'document_number'
|
70
|
+
}
|
71
|
+
},
|
72
|
+
'p' => 'eq',
|
73
|
+
'v' => {
|
74
|
+
'0' => {
|
75
|
+
'value' => '111'
|
76
|
+
}
|
77
|
+
}
|
78
|
+
},
|
79
|
+
3 => {
|
80
|
+
'a' => {
|
81
|
+
'0' => {
|
82
|
+
'name' => 'driver_name'
|
80
83
|
}
|
81
84
|
},
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
}
|
87
|
-
},
|
88
|
-
'p' => 'eq',
|
89
|
-
'v' => {
|
90
|
-
'0' => {
|
91
|
-
'value' => 'Smith'
|
92
|
-
}
|
85
|
+
'p' => 'eq',
|
86
|
+
'v' => {
|
87
|
+
'0' => {
|
88
|
+
'value' => '222'
|
93
89
|
}
|
94
90
|
}
|
95
91
|
}
|
@@ -13,7 +13,7 @@ describe RansackQuery do
|
|
13
13
|
klass.should equal Grouping
|
14
14
|
end
|
15
15
|
|
16
|
-
it 'should return a
|
16
|
+
it 'should return a hash' do
|
17
17
|
RansackQuery.build do |grouping|
|
18
18
|
grouping.id = 'id1'
|
19
19
|
grouping.add_condition do |condition|
|
@@ -21,7 +21,18 @@ describe RansackQuery do
|
|
21
21
|
condition.attribute = 'attribute'
|
22
22
|
condition.value = 'value'
|
23
23
|
end
|
24
|
-
end.should eq(
|
24
|
+
end.should eq({'g' => {'id1' => {'m' => 'and', 'c' => {'id2' => {'a' => {'0' => {'name' => 'attribute'}}, 'p' => 'eq', 'v' => {'0' => {'value' => 'value'}}}}}}})
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should return a json string if specified' do
|
28
|
+
RansackQuery.build(format: :json) do |grouping|
|
29
|
+
grouping.id = 'id1'
|
30
|
+
grouping.add_condition do |condition|
|
31
|
+
condition.id = 'id2'
|
32
|
+
condition.attribute = 'attribute'
|
33
|
+
condition.value = 'value'
|
34
|
+
end
|
35
|
+
end.should eq "{\"g\":{\"id1\":{\"m\":\"and\",\"c\":{\"id2\":{\"a\":{\"0\":{\"name\":\"attribute\"}},\"p\":\"eq\",\"v\":{\"0\":{\"value\":\"value\"}}}}}}}"
|
25
36
|
end
|
26
37
|
|
27
38
|
it 'should allow a prefix to be passed in' do
|
@@ -32,7 +43,7 @@ describe RansackQuery do
|
|
32
43
|
condition.attribute = 'attribute'
|
33
44
|
condition.value = 'value'
|
34
45
|
end
|
35
|
-
end.should eq(
|
46
|
+
end.should eq({'q' => {'g' => {'id1' => {'m' => 'and', 'c' => {'id2' => {'a' => {'0' => {'name' => 'attribute'}}, 'p' => 'eq', 'v' => {'0' => {'value' => 'value'}}}}}}}})
|
36
47
|
end
|
37
48
|
|
38
49
|
end
|