Module: Motor::ApiQuery::ApplyScope
- Defined in:
- lib/motor/api_query/apply_scope.rb
Class Method Summary collapse
- .apply_filter_scope(rel, scope) ⇒ Object
- .apply_order(rel, params) ⇒ Object
- .call(rel, scope) ⇒ Object
Class Method Details
.apply_filter_scope(rel, scope) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# 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 rel = ApiQuery::Filter.call(rel, scope_configs[:preferences][:filter]) apply_order(rel, scope_configs[:preferences][:sort]) end |
.apply_order(rel, params) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/motor/api_query/apply_scope.rb', line 34 def apply_order(rel, params) return rel if params.blank? sort_key, sort_order = params.values_at(:key, :order) rel.order(sort_key => sort_order) 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 |