Skip to content

Commit

Permalink
Refactor to use only arel queries in pg jsonb + hstore query methods
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed Feb 23, 2017
1 parent 7586082 commit 5e6addd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
30 changes: 18 additions & 12 deletions lib/mobility/backend/active_record/hstore/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@ def initialize(attributes, **options)

define_method :where! do |opts, *rest|
if i18n_keys = attributes_extractor.call(opts)
locale = Mobility.locale
m = arel_table
locale = Arel::Nodes.build_quoted(Mobility.locale.to_s)
opts = opts.with_indifferent_access
infix = Arel::Nodes::InfixOperation

result = i18n_keys.inject(all) do |scope, attr|
i18n_query = i18n_keys.inject(nil) { |ops, attr|
column = m[attr.to_sym]
value = opts.delete(attr)
if value.nil?
scope.where.not("#{table_name}.#{attr} ? '#{locale}'")
else
scope.where!("#{table_name}.#{attr} @> hstore('#{locale}', ?)", value.to_s)
end
end
result = result.where!(opts, *rest) if opts.present?
result

op =
if value.nil?
infix.new(:'?', column, locale).not
else
infix.new(:'->', m[attr.to_sym], locale).eq(value)
end
ops ? ops.and(op) : op
}

opts.empty? ? where(i18n_query) : super(opts, *rest).where(i18n_query)
else
super(opts, *rest)
end
Expand All @@ -38,7 +44,7 @@ def extended(relation)
opts = opts.with_indifferent_access
infix = Arel::Nodes::InfixOperation

query = i18n_keys.inject(nil) { |ops, attr|
i18n_query = i18n_keys.inject(nil) { |ops, attr|
column = m[attr.to_sym]
value = Arel::Nodes.build_quoted(opts.delete(attr).to_s)
has_key = infix.new(:'?', column, locale)
Expand All @@ -47,7 +53,7 @@ def extended(relation)
ops ? ops.and(op) : op
}

super(opts, *rest).where(query)
super(opts, *rest).where(i18n_query)
else
super(opts, *rest)
end
Expand Down
31 changes: 19 additions & 12 deletions lib/mobility/backend/active_record/jsonb/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,26 @@ def initialize(attributes, **options)

define_method :where! do |opts, *rest|
if i18n_keys = attributes_extractor.call(opts)
locale = Mobility.locale
m = arel_table
locale = Arel::Nodes.build_quoted(Mobility.locale.to_s)
opts = opts.with_indifferent_access
infix = Arel::Nodes::InfixOperation

result = i18n_keys.inject(all) do |scope, attr|
i18n_query = i18n_keys.inject(nil) { |ops, attr|
column = m[attr.to_sym]
value = opts.delete(attr)
if value.nil?
scope.where.not("#{table_name}.#{attr} ? :locale", locale: locale)
else
scope.where!("#{table_name}.#{attr} @> (?)::jsonb", { locale => value }.to_json)
end
end
result = result.where!(opts, *rest) if opts.present?
result

op =
if value.nil?
infix.new(:'?', column, locale).not
else
predicate = Arel::Nodes.build_quoted({ Mobility.locale => value }.to_json)
infix.new(:'@>', m[attr.to_sym], predicate)
end
ops ? ops.and(op) : op
}

opts.empty? ? where(i18n_query) : super(opts, *rest).where(i18n_query)
else
super(opts, *rest)
end
Expand All @@ -38,7 +45,7 @@ def extended(relation)
opts = opts.with_indifferent_access
infix = Arel::Nodes::InfixOperation

query = i18n_keys.inject(nil) { |ops, attr|
i18n_query = i18n_keys.inject(nil) { |ops, attr|
column = m[attr.to_sym]
has_key = infix.new(:'?', column, locale)
predicate = Arel::Nodes.build_quoted({ Mobility.locale => opts.delete(attr) }.to_json)
Expand All @@ -47,7 +54,7 @@ def extended(relation)
ops ? ops.and(op) : op
}

super(opts, *rest).where(query)
super(opts, *rest).where(i18n_query)
else
super(opts, *rest)
end
Expand Down

0 comments on commit 5e6addd

Please sign in to comment.