Module: Mobility::Plugins::ActiveRecord::Query

Defined in:
lib/mobility/plugins/active_record/query.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.apply(attributes) ⇒ Object


19
20
21
22
23
24
25
26
# File 'lib/mobility/plugins/active_record/query.rb', line 19

def apply(attributes)
  attributes.model_class.class_eval do
    extend QueryMethod
    extend FindByMethods.new(*attributes.names)
    singleton_class.send :alias_method, Mobility.query_method, :__mobility_query_scope__
  end
  attributes.backend_class.include self
end

.attribute_alias(attribute, locale = Mobility.locale) ⇒ Object


28
29
30
# File 'lib/mobility/plugins/active_record/query.rb', line 28

def attribute_alias(attribute, locale = Mobility.locale)
  "__mobility_%s_%s__"  % [attribute, ::Mobility.normalize_locale(locale)]
end

Instance Method Details

#read(locale) ⇒ Object

Note:

We use instance_variable_get here to get the AttributeSet rather than the hash of attributes. Getting the full hash of attributes is a performance hit and better to avoid if unnecessary.

TODO: Improve this.


37
38
39
40
41
42
43
44
# File 'lib/mobility/plugins/active_record/query.rb', line 37

def read(locale, **)
  if (model_attributes_defined? &&
      model_attributes.key?(alias_ = Query.attribute_alias(attribute, locale)))
    model_attributes[alias_].value
  else
    super
  end
end