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

Inherits:
QueryMethods show all
Defined in:
lib/mobility/backend/active_record/table/query_methods.rb

Instance Method Summary collapse

Constructor Details

#initialize(attributes, **options) ⇒ QueryMethods

Returns a new instance of QueryMethods.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mobility/backend/active_record/table/query_methods.rb', line 4

def initialize(attributes, **options)
  super
  association_name     = options[:association_name]
  foreign_key          = options[:foreign_key]
  @association_name    = association_name
  attributes_extractor = @attributes_extractor
  translation_class    = options[:model_class].const_get(options[:subclass_name])
  @translation_class   = translation_class
  table_name           = options[:table_name]

  define_method :"join_#{association_name}" do |**options|
    return self if (@__mobility_table_joined || []).include?(table_name)
    (@__mobility_table_joined ||= []) << table_name
    t = translation_class.arel_table
    m = arel_table
    join_type = options[:outer_join] ? Arel::Nodes::OuterJoin : Arel::Nodes::InnerJoin
    joins(m.join(t, join_type).
          on(t[foreign_key].eq(m[:id]).
             and(t[:locale].eq(Mobility.locale))).join_sources)
  end

  # Note that Mobility will try to use inner/outer joins appropriate to the query,
  # so for example:
  #
  # Article.where(title: nil, content: nil)   #=> OUTER JOIN (all nils)
  # Article.where(title: "foo", content: nil) #=> INNER JOIN (one non-nil)
  #
  # In the first case, if we are in (say) the "en" locale, then we should match articles
  # that have *no* article_translations with English locales (since no translation is
  # equivalent to a nil value). If we used an inner join in the first case, an article
  # with no English translations would be filtered out, so we use an outer join.
  #
  # However, if you call `where` multiple times, you may end up with an outer join
  # when a (faster) inner join would have worked fine:
  #
  # Article.where(title: nil).where(content: "foo") #=> OUTER JOIN
  #
  # In this case, we are searching for a match on the article_translations table
  # which has a NULL title and a content equal to "foo". Since we need a positive
  # match for content, there must be an English translation on the article, thus
  # we can use an inner join. However, Mobility will use an outer join since we don't
  # want to modify the existing relation which has already been joined.
  #
  # To avoid this problem, simply make sure to either order your queries to place nil
  # values last, or include all queried attributes in a single `where`:
  #
  # Article.where(title: nil, content: "foo") #=> INNER JOIN
  #
  define_method :where! do |opts, *rest|
    if i18n_keys = attributes_extractor.call(opts)
      opts = opts.with_indifferent_access
      options = { outer_join: i18n_keys.all? { |attr| opts[attr].nil? } }
      i18n_keys.each { |attr| opts["#{translation_class.table_name}.#{attr}"] = opts.delete(attr) }
      super(opts, *rest).send("join_#{association_name}", options)
    else
      super(opts, *rest)
    end
  end

  attributes.each do |attribute|
    define_method :"find_by_#{attribute}" do |value|
      find_by(attribute.to_sym => value)
    end
  end
end

Instance Method Details

#extended(relation) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/mobility/backend/active_record/table/query_methods.rb', line 70

def extended(relation)
  super
  association_name     = @association_name
  attributes_extractor = @attributes_extractor
  translation_class    = @translation_class

  mod = Module.new do
    define_method :not do |opts, *rest|
      if i18n_keys = attributes_extractor.call(opts)
        opts = opts.with_indifferent_access
        i18n_keys.each { |attr| opts["#{translation_class.table_name}.#{attr}"] = opts.delete(attr) }
        super(opts, *rest).send("join_#{association_name}")
      else
        super(opts, *rest)
      end
    end
  end
  relation.model.mobility_where_chain.prepend(mod)
end