search_object 1.2.2 → 1.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -3
- data/.travis.yml +2 -1
- data/CHANGELOG.md +3 -0
- data/Gemfile +2 -0
- data/Rakefile +2 -0
- data/example/Gemfile +2 -0
- data/example/Rakefile +3 -1
- data/example/app/controllers/application_controller.rb +2 -0
- data/example/app/controllers/posts_controller.rb +2 -0
- data/example/app/models/post.rb +2 -0
- data/example/app/models/post_search.rb +2 -0
- data/example/app/models/user.rb +2 -0
- data/example/bin/bundle +3 -1
- data/example/bin/rails +3 -1
- data/example/bin/rake +2 -0
- data/example/config.ru +2 -0
- data/example/spec/models/post_search_spec.rb +19 -19
- data/example/spec/spec_helper.rb +3 -1
- data/lib/search_object/base.rb +1 -1
- data/lib/search_object/plugin/enum.rb +2 -5
- data/lib/search_object/plugin/paging.rb +6 -3
- data/lib/search_object/version.rb +1 -1
- data/search_object.gemspec +3 -3
- data/spec/search_object/base_spec.rb +2 -0
- data/spec/search_object/helper_spec.rb +2 -0
- data/spec/search_object/plugin/enum_spec.rb +32 -23
- data/spec/search_object/plugin/kaminari_spec.rb +2 -0
- data/spec/search_object/plugin/model_spec.rb +2 -0
- data/spec/search_object/plugin/paging_spec.rb +2 -0
- data/spec/search_object/plugin/sorting_spec.rb +2 -0
- data/spec/search_object/plugin/will_paginate_spec.rb +2 -0
- data/spec/search_object/search_spec.rb +2 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/spec_helper_active_record.rb +2 -0
- data/spec/support/paging_shared_example.rb +2 -0
- metadata +10 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 185c2ec2237293e6a214c921457a18ef221402c395a74c66a774d2f8e2b4e12b
|
4
|
+
data.tar.gz: 81dcfc6e5bfc8a3499cf24f37be6d2dd2fac23bdd6da39162a24ef66c20063bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2f66341b1d6e542396ac8930b3b29eba4124081e4e9e08d208208667856c80253ca6c33136afca304a47e2a2bbff03b4202170acd71e1da012058b0aaa05b144
|
7
|
+
data.tar.gz: 1af8e023f69d4f30217a259018c6a866fb65ec26bf904ccbd28331cf62aa2bfce54cf937d8f9af24c30e2aa7a817eac0e7d51a9fcae6eb4f24fc737794eeea20
|
data/.rubocop.yml
CHANGED
@@ -7,15 +7,15 @@ AllCops:
|
|
7
7
|
- search_object.gemspec
|
8
8
|
|
9
9
|
# Disables "Line is too long"
|
10
|
-
LineLength:
|
10
|
+
Metrics/LineLength:
|
11
11
|
Enabled: false
|
12
12
|
|
13
13
|
# Disables Module has too many lines
|
14
|
-
ModuleLength:
|
14
|
+
Metrics/ModuleLength:
|
15
15
|
Enabled: false
|
16
16
|
|
17
17
|
# Disables "Missing top-level class documentation comment"
|
18
|
-
Documentation:
|
18
|
+
Style/Documentation:
|
19
19
|
Enabled: false
|
20
20
|
|
21
21
|
# Disables "Use each_with_object instead of inject"
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/Rakefile
CHANGED
data/example/Gemfile
CHANGED
data/example/Rakefile
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
2
4
|
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
3
5
|
|
4
|
-
require File.expand_path('
|
6
|
+
require File.expand_path('config/application', __dir__)
|
5
7
|
|
6
8
|
Example::Application.load_tasks
|
data/example/app/models/post.rb
CHANGED
data/example/app/models/user.rb
CHANGED
data/example/bin/bundle
CHANGED
data/example/bin/rails
CHANGED
data/example/bin/rake
CHANGED
data/example/config.ru
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
@@ -11,11 +11,11 @@ describe PostSearch do
|
|
11
11
|
|
12
12
|
def create(attributes = {})
|
13
13
|
Post.create! attributes.reverse_merge(
|
14
|
-
user:
|
15
|
-
title:
|
16
|
-
body:
|
17
|
-
category_name:
|
18
|
-
published:
|
14
|
+
user: user,
|
15
|
+
title: 'Title',
|
16
|
+
body: 'Body',
|
17
|
+
category_name: 'Tech',
|
18
|
+
published: true
|
19
19
|
)
|
20
20
|
end
|
21
21
|
|
@@ -24,29 +24,29 @@ describe PostSearch do
|
|
24
24
|
end
|
25
25
|
|
26
26
|
it 'can search by category name' do
|
27
|
-
post
|
28
|
-
|
27
|
+
post = create category_name: 'Personal'
|
28
|
+
_other = create category_name: 'Other'
|
29
29
|
|
30
30
|
expect_search(category_name: 'Personal').to eq [post]
|
31
31
|
end
|
32
32
|
|
33
33
|
it 'can search by user_id' do
|
34
|
-
post
|
35
|
-
|
34
|
+
post = create user: create_user
|
35
|
+
_other = create user: create_user
|
36
36
|
|
37
37
|
expect_search(user_id: post.user_id).to eq [post]
|
38
38
|
end
|
39
39
|
|
40
40
|
it 'can search by title' do
|
41
|
-
post
|
42
|
-
|
41
|
+
post = create title: 'Title'
|
42
|
+
_other = create title: 'Other'
|
43
43
|
|
44
44
|
expect_search(title: 'itl').to eq [post]
|
45
45
|
end
|
46
46
|
|
47
47
|
it 'can search by published' do
|
48
|
-
post
|
49
|
-
|
48
|
+
post = create published: true
|
49
|
+
_other = create published: false
|
50
50
|
|
51
51
|
expect_search(published: true).to eq [post]
|
52
52
|
end
|
@@ -54,21 +54,21 @@ describe PostSearch do
|
|
54
54
|
it 'can search by term' do
|
55
55
|
post_with_body = create body: 'pattern'
|
56
56
|
post_with_title = create title: 'pattern'
|
57
|
-
|
57
|
+
_other = create
|
58
58
|
|
59
59
|
expect_search(term: 'pattern').to eq [post_with_title, post_with_body]
|
60
60
|
end
|
61
61
|
|
62
62
|
it 'can search by created after' do
|
63
|
-
post
|
64
|
-
|
63
|
+
post = create created_at: 1.month.ago
|
64
|
+
_other = create created_at: 3.month.ago
|
65
65
|
|
66
66
|
expect_search(created_after: 2.month.ago.strftime('%Y-%m-%d')).to eq [post]
|
67
67
|
end
|
68
68
|
|
69
69
|
it 'can search by created before' do
|
70
|
-
post
|
71
|
-
|
70
|
+
post = create created_at: 3.month.ago
|
71
|
+
_other = create created_at: 1.month.ago
|
72
72
|
|
73
73
|
expect_search(created_before: 2.month.ago.strftime('%Y-%m-%d')).to eq [post]
|
74
74
|
end
|
data/example/spec/spec_helper.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# This file is copied to spec/ when you run 'rails generate rspec:install'
|
2
4
|
ENV['RAILS_ENV'] ||= 'test'
|
3
5
|
|
4
|
-
require File.expand_path('
|
6
|
+
require File.expand_path('../config/environment', __dir__)
|
5
7
|
require 'rspec/rails'
|
6
8
|
|
7
9
|
# Requires supporting ruby files with custom matchers and macros, etc,
|
data/lib/search_object/base.rb
CHANGED
@@ -32,9 +32,7 @@ module SearchObject
|
|
32
32
|
def apply_filter(object:, option:, enums:, scope:, value:)
|
33
33
|
return if value.nil? || value == ''
|
34
34
|
|
35
|
-
unless enums.include? value
|
36
|
-
return handle_invalid_value(object: object, option: option, enums: enums, scope: scope, value: value)
|
37
|
-
end
|
35
|
+
return handle_invalid_value(object: object, option: option, enums: enums, scope: scope, value: value) unless enums.include? value.to_s
|
38
36
|
|
39
37
|
object.send("apply_#{Helper.underscore(option)}_with_#{Helper.underscore(value)}", scope)
|
40
38
|
end
|
@@ -43,8 +41,7 @@ module SearchObject
|
|
43
41
|
specific = "handle_invalid_#{option}"
|
44
42
|
return object.send(specific, scope, value) if object.respond_to? specific, true
|
45
43
|
|
46
|
-
|
47
|
-
return object.send(catch_all, option, scope, value) if object.respond_to? catch_all, true
|
44
|
+
return object.handle_invalid_enum(option, scope, value) if object.respond_to? :handle_invalid_enum, true
|
48
45
|
|
49
46
|
raise InvalidEnumValueError.new(option, enums, value)
|
50
47
|
end
|
@@ -28,17 +28,20 @@ module SearchObject
|
|
28
28
|
|
29
29
|
module ClassMethods
|
30
30
|
def per_page(number)
|
31
|
-
raise InvalidNumberError.new('Per page', number) unless number
|
31
|
+
raise InvalidNumberError.new('Per page', number) unless number.positive?
|
32
|
+
|
32
33
|
config[:per_page] = number
|
33
34
|
end
|
34
35
|
|
35
36
|
def min_per_page(number)
|
36
|
-
raise InvalidNumberError.new('Min per page', number) unless number
|
37
|
+
raise InvalidNumberError.new('Min per page', number) unless number.positive?
|
38
|
+
|
37
39
|
config[:min_per_page] = number
|
38
40
|
end
|
39
41
|
|
40
42
|
def max_per_page(number)
|
41
|
-
raise InvalidNumberError.new('Max per page', number) unless number
|
43
|
+
raise InvalidNumberError.new('Max per page', number) unless number.positive?
|
44
|
+
|
42
45
|
config[:max_per_page] = number
|
43
46
|
end
|
44
47
|
|
data/search_object.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_development_dependency "bundler"
|
21
|
+
spec.add_development_dependency "bundler"
|
22
22
|
spec.add_development_dependency "rake"
|
23
23
|
spec.add_development_dependency 'rspec', '~> 3.5'
|
24
24
|
spec.add_development_dependency 'rspec-mocks', '~> 3.5'
|
@@ -29,6 +29,6 @@ Gem::Specification.new do |spec|
|
|
29
29
|
spec.add_development_dependency 'will_paginate'
|
30
30
|
spec.add_development_dependency 'kaminari'
|
31
31
|
spec.add_development_dependency 'kaminari-activerecord'
|
32
|
-
spec.add_development_dependency 'rubocop', '0.
|
33
|
-
spec.add_development_dependency 'rubocop-rspec', '1.
|
32
|
+
spec.add_development_dependency 'rubocop', '0.75.1'
|
33
|
+
spec.add_development_dependency 'rubocop-rspec', '1.36.0'
|
34
34
|
end
|
@@ -1,52 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
require 'ostruct'
|
3
5
|
|
4
6
|
module SearchObject
|
5
7
|
module Plugin
|
6
8
|
describe Enum do
|
7
|
-
|
8
|
-
|
9
|
+
let(:test_search) do
|
10
|
+
Class.new do
|
11
|
+
include SearchObject.module(:enum)
|
9
12
|
|
10
|
-
|
13
|
+
scope { [1, 2, 3, 4, 5] }
|
11
14
|
|
12
|
-
|
13
|
-
|
15
|
+
option :filter, enum: %w[odd even]
|
16
|
+
option :camelCase, enum: %w[someValue]
|
14
17
|
|
15
|
-
|
18
|
+
private
|
16
19
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
+
def apply_filter_with_odd(scope)
|
21
|
+
scope.select(&:odd?)
|
22
|
+
end
|
20
23
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
+
def apply_filter_with_even(scope)
|
25
|
+
scope.select(&:even?)
|
26
|
+
end
|
24
27
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
+
def apply_camel_case_with_some_value(_scope)
|
29
|
+
[1]
|
30
|
+
end
|
28
31
|
|
29
|
-
|
30
|
-
|
32
|
+
def handle_invalid_filter(_scope, value)
|
33
|
+
"invalid filter - #{value}"
|
34
|
+
end
|
31
35
|
end
|
32
36
|
end
|
33
37
|
|
34
38
|
it 'can filter by enum values' do
|
35
|
-
expect(
|
36
|
-
expect(
|
39
|
+
expect(test_search.results(filters: { filter: 'odd' })).to eq [1, 3, 5]
|
40
|
+
expect(test_search.results(filters: { filter: 'even' })).to eq [2, 4]
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'converts input to string' do
|
44
|
+
expect(test_search.results(filters: { filter: :odd })).to eq [1, 3, 5]
|
45
|
+
expect(test_search.results(filters: { filter: :even })).to eq [2, 4]
|
37
46
|
end
|
38
47
|
|
39
48
|
it 'ignores blank values' do
|
40
|
-
expect(
|
41
|
-
expect(
|
49
|
+
expect(test_search.results(filters: { filter: nil })).to eq [1, 2, 3, 4, 5]
|
50
|
+
expect(test_search.results(filters: { filter: '' })).to eq [1, 2, 3, 4, 5]
|
42
51
|
end
|
43
52
|
|
44
53
|
it 'handles wrong enum values' do
|
45
|
-
expect(
|
54
|
+
expect(test_search.results(filters: { filter: 'foo' })).to eq 'invalid filter - foo'
|
46
55
|
end
|
47
56
|
|
48
57
|
it 'underscores method and enum values' do
|
49
|
-
expect(
|
58
|
+
expect(test_search.results(filters: { camelCase: 'someValue' })).to eq [1]
|
50
59
|
end
|
51
60
|
|
52
61
|
it 'raises when block is passed with enum option' do
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: search_object
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Radoslav Stankov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-10-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -170,28 +170,28 @@ dependencies:
|
|
170
170
|
requirements:
|
171
171
|
- - '='
|
172
172
|
- !ruby/object:Gem::Version
|
173
|
-
version: 0.
|
173
|
+
version: 0.75.1
|
174
174
|
type: :development
|
175
175
|
prerelease: false
|
176
176
|
version_requirements: !ruby/object:Gem::Requirement
|
177
177
|
requirements:
|
178
178
|
- - '='
|
179
179
|
- !ruby/object:Gem::Version
|
180
|
-
version: 0.
|
180
|
+
version: 0.75.1
|
181
181
|
- !ruby/object:Gem::Dependency
|
182
182
|
name: rubocop-rspec
|
183
183
|
requirement: !ruby/object:Gem::Requirement
|
184
184
|
requirements:
|
185
185
|
- - '='
|
186
186
|
- !ruby/object:Gem::Version
|
187
|
-
version: 1.
|
187
|
+
version: 1.36.0
|
188
188
|
type: :development
|
189
189
|
prerelease: false
|
190
190
|
version_requirements: !ruby/object:Gem::Requirement
|
191
191
|
requirements:
|
192
192
|
- - '='
|
193
193
|
- !ruby/object:Gem::Version
|
194
|
-
version: 1.
|
194
|
+
version: 1.36.0
|
195
195
|
description: Search object DSL
|
196
196
|
email:
|
197
197
|
- rstankov@gmail.com
|