Class: Paraphrase::ScopeMapping

Inherits:
Object
  • Object
show all
Defined in:
lib/paraphrase/scope_mapping.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ ScopeMapping

Returns a new instance of ScopeMapping.

Parameters:

  • name (Symbol)

    name of the scope

  • options (Hash)

    options to configure ScopeMapping instance

Options Hash (options):

  • :to (Symbol, Array<Symbol>)

    param key(s) to extract values from

  • :require (true, Symbol, Array<Symbol>)

    lists all or a subset of param keys as required

  • :whitelist (true, Symbol, Array<Symbol>)

    lists all or a subset of param keys as whitelisted



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/paraphrase/scope_mapping.rb', line 25

def initialize(name, options)
  @method_name = name
  @keys = Array(options.delete(:to))

  @required = register_keys(options[:require])
  @whitelist = register_keys(options[:whitelist])

  if @whitelist.empty? && !@required.empty?
    @whitelist = @keys - @required
  end

  if (whitelist & required).any?
    raise ArgumentError, "cannot whitelist and require the same keys"
  end
end

Instance Attribute Details

#keysArray<Symbol> (readonly)

Returns param keys to extract.

Returns:

  • (Array<Symbol>)

    param keys to extract



16
17
18
# File 'lib/paraphrase/scope_mapping.rb', line 16

def keys
  @keys
end

#method_nameSymbol (readonly)

Returns scope name.

Returns:

  • (Symbol)

    scope name



16
# File 'lib/paraphrase/scope_mapping.rb', line 16

attr_reader :keys, :method_name, :required, :whitelist

#requiredArray (readonly)

Returns keys required for query.

Returns:

  • (Array)

    keys required for query



16
# File 'lib/paraphrase/scope_mapping.rb', line 16

attr_reader :keys, :method_name, :required, :whitelist

#whitelistObject (readonly)

Returns the value of attribute whitelist.



16
# File 'lib/paraphrase/scope_mapping.rb', line 16

attr_reader :keys, :method_name, :required, :whitelist

Instance Method Details

#chain(params, relation) ⇒ ActiveRecord::Relation

Sends #method_name to chain, extracting arguments from params. If values are missing for any #keys, return the chain unmodified. If required, errors are added to the Query instance as well.

Parameters:

  • params (Hash)

    hash of query parameters

  • relation (ActiveRecord::Relation, ActiveRecord::Base)

    scope chain

Returns:

  • (ActiveRecord::Relation)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/paraphrase/scope_mapping.rb', line 49

def chain(params, relation)
  scope = relation.respond_to?(:klass) ? relation.klass.method(method_name) : relation.method(method_name)

  inputs = keys.map do |key|
    input = params[key]

    if input.nil?
      break    if required.include?(key)
      break [] if !whitelist.include?(key)
    end

    input
  end

  if inputs.nil?
    return
  elsif inputs.empty?
    return relation
  end

  scope.arity == 0 ? relation.send(method_name) : relation.send(method_name, *inputs)
end