Class: Oso::Polar::Data::Adapter::ActiveRecordAdapter
- Inherits:
-
Oso::Polar::Data::Adapter
- Object
- Oso::Polar::Data::Adapter
- Oso::Polar::Data::Adapter::ActiveRecordAdapter
- Defined in:
- lib/oso/polar/data/adapter/active_record_adapter.rb
Overview
Example data filtering adapter for ActiveRecord
Constant Summary collapse
- OPS =
{ 'Eq' => '=', 'In' => 'IN', 'Nin' => 'NOT IN', 'Neq' => '!=', 'Lt' => '<', 'Gt' => '>', 'Leq' => '<=', 'Geq' => '>=' }.freeze
Instance Method Summary collapse
-
#build_query(filter) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
- #execute_query(query) ⇒ Object
Instance Method Details
#build_query(filter) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/oso/polar/data/adapter/active_record_adapter.rb', line 9 def build_query(filter) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength types = filter.types query = filter.relations.reduce(filter.model.all) do |q, rel| rec = types[rel.left].fields[rel.name] join_type = rec.kind == 'one' ? 'LEFT' : 'INNER' q.joins( "#{join_type} JOIN #{rel.right.table_name} ON " \ "#{rel.left.table_name}.#{rec.my_field} = " \ "#{rel.right.table_name}.#{rec.other_field}" ) end filter.conditions.map do |conjs| conjs.reduce(query) do |q, conj| q.where(*sqlize(conj)) end end.reduce(:or).distinct end |
#execute_query(query) ⇒ Object
28 29 30 |
# File 'lib/oso/polar/data/adapter/active_record_adapter.rb', line 28 def execute_query(query) query.to_a end |