ransack_query 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 77ba48428f3a018d79c6ea475d82d3f8b31201c7
4
- data.tar.gz: d372b67bebccd07ff7823d376ffb6d4c3dfb611b
3
+ metadata.gz: ba83db8ce90749467d06d5dd8bc18813b6f8103e
4
+ data.tar.gz: 04bd58590bd3725568971bec161f328263994f59
5
5
  SHA512:
6
- metadata.gz: 1c9ec53846f5b04e2db3640932d9d853a19565ccab19be3bd68930e8194d46ac8c0454e3e923a51b974c7ab2fab0b22bdb818dc215d34878c42c1a9ebde08c94
7
- data.tar.gz: 9761dfeb707d6d5930e84ffeaefe5c49bf9a5cf1db6c47fb2b5e08b43250f5fe25e4e8703366c2781d569acea41957bf119d3c8f3fab71e0e1ca1d5a1e4ffd6f
6
+ metadata.gz: ddc574e4fdd3f6bdadcf020ec743aaa5a13d83faed21ba824a61594296c5d8b3fbdb8c9ed82718264fda34b55dac34bd9b7503ecbbc86cf7542b3d3e86b2ec56
7
+ data.tar.gz: 2ea8de2fc4ba1334ddb7708713226d1d68bb92c4382a86a553bad5288d30e39d60c0738a19f2bf63a13512c55238565c0f7b50c57a01b374b28f8723fc584b44
data/README.md CHANGED
@@ -26,27 +26,23 @@ Simple Usage:
26
26
  grouping.add_condition(Condition.new(attribute: 'first_name', value: 'Bob'))
27
27
  grouping.add_condition(Condition.new(attribute: 'last_name', value: 'Smith', predicate: :not_eq))
28
28
  end
29
- and
29
+ and chainable
30
30
 
31
31
  RansackQuery.build do |grouping|
32
32
  grouping.add_condition(Condition.new(attribute: 'first_name', value: 'Bob')).
33
33
  add_condition(Condition.new(attribute: 'last_name', value: 'Smith', predicate: :not_eq))
34
34
  end
35
- and
35
+ and with arrays
36
36
 
37
37
  RansackQuery.build do |grouping|
38
38
  conditions = []
39
39
  conditions << Condition.new(attribute: 'first_name', value: 'Bob')
40
- conditions << Condition.new do |condition|
41
- condition.attribute = 'last_name'
42
- condition.value = 'Smith'
43
- condition.predicate = :not_eq
44
- end
40
+ conditions << Condition.new(attribute: 'last_name', value: 'Smith', predicate: :not_eq)
45
41
  grouping.add_conditions(conditions)
46
42
  end
47
- and
43
+ and with blocks
48
44
 
49
- RansackQuery.build(prefix: 'q') do |grouping|
45
+ RansackQuery.build do |grouping|
50
46
  grouping.add_condition do |condition|
51
47
  condition.attribute = 'first_name'
52
48
  condition.value = 'Bob'
@@ -60,7 +56,7 @@ and
60
56
  end
61
57
 
62
58
 
63
- All Produce the following output (with different ids):
59
+ All Produce the following output in json (with different ids):
64
60
 
65
61
  {
66
62
  "g" => {
@@ -101,7 +97,7 @@ All Produce the following output (with different ids):
101
97
 
102
98
  and passing the following options hash to build
103
99
 
104
- RansackQuery.build(format: :json, prefix: 'q')
100
+ RansackQuery.build(prefix: 'q')
105
101
 
106
102
  will produce the following in json
107
103
 
@@ -229,7 +225,7 @@ Will produce:
229
225
 
230
226
  ## Contributing
231
227
 
232
- 1. Fork it ( https://github.com/[my-github-username]/ransack_query/fork )
228
+ 1. Fork it ( https://github.com/frank-west-iii/ransack_query/fork )
233
229
  2. Create your feature branch (`git checkout -b my-new-feature`)
234
230
  3. Commit your changes (`git commit -am 'Add some feature'`)
235
231
  4. Push to the branch (`git push origin my-new-feature`)
data/lib/grouping.rb CHANGED
@@ -42,7 +42,7 @@ class Grouping
42
42
 
43
43
  def ransackify
44
44
  ransack_hash = {
45
- 'g' => {
45
+ group_id => {
46
46
  @id => {
47
47
  'm' => @combinator.to_s
48
48
  }
@@ -55,14 +55,18 @@ class Grouping
55
55
 
56
56
  private
57
57
 
58
+ def group_id
59
+ "ransack_group_#{id}"
60
+ end
61
+
58
62
  def ransackify_conditions(ransack_hash)
59
63
  return if conditions.empty?
60
- ransack_hash['g'][@id].merge!({'c' => conditions.reduce({}) {|result, condition| result.merge! condition.ransackify}})
64
+ ransack_hash[group_id][@id].merge!({'c' => conditions.reduce({}) { |result, condition| result.merge! condition.ransackify }})
61
65
  end
62
66
 
63
67
  def ransackify_groupings(ransack_hash)
64
68
  return if groupings.empty?
65
- groupings.each {|grouping| ransack_hash['g'][@id].merge!(grouping.ransackify)}
69
+ groupings.each { |grouping| ransack_hash[group_id][@id].merge!(grouping.ransackify) }
66
70
  end
67
71
 
68
72
  end
@@ -1,3 +1,3 @@
1
1
  module RansackQuery
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/ransack_query.rb CHANGED
@@ -12,8 +12,7 @@ module RansackQuery
12
12
  end
13
13
  ransack_hash = grouping.ransackify
14
14
  ransack_hash = {options[:prefix] => ransack_hash} if options[:prefix]
15
- ransack_hash = ransack_hash.to_json if options[:format] == :json
16
- ransack_hash
15
+ ransack_hash.to_json.gsub(/"ransack_group.*?"/, '"g"')
17
16
  end
18
17
 
19
18
  def self.generate_id
@@ -7,7 +7,12 @@ FactoryGirl.define do
7
7
  trait :complex do
8
8
  combinator :or
9
9
  after(:build) do |obj|
10
- obj.add_grouping(FactoryGirl.build(:grouping, :with_conditions))
10
+ grouping = FactoryGirl.build(:grouping, :with_conditions)
11
+ grouping.id = 2
12
+ obj.add_grouping(grouping)
13
+ grouping = FactoryGirl.build(:grouping, :with_conditions)
14
+ grouping.id = 3
15
+ obj.add_grouping(grouping)
11
16
  end
12
17
  end
13
18
 
@@ -26,10 +26,10 @@ FactoryGirl.define do
26
26
  skip_create
27
27
  initialize_with {
28
28
  {
29
- 'g' => {
29
+ 'ransack_group_1' => {
30
30
  1 => {
31
31
  'm' => 'or',
32
- 'g' => {
32
+ 'ransack_group_2' => {
33
33
  2 => {
34
34
  'm' => 'and',
35
35
  'c' => {
@@ -61,10 +61,44 @@ FactoryGirl.define do
61
61
  }
62
62
  }
63
63
  }
64
+ },
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
+ }
80
+ }
81
+ },
82
+ 3 => {
83
+ 'a' => {
84
+ '0' => {
85
+ 'name' => 'last_name'
86
+ }
87
+ },
88
+ 'p' => 'eq',
89
+ 'v' => {
90
+ '0' => {
91
+ 'value' => 'Smith'
92
+ }
93
+ }
94
+ }
95
+ }
96
+ }
64
97
  }
65
98
  }
66
99
  }
67
- } }
100
+ }
101
+ }
68
102
  end
