lib/mobility/plugins/sequel/query.rb in mobility-0.8.13 vs lib/mobility/plugins/sequel/query.rb in mobility-1.0.0.alpha
- old
+ new
@@ -6,15 +6,22 @@
See ActiveRecord::Query plugin.
=end
module Sequel
module Query
- class << self
- def apply(attributes)
- attributes.model_class.class_eval do
+ extend Plugin
+
+ requires :query, include: false
+
+ included_hook do |klass, _|
+ plugin = self
+ if options[:query]
+ raise MissingBackend, "backend required for Query plugin" unless backend_class
+
+ klass.class_eval do
extend QueryMethod
- singleton_class.send :alias_method, Mobility.query_method, :__mobility_query_dataset__
+ singleton_class.send :alias_method, plugin.query_method, :__mobility_query_dataset__
end
end
end
module QueryMethod
@@ -93,11 +100,11 @@
module QueryBuilder
IDENTITY = ->(x) { x }.freeze
class << self
def build(dataset, query_method, query_conds, &block)
- return yield unless Hash === query_conds.first
+ return yield unless ::Hash === query_conds.first
cond = query_conds.first.dup
locale = cond.delete(:locale) || Mobility.locale
_build(dataset, cond, locale, query_method, &block)
@@ -107,11 +114,11 @@
def _build(dataset, cond, locale, query_method)
keys, predicates = cond.keys, []
model = dataset.model
- query_map = model.mobility_modules.inject(IDENTITY) do |qm, mod|
+ query_map = attribute_modules(model).inject(IDENTITY) do |qm, mod|
i18n_keys = mod.names.map(&:to_sym) & keys
next qm if i18n_keys.empty?
mod_predicates = i18n_keys.map do |key|
build_predicate(dataset.backend_op(key, locale), cond.delete(key))
@@ -125,16 +132,24 @@
predicates = ::Sequel.&(*predicates, cond) unless cond.empty?
query_map[dataset.public_send(query_method, ::Sequel.&(*predicates))]
end
+ def attribute_modules(model)
+ model.ancestors.grep(::Mobility::Translations)
+ end
+
def build_predicate(op, values)
vals = values.is_a?(Array) ? values.uniq: [values]
vals = vals.first if vals.size == 1
op =~ vals
end
end
end
end
+
+ class MissingBackend < Mobility::Error; end
end
+
+ register_plugin(:sequel_query, Sequel::Query)
end
end