Class: Mobility::Backend::Sequel::KeyValue::QueryMethods

Inherits:
QueryMethods show all
Defined in:
lib/mobility/backend/sequel/key_value/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
# File 'lib/mobility/backend/sequel/key_value/query_methods.rb', line 4

def initialize(attributes, **options)
  super
  attributes_extractor = @attributes_extractor
  association_name, translations_class = options[:association_name], options[:class_name]

  define_method :"join_#{association_name}" do |*attributes, **options|
    attributes.inject(self) do |relation, attribute|
      join_type = options[:outer_join] ? :left_outer : :inner
      relation.join_table(join_type,
                          translations_class.table_name,
                          {
                            key: attribute.to_s,
                            locale: Mobility.locale.to_s,
                            translatable_type: model.name,
                            translatable_id: ::Sequel[:"#{model.table_name}"][:id]
                          },
                          table_alias: "#{attribute}_#{association_name}")
    end
  end

  # TODO: find a better way to do this that doesn't involve overriding
  # a private method...
  define_method :_filter_or_exclude do |invert, clause, *cond, &block|
    if i18n_keys = attributes_extractor.call(cond.first)
      cond = cond.first.dup
      i18n_nulls = i18n_keys.select { |key| cond[key].nil? }
      i18n_keys.each { |attr| cond[::Sequel[:"#{attr}_#{association_name}"][:value]] = cond.delete(attr) }
      super(invert, clause, cond, &block).
        send("join_#{association_name}", *(i18n_keys - i18n_nulls)).
        send("join_#{association_name}", *i18n_nulls, outer_join: true)
    else
      super(invert, clause, *cond, &block)
    end
  end
  private :_filter_or_exclude

  attributes.each do |attribute|
    define_method :"first_by_#{attribute}" do |value|
      where(attribute => value).select_all(model.table_name).first
    end
  end
end