Class: QueryFilter::Rules::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/query_filter/rules/scope.rb

Direct Known Subclasses

DateRange, OrderBy, Range, SplitterRange

Constant Summary collapse

VALIDATON_KEYS =
[:in, :only, :format, :if, :unless].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keys, options = {}) ⇒ Scope

Returns a new instance of Scope.



12
13
14
15
# File 'lib/query_filter/rules/scope.rb', line 12

def initialize(keys, options = {})
  @keys = Array(keys)
  @options = options
end

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys.



10
11
12
# File 'lib/query_filter/rules/scope.rb', line 10

def keys
  @keys
end

Instance Method Details

#blank_validation?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/query_filter/rules/scope.rb', line 17

def blank_validation?
  (@options.keys & VALIDATON_KEYS).empty?
end

#can_apply?(target) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
# File 'lib/query_filter/rules/scope.rb', line 49

def can_apply?(target)
  conditions = QueryFilter::Utils::UserConditions.new(target, @options)

  if conditions.present?
    conditions.passed?
  else
    valid?(target.params)
  end
end

#endpointObject



33
34
35
# File 'lib/query_filter/rules/scope.rb', line 33

def endpoint
  @options[:to] || "#{name}_#{key}"
end

#keyObject



41
42
43
# File 'lib/query_filter/rules/scope.rb', line 41

def key
  @key ||= @keys.first
end

#nameObject



37
38
39
# File 'lib/query_filter/rules/scope.rb', line 37

def name
  'scope'
end

#normalize_params(params) ⇒ Object



45
46
47
# File 'lib/query_filter/rules/scope.rb', line 45

def normalize_params(params)
  params.values_at(*keys)
end

#valid?(params) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
# File 'lib/query_filter/rules/scope.rb', line 21

def valid?(params)
  values = normalize_params(params)

  checks = []
  checks << @options[:in].include?(values.first) if @options[:in]
  checks << @options[:only].include?(values.first) if @options[:only]
  checks << !values.map(&:blank?).all? if blank_validation?
  checks << @options[:intersect] & Array.wrap(values) if @options[:intersect]

  !checks.empty? && checks.all?
end