Class: Mobility::Backends::ActiveRecord::QueryMethods
- Inherits:
-
Module
- Object
- Module
- Mobility::Backends::ActiveRecord::QueryMethods
- Defined in:
- lib/mobility/backends/active_record/query_methods.rb
Overview
Defines query method overrides to handle translated attributes for ActiveRecord models. For details see backend-specific subclasses.
Direct Known Subclasses
Column::QueryMethods, Hstore::QueryMethods, Jsonb::QueryMethods, KeyValue::QueryMethods, Serialized::QueryMethods, Table::QueryMethods
Instance Method Summary collapse
- #extended(relation) ⇒ Object
-
#initialize(attributes, _) ⇒ QueryMethods
constructor
A new instance of QueryMethods.
Constructor Details
#initialize(attributes, _) ⇒ QueryMethods
Returns a new instance of QueryMethods.
12 13 14 15 16 17 |
# File 'lib/mobility/backends/active_record/query_methods.rb', line 12 def initialize(attributes, _) @attributes = attributes @attributes_extractor = lambda do |opts| opts.is_a?(Hash) && (opts.keys.map(&:to_s) & attributes).presence end end |
Instance Method Details
#extended(relation) ⇒ Object
Note:
Only want to define this once, even if multiple QueryMethods modules are included, so define it here in extended method
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/mobility/backends/active_record/query_methods.rb', line 22 def extended(relation) unless relation.methods(false).include?(:mobility_where_chain) relation.define_singleton_method(:mobility_where_chain) do @mobility_where_chain ||= Class.new(::ActiveRecord::QueryMethods::WhereChain) end relation.define_singleton_method :where do |opts = :chain, *rest| opts == :chain ? mobility_where_chain.new(spawn) : super(opts, *rest) end end end |