ransack_query 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -0
- data/README.md +3 -1
- data/lib/{condition.rb → ransack_condition.rb} +6 -2
- data/lib/{grouping.rb → ransack_grouping.rb} +11 -7
- data/lib/ransack_query/version.rb +1 -1
- data/lib/ransack_query.rb +3 -17
- data/spec/factories/{condition.rb → ransack_condition.rb} +2 -2
- data/spec/factories/{grouping.rb → ransack_grouping.rb} +6 -6
- data/spec/models/ransack_condition_spec.rb +50 -0
- data/spec/models/ransack_grouping_spec.rb +104 -0
- data/spec/models/ransack_query_spec.rb +12 -10
- metadata +14 -12
- data/spec/models/condition_spec.rb +0 -51
- data/spec/models/grouping_spec.rb +0 -104
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 553fe942438f924f048fcd4c1fa83366ac4fdb90
|
4
|
+
data.tar.gz: 42fecaf7185e415d54973863339ea75ab00f774f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9106e9af0080f40e4101f716181568f90a05361612a0d73125dfdfab69d192153a8fb36e2954c0e96ae0b97d666316687e89ee56b9a65ab292eb74cbb4e306e
|
7
|
+
data.tar.gz: 411acd4b6f5f3e10c9b0d2661ba2ce70fa3250e366445de7a4016851e8ecb8719d5c3ea7c3aa1d10a57a140e33bcd92269f07a010e0ea9c829f3cff19607cc6a
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Change Log
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
|
4
|
+
## 0.4.0 - 2014-09-01
|
5
|
+
### Added
|
6
|
+
- None
|
7
|
+
|
8
|
+
### Changed
|
9
|
+
- Changed Condition model to RansackCondition.
|
10
|
+
Changed Grouping model to RansackGrouping.
|
11
|
+
|
12
|
+
### Removed
|
13
|
+
- None
|
data/README.md
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
[![Code Climate](https://codeclimate.com/github/frank-west-iii/ransack_query.png)](https://codeclimate.com/github/frank-west-iii/ransack_query)
|
2
|
+
|
1
3
|
# RansackQuery
|
2
4
|
|
3
5
|
This gem provides a semantic way of building advanced ransack queries outside of using the form.
|
@@ -42,7 +44,7 @@ and with arrays
|
|
42
44
|
end
|
43
45
|
and with blocks
|
44
46
|
|
45
|
-
RansackQuery.build
|
47
|
+
RansackQuery.build do |grouping|
|
46
48
|
grouping.add_condition do |condition|
|
47
49
|
condition.attribute = 'first_name'
|
48
50
|
condition.value = 'Bob'
|
@@ -1,4 +1,4 @@
|
|
1
|
-
class
|
1
|
+
class RansackCondition
|
2
2
|
attr_accessor :id, :predicate, :attribute, :value
|
3
3
|
|
4
4
|
def initialize(attributes={})
|
@@ -19,6 +19,10 @@ class Condition
|
|
19
19
|
}
|
20
20
|
end
|
21
21
|
|
22
|
+
def to_condition
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
22
26
|
private
|
23
27
|
def build_ransack_array(array, hash_key)
|
24
28
|
counter = -1
|
@@ -32,4 +36,4 @@ class Condition
|
|
32
36
|
end
|
33
37
|
end
|
34
38
|
|
35
|
-
end
|
39
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
class
|
1
|
+
class RansackGrouping
|
2
2
|
attr_accessor :id, :combinator
|
3
3
|
attr_reader :conditions, :groupings
|
4
4
|
|
@@ -12,31 +12,31 @@ class Grouping
|
|
12
12
|
|
13
13
|
def add_condition(condition=nil)
|
14
14
|
if block_given?
|
15
|
-
condition =
|
15
|
+
condition = RansackCondition.new do |new_condition|
|
16
16
|
yield new_condition
|
17
17
|
end
|
18
18
|
end
|
19
|
-
conditions << condition
|
19
|
+
conditions << condition.to_condition
|
20
20
|
self
|
21
21
|
end
|
22
22
|
|
23
23
|
def add_grouping(grouping=nil)
|
24
24
|
if block_given?
|
25
|
-
grouping =
|
25
|
+
grouping = RansackGrouping.new do |new_grouping|
|
26
26
|
yield new_grouping
|
27
27
|
end
|
28
28
|
end
|
29
|
-
groupings << grouping
|
29
|
+
groupings << grouping.to_grouping
|
30
30
|
self
|
31
31
|
end
|
32
32
|
|
33
33
|
def add_conditions(conditions)
|
34
|
-
|
34
|
+
Array(conditions).each {|c| add_condition(c) }
|
35
35
|
self
|
36
36
|
end
|
37
37
|
|
38
38
|
def add_groupings(groupings)
|
39
|
-
|
39
|
+
Array(groupings).each {|g| add_grouping(g) }
|
40
40
|
self
|
41
41
|
end
|
42
42
|
|
@@ -51,6 +51,10 @@ class Grouping
|
|
51
51
|
ransack_hash
|
52
52
|
end
|
53
53
|
|
54
|
+
def to_grouping
|
55
|
+
self
|
56
|
+
end
|
57
|
+
|
54
58
|
private
|
55
59
|
|
56
60
|
def ransackify_conditions(ransack_hash)
|
data/lib/ransack_query.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'ransack_query/version'
|
2
|
-
require '
|
3
|
-
require '
|
2
|
+
require 'ransack_grouping'
|
3
|
+
require 'ransack_condition'
|
4
4
|
require 'json'
|
5
5
|
require 'securerandom'
|
6
6
|
|
7
7
|
module RansackQuery
|
8
8
|
|
9
9
|
def self.build(options = {})
|
10
|
-
grouping =
|
10
|
+
grouping = RansackGrouping.new do |new_grouping|
|
11
11
|
yield new_grouping
|
12
12
|
end
|
13
13
|
ransack_hash = {'g' => grouping.ransackify}
|
@@ -20,18 +20,4 @@ module RansackQuery
|
|
20
20
|
SecureRandom.hex
|
21
21
|
end
|
22
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
|
-
|
37
23
|
end
|
@@ -1,16 +1,16 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
FactoryGirl.define do
|
3
3
|
|
4
|
-
factory :
|
4
|
+
factory :ransack_grouping do
|
5
5
|
id 1
|
6
6
|
|
7
7
|
trait :complex do
|
8
8
|
combinator :or
|
9
9
|
after(:build) do |obj|
|
10
|
-
grouping = FactoryGirl.build(:
|
10
|
+
grouping = FactoryGirl.build(:ransack_grouping, :with_conditions)
|
11
11
|
grouping.id = 2
|
12
12
|
obj.add_grouping(grouping)
|
13
|
-
grouping = FactoryGirl.build(:
|
13
|
+
grouping = FactoryGirl.build(:ransack_grouping, :with_conditions)
|
14
14
|
grouping.id = 3
|
15
15
|
obj.add_grouping(grouping)
|
16
16
|
end
|
@@ -19,11 +19,11 @@ 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(:
|
23
|
-
obj.add_condition FactoryGirl.build(:
|
22
|
+
obj.add_condition FactoryGirl.build(:ransack_condition, :document_number)
|
23
|
+
obj.add_condition FactoryGirl.build(:ransack_condition, :driver_name)
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
27
|
end
|
28
28
|
|
29
|
-
end
|
29
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RansackCondition do
|
4
|
+
let(:condition) { FactoryGirl.build(:ransack_condition) }
|
5
|
+
let(:condition_hash) { FactoryGirl.build(:condition_hash) }
|
6
|
+
|
7
|
+
it 'should have an id' do
|
8
|
+
expect(subject.id).to_not be_nil
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should have a default predicate of eq' do
|
12
|
+
expect(subject.predicate).to eq :eq
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should accept a predicate' do
|
16
|
+
expect(lambda { subject.predicate = :eq }).to_not raise_error
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should accept an attribute' do
|
20
|
+
expect(lambda { subject.attribute = 'attribute' }).to_not raise_error
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should accept a value' do
|
24
|
+
expect(lambda { subject.value = 'value' }).to_not raise_error
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should accept setting the attribute via the initializer' do
|
28
|
+
expect(RansackCondition.new(attribute: 'new_attribute').attribute).to eq 'new_attribute'
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should accept setting the value via the initializer' do
|
32
|
+
expect(RansackCondition.new(value: 'new_value').value).to eq 'new_value'
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should accept setting the predicate via the initializer' do
|
36
|
+
expect(RansackCondition.new(predicate: 'new_predicate').predicate).to eq 'new_predicate'
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should allow initializing via a block' do
|
40
|
+
id = nil
|
41
|
+
new_condition = RansackCondition.new do |condition|
|
42
|
+
id = condition.id
|
43
|
+
end
|
44
|
+
expect(id).to eq new_condition.id
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should build a ransack hash' do
|
48
|
+
expect(condition.ransackify).to eq condition_hash
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RansackGrouping do
|
4
|
+
subject { FactoryGirl.build(:ransack_grouping) }
|
5
|
+
let(:complex) { FactoryGirl.build(:ransack_grouping, :complex) }
|
6
|
+
let(:with_conditions) { FactoryGirl.build(:grouping, :with_conditions) }
|
7
|
+
|
8
|
+
it 'should have an id' do
|
9
|
+
expect(subject.id).to_not be_nil
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should have a default combinator of and' do
|
13
|
+
expect(subject.combinator).to eq :and
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should return a accept setting combinator' do
|
17
|
+
expect(lambda { subject.combinator = :and }).to_not raise_error
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should allow you to add a condition' do
|
21
|
+
subject.add_condition(RansackCondition.new({attribute: 'location_code', value: '001', predicate: 'eq'}))
|
22
|
+
expect(subject.conditions.size).to eq 1
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should allow adding conditions through chaining' do
|
26
|
+
subject.add_condition(RansackCondition.new(attribute: 'location_code', value: '001', predicate: 'eq')).
|
27
|
+
add_condition(RansackCondition.new(attribute: 'location_code', value: '002', predicate: 'eq'))
|
28
|
+
expect(subject.conditions.size).to eq 2
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should allow adding groupings' do
|
32
|
+
subject.add_grouping(RansackGrouping.new)
|
33
|
+
expect(subject.groupings.size).to eq 1
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should allow adding groupings through chaining' do
|
37
|
+
subject.add_grouping(RansackGrouping.new).
|
38
|
+
add_grouping(RansackGrouping.new)
|
39
|
+
expect(subject.groupings.size).to eq 2
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should allow adding groupings without chaining' do
|
43
|
+
subject.add_grouping(RansackGrouping.new)
|
44
|
+
subject.add_grouping(RansackGrouping.new)
|
45
|
+
expect(subject.groupings.size).to eq 2
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should allow building a group via a block' do
|
49
|
+
g = RansackGrouping.new do |grouping|
|
50
|
+
grouping.combinator = :or
|
51
|
+
grouping.add_grouping do |new_grouping|
|
52
|
+
new_grouping.add_condition(RansackCondition.new(attribute: 'ItemNumber', value: 'RC')).
|
53
|
+
add_condition(RansackCondition.new(attribute: 'LocationCode', value: '002'))
|
54
|
+
end
|
55
|
+
grouping.add_grouping do |new_grouping|
|
56
|
+
new_grouping.add_condition(RansackCondition.new(attribute: 'ItemNumber', value: 'RC')).
|
57
|
+
add_condition(RansackCondition.new(attribute: 'LocationCode', value: '001'))
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
expect(g.groupings.size).to eq 2
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'should allow adding conditions en masse' do
|
65
|
+
subject.add_conditions(
|
66
|
+
[RansackCondition.new(attribute: 'location_code', value: '001', predicate: 'eq'),
|
67
|
+
RansackCondition.new(attribute: 'location_code', value: '001', predicate: 'eq')])
|
68
|
+
expect(subject.conditions.size).to eq 2
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should allow adding groupings en masse' do
|
72
|
+
subject.add_groupings([RansackGrouping.new, RansackGrouping.new])
|
73
|
+
expect(subject.groupings.size).to eq 2
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'should allow adding a condition via a block' do
|
77
|
+
klass = nil
|
78
|
+
subject.add_condition do |condition|
|
79
|
+
klass = condition.class
|
80
|
+
end
|
81
|
+
expect(klass).to eq RansackCondition
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'should allow adding a group via a block' do
|
85
|
+
klass = nil
|
86
|
+
subject.add_grouping do |grouping|
|
87
|
+
klass = grouping.class
|
88
|
+
end
|
89
|
+
expect(klass).to eq RansackGrouping
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'should allow initializing via a block' do
|
93
|
+
id = nil
|
94
|
+
grouping = RansackGrouping.new do |grp|
|
95
|
+
id = grp.id
|
96
|
+
end
|
97
|
+
expect(id).to eq grouping.id
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'should build a complex ransack hash' do
|
101
|
+
expect(complex.ransackify).to eq FactoryGirl.build(:grouping_hash)
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe RansackQuery do
|
4
4
|
it 'should not build without a block' do
|
5
|
-
lambda { RansackQuery.build }.
|
5
|
+
expect(lambda { RansackQuery.build }).to raise_error
|
6
6
|
end
|
7
7
|
|
8
8
|
it 'should return a grouping to build off' do
|
@@ -10,40 +10,42 @@ describe RansackQuery do
|
|
10
10
|
RansackQuery.build do |grouping|
|
11
11
|
klass = grouping.class
|
12
12
|
end
|
13
|
-
klass.
|
13
|
+
expect(klass).to eq RansackGrouping
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'should return a hash' do
|
17
|
-
RansackQuery.build do |grouping|
|
17
|
+
result = RansackQuery.build do |grouping|
|
18
18
|
grouping.id = 'id1'
|
19
19
|
grouping.add_condition do |condition|
|
20
20
|
condition.id = 'id2'
|
21
21
|
condition.attribute = 'attribute'
|
22
22
|
condition.value = 'value'
|
23
23
|
end
|
24
|
-
end
|
24
|
+
end
|
25
|
+
expect(result).to eq({'g' => {'id1' => {'m' => 'and', 'c' => {'id2' => {'a' => {'0' => {'name' => 'attribute'}}, 'p' => 'eq', 'v' => {'0' => {'value' => 'value'}}}}}}})
|
25
26
|
end
|
26
27
|
|
27
28
|
it 'should return a json string if specified' do
|
28
|
-
RansackQuery.build(format: :json) do |grouping|
|
29
|
+
result = RansackQuery.build(format: :json) do |grouping|
|
29
30
|
grouping.id = 'id1'
|
30
31
|
grouping.add_condition do |condition|
|
31
32
|
condition.id = 'id2'
|
32
33
|
condition.attribute = 'attribute'
|
33
34
|
condition.value = 'value'
|
34
35
|
end
|
35
|
-
end
|
36
|
+
end
|
37
|
+
expect(result).to eq "{\"g\":{\"id1\":{\"m\":\"and\",\"c\":{\"id2\":{\"a\":{\"0\":{\"name\":\"attribute\"}},\"p\":\"eq\",\"v\":{\"0\":{\"value\":\"value\"}}}}}}}"
|
36
38
|
end
|
37
39
|
|
38
40
|
it 'should allow a prefix to be passed in' do
|
39
|
-
RansackQuery.build(prefix: 'q') do |grouping|
|
41
|
+
result = RansackQuery.build(prefix: 'q') do |grouping|
|
40
42
|
grouping.id = 'id1'
|
41
43
|
grouping.add_condition do |condition|
|
42
44
|
condition.id = 'id2'
|
43
45
|
condition.attribute = 'attribute'
|
44
46
|
condition.value = 'value'
|
45
47
|
end
|
46
|
-
end
|
48
|
+
end
|
49
|
+
expect(result).to eq({'q' => {'g' => {'id1' => {'m' => 'and', 'c' => {'id2' => {'a' => {'0' => {'name' => 'attribute'}}, 'p' => 'eq', 'v' => {'0' => {'value' => 'value'}}}}}}}})
|
47
50
|
end
|
48
|
-
|
49
|
-
end
|
51
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ransack_query
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Frank West
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-09-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -160,21 +160,22 @@ extra_rdoc_files: []
|
|
160
160
|
files:
|
161
161
|
- ".gitignore"
|
162
162
|
- ".rspec"
|
163
|
+
- CHANGELOG.md
|
163
164
|
- Gemfile
|
164
165
|
- Guardfile
|
165
166
|
- LICENSE.txt
|
166
167
|
- README.md
|
167
168
|
- Rakefile
|
168
|
-
- lib/
|
169
|
-
- lib/
|
169
|
+
- lib/ransack_condition.rb
|
170
|
+
- lib/ransack_grouping.rb
|
170
171
|
- lib/ransack_query.rb
|
171
172
|
- lib/ransack_query/version.rb
|
172
173
|
- ransack_query.gemspec
|
173
|
-
- spec/factories/condition.rb
|
174
|
-
- spec/factories/grouping.rb
|
175
174
|
- spec/factories/hash.rb
|
176
|
-
- spec/
|
177
|
-
- spec/
|
175
|
+
- spec/factories/ransack_condition.rb
|
176
|
+
- spec/factories/ransack_grouping.rb
|
177
|
+
- spec/models/ransack_condition_spec.rb
|
178
|
+
- spec/models/ransack_grouping_spec.rb
|
178
179
|
- spec/models/ransack_query_spec.rb
|
179
180
|
- spec/spec_helper.rb
|
180
181
|
homepage: https://github.com/frank-west-iii/ransack_query
|
@@ -202,10 +203,11 @@ signing_key:
|
|
202
203
|
specification_version: 4
|
203
204
|
summary: Provides a way to build advanced ransack queries.
|
204
205
|
test_files:
|
205
|
-
- spec/factories/condition.rb
|
206
|
-
- spec/factories/grouping.rb
|
207
206
|
- spec/factories/hash.rb
|
208
|
-
- spec/
|
209
|
-
- spec/
|
207
|
+
- spec/factories/ransack_condition.rb
|
208
|
+
- spec/factories/ransack_grouping.rb
|
209
|
+
- spec/models/ransack_condition_spec.rb
|
210
|
+
- spec/models/ransack_grouping_spec.rb
|
210
211
|
- spec/models/ransack_query_spec.rb
|
211
212
|
- spec/spec_helper.rb
|
213
|
+
has_rdoc:
|
@@ -1,51 +0,0 @@
|
|
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
|
@@ -1,104 +0,0 @@
|
|
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 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
|
-
|
64
|
-
it 'should allow adding conditions en masse' do
|
65
|
-
subject.add_conditions(
|
66
|
-
[Condition.new(attribute: 'location_code', value: '001', predicate: 'eq'),
|
67
|
-
Condition.new(attribute: 'location_code', value: '001', predicate: 'eq')])
|
68
|
-
subject.conditions.size.should eq 2
|
69
|
-
end
|
70
|
-
|
71
|
-
it 'should allow adding groupings en masse' do
|
72
|
-
subject.add_groupings([Grouping.new, Grouping.new])
|
73
|
-
subject.groupings.size.should eq 2
|
74
|
-
end
|
75
|
-
|
76
|
-
it 'should allow adding a condition via a block' do
|
77
|
-
klass = nil
|
78
|
-
subject.add_condition do |condition|
|
79
|
-
klass = condition.class
|
80
|
-
end
|
81
|
-
klass.should eq Condition
|
82
|
-
end
|
83
|
-
|
84
|
-
it 'should allow adding a group via a block' do
|
85
|
-
klass = nil
|
86
|
-
subject.add_grouping do |grouping|
|
87
|
-
klass = grouping.class
|
88
|
-
end
|
89
|
-
klass.should eq Grouping
|
90
|
-
end
|
91
|
-
|
92
|
-
it 'should allow initializing via a block' do
|
93
|
-
id = nil
|
94
|
-
grouping = Grouping.new do |grouping|
|
95
|
-
id = grouping.id
|
96
|
-
end
|
97
|
-
id.should eq grouping.id
|
98
|
-
end
|
99
|
-
|
100
|
-
it 'should build a complex ransack hash' do
|
101
|
-
complex.ransackify.should eq FactoryGirl.build(:grouping_hash)
|
102
|
-
end
|
103
|
-
|
104
|
-
end
|