Skip to content

Commit

Permalink
Add internal __mobility_query_scope__ method
Browse files Browse the repository at this point in the history
Using send(Mobility.query_method) everywhere is clumsy for gem code, so
instead of defining the method dynamically, we can define it as
__mobility_query_scope__ and alias Mobility.query_method to this method.
  • Loading branch information
shioyama committed May 29, 2018
1 parent 1500871 commit 60d562b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/mobility/active_record/uniqueness_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def validate_each(record, attribute, value)

if (([*options[:scope]] + [attribute]).map(&:to_s) & klass.mobility_attributes).present?
return unless value.present?
relation = klass.send(Mobility.query_method) do |m|
relation = klass.__mobility_query_scope__ do |m|
node = m.__send__(attribute)
options[:case_sensitive] == false ? node.lower.eq(value.downcase) : node.eq(value)
end
Expand All @@ -50,7 +50,7 @@ def validate_each(record, attribute, value)

def mobility_scope_relation(record, relation)
[*options[:scope]].inject(relation) do |scoped_relation, scope_item|
scoped_relation.send(Mobility.query_method) do |m|
scoped_relation.__mobility_query_scope__ do |m|
m.__send__(scope_item).eq(record.send(scope_item))
end
end
Expand Down
29 changes: 14 additions & 15 deletions lib/mobility/plugins/active_record/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,21 @@ class << self
def apply(attributes)
model_class = attributes.model_class

unless const_defined?(:QueryMethod)
const_set :QueryMethod, Module.new
QueryMethod.module_eval <<-EOM, __FILE__, __LINE__ + 1
def #{Mobility.query_method}(locale: Mobility.locale, &block)
if block_given?
VirtualRow.build_query(self, locale, &block)
else
all.extending(QueryExtension)
end
end
EOM
private_constant :QueryMethod
model_class.class_eval do
extend QueryMethod
extend FindByMethods.new(*attributes.names)
singleton_class.alias_method Mobility.query_method, :__mobility_query_scope__
end
end
end

model_class.extend QueryMethod
model_class.extend FindByMethods.new(*attributes.names)
module QueryMethod
def __mobility_query_scope__(locale: Mobility.locale, &block)
if block_given?
VirtualRow.build_query(self, locale, &block)
else
all.extending(QueryExtension)
end
end
end

Expand Down Expand Up @@ -79,7 +78,7 @@ def apply_scopes(scope, backends, locale, predicates)
end
end
end
private_constant :VirtualRow
private_constant :QueryMethod, :VirtualRow

module QueryExtension
def where!(opts, *rest)
Expand Down
15 changes: 15 additions & 0 deletions spec/mobility/plugins/active_record/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@
end
end

describe "query method" do
# NOTE: __mobility_query_scope__ is a public method for convenience, but is
# intended for internal use. For application code, use i18n or whatever you
# define in Mobility.query_method instead.
it "creates a __mobility_query_scope method" do
stub_const 'Article', Class.new(ActiveRecord::Base)
Article.class_eval do
extend Mobility
translates :title, backend: :table
end
article = Article.create(title: "foo")
expect(Article.__mobility_query_scope__.first).to eq(article)
end
end

describe "virtual row handling" do
before do
stub_const 'Article', Class.new(ActiveRecord::Base)
Expand Down

0 comments on commit 60d562b

Please sign in to comment.