Skip to content

Commit

Permalink
Replace QueryMethods with nodes and Query plugin
Browse files Browse the repository at this point in the history
I've left this as one giant commit, because to split it is more work
that it is worth. This change replaces module builders for each backend
with a single plugin which delegates to the methods build_node (which
returns an Arel node) and (optionally) add_translations (which modifies
an AR::Relation) on the backend class.

Backend classes now need only to implement these one or two methods and
querying is supported without any extra work.
  • Loading branch information
shioyama committed May 8, 2018
1 parent fcb732a commit d3ca0db
Show file tree
Hide file tree
Showing 28 changed files with 372 additions and 675 deletions.
12 changes: 0 additions & 12 deletions lib/mobility/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,12 @@ module ActiveRecord

def self.included(model_class)
model_class.class_eval do
extend QueryMethod.new(Mobility.query_method)
unless const_defined?(:UniquenessValidator)
const_set(:UniquenessValidator,
Class.new(::Mobility::ActiveRecord::UniquenessValidator))
end
delegate :translated_attribute_names, to: :class
end
end

class QueryMethod < Module
def initialize(query_method)
module_eval <<-EOM, __FILE__, __LINE__ + 1
def #{query_method}
all
end
EOM
end
end
private_constant :QueryMethod
end
end
34 changes: 23 additions & 11 deletions lib/mobility/backends/active_record.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
module Mobility
module Backends
module ActiveRecord
def setup_query_methods(query_methods)
setup do |attributes, options|
extend(Module.new do
define_method ::Mobility.query_method do
super().extending(query_methods.new(attributes, options))
end
end)
end
end

def self.included(backend_class)
backend_class.include(Backend)
backend_class.extend(self)
backend_class.extend(ClassMethods)
end

module ClassMethods
# @param [String] _attr Attribute name
# @param [Symbol] _locale Locale
def build_node(_attr, _locale)
raise NotImplementedError
end

# @param [ActiveRecord::Relation] relation Relation to scope
# @param [Symbol] locale Locale
# @option [Boolean] invert
# @return [ActiveRecord::Relation] Relation with scope added
def add_translations(relation, _opts, _locale, invert: false)
relation
end

private

def build_quoted(value)
::Arel::Nodes.build_quoted(value)
end
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/mobility/backends/active_record/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ class ActiveRecord::Column
include ActiveRecord
include Column

require 'mobility/backends/active_record/column/query_methods'

# @!group Backend Accessors
# @!macro backend_reader
def read(locale, _ = {})
Expand All @@ -53,7 +51,9 @@ def each_locale
available_locales.each { |l| yield(l) if present?(l) }
end

setup_query_methods(QueryMethods)
def self.build_node(attr, locale)
model_class.arel_table[Column.column_name_for(attr, locale)]
end

private

Expand Down
42 changes: 0 additions & 42 deletions lib/mobility/backends/active_record/column/query_methods.rb

This file was deleted.

29 changes: 16 additions & 13 deletions lib/mobility/backends/active_record/container.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true
require "mobility/backends/active_record"
require "mobility/arel/nodes/pg_ops"

module Mobility
module Backends
Expand All @@ -11,9 +12,6 @@ module Backends
class ActiveRecord::Container
include ActiveRecord

require 'mobility/backends/active_record/container/json_query_methods'
require 'mobility/backends/active_record/container/jsonb_query_methods'

# @!method column_name
# Returns name of json or jsonb column used to store translations
# @return [Symbol] (:translations) Name of translations column
Expand Down Expand Up @@ -59,16 +57,28 @@ def self.configure(options)
end
# @!endgroup

# @param [String] attr Attribute name
# @param [Symbol] locale Locale
def self.build_node(attr, locale)
column = model_class.arel_table[column_name]
quoted_locale = build_quoted(locale)
quoted_attr = build_quoted(attr)
case column_type
when :json
Arel::Nodes::Json.new(Arel::Nodes::JsonDashArrow.new(column, quoted_locale), quoted_attr)
when :jsonb
Arel::Nodes::Jsonb.new(Arel::Nodes::Jsonb.new(column, quoted_locale), quoted_attr)
end
end

# @!macro backend_iterator
def each_locale
model[column_name].each do |l, v|
yield l.to_sym if v.present?
end
end

backend_class = self

setup do |attributes, options|
setup do |_attributes, options|
store options[:column_name], coder: Coder

# Fix for duping depth-2 jsonb column in AR < 5.0
Expand All @@ -87,13 +97,6 @@ def initialize_dup(source)
include const_set(module_name, dupable)
end
end

query_methods = backend_class.const_get("#{options[:column_type].capitalize}QueryMethods")
extend(Module.new do
define_method ::Mobility.query_method do
super().extending(query_methods.new(attributes, options))
end
end)
end

private
Expand Down

This file was deleted.

This file was deleted.

10 changes: 7 additions & 3 deletions lib/mobility/backends/active_record/hstore.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'mobility/backends/active_record/pg_hash'
require 'mobility/arel/nodes/pg_ops'

module Mobility
module Backends
Expand All @@ -11,8 +12,6 @@ module Backends
=end
module ActiveRecord
class Hstore < PgHash
require 'mobility/backends/active_record/hstore/query_methods'

# @!group Backend Accessors
# @!macro backend_reader
# @!method read(locale, options = {})
Expand All @@ -23,7 +22,12 @@ def write(locale, value, options = {})
end
# @!endgroup

setup_query_methods(QueryMethods)
# @param [String] attr Attribute name
# @param [Symbol] locale Locale
def self.build_node(attr, locale)
column_name = column_affix % attr
Arel::Nodes::Hstore.new(model_class.arel_table[column_name], build_quoted(locale))
end
end
end
end
Expand Down
25 changes: 0 additions & 25 deletions lib/mobility/backends/active_record/hstore/query_methods.rb

This file was deleted.

10 changes: 7 additions & 3 deletions lib/mobility/backends/active_record/json.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'mobility/backends/active_record/pg_hash'
require 'mobility/arel/nodes/pg_ops'

module Mobility
module Backends
Expand All @@ -11,8 +12,6 @@ module Backends
=end
module ActiveRecord
class Json < PgHash
require 'mobility/backends/active_record/json/query_methods'

# @!group Backend Accessors
#
# @!method read(locale, **options)
Expand All @@ -31,7 +30,12 @@ class Json < PgHash
# @return [String,Integer,Boolean] Updated value
# @!endgroup

setup_query_methods(QueryMethods)
# @param [String] attr Attribute name
# @param [Symbol] locale Locale
def self.build_node(attr, locale)
column_name = column_affix % attr
Arel::Nodes::Json.new(model_class.arel_table[column_name], build_quoted(locale))
end
end
end
end
Expand Down
30 changes: 0 additions & 30 deletions lib/mobility/backends/active_record/json/query_methods.rb

This file was deleted.

Loading

0 comments on commit d3ca0db

Please sign in to comment.