69
103
 
70
104
  end
@@ -39,6 +39,28 @@ describe Grouping do
39
39
  subject.groupings.size.should eq 2
40
40
  end
41
41
 
42
+ it 'should allow adding groupings without chaining' do
43
+ subject.add_grouping(Grouping.new)
44
+ subject.add_grouping(Grouping.new)
45
+ subject.groupings.size.should eq 2
46
+ end
47
+
48
+ it 'should do stuff' do
49
+ g = Grouping.new do |grouping|
50
+ grouping.combinator = :or
51
+ grouping.add_grouping do |new_grouping|
52
+ new_grouping.add_condition(Condition.new(attribute: 'ItemNumber', value: 'RC')).
53
+ add_condition(Condition.new(attribute: 'LocationCode', value: '002'))
54
+ end
55
+ grouping.add_grouping do |new_grouping|
56
+ new_grouping.add_condition(Condition.new(attribute: 'ItemNumber', value: 'RC')).
57
+ add_condition(Condition.new(attribute: 'LocationCode', value: '001'))
58
+ end
59
+ end
60
+
61
+ g.groupings.size.should eq 2
62
+ end
63
+
42
64
  it 'should allow adding conditions en masse' do
43
65
  subject.add_conditions(
44
66
  [Condition.new(attribute: 'location_code', value: '001', predicate: 'eq'),
@@ -75,7 +97,7 @@ describe Grouping do
75
97
  id.should eq grouping.id
76
98
  end
77
99
 
78
- it 'should build a ransack hash' do
100
+ it 'should build a complex ransack hash' do
79
101
  complex.ransackify.should eq FactoryGirl.build(:grouping_hash)
80
102
  end
81
103
 
@@ -13,7 +13,7 @@ describe RansackQuery do
13
13
  klass.should equal Grouping
14
14
  end
15
15
 
16
- it 'should return a hash' do
16
+ it 'should return a json string' do
17
17
  RansackQuery.build do |grouping|
18
18
  grouping.id = 'id1'
19
19
  grouping.add_condition do |condition|
@@ -21,18 +21,7 @@ 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'}}}}}}})
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\"}}}}}}}"
24
+ end.should eq("{\"g\":{\"id1\":{\"m\":\"and\",\"c\":{\"id2\":{\"a\":{\"0\":{\"name\":\"attribute\"}},\"p\":\"eq\",\"v\":{\"0\":{\"value\":\"value\"}}}}}}}" )
36
25
  end
37
26
 
38
27
  it 'should allow a prefix to be passed in' do
@@ -43,7 +32,7 @@ describe RansackQuery do
43
32
  condition.attribute = 'attribute'
44
33
  condition.value = 'value'
45
34
  end
46
- end.should eq({'q' => {'g' => {'id1' => {'m' => 'and', 'c' => {'id2' => {'a' => {'0' => {'name' => 'attribute'}}, 'p' => 'eq', 'v' => {'0' => {'value' => 'value'}}}}}}}})
35
+ end.should eq("{\"q\":{\"g\":{\"id1\":{\"m\":\"and\",\"c\":{\"id2\":{\"a\":{\"0\":{\"name\":\"attribute\"}},\"p\":\"eq\",\"v\":{\"0\":{\"value\":\"value\"}}}}}}}}")
47
36
  end
48
37
 
49
38
  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.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frank West