Module: Globalize::ActiveRecord::QueryMethods

Defined in:
lib/globalize/active_record/query_methods.rb

Defined Under Namespace

Classes: WhereChain

Instance Method Summary collapse

Instance Method Details

#exists?(conditions = :none) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
# File 'lib/globalize/active_record/query_methods.rb', line 25

def exists?(conditions = :none)
  if parsed = parse_translated_conditions(conditions)
    with_translations_in_fallbacks.exists?(parsed)
  else
    super
  end
end

#join_translations(relation = self) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/globalize/active_record/query_methods.rb', line 59

def join_translations(relation = self)
  if relation.joins_values.include?(:translations)
    relation
  else
    relation.with_translations_in_fallbacks
  end
end

#parse_translated_conditions(opts) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/globalize/active_record/query_methods.rb', line 37

def parse_translated_conditions(opts)
  if opts.is_a?(Hash) && respond_to?(:translated_attribute_names) && (keys = opts.symbolize_keys.keys & translated_attribute_names).present?
    opts = opts.dup
    keys.each { |key| opts[translated_column_name(key)] = opts.delete(key) || opts.delete(key.to_s) }
    opts
  end
end

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



15
16
17
18
19
20
21
22
23
# File 'lib/globalize/active_record/query_methods.rb', line 15

def where(opts = :chain, *rest)
  if opts == :chain
    WhereChain.new(spawn)
  elsif parsed = parse_translated_conditions(opts)
    join_translations(super(parsed, *rest))
  else
    super
  end
end

#where_values_hash(*args) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/globalize/active_record/query_methods.rb', line 45

def where_values_hash(*args)
  equalities = respond_to?(:with_default_scope) ? with_default_scope.where_values : where_values
  equalities = equalities.grep(Arel::Nodes::Equality).find_all { |node|
    node.left.relation.name == translations_table_name
  }

  binds = Hash[bind_values.find_all(&:first).map { |column, v| [column.name, v] }]

  super.merge(Hash[equalities.map { |where|
    name = where.left.name
    [name, binds.fetch(name.to_s) { where.right }]
  }])
end

#with_translations_in_fallbacksObject



33
34
35
# File 'lib/globalize/active_record/query_methods.rb', line 33

def with_translations_in_fallbacks
  with_translations(Globalize.fallbacks)
end