Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed May 6, 2018
1 parent 19f30ea commit f8153d0
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions lib/mobility/plugins/active_record/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,16 @@ def build(scope, where_opts, invert: false)
locale = Mobility.locale
opts = where_opts.with_indifferent_access

mods_map = extract_opts!(scope.mobility.modules, opts, invert: invert, locale: locale)
return yield if mods_map.empty?
maps = build_maps!(scope.mobility.modules, opts, invert: invert, locale: locale)
return yield if maps.empty?

base = opts.empty? ? scope : yield(opts)

mods_map.inject(base) { |rel, (mod, mod_opts, mod_clause)|
mod.backend_class
.add_translations(rel, mod_opts, locale: locale, invert: invert, **mod.options)
.where(mod_clause)
}
maps.inject(base) { |rel, map| map[rel] }
end

private

def extract_opts!(mods, opts, invert:, locale:)
def build_maps!(mods, opts, invert:, locale:)
keys = opts.keys.map(&:to_s)
mods.select { |mod| mod.options[:query] }.map { |mod|
next if (mod_keys = mod.names & keys).empty?
Expand All @@ -74,7 +69,11 @@ def extract_opts!(mods, opts, invert:, locale:)
build_predicate(mod, key, locale, opts.delete(key), invert: invert)
end

[mod, mod_opts, predicates.inject(&:and)]
->(rel) {
mod.backend_class.
add_translations(rel, mod_opts, locale: locale, invert: invert, **mod.options).
where(predicates.inject(&:and))
}
}.compact
end

Expand Down

0 comments on commit f8153d0

Please sign in to comment.