Class: Mobility::Backend::ActiveRecord::QueryMethods

Inherits:
Module
  • Object
show all
Defined in:
lib/mobility/backend/active_record/query_methods.rb

Overview

Defines query method overrides to handle translated attributes for ActiveRecord models. For details see backend-specific subclasses.

Instance Method Summary collapse

Constructor Details

#initialize(attributes, **options) ⇒ QueryMethods

Returns a new instance of QueryMethods.

Parameters:

  • attributes (Array<String>)

    Translated attributes

  • options (Hash)

    Backend options



13
14
15
16
17
18
# File 'lib/mobility/backend/active_record/query_methods.rb', line 13

def initialize(attributes, **options)
  @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

Parameters:

  • relation (ActiveRecord::Relation)

    Relation being extended



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mobility/backend/active_record/query_methods.rb', line 21

def extended(relation)
  model_class = relation.model
  unless model_class.respond_to?(:mobility_where_chain)
    model_class.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