Class: Mobility::Backend::ActiveRecord::Column::QueryMethods

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

def initialize(attributes, **options)
  super
  attributes_extractor = @attributes_extractor

  define_method :where! do |opts, *rest|
    if i18n_keys = attributes_extractor.call(opts)
      opts = opts.with_indifferent_access
      i18n_keys.each { |attr| opts[Column.column_name_for(attr)] = opts.delete(attr) }
    end
    super(opts, *rest)
  end

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

Instance Method Details

#extended(relation) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mobility/backend/active_record/column/query_methods.rb', line 23

def extended(relation)
  super
  attributes_extractor = @attributes_extractor

  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[Column.column_name_for(attr)] = opts.delete(attr) }
      end
      super(opts, *rest)
    end
  end
  relation.model.mobility_where_chain.prepend(mod)
end