ransack_query 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/Guardfile +11 -0
- data/LICENSE.txt +22 -0
- data/README.md +236 -0
- data/Rakefile +10 -0
- data/lib/condition.rb +35 -0
- data/lib/grouping.rb +68 -0
- data/lib/ransack_query/version.rb +3 -0
- data/lib/ransack_query.rb +23 -0
- data/ransack_query.gemspec +31 -0
- data/spec/factories/condition.rb +22 -0
- data/spec/factories/grouping.rb +24 -0
- data/spec/factories/hash.rb +70 -0
- data/spec/models/condition_spec.rb +51 -0
- data/spec/models/grouping_spec.rb +82 -0
- data/spec/models/ransack_query_spec.rb +49 -0
- data/spec/spec_helper.rb +9 -0
- metadata +211 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 77ba48428f3a018d79c6ea475d82d3f8b31201c7
|
4
|
+
data.tar.gz: d372b67bebccd07ff7823d376ffb6d4c3dfb611b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1c9ec53846f5b04e2db3640932d9d853a19565ccab19be3bd68930e8194d46ac8c0454e3e923a51b974c7ab2fab0b22bdb818dc215d34878c42c1a9ebde08c94
|
7
|
+
data.tar.gz: 9761dfeb707d6d5930e84ffeaefe5c49bf9a5cf1db6c47fb2b5e08b43250f5fe25e4e8703366c2781d569acea41957bf119d3c8f3fab71e0e1ca1d5a1e4ffd6f
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Frank West
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,236 @@
|
|
1
|
+
# RansackQuery
|
2
|
+
|
3
|
+
This gem provides a semantic way of building advanced ransack queries outside of using the form.
|
4
|
+
|
5
|
+
This was built because we needed a quick way to build queries to pass to ransack using the console.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'ransack_query'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install ransack_query
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Simple Usage:
|
24
|
+
|
25
|
+
RansackQuery.build do |grouping|
|
26
|
+
grouping.add_condition(Condition.new(attribute: 'first_name', value: 'Bob'))
|
27
|
+
grouping.add_condition(Condition.new(attribute: 'last_name', value: 'Smith', predicate: :not_eq))
|
28
|
+
end
|
29
|
+
and
|
30
|
+
|
31
|
+
RansackQuery.build do |grouping|
|
32
|
+
grouping.add_condition(Condition.new(attribute: 'first_name', value: 'Bob')).
|
33
|
+
add_condition(Condition.new(attribute: 'last_name', value: 'Smith', predicate: :not_eq))
|
34
|
+
end
|
35
|
+
and
|
36
|
+
|
37
|
+
RansackQuery.build do |grouping|
|
38
|
+
conditions = []
|
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
|
45
|
+
grouping.add_conditions(conditions)
|
46
|
+
end
|
47
|
+
and
|
48
|
+
|
49
|
+
RansackQuery.build(prefix: 'q') do |grouping|
|
50
|
+
grouping.add_condition do |condition|
|
51
|
+
condition.attribute = 'first_name'
|
52
|
+
condition.value = 'Bob'
|
53
|
+
condition.predicate = :eq
|
54
|
+
end
|
55
|
+
grouping.add_condition do |condition|
|
56
|
+
condition.attribute = 'last_name'
|
57
|
+
condition.value = 'Smith'
|
58
|
+
condition.predicate = :not_eq
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
All Produce the following output (with different ids):
|
64
|
+
|
65
|
+
{
|
66
|
+
"g" => {
|
67
|
+
"90eb39b79709822408ca45f551532893" => {
|
68
|
+
"m" => "and",
|
69
|
+
"c" => {
|
70
|
+
"2bd58799fb5e609164d9a9fa674075e9" => {
|
71
|
+
"a" => {
|
72
|
+
"0" => {
|
73
|
+
"name" => "first_name"
|
74
|
+
}
|
75
|
+
},
|
76
|
+
"p" => "eq",
|
77
|
+
"v" => {
|
78
|
+
"0" => {
|
79
|
+
"value" => "Bob"
|
80
|
+
}
|
81
|
+
}
|
82
|
+
},
|
83
|
+
"7a6d5f805afbfeccf51be1f6eaaadecb" => {
|
84
|
+
"a" => {
|
85
|
+
"0" => {
|
86
|
+
"name" => "last_name"
|
87
|
+
}
|
88
|
+
},
|
89
|
+
"p" => "not_eq",
|
90
|
+
"v" => {
|
91
|
+
"0" => {
|
92
|
+
"value" => "Smith"
|
93
|
+
}
|
94
|
+
}
|
95
|
+
}
|
96
|
+
}
|
97
|
+
}
|
98
|
+
}
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
and passing the following options hash to build
|
103
|
+
|
104
|
+
RansackQuery.build(format: :json, prefix: 'q')
|
105
|
+
|
106
|
+
will produce the following in json
|
107
|
+
|
108
|
+
{
|
109
|
+
"q" => {
|
110
|
+
"g" => {
|
111
|
+
"8b489d7ae7573fca7bb7ee9762dfb254" => {
|
112
|
+
"m" => "and",
|
113
|
+
"c" => {
|
114
|
+
"9a8c44777d17ba6bea0f9257415fb1aa" => {
|
115
|
+
"a" => {
|
116
|
+
"0" => {
|
117
|
+
"name" => "first_name"
|
118
|
+
}
|
119
|
+
},
|
120
|
+
"p" => "eq",
|
121
|
+
"v" => {
|
122
|
+
"0" => {
|
123
|
+
"value" => "Bob"
|
124
|
+
}
|
125
|
+
}
|
126
|
+
},
|
127
|
+
"7d0cee690a16e90db20318070f0841f6" => {
|
128
|
+
"a" => {
|
129
|
+
"0" => {
|
130
|
+
"name" => "last_name"
|
131
|
+
}
|
132
|
+
},
|
133
|
+
"p" => "not_eq",
|
134
|
+
"v" => {
|
135
|
+
"0" => {
|
136
|
+
"value" => "Smith"
|
137
|
+
}
|
138
|
+
}
|
139
|
+
}
|
140
|
+
}
|
141
|
+
}
|
142
|
+
}
|
143
|
+
}
|
144
|
+
}
|
145
|
+
|
146
|
+
A more complex example with arrays and subgroups
|
147
|
+
|
148
|
+
RansackQuery.build do |grouping|
|
149
|
+
grouping.combinator = :or
|
150
|
+
grouping.add_condition do |condition|
|
151
|
+
condition.attribute = 'first_name'
|
152
|
+
condition.value = 'Bob'
|
153
|
+
end
|
154
|
+
|
155
|
+
grouping.add_grouping do |new_grouping|
|
156
|
+
new_grouping.add_condition do |condition|
|
157
|
+
condition.attribute = 'last_name'
|
158
|
+
condition.value = 'Smith'
|
159
|
+
end
|
160
|
+
new_grouping.add_condition do |condition|
|
161
|
+
condition.attribute = 'state'
|
162
|
+
condition.value = %w(CA TX NY)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
Will produce:
|
167
|
+
|
168
|
+
{
|
169
|
+
"g" => {
|
170
|
+
"05fdf6e8a730331872050faf8856501e" => {
|
171
|
+
"m" => "or",
|
172
|
+
"c" => {
|
173
|
+
"bbd1916d8afe8c6def686041ad5ab167" => {
|
174
|
+
"a" => {
|
175
|
+
"0" => {
|
176
|
+
"name" => "first_name"
|
177
|
+
}
|
178
|
+
},
|
179
|
+
"p" => "eq",
|
180
|
+
"v" => {
|
181
|
+
"0" => {
|
182
|
+
"value" => "Bob"
|
183
|
+
}
|
184
|
+
}
|
185
|
+
}
|
186
|
+
},
|
187
|
+
"g" => {
|
188
|
+
"4e3e3095f1caf8f3479b49f394fe42d6" => {
|
189
|
+
"m" => "and",
|
190
|
+
"c" => {
|
191
|
+
"023e5b5ff8384f474372c3f3ee8e666d" => {
|
192
|
+
"a" => {
|
193
|
+
"0" => {
|
194
|
+
"name" => "last_name"
|
195
|
+
}
|
196
|
+
},
|
197
|
+
"p" => "eq",
|
198
|
+
"v" => {
|
199
|
+
"0" => {
|
200
|
+
"value" => "Smith"
|
201
|
+
}
|
202
|
+
}
|
203
|
+
},
|
204
|
+
"df4731b72af62b2788c4428a59aef4d3" => {
|
205
|
+
"a" => {
|
206
|
+
"0" => {
|
207
|
+
"name" => "state"
|
208
|
+
}
|
209
|
+
},
|
210
|
+
"p" => "eq",
|
211
|
+
"v" => {
|
212
|
+
"0" => {
|
213
|
+
"value" => "CA"
|
214
|
+
},
|
215
|
+
"1" => {
|
216
|
+
"value" => "TX"
|
217
|
+
},
|
218
|
+
"2" => {
|
219
|
+
"value" => "NY"
|
220
|
+
}
|
221
|
+
}
|
222
|
+
}
|
223
|
+
}
|
224
|
+
}
|
225
|
+
}
|
226
|
+
}
|
227
|
+
}
|
228
|
+
}
|
229
|
+
|
230
|
+
## Contributing
|
231
|
+
|
232
|
+
1. Fork it ( https://github.com/[my-github-username]/ransack_query/fork )
|
233
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
234
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
235
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
236
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/condition.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
class Condition
|
2
|
+
attr_accessor :id, :predicate, :attribute, :value
|
3
|
+
|
4
|
+
def initialize(attributes={})
|
5
|
+
@id = RansackQuery.generate_id
|
6
|
+
@predicate = attributes[:predicate] || :eq
|
7
|
+
@attribute = attributes[:attribute]
|
8
|
+
@value = attributes[:value]
|
9
|
+
yield self if block_given?
|
10
|
+
end
|
11
|
+
|
12
|
+
def ransackify
|
13
|
+
{
|
14
|
+
@id => {
|
15
|
+
'a' => build_ransack_array(@attribute, 'name'),
|
16
|
+
'p' => @predicate.to_s,
|
17
|
+
'v' => build_ransack_array(@value, 'value')
|
18
|
+
}
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def build_ransack_array(array, hash_key)
|
24
|
+
counter = -1
|
25
|
+
[array].flatten.reduce({}) do |result, attribute|
|
26
|
+
counter += 1
|
27
|
+
result.merge({
|
28
|
+
counter.to_s => {
|
29
|
+
hash_key => attribute
|
30
|
+
}
|
31
|
+
})
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
data/lib/grouping.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
class Grouping
|
2
|
+
attr_accessor :id, :combinator
|
3
|
+
attr_reader :conditions, :groupings
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@id = RansackQuery.generate_id
|
7
|
+
@combinator = :and
|
8
|
+
@conditions = []
|
9
|
+
@groupings = []
|
10
|
+
yield self if block_given?
|
11
|
+
end
|
12
|
+
|
13
|
+
def add_condition(condition=nil)
|
14
|
+
if block_given?
|
15
|
+
condition = Condition.new do |new_condition|
|
16
|
+
yield new_condition
|
17
|
+
end
|
18
|
+
end
|
19
|
+
conditions << condition
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
def add_grouping(grouping=nil)
|
24
|
+
if block_given?
|
25
|
+
grouping = Grouping.new do |new_grouping|
|
26
|
+
yield new_grouping
|
27
|
+
end
|
28
|
+
end
|
29
|
+
groupings << grouping
|
30
|
+
self
|
31
|
+
end
|
32
|
+
|
33
|
+
def add_conditions(conditions)
|
34
|
+
@conditions += conditions
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
38
|
+
def add_groupings(groupings)
|
39
|
+
@groupings += groupings
|
40
|
+
self
|
41
|
+
end
|
42
|
+
|
43
|
+
def ransackify
|
44
|
+
ransack_hash = {
|
45
|
+
'g' => {
|
46
|
+
@id => {
|
47
|
+
'm' => @combinator.to_s
|
48
|
+
}
|
49
|
+
}
|
50
|
+
}
|
51
|
+
ransackify_conditions(ransack_hash)
|
52
|
+
ransackify_groupings(ransack_hash)
|
53
|
+
ransack_hash
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def ransackify_conditions(ransack_hash)
|
59
|
+
return if conditions.empty?
|
60
|
+
ransack_hash['g'][@id].merge!({'c' => conditions.reduce({}) {|result, condition| result.merge! condition.ransackify}})
|
61
|
+
end
|
62
|
+
|
63
|
+
def ransackify_groupings(ransack_hash)
|
64
|
+
return if groupings.empty?
|
65
|
+
groupings.each {|grouping| ransack_hash['g'][@id].merge!(grouping.ransackify)}
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'ransack_query/version'
|
2
|
+
require 'grouping'
|
3
|
+
require 'condition'
|
4
|
+
require 'json'
|
5
|
+
require 'securerandom'
|
6
|
+
|
7
|
+
module RansackQuery
|
8
|
+
|
9
|
+
def self.build(options = {})
|
10
|
+
grouping = Grouping.new do |new_grouping|
|
11
|
+
yield new_grouping
|
12
|
+
end
|
13
|
+
ransack_hash = grouping.ransackify
|
14
|
+
ransack_hash = {options[:prefix] => ransack_hash} if options[:prefix]
|
15
|
+
ransack_hash = ransack_hash.to_json if options[:format] == :json
|
16
|
+
ransack_hash
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.generate_id
|
20
|
+
SecureRandom.hex
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ransack_query/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'ransack_query'
|
8
|
+
spec.version = RansackQuery::VERSION
|
9
|
+
spec.authors = ['Frank West']
|
10
|
+
spec.email = ['fwest@heiskell.com']
|
11
|
+
spec.summary = %q{Provides a way to build advanced ransack queries.}
|
12
|
+
spec.description = %q{Provides a way to build ransack queries without building a form and usable to generate testing queries. }
|
13
|
+
spec.homepage = 'https://github.com/frank-west-iii/ransack_query'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
spec.add_development_dependency 'rspec-nc'
|
25
|
+
spec.add_development_dependency 'guard'
|
26
|
+
spec.add_development_dependency 'guard-rspec'
|
27
|
+
spec.add_development_dependency 'pry'
|
28
|
+
spec.add_development_dependency 'pry-remote'
|
29
|
+
spec.add_development_dependency 'pry-nav'
|
30
|
+
spec.add_development_dependency 'factory_girl', '~> 4.4.0'
|
31
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
FactoryGirl.define do
|
3
|
+
|
4
|
+
factory :condition do
|
5
|
+
id 1
|
6
|
+
attribute 'attribute'
|
7
|
+
value 'value'
|
8
|
+
|
9
|
+
trait :first_name do
|
10
|
+
id 2
|
11
|
+
attribute 'first_name'
|
12
|
+
value 'Bob'
|
13
|
+
end
|
14
|
+
|
15
|
+
trait :last_name do
|
16
|
+
id 3
|
17
|
+
attribute 'last_name'
|
18
|
+
value 'Smith'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
FactoryGirl.define do
|
3
|
+
|
4
|
+
factory :grouping do
|
5
|
+
id 1
|
6
|
+
|
7
|
+
trait :complex do
|
8
|
+
combinator :or
|
9
|
+
after(:build) do |obj|
|
10
|
+
obj.add_grouping(FactoryGirl.build(:grouping, :with_conditions))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
trait :with_conditions do
|
15
|
+
id 2
|
16
|
+
after(:build) do |obj|
|
17
|
+
obj.add_condition FactoryGirl.build(:condition, :first_name)
|
18
|
+
obj.add_condition FactoryGirl.build(:condition, :last_name)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
FactoryGirl.define do
|
4
|
+
|
5
|
+
factory :condition_hash, class: Hash do
|
6
|
+
skip_create
|
7
|
+
initialize_with {
|
8
|
+
{
|
9
|
+
1 => {
|
10
|
+
'a' => {
|
11
|
+
'0' => {
|
12
|
+
'name' => 'attribute'
|
13
|
+
}
|
14
|
+
},
|
15
|
+
'p' => 'eq',
|
16
|
+
'v' => {
|
17
|
+
'0' => {
|
18
|
+
'value' => 'value'
|
19
|
+
}
|
20
|
+
}
|
21
|
+
}
|
22
|
+
} }
|
23
|
+
end
|
24
|
+
|
25
|
+
factory :grouping_hash, class: Hash do
|
26
|
+
skip_create
|
27
|
+
initialize_with {
|
28
|
+
{
|
29
|
+
'g' => {
|
30
|
+
1 => {
|
31
|
+
'm' => 'or',
|
32
|
+
'g' => {
|
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
|
+
}
|
47
|
+
}
|
48
|
+
},
|
49
|
+
3 => {
|
50
|
+
'a' => {
|
51
|
+
'0' => {
|
52
|
+
'name' => 'last_name'
|
53
|
+
}
|
54
|
+
},
|
55
|
+
'p' => 'eq',
|
56
|
+
'v' => {
|
57
|
+
'0' => {
|
58
|
+
'value' => 'Smith'
|
59
|
+
}
|
60
|
+
}
|
61
|
+
}
|
62
|
+
}
|
63
|
+
}
|
64
|
+
}
|
65
|
+
}
|
66
|
+
}
|
67
|
+
} }
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Condition do
|
4
|
+
let(:condition) { FactoryGirl.build(:condition) }
|
5
|
+
let(:condition_hash) { FactoryGirl.build(:condition_hash) }
|
6
|
+
|
7
|
+
it 'should have an id' do
|
8
|
+
subject.id.should_not be nil
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should have a default predicate of eq' do
|
12
|
+
subject.predicate.should eq :eq
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should accept a predicate' do
|
16
|
+
lambda { subject.predicate = :eq }.should_not raise_error
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should accept an attribute' do
|
20
|
+
lambda { subject.attribute = 'attribute' }.should_not raise_error
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should accept a value' do
|
24
|
+
lambda { subject.value = 'value' }.should_not raise_error
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should accept setting the attribute via the initializer' do
|
28
|
+
Condition.new(attribute: 'new_attribute').attribute.should eq 'new_attribute'
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should accept setting the value via the initializer' do
|
32
|
+
Condition.new(value: 'new_value').value.should eq 'new_value'
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should accept setting the predicate via the initializer' do
|
36
|
+
Condition.new(predicate: 'new_predicate').predicate.should eq 'new_predicate'
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should allow initializing via a block' do
|
40
|
+
id = nil
|
41
|
+
new_condition = Condition.new do |condition|
|
42
|
+
id = condition.id
|
43
|
+
end
|
44
|
+
id.should eq new_condition.id
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should build a ransack hash' do
|
48
|
+
condition.ransackify.should eq condition_hash
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Grouping do
|
4
|
+
subject { FactoryGirl.build(:grouping) }
|
5
|
+
let(:complex) { FactoryGirl.build(:grouping, :complex) }
|
6
|
+
let(:with_conditions) { FactoryGirl.build(:grouping, :with_conditions) }
|
7
|
+
|
8
|
+
it 'should have an id' do
|
9
|
+
subject.id.should_not be nil
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should have a default combinator of and' do
|
13
|
+
subject.combinator.should eq :and
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should return a accept setting combinator' do
|
17
|
+
lambda { subject.combinator = :and }.should_not raise_error
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should allow you to add a condition' do
|
21
|
+
subject.add_condition({attribute: 'location_code', value: '001', predicate: 'eq'})
|
22
|
+
subject.conditions.size.should eq 1
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should allow adding conditions through chaining' do
|
26
|
+
subject.add_condition(Condition.new(attribute: 'location_code', value: '001', predicate: 'eq')).
|
27
|
+
add_condition(Condition.new(attribute: 'location_code', value: '002', predicate: 'eq'))
|
28
|
+
subject.conditions.size.should eq 2
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should allow adding groupings' do
|
32
|
+
subject.add_grouping(Grouping.new)
|
33
|
+
subject.groupings.size.should eq 1
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should allow adding groupings through chaining' do
|
37
|
+
subject.add_grouping(Grouping.new).
|
38
|
+
add_grouping(Grouping.new)
|
39
|
+
subject.groupings.size.should eq 2
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should allow adding conditions en masse' do
|
43
|
+
subject.add_conditions(
|
44
|
+
[Condition.new(attribute: 'location_code', value: '001', predicate: 'eq'),
|
45
|
+
Condition.new(attribute: 'location_code', value: '001', predicate: 'eq')])
|
46
|
+
subject.conditions.size.should eq 2
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should allow adding groupings en masse' do
|
50
|
+
subject.add_groupings([Grouping.new, Grouping.new])
|
51
|
+
subject.groupings.size.should eq 2
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should allow adding a condition via a block' do
|
55
|
+
klass = nil
|
56
|
+
subject.add_condition do |condition|
|
57
|
+
klass = condition.class
|
58
|
+
end
|
59
|
+
klass.should eq Condition
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should allow adding a group via a block' do
|
63
|
+
klass = nil
|
64
|
+
subject.add_grouping do |grouping|
|
65
|
+
klass = grouping.class
|
66
|
+
end
|
67
|
+
klass.should eq Grouping
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should allow initializing via a block' do
|
71
|
+
id = nil
|
72
|
+
grouping = Grouping.new do |grouping|
|
73
|
+
id = grouping.id
|
74
|
+
end
|
75
|
+
id.should eq grouping.id
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should build a ransack hash' do
|
79
|
+
complex.ransackify.should eq FactoryGirl.build(:grouping_hash)
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RansackQuery do
|
4
|
+
it 'should not build without a block' do
|
5
|
+
lambda { RansackQuery.build }.should raise_error
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should return a grouping to build off' do
|
9
|
+
klass = nil
|
10
|
+
RansackQuery.build do |grouping|
|
11
|
+
klass = grouping.class
|
12
|
+
end
|
13
|
+
klass.should equal Grouping
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should return a hash' do
|
17
|
+
RansackQuery.build do |grouping|
|
18
|
+
grouping.id = 'id1'
|
19
|
+
grouping.add_condition do |condition|
|
20
|
+
condition.id = 'id2'
|
21
|
+
condition.attribute = 'attribute'
|
22
|
+
condition.value = 'value'
|
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\"}}}}}}}"
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should allow a prefix to be passed in' do
|
39
|
+
RansackQuery.build(prefix: 'q') do |grouping|
|
40
|
+
grouping.id = 'id1'
|
41
|
+
grouping.add_condition do |condition|
|
42
|
+
condition.id = 'id2'
|
43
|
+
condition.attribute = 'attribute'
|
44
|
+
condition.value = 'value'
|
45
|
+
end
|
46
|
+
end.should eq({'q' => {'g' => {'id1' => {'m' => 'and', 'c' => {'id2' => {'a' => {'0' => {'name' => 'attribute'}}, 'p' => 'eq', 'v' => {'0' => {'value' => 'value'}}}}}}}})
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,211 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ransack_query
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Frank West
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-nc
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: guard
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: guard-rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: pry-remote
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: pry-nav
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: factory_girl
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: 4.4.0
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: 4.4.0
|
153
|
+
description: 'Provides a way to build ransack queries without building a form and
|
154
|
+
usable to generate testing queries. '
|
155
|
+
email:
|
156
|
+
- fwest@heiskell.com
|
157
|
+
executables: []
|
158
|
+
extensions: []
|
159
|
+
extra_rdoc_files: []
|
160
|
+
files:
|
161
|
+
- ".gitignore"
|
162
|
+
- ".rspec"
|
163
|
+
- Gemfile
|
164
|
+
- Guardfile
|
165
|
+
- LICENSE.txt
|
166
|
+
- README.md
|
167
|
+
- Rakefile
|
168
|
+
- lib/condition.rb
|
169
|
+
- lib/grouping.rb
|
170
|
+
- lib/ransack_query.rb
|
171
|
+
- lib/ransack_query/version.rb
|
172
|
+
- ransack_query.gemspec
|
173
|
+
- spec/factories/condition.rb
|
174
|
+
- spec/factories/grouping.rb
|
175
|
+
- spec/factories/hash.rb
|
176
|
+
- spec/models/condition_spec.rb
|
177
|
+
- spec/models/grouping_spec.rb
|
178
|
+
- spec/models/ransack_query_spec.rb
|
179
|
+
- spec/spec_helper.rb
|
180
|
+
homepage: https://github.com/frank-west-iii/ransack_query
|
181
|
+
licenses:
|
182
|
+
- MIT
|
183
|
+
metadata: {}
|
184
|
+
post_install_message:
|
185
|
+
rdoc_options: []
|
186
|
+
require_paths:
|
187
|
+
- lib
|
188
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
189
|
+
requirements:
|
190
|
+
- - ">="
|
191
|
+
- !ruby/object:Gem::Version
|
192
|
+
version: '0'
|
193
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
194
|
+
requirements:
|
195
|
+
- - ">="
|
196
|
+
- !ruby/object:Gem::Version
|
197
|
+
version: '0'
|
198
|
+
requirements: []
|
199
|
+
rubyforge_project:
|
200
|
+
rubygems_version: 2.2.2
|
201
|
+
signing_key:
|
202
|
+
specification_version: 4
|
203
|
+
summary: Provides a way to build advanced ransack queries.
|
204
|
+
test_files:
|
205
|
+
- spec/factories/condition.rb
|
206
|
+
- spec/factories/grouping.rb
|
207
|
+
- spec/factories/hash.rb
|
208
|
+
- spec/models/condition_spec.rb
|
209
|
+
- spec/models/grouping_spec.rb
|
210
|
+
- spec/models/ransack_query_spec.rb
|
211
|
+
- spec/spec_helper.rb
|