Skip to content

Commit

Permalink
Refactor AR Column backend QueryMethods class
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed Feb 26, 2017
1 parent 991f2f6 commit 8df2bbd
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions lib/mobility/backend/active_record/column/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ class ActiveRecord::Column::QueryMethods < Backend::ActiveRecord::QueryMethods
def initialize(attributes, **options)
super
attributes_extractor = @attributes_extractor

define_method :where! do |opts, *rest|
@opts_converter = opts_converter = lambda do |opts|
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)
return opts
end

define_method :where! do |opts, *rest|
super(opts_converter.call(opts), *rest)
end

attributes.each do |attribute|
Expand All @@ -22,15 +25,11 @@ def initialize(attributes, **options)

def extended(relation)
super
attributes_extractor = @attributes_extractor
opts_converter = @opts_converter

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)
super(opts_converter.call(opts), *rest)
end
end
relation.model.mobility_where_chain.prepend(mod)
Expand Down

0 comments on commit 8df2bbd

Please sign in to comment.