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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ba83db8ce90749467d06d5dd8bc18813b6f8103e
4
- data.tar.gz: 04bd58590bd3725568971bec161f328263994f59
3
+ metadata.gz: 29866fedcac520d326da58852285d277c3c8a5cf
4
+ data.tar.gz: 4316ef41d3eb2d83f10b5dd6641dae14faaa1718
5
5
  SHA512:
6
- metadata.gz: ddc574e4fdd3f6bdadcf020ec743aaa5a13d83faed21ba824a61594296c5d8b3fbdb8c9ed82718264fda34b55dac34bd9b7503ecbbc86cf7542b3d3e86b2ec56
7
- data.tar.gz: 2ea8de2fc4ba1334ddb7708713226d1d68bb92c4382a86a553bad5288d30e39d60c0738a19f2bf63a13512c55238565c0f7b50c57a01b374b28f8723fc584b44
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 in json (with different ids):
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
- group_id => {
46
- @id => {
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[group_id][@id].merge!({'c' => conditions.reduce({}) { |result, condition| result.merge! condition.ransackify }})
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
- groupings.each { |grouping| ransack_hash[group_id][@id].merge!(grouping.ransackify) }
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
@@ -1,3 +1,3 @@
1
1
  module RansackQuery
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  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.gsub(/"ransack_group.*?"/, '"g"')
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
@@ -6,16 +6,16 @@ FactoryGirl.define do
6
6
  attribute 'attribute'
7
7
  value 'value'
8
8
 
9
- trait :first_name do
9
+ trait :document_number do
10
10
  id 2
11
- attribute 'first_name'
12
- value 'Bob'
11
+ attribute 'document_number'
12
+ value '111'
13
13
  end
14
14
 
15
- trait :last_name do
15
+ trait :driver_name do
16
16
  id 3
17
- attribute 'last_name'
18
- value 'Smith'
17
+ attribute 'driver_name'
18
+ value '222'
19
19
  end
20
20
  end
21
21
 
@@ -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, :first_name)
23
- obj.add_condition FactoryGirl.build(:condition, :last_name)
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
 
@@ -26,70 +26,66 @@ FactoryGirl.define do
26
26
  skip_create
27
27
  initialize_with {
28
28
  {
29
- 'ransack_group_1' => {
30
- 1 => {
31
- 'm' => 'or',
32
- 'ransack_group_2' => {
33
- 2 => {
34
- 'm' => 'and',
35
- 'c' => {
36
- 2 => {
37
- 'a' => {
38
- '0' => {
39
- 'name' => 'first_name'
40
- }
41
- },
42
- 'p' => 'eq',
43
- 'v' => {
44
- '0' => {
45
- 'value' => 'Bob'
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
- 3 => {
50
- 'a' => {
51
- '0' => {
52
- 'name' => 'last_name'
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
- 'ransack_group_3' => {
66
- 3 => {
67
- 'm' => 'and',
68
- 'c' => {
69
- 2 => {
70
- 'a' => {
71
- '0' => {
72
- 'name' => 'first_name'
73
- }
74
- },
75
- 'p' => 'eq',
76
- 'v' => {
77
- '0' => {
78
- 'value' => 'Bob'
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
- 3 => {
83
- 'a' => {
84
- '0' => {
85
- 'name' => 'last_name'
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 json string' do
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("{\"g\":{\"id1\":{\"m\":\"and\",\"c\":{\"id2\":{\"a\":{\"0\":{\"name\":\"attribute\"}},\"p\":\"eq\",\"v\":{\"0\":{\"value\":\"value\"}}}}}}}" )
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("{\"q\":{\"g\":{\"id1\":{\"m\":\"and\",\"c\":{\"id2\":{\"a\":{\"0\":{\"name\":\"attribute\"}},\"p\":\"eq\",\"v\":{\"0\":{\"value\":\"value\"}}}}}}}}")
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ransack_query
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frank West