filter_factory 0.0.3 → 0.1.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 +7 -0
- data/.gitignore +2 -2
- data/.rspec +1 -0
- data/.travis.yml +7 -0
- data/Gemfile +3 -4
- data/Gemfile.lock +141 -0
- data/README.md +2 -0
- data/lib/filter_factory/field.rb +1 -1
- data/lib/filter_factory/filter.rb +18 -16
- data/lib/filter_factory/mongoid/condition.rb +17 -11
- data/lib/filter_factory/mongoid/filter.rb +3 -3
- data/lib/filter_factory/version.rb +1 -1
- data/lib/filter_factory.rb +11 -11
- data/spec/factories/factories.rb +1 -3
- data/spec/filter_factory/active_record/model_spec.rb +70 -84
- data/spec/filter_factory/field_spec.rb +31 -33
- data/spec/filter_factory/filter_spec.rb +88 -88
- data/spec/filter_factory/mongoid/model_spec.rb +69 -83
- data/spec/models/ar_post.rb +1 -1
- data/spec/spec_helper.rb +18 -26
- metadata +11 -10
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a20357253719ea11df36bea4aa7d70143b595c69
|
4
|
+
data.tar.gz: f9ba46a6e9d1e41e8690dde28861fa5ff00c5d78
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 053d07b99b084c136f9982e7b65f30fdb922759f6e669e1e0def7a31e45684e58961322cd160edce9bf08fbc87063ef940db8805cee1730a5f7a382b013644be
|
7
|
+
data.tar.gz: fc547558634b40fe8a0859d5ffb08d9948d3c04b238b36ad2e333f5c8dd61a2d14a45aaa2bf5b0f84d613b09f2f8ae7b747b67659a8a82474bc23d844790db82
|
data/.gitignore
CHANGED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/.travis.yml
ADDED
data/Gemfile
CHANGED
@@ -3,15 +3,14 @@ source 'https://rubygems.org'
|
|
3
3
|
# Specify your gem's dependencies in filter_factory.gemspec
|
4
4
|
gemspec
|
5
5
|
|
6
|
-
gem 'rails', '>=
|
6
|
+
gem 'rails', '>= 4.0'
|
7
7
|
|
8
8
|
group :development do
|
9
|
-
gem 'mongoid', '>=
|
9
|
+
gem 'mongoid', '>= 5.0'
|
10
10
|
gem 'mysql2'
|
11
11
|
end
|
12
12
|
|
13
13
|
group :test do
|
14
14
|
gem 'rspec'
|
15
|
-
gem 'database_cleaner'
|
16
15
|
gem 'factory_girl'
|
17
|
-
end
|
16
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
filter_factory (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
actionmailer (4.2.6)
|
10
|
+
actionpack (= 4.2.6)
|
11
|
+
actionview (= 4.2.6)
|
12
|
+
activejob (= 4.2.6)
|
13
|
+
mail (~> 2.5, >= 2.5.4)
|
14
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
15
|
+
actionpack (4.2.6)
|
16
|
+
actionview (= 4.2.6)
|
17
|
+
activesupport (= 4.2.6)
|
18
|
+
rack (~> 1.6)
|
19
|
+
rack-test (~> 0.6.2)
|
20
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
21
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
22
|
+
actionview (4.2.6)
|
23
|
+
activesupport (= 4.2.6)
|
24
|
+
builder (~> 3.1)
|
25
|
+
erubis (~> 2.7.0)
|
26
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
27
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
28
|
+
activejob (4.2.6)
|
29
|
+
activesupport (= 4.2.6)
|
30
|
+
globalid (>= 0.3.0)
|
31
|
+
activemodel (4.2.6)
|
32
|
+
activesupport (= 4.2.6)
|
33
|
+
builder (~> 3.1)
|
34
|
+
activerecord (4.2.6)
|
35
|
+
activemodel (= 4.2.6)
|
36
|
+
activesupport (= 4.2.6)
|
37
|
+
arel (~> 6.0)
|
38
|
+
activesupport (4.2.6)
|
39
|
+
i18n (~> 0.7)
|
40
|
+
json (~> 1.7, >= 1.7.7)
|
41
|
+
minitest (~> 5.1)
|
42
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
43
|
+
tzinfo (~> 1.1)
|
44
|
+
arel (6.0.3)
|
45
|
+
bson (4.0.4)
|
46
|
+
builder (3.2.2)
|
47
|
+
concurrent-ruby (1.0.1)
|
48
|
+
diff-lcs (1.2.5)
|
49
|
+
erubis (2.7.0)
|
50
|
+
factory_girl (4.7.0)
|
51
|
+
activesupport (>= 3.0.0)
|
52
|
+
globalid (0.3.6)
|
53
|
+
activesupport (>= 4.1.0)
|
54
|
+
i18n (0.7.0)
|
55
|
+
json (1.8.3)
|
56
|
+
loofah (2.0.3)
|
57
|
+
nokogiri (>= 1.5.9)
|
58
|
+
mail (2.6.4)
|
59
|
+
mime-types (>= 1.16, < 4)
|
60
|
+
mime-types (3.0)
|
61
|
+
mime-types-data (~> 3.2015)
|
62
|
+
mime-types-data (3.2016.0221)
|
63
|
+
mini_portile2 (2.0.0)
|
64
|
+
minitest (5.8.4)
|
65
|
+
mongo (2.2.4)
|
66
|
+
bson (~> 4.0)
|
67
|
+
mongoid (5.1.2)
|
68
|
+
activemodel (~> 4.0)
|
69
|
+
mongo (~> 2.1)
|
70
|
+
origin (~> 2.2)
|
71
|
+
tzinfo (>= 0.3.37)
|
72
|
+
mysql2 (0.4.3)
|
73
|
+
nokogiri (1.6.7.2)
|
74
|
+
mini_portile2 (~> 2.0.0.rc2)
|
75
|
+
origin (2.2.0)
|
76
|
+
rack (1.6.4)
|
77
|
+
rack-test (0.6.3)
|
78
|
+
rack (>= 1.0)
|
79
|
+
rails (4.2.6)
|
80
|
+
actionmailer (= 4.2.6)
|
81
|
+
actionpack (= 4.2.6)
|
82
|
+
actionview (= 4.2.6)
|
83
|
+
activejob (= 4.2.6)
|
84
|
+
activemodel (= 4.2.6)
|
85
|
+
activerecord (= 4.2.6)
|
86
|
+
activesupport (= 4.2.6)
|
87
|
+
bundler (>= 1.3.0, < 2.0)
|
88
|
+
railties (= 4.2.6)
|
89
|
+
sprockets-rails
|
90
|
+
rails-deprecated_sanitizer (1.0.3)
|
91
|
+
activesupport (>= 4.2.0.alpha)
|
92
|
+
rails-dom-testing (1.0.7)
|
93
|
+
activesupport (>= 4.2.0.beta, < 5.0)
|
94
|
+
nokogiri (~> 1.6.0)
|
95
|
+
rails-deprecated_sanitizer (>= 1.0.1)
|
96
|
+
rails-html-sanitizer (1.0.3)
|
97
|
+
loofah (~> 2.0)
|
98
|
+
railties (4.2.6)
|
99
|
+
actionpack (= 4.2.6)
|
100
|
+
activesupport (= 4.2.6)
|
101
|
+
rake (>= 0.8.7)
|
102
|
+
thor (>= 0.18.1, < 2.0)
|
103
|
+
rake (11.1.2)
|
104
|
+
rspec (3.4.0)
|
105
|
+
rspec-core (~> 3.4.0)
|
106
|
+
rspec-expectations (~> 3.4.0)
|
107
|
+
rspec-mocks (~> 3.4.0)
|
108
|
+
rspec-core (3.4.4)
|
109
|
+
rspec-support (~> 3.4.0)
|
110
|
+
rspec-expectations (3.4.0)
|
111
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
112
|
+
rspec-support (~> 3.4.0)
|
113
|
+
rspec-mocks (3.4.1)
|
114
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
115
|
+
rspec-support (~> 3.4.0)
|
116
|
+
rspec-support (3.4.1)
|
117
|
+
sprockets (3.6.0)
|
118
|
+
concurrent-ruby (~> 1.0)
|
119
|
+
rack (> 1, < 3)
|
120
|
+
sprockets-rails (3.0.4)
|
121
|
+
actionpack (>= 4.0)
|
122
|
+
activesupport (>= 4.0)
|
123
|
+
sprockets (>= 3.0.0)
|
124
|
+
thor (0.19.1)
|
125
|
+
thread_safe (0.3.5)
|
126
|
+
tzinfo (1.2.2)
|
127
|
+
thread_safe (~> 0.1)
|
128
|
+
|
129
|
+
PLATFORMS
|
130
|
+
ruby
|
131
|
+
|
132
|
+
DEPENDENCIES
|
133
|
+
factory_girl
|
134
|
+
filter_factory!
|
135
|
+
mongoid (>= 5.0)
|
136
|
+
mysql2
|
137
|
+
rails (>= 4.0)
|
138
|
+
rspec
|
139
|
+
|
140
|
+
BUNDLED WITH
|
141
|
+
1.11.2
|
data/README.md
CHANGED
data/lib/filter_factory/field.rb
CHANGED
@@ -7,7 +7,7 @@ module FilterFactory
|
|
7
7
|
raise ArgumentError unless FilterFactory::Filter::CONDITIONS.include?(condition)
|
8
8
|
|
9
9
|
valid_options = [:alias]
|
10
|
-
@name, @condition, @options = name, condition, options.reject{|k
|
10
|
+
@name, @condition, @options = name, condition, options.reject{|k,| !valid_options.include?(k)}
|
11
11
|
@alias = @options[:alias] || @name
|
12
12
|
end
|
13
13
|
|
@@ -7,14 +7,14 @@ module FilterFactory
|
|
7
7
|
|
8
8
|
attr_reader :fields
|
9
9
|
|
10
|
-
CONDITIONS = [:eq, :ne, :lt, :lte, :gt, :gte, :all, :in, :nin, :regex, :exists, :presents]
|
10
|
+
CONDITIONS = [:eq, :ne, :lt, :lte, :gt, :gte, :all, :in, :nin, :regex, :exists, :presents].freeze
|
11
11
|
|
12
12
|
def initialize
|
13
13
|
@fields = []
|
14
14
|
end
|
15
15
|
|
16
16
|
def attributes
|
17
|
-
@fields.inject(HashWithIndifferentAccess.new) do |acc,field|
|
17
|
+
@fields.inject(HashWithIndifferentAccess.new) do |acc, field|
|
18
18
|
acc[field.alias] = field.value
|
19
19
|
acc
|
20
20
|
end
|
@@ -28,11 +28,11 @@ module FilterFactory
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def filled_fields
|
31
|
-
fields.select{|f| !f.value.nil? && f.value != ''}
|
31
|
+
fields.select { |f| !f.value.nil? && f.value != '' }
|
32
32
|
end
|
33
33
|
|
34
34
|
def get_field(name)
|
35
|
-
fields.find{|f| f.name == name}
|
35
|
+
fields.find { |f| f.name == name }
|
36
36
|
end
|
37
37
|
|
38
38
|
def persisted?
|
@@ -40,29 +40,31 @@ module FilterFactory
|
|
40
40
|
end
|
41
41
|
|
42
42
|
CONDITIONS.each do |condition|
|
43
|
-
define_method condition do |name,options={}|
|
43
|
+
define_method condition do |name, options={}|
|
44
44
|
field(name, condition, options)
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
+
class << self
|
49
|
+
def create(&block)
|
50
|
+
new.tap { |filter| filter.instance_eval &block }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class DuplicateFieldError < StandardError;
|
55
|
+
end
|
56
|
+
|
48
57
|
private
|
58
|
+
|
49
59
|
def field(name, condition, options={})
|
50
60
|
Field.new(name, condition, options).tap do |field|
|
51
61
|
raise DuplicateFieldError if fields.include?(field)
|
52
62
|
|
53
|
-
define_singleton_method(field.alias){ field.value }
|
54
|
-
define_singleton_method("#{field.alias}="){|val| field.value = val }
|
63
|
+
define_singleton_method(field.alias) { field.value }
|
64
|
+
define_singleton_method("#{field.alias}=") { |val| field.value = val }
|
55
65
|
|
56
66
|
@fields << field
|
57
67
|
end
|
58
68
|
end
|
59
|
-
|
60
|
-
class << self
|
61
|
-
def create &block
|
62
|
-
new.tap{|filter| filter.instance_eval &block}
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
class DuplicateFieldError < StandardError; end
|
67
69
|
end
|
68
|
-
end
|
70
|
+
end
|
@@ -6,35 +6,35 @@ module FilterFactory
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def ne(obj)
|
9
|
-
obj.where(field_name => {'$ne' => value})
|
9
|
+
obj.where(field_name => { '$ne' => value })
|
10
10
|
end
|
11
11
|
|
12
12
|
def lt(obj)
|
13
|
-
obj.where(field_name => {'$lt' => value})
|
13
|
+
obj.where(field_name => { '$lt' => value })
|
14
14
|
end
|
15
15
|
|
16
16
|
def lte(obj)
|
17
|
-
obj.where(field_name => {'$lte' => value})
|
17
|
+
obj.where(field_name => { '$lte' => value })
|
18
18
|
end
|
19
19
|
|
20
20
|
def gt(obj)
|
21
|
-
obj.where(field_name => {'$gt' => value})
|
21
|
+
obj.where(field_name => { '$gt' => value })
|
22
22
|
end
|
23
23
|
|
24
24
|
def gte(obj)
|
25
|
-
obj.where(field_name => {'$gte' => value})
|
25
|
+
obj.where(field_name => { '$gte' => value })
|
26
26
|
end
|
27
27
|
|
28
28
|
def all(obj)
|
29
|
-
obj.where(field_name => {'$all' => value})
|
29
|
+
obj.where(field_name => { '$all' => value })
|
30
30
|
end
|
31
31
|
|
32
32
|
def in(obj)
|
33
|
-
obj.where(field_name => {'$in' => value})
|
33
|
+
obj.where(field_name => { '$in' => value })
|
34
34
|
end
|
35
35
|
|
36
36
|
def nin(obj)
|
37
|
-
obj.where(field_name => {'$nin' => value})
|
37
|
+
obj.where(field_name => { '$nin' => value })
|
38
38
|
end
|
39
39
|
|
40
40
|
def regex(obj)
|
@@ -42,12 +42,18 @@ module FilterFactory
|
|
42
42
|
end
|
43
43
|
|
44
44
|
def exists(obj)
|
45
|
-
obj.where(field_name => {'$exists' => value})
|
45
|
+
obj.where(field_name => { '$exists' => value })
|
46
46
|
end
|
47
47
|
|
48
48
|
def presents(obj)
|
49
|
-
['true', '1', 1].include?(value)
|
49
|
+
query_opts = if ['true', '1', 1].include?(value)
|
50
|
+
{ field_name => { '$nin' => [nil, '', []] } }
|
51
|
+
else
|
52
|
+
{ field_name => { '$in' => [nil, '', []] } }
|
53
|
+
end
|
54
|
+
|
55
|
+
obj.where(query_opts)
|
50
56
|
end
|
51
57
|
end
|
52
58
|
end
|
53
|
-
end
|
59
|
+
end
|
@@ -6,8 +6,8 @@ module FilterFactory
|
|
6
6
|
FilterFactory::Mongoid::Condition.new(field.name, field.value).method(field.condition)
|
7
7
|
end
|
8
8
|
|
9
|
-
conditions.inject(self) do |res,condition|
|
10
|
-
res
|
9
|
+
conditions.inject(self) do |res, condition|
|
10
|
+
res.instance_eval(&condition)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
@@ -20,4 +20,4 @@ if defined?(Mongoid::Document)
|
|
20
20
|
base.extend FilterFactory::Mongoid::Filter
|
21
21
|
end
|
22
22
|
end
|
23
|
-
end
|
23
|
+
end
|
data/lib/filter_factory.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
|
-
require
|
1
|
+
require 'filter_factory/version'
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
3
|
+
require 'filter_factory/condition'
|
4
|
+
require 'filter_factory/field'
|
5
|
+
require 'filter_factory/filter'
|
6
6
|
|
7
|
-
require
|
8
|
-
require
|
7
|
+
require 'filter_factory/active_record/condition'
|
8
|
+
require 'filter_factory/active_record/filter'
|
9
9
|
|
10
|
-
require
|
11
|
-
require
|
10
|
+
require 'filter_factory/mongoid/condition'
|
11
|
+
require 'filter_factory/mongoid/filter'
|
12
12
|
|
13
13
|
module FilterFactory
|
14
|
-
def self.create
|
15
|
-
FilterFactory::Filter.create
|
14
|
+
def self.create(&block)
|
15
|
+
FilterFactory::Filter.create(&block)
|
16
16
|
end
|
17
|
-
end
|
17
|
+
end
|
data/spec/factories/factories.rb
CHANGED
@@ -3,13 +3,11 @@ FactoryGirl.define do
|
|
3
3
|
sequence(:title) {|n| "post_#{n}"}
|
4
4
|
sequence(:author) {|n| "author_#{n}"}
|
5
5
|
sequence(:views) {|n| n}
|
6
|
-
#views {rand(0..1000)}
|
7
6
|
end
|
8
7
|
|
9
8
|
factory :active_record_post, class: ARPost do
|
10
9
|
sequence(:title) {|n| "post_#{n}"}
|
11
10
|
sequence(:author) {|n| "author_#{n}"}
|
12
11
|
sequence(:views) {|n| n}
|
13
|
-
#views {rand(0..1000)}
|
14
12
|
end
|
15
|
-
end
|
13
|
+
end
|
@@ -1,157 +1,143 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ARPost do
|
4
|
-
|
5
|
-
@posts = FactoryGirl.create_list(:active_record_post, 10)
|
6
|
-
end
|
3
|
+
RSpec.describe ARPost do
|
4
|
+
let!(:posts) { posts = FactoryGirl.create_list(:active_record_post, 5) }
|
7
5
|
|
8
|
-
it
|
9
|
-
described_class.
|
6
|
+
it 'responds to ::filter method' do
|
7
|
+
expect(described_class).to respond_to(:filter)
|
10
8
|
end
|
11
9
|
|
12
|
-
it
|
10
|
+
it 'executes filter methods chain' do
|
11
|
+
sample = posts.sample
|
12
|
+
|
13
13
|
filter = FilterFactory.create do
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
eq :title
|
15
|
+
eq :author, alias: :user
|
16
|
+
gte :views
|
17
17
|
end
|
18
18
|
|
19
|
-
sample = @posts.sample
|
20
|
-
|
21
19
|
filter.title = sample.title
|
22
20
|
filter.user = sample.author
|
23
21
|
|
24
|
-
described_class.filter(filter).to_a.
|
22
|
+
expect(described_class.filter(filter).to_a).to eq([sample])
|
25
23
|
end
|
26
24
|
|
27
|
-
it
|
28
|
-
sample =
|
25
|
+
it 'returns records with column values equal to specified value' do
|
26
|
+
sample = posts.sample
|
29
27
|
|
30
|
-
filter = FilterFactory.create
|
31
|
-
field :id, :eq
|
32
|
-
end
|
28
|
+
filter = FilterFactory.create { eq :id }
|
33
29
|
filter.id = sample.id
|
34
30
|
|
35
|
-
described_class.filter(filter).to_a.
|
31
|
+
expect(described_class.filter(filter).to_a).to eq([sample])
|
36
32
|
end
|
37
33
|
|
38
|
-
it
|
39
|
-
sample =
|
34
|
+
it 'returns records with column values not equal specified value' do
|
35
|
+
sample = posts.sample
|
40
36
|
|
41
|
-
filter = FilterFactory.create
|
42
|
-
field :id, :ne
|
43
|
-
end
|
37
|
+
filter = FilterFactory.create { ne :id }
|
44
38
|
filter.id = sample.id
|
45
39
|
|
46
|
-
|
40
|
+
expected_result = posts.reject { |p| p.id == sample.id }
|
41
|
+
expect(described_class.filter(filter).to_a.sort).to eq(expected_result)
|
47
42
|
end
|
48
43
|
|
49
|
-
it
|
50
|
-
sample =
|
44
|
+
it 'returns records with column values less than specified value' do
|
45
|
+
sample = posts.sample
|
51
46
|
|
52
|
-
filter = FilterFactory.create
|
53
|
-
field :views, :lt
|
54
|
-
end
|
47
|
+
filter = FilterFactory.create { lt :views }
|
55
48
|
filter.views = sample.views
|
56
49
|
|
57
|
-
|
50
|
+
expected_result = posts.select { |p| p.views < sample.views }.map(&:id).sort
|
51
|
+
expect(described_class.filter(filter).map(&:id).sort).to eq(expected_result)
|
58
52
|
end
|
59
53
|
|
60
|
-
it
|
61
|
-
sample =
|
54
|
+
it 'returns records with column values less than or equal to specified value' do
|
55
|
+
sample = posts.sample
|
62
56
|
|
63
|
-
filter = FilterFactory.create
|
64
|
-
field :views, :lte
|
65
|
-
end
|
57
|
+
filter = FilterFactory.create { lte :views }
|
66
58
|
filter.views = sample.views
|
67
59
|
|
68
|
-
|
60
|
+
expected_result = posts.select { |p| p.views <= sample.views }.map(&:id).sort
|
61
|
+
expect(described_class.filter(filter).map(&:id).sort).to eq(expected_result)
|
69
62
|
end
|
70
63
|
|
71
|
-
it
|
72
|
-
sample =
|
64
|
+
it 'returns records with column values greater than specified value' do
|
65
|
+
sample = posts.sample
|
73
66
|
|
74
|
-
filter = FilterFactory.create
|
75
|
-
field :views, :gt
|
76
|
-
end
|
67
|
+
filter = FilterFactory.create { gt :views }
|
77
68
|
filter.views = sample.views
|
78
69
|
|
79
|
-
|
70
|
+
expected_result = posts.select { |p| p.views > sample.views }.map(&:id).sort
|
71
|
+
expect(described_class.filter(filter).map(&:id).sort).to eq(expected_result)
|
80
72
|
end
|
81
73
|
|
82
|
-
it
|
83
|
-
sample =
|
74
|
+
it 'returns records with column values greater than or equal to specified value' do
|
75
|
+
sample = posts.sample
|
84
76
|
|
85
|
-
filter = FilterFactory.create
|
86
|
-
field :views, :gte
|
87
|
-
end
|
77
|
+
filter = FilterFactory.create { gte :views }
|
88
78
|
filter.views = sample.views
|
89
79
|
|
90
|
-
|
80
|
+
expected_result = posts.select { |p| p.views >= sample.views }.map(&:id).sort
|
81
|
+
expect(described_class.filter(filter).map(&:id).sort).to eq(expected_result)
|
91
82
|
end
|
92
83
|
|
93
|
-
it
|
84
|
+
it 'raises NotImplementedError if using \'all\' condition' do
|
94
85
|
filter = FilterFactory.create do
|
95
|
-
field :opts,
|
86
|
+
field :opts, :all
|
96
87
|
end
|
97
88
|
filter.opts = [1, 2, 3, 4]
|
98
89
|
|
99
|
-
expect {described_class.filter(filter)}.to raise_error(NotImplementedError)
|
90
|
+
expect { described_class.filter(filter) }.to raise_error(NotImplementedError)
|
100
91
|
end
|
101
92
|
|
102
|
-
it
|
103
|
-
sample =
|
93
|
+
it 'returns records with column values in specified values' do
|
94
|
+
sample = posts.sample(3)
|
104
95
|
|
105
96
|
filter = FilterFactory.create do
|
106
|
-
field :id,
|
97
|
+
field :id, :in
|
107
98
|
end
|
108
99
|
filter.id = sample.map(&:id)
|
109
100
|
|
110
|
-
|
101
|
+
expected_result = sample.map(&:id).sort
|
102
|
+
expect(described_class.filter(filter).map(&:id).sort).to eq(expected_result)
|
111
103
|
end
|
112
104
|
|
113
|
-
it
|
114
|
-
sample =
|
105
|
+
it 'returns records with column values not in specified values' do
|
106
|
+
sample = posts.sample(3)
|
115
107
|
|
116
|
-
filter = FilterFactory.create
|
117
|
-
field :id, :nin
|
118
|
-
end
|
108
|
+
filter = FilterFactory.create { nin :id }
|
119
109
|
filter.id = sample.map(&:id)
|
120
110
|
|
121
|
-
|
111
|
+
expected_result = (posts.map(&:id) - sample.map(&:id)).sort
|
112
|
+
expect(described_class.filter(filter).map(&:id).sort).to eq(expected_result)
|
122
113
|
end
|
123
114
|
|
124
|
-
it
|
125
|
-
sample =
|
126
|
-
sample.each_with_index{|r,i| r.update_attribute(:title, "my_title_#{i}")}
|
127
|
-
|
128
|
-
filter = FilterFactory.create do
|
129
|
-
field :title, :regex
|
130
|
-
end
|
115
|
+
it 'returns records with column values which match the specified regexp' do
|
116
|
+
sample = posts.sample(3)
|
117
|
+
sample.each_with_index { |r, i| r.update_attribute(:title, "my_title_#{i}") }
|
131
118
|
|
119
|
+
filter = FilterFactory.create { regex :title }
|
132
120
|
filter.title = '_title_'
|
133
121
|
|
134
|
-
|
122
|
+
expected_result = sample.map(&:id).sort
|
123
|
+
expect(described_class.filter(filter).map(&:id).sort).to eq(expected_result)
|
135
124
|
end
|
136
125
|
|
137
|
-
it
|
138
|
-
filter = FilterFactory.create
|
139
|
-
field :not_exists, :exists
|
140
|
-
end
|
126
|
+
it 'raises NotImplementedError if using \'exists\' condition' do
|
127
|
+
filter = FilterFactory.create { exists :not_exists }
|
141
128
|
filter.not_exists = true
|
142
129
|
|
143
|
-
expect {described_class.filter(filter)}.to raise_error(NotImplementedError)
|
130
|
+
expect { described_class.filter(filter) }.to raise_error(NotImplementedError)
|
144
131
|
end
|
145
132
|
|
146
|
-
it
|
147
|
-
sample =
|
148
|
-
sample.each{|r| r.update_attribute(:title, nil)}
|
133
|
+
it 'returns records with column values not nil equal to specified value' do
|
134
|
+
sample = posts.sample(3)
|
135
|
+
sample.each { |r| r.update_attribute(:title, nil) }
|
149
136
|
|
150
|
-
filter = FilterFactory.create
|
151
|
-
field :title, :presents
|
152
|
-
end
|
137
|
+
filter = FilterFactory.create { presents :title }
|
153
138
|
filter.title = false
|
154
139
|
|
155
|
-
|
140
|
+
expected_result = sample.map(&:id).sort
|
141
|
+
expect(described_class.filter(filter).map(&:id).sort).to eq(expected_result)
|
156
142
|
end
|
157
|
-
end
|
143
|
+
end
|