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



213
214
215
# File 'lib/mobility/plugins/active_record/query.rb', line 213

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

#order(opts, *rest) ⇒ Object



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

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

#select_for_countObject



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/mobility/plugins/active_record/query.rb', line 191

def select_for_count
  return super unless klass.respond_to?(:mobility_attribute?)

  if select_values.any? { |value| value.right.start_with?(ATTRIBUTE_ALIAS_PREFIX) }
    filtered_select_values = select_values.map do |value|
      value.right.start_with?(ATTRIBUTE_ALIAS_PREFIX) ? value.left : value
    end

    # Copied from lib/active_record/relation/calculations.rb
    with_connection do |conn|
      arel_columns(filtered_select_values).map { |column| conn.visitor.compile(column) }.join(", ")
    end
  else
    super
  end
end

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



138
139
140
# File 'lib/mobility/plugins/active_record/query.rb', line 138

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

#where!(opts, *rest) ⇒ Object



132
133
134
135
136
# File 'lib/mobility/plugins/active_record/query.rb', line 132

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