paraphrase 0.9.0 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -1
- data/Appraisals +12 -18
- data/CHANGELOG.md +13 -1
- data/README.md +53 -11
- data/gemfiles/3.1.gemfile +3 -3
- data/gemfiles/3.1.gemfile.lock +47 -42
- data/gemfiles/3.2.gemfile +3 -3
- data/gemfiles/3.2.gemfile.lock +36 -32
- data/gemfiles/4.0.gemfile +3 -3
- data/gemfiles/4.0.gemfile.lock +38 -36
- data/gemfiles/4.1.gemfile +3 -3
- data/gemfiles/4.1.gemfile.lock +35 -31
- data/lib/paraphrase.rb +14 -3
- data/lib/paraphrase/active_model.rb +1 -0
- data/lib/paraphrase/mapping.rb +28 -0
- data/lib/paraphrase/{params.rb → params_filter.rb} +10 -4
- data/lib/paraphrase/query.rb +64 -41
- data/lib/paraphrase/rails.rb +2 -0
- data/lib/paraphrase/repository.rb +36 -0
- data/lib/paraphrase/syntax.rb +12 -2
- data/lib/paraphrase/version.rb +1 -1
- data/paraphrase.gemspec +5 -5
- data/spec/paraphrase/query_spec.rb +39 -10
- data/spec/paraphrase/syntax_spec.rb +1 -1
- data/spec/spec_helper.rb +7 -8
- metadata +15 -19
- data/gemfiles/3.0.gemfile +0 -9
- data/gemfiles/3.0.gemfile.lock +0 -100
- data/lib/paraphrase/errors.rb +0 -3
- data/lib/paraphrase/scope.rb +0 -56
- data/spec/paraphrase/scope_spec.rb +0 -51
data/lib/paraphrase/errors.rb
DELETED
data/lib/paraphrase/scope.rb
DELETED
@@ -1,56 +0,0 @@
|
|
1
|
-
require 'active_support/core_ext/object/blank'
|
2
|
-
require 'active_support/core_ext/array/wrap'
|
3
|
-
|
4
|
-
module Paraphrase
|
5
|
-
class Scope
|
6
|
-
# @!attribute [r] keys
|
7
|
-
# @return [Array<Symbol>] param keys to extract
|
8
|
-
#
|
9
|
-
# @!attribute [r] name
|
10
|
-
# @return [Symbol] scope name
|
11
|
-
#
|
12
|
-
# @!attribute [r] required_keys
|
13
|
-
# @return [Array<Symbol>] keys required for query
|
14
|
-
attr_reader :keys, :name, :required_keys
|
15
|
-
|
16
|
-
# @param [Symbol] keys query params to be mapped to the scope
|
17
|
-
# @param [Hash] options options to configure {Scope Scope} instance
|
18
|
-
# @option options [Symbol, Array<Symbol>] :to scope to map query params to
|
19
|
-
# @option options [true, Symbol, Array<Symbol>] :whitelist lists all or a
|
20
|
-
# subset of param keys as optional
|
21
|
-
def initialize(keys, options)
|
22
|
-
@keys = keys
|
23
|
-
@name = options[:to]
|
24
|
-
|
25
|
-
@required_keys = if options[:whitelist] == true
|
26
|
-
[]
|
27
|
-
else
|
28
|
-
@keys - Array.wrap(options[:whitelist])
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
# Sends {#name} to `relation` if `query` has a value for all the
|
33
|
-
# {Scope#required_keys}. Passes through `relation` if any
|
34
|
-
# values are missing. Detects if the scope takes no arguments to determine
|
35
|
-
# if values should be passed to the scope.
|
36
|
-
#
|
37
|
-
# @param [Hash] params filtered params from the query
|
38
|
-
# @param [ActiveRecord::Relation] relation scope chain
|
39
|
-
# @return [ActiveRecord::Relation]
|
40
|
-
def chain(params, relation)
|
41
|
-
if required_keys.all? { |key| params[key] }
|
42
|
-
klass = relation.respond_to?(:klass) ? relation.klass : relation
|
43
|
-
arity = klass.method(name).arity
|
44
|
-
|
45
|
-
if arity == 0
|
46
|
-
relation.send(name)
|
47
|
-
else
|
48
|
-
values = keys.map { |key| params[key] }
|
49
|
-
relation.send(name, *values)
|
50
|
-
end
|
51
|
-
else
|
52
|
-
relation
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
@@ -1,51 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Paraphrase
|
4
|
-
describe Scope do
|
5
|
-
def build_account_scope(options = {})
|
6
|
-
options.reverse_merge!(
|
7
|
-
keys: [:name],
|
8
|
-
to: :name_like
|
9
|
-
)
|
10
|
-
|
11
|
-
keys = options.delete(:keys)
|
12
|
-
|
13
|
-
Scope.new(keys, options)
|
14
|
-
end
|
15
|
-
|
16
|
-
describe "#chain" do
|
17
|
-
it "applies scope method to relation with values from params hash" do
|
18
|
-
scope = build_account_scope
|
19
|
-
|
20
|
-
expect(Account).to receive(:name_like).with('Jon Snow')
|
21
|
-
scope.chain({ name: 'Jon Snow' }, Account)
|
22
|
-
end
|
23
|
-
|
24
|
-
it "does nothing if values are missing" do
|
25
|
-
scope = build_account_scope
|
26
|
-
|
27
|
-
expect(Account).not_to receive(:name_like)
|
28
|
-
scope.chain({}, Account)
|
29
|
-
end
|
30
|
-
|
31
|
-
it "passes through blank values if scope has been whitelisted" do
|
32
|
-
scope = build_account_scope(whitelist: true)
|
33
|
-
|
34
|
-
expect(Account).to receive(:name_like).with(nil)
|
35
|
-
scope.chain({}, Account)
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'allows whitelisting a subset of keys' do
|
39
|
-
scope = build_account_scope(
|
40
|
-
keys: [:name, :status],
|
41
|
-
to: :with_name_and_status,
|
42
|
-
whitelist: true
|
43
|
-
)
|
44
|
-
|
45
|
-
expect(Account).to receive(:with_name_and_status).with('George', nil)
|
46
|
-
|
47
|
-
scope.chain({ name: 'George' }, Account)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|