Class: Mobility::Backends::ActiveRecord::KeyValue::QueryMethods

Inherits:
QueryMethods
  • Object
show all
Defined in:
lib/mobility/backends/active_record/key_value/query_methods.rb

Instance Method Summary collapse

Constructor Details

#initialize(attributes, association_name: nil, class_name: nil) ⇒ QueryMethods

Returns a new instance of QueryMethods.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/mobility/backends/active_record/key_value/query_methods.rb', line 6

def initialize(attributes, association_name: nil, class_name: nil, **)
  super
  @association_name = association_name

  define_join_method(association_name, class_name)
  define_query_methods(association_name)

  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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mobility/backends/active_record/key_value/query_methods.rb', line 20

def extended(relation)
  super
  association_name     = @association_name
  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["#{attr}_#{association_name}"] = { value: opts.delete(attr) }}
        super(opts, *rest).send(:"join_#{association_name}", *i18n_keys)
      else
        super(opts, *rest)
      end
    end
  end
  relation.mobility_where_chain.include(mod)
end