Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor find_by for translated attributes #160

Merged
merged 1 commit into from
Jan 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions lib/mobility/backends/active_record/column/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ def initialize(attributes, _)
define_method :where! do |opts, *rest|
super(q.convert_opts(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

def extended(relation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ def initialize(attributes, association_name: nil, class_name: nil, **)

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

def extended(relation)
Expand Down
6 changes: 6 additions & 0 deletions lib/mobility/backends/active_record/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ class QueryMethods < Module
# @param [Array<String>] attributes Translated attributes
def initialize(attributes, _)
@attributes = attributes

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

# @param [ActiveRecord::Relation] relation Relation being extended
Expand Down
6 changes: 0 additions & 6 deletions lib/mobility/backends/active_record/table/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ def initialize(attributes, association_name: nil, model_class: nil, subclass_nam

define_join_method(association_name, translation_class, **options)
define_query_methods(association_name, translation_class, **options)

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

def extended(relation)
Expand Down
6 changes: 0 additions & 6 deletions lib/mobility/backends/sequel/column/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ def initialize(attributes, _)
end
end
end

attributes.each do |attribute|
define_method :"first_by_#{attribute}" do |value|
where(attribute.to_sym => value).first
end
end
end
end
end
Expand Down
10 changes: 1 addition & 9 deletions lib/mobility/backends/sequel/container/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,8 @@ class Sequel::Container::QueryMethods < Sequel::QueryMethods

def initialize(attributes, options)
super
column_name = @column_name = options[:column_name]

@column_name = options[:column_name]
define_query_methods

attributes.each do |attribute|
define_method :"first_by_#{attribute}" do |value|
where(::Sequel.pg_jsonb_op(column_name)[Mobility.locale.to_s].contains({ attribute => value })).
select_all(model.table_name).first
end
end
end

private
Expand Down
15 changes: 1 addition & 14 deletions lib/mobility/backends/sequel/hstore/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,10 @@ module Backends
class Sequel::Hstore::QueryMethods < Sequel::QueryMethods
include Sequel::PgQueryMethods

def initialize(attributes, _)
super

define_query_methods

attributes.each do |attribute|
define_method :"first_by_#{attribute}" do |value|
where(::Sequel.hstore(attribute.to_sym).contains(::Sequel.hstore({ Mobility.locale.to_s => value }))).
select_all(model.table_name).first
end
end
end

private

def matches(key, value, locale)
build_op(key).contains(locale => value.to_s)
build_op(key)[locale] =~ value.to_s
end

def has_locale(key, locale)
Expand Down
13 changes: 0 additions & 13 deletions lib/mobility/backends/sequel/jsonb/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,6 @@ module Backends
class Sequel::Jsonb::QueryMethods < Sequel::QueryMethods
include Sequel::PgQueryMethods

def initialize(attributes, _)
super

define_query_methods

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

private

def matches(key, value, locale)
Expand Down
6 changes: 0 additions & 6 deletions lib/mobility/backends/sequel/key_value/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ def initialize(attributes, association_name: nil, class_name: nil, **)

define_join_method(association_name, class_name)
define_query_methods(association_name)

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

private
Expand Down
4 changes: 4 additions & 0 deletions lib/mobility/backends/sequel/pg_query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ module Sequel

=end
module PgQueryMethods
def initialize(attributes, _)
super
define_query_methods
end

# Create query for conditions and translated keys
# @note This is a destructive action, it will alter +cond+.
Expand Down
6 changes: 6 additions & 0 deletions lib/mobility/backends/sequel/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ class QueryMethods < Module
# @param [Array<String>] attributes Translated attributes
def initialize(attributes, _)
@attributes = attributes.map!(&:to_sym)

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

def extract_attributes(cond)
Expand Down
6 changes: 0 additions & 6 deletions lib/mobility/backends/sequel/table/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ def initialize(attributes, association_name: nil, model_class: nil, subclass_nam

define_join_method(association_name, translation_class, **options)
define_query_methods(association_name, translation_class, **options)

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

def define_join_method(association_name, translation_class, table_name: nil, foreign_key: nil, **)
Expand Down