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

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

Instance Method Summary collapse

Instance Method Details

#backend_node(name, locale = Mobility.locale) ⇒ Arel::Node

Return backend node for attribute name.

Parameters:

  • name (Symbol, String)

    Name of attribute

  • locale (Symbol) (defaults to: Mobility.locale)

    Locale

Returns:

  • (Arel::Node)

    Arel node for this attribute in given locale



191
192
193
# File 'lib/mobility/plugins/active_record/query.rb', line 191

def backend_node(name, locale = Mobility.locale)
  @klass.mobility_backend_class(name)[name, locale]
end

#order(opts, *rest) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/mobility/plugins/active_record/query.rb', line 139

def order(opts, *rest)
  return super unless @klass.respond_to?(:mobility_attribute?)

  case opts
  when Symbol, String
    @klass.mobility_attribute?(opts) ? order({ opts => :asc }, *rest) : super
  when ::Hash
    i18n_keys, keys = opts.keys.partition(&@klass.method(:mobility_attribute?))
    return super if i18n_keys.empty?

    base = keys.empty? ? self : super(opts.slice(keys))

    i18n_keys.inject(base) do |query, key|
      backend_class = @klass.mobility_backend_class(key)
      dir, node = opts[key], backend_node(key)
      backend_class.apply_scope(query, node).order(node.send(dir.downcase))
    end
  else
    super
  end
end

#where(opts = :chain, *rest) ⇒ Object



135
136
137
# File 'lib/mobility/plugins/active_record/query.rb', line 135

def where(opts = :chain, *rest)
  opts == :chain ? WhereChain.new(spawn) : super
end

#where!(opts, *rest) ⇒ Object



129
130
131
132
133
# File 'lib/mobility/plugins/active_record/query.rb', line 129

def where!(opts, *rest)
  QueryBuilder.build(self, opts) do |untranslated_opts|
    untranslated_opts ? super(untranslated_opts, *rest) : super
  end
end