Module: Motor::ApiQuery::ApplyScope

Defined in:
lib/motor/api_query/apply_scope.rb

Class Method Summary collapse

Class Method Details

.apply_filter_scope(rel, scope) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/motor/api_query/apply_scope.rb', line 20

def apply_filter_scope(rel, scope)
  configs = Motor::Resource.find_by(name: rel.klass.name.underscore)

  return rel unless configs

  scope_configs = configs.preferences[:scopes].find { |s| s[:name] == scope }

  return rel unless scope_configs

  ApiQuery::Filter.call(rel, scope_configs[:preferences][:filter])
end

.call(rel, scope) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
# File 'lib/motor/api_query/apply_scope.rb', line 8

def call(rel, scope)
  return rel if scope.blank?

  scope_symbol = scope.to_sym

  if rel.klass.defined_scopes.include?(scope_symbol)
    rel.public_send(scope_symbol)
  else
    apply_filter_scope(rel, scope)
  end
end