paraphrase 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +0,0 @@
1
- module Paraphrase
2
- class DuplicateScopeError < StandardError; end
3
- end
@@ -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