Skip to content

Commit

Permalink
Bind parameters in where_object{,_changes}
Browse files Browse the repository at this point in the history
[Fixes #696]
  • Loading branch information
jaredbeck committed Jan 18, 2016
1 parent c97eb17 commit 3c47da9
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions lib/paper_trail/version_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,48 +110,49 @@ def where_object(args = {})
raise ArgumentError, 'expected to receive a Hash' unless args.is_a?(Hash)

if columns_hash['object'].type == :jsonb
where_conditions = "object @> '#{args.to_json}'::jsonb"
where("object @> ?", args.to_json)
elsif columns_hash['object'].type == :json
where_conditions = args.map do |field, value|
"object->>'#{field}' = '#{value}'"
predicates = []
values = []
args.each do |field, value|
predicates.push "object->>? = ?"
values.concat([field, value])
end
where_conditions = where_conditions.join(" AND ")
sql = predicates.join(" and ")
where(sql, *values)
else
arel_field = arel_table[:object]

where_conditions = args.map do |field, value|
where_conditions = args.map { |field, value|
PaperTrail.serializer.where_object_condition(arel_field, field, value)
end.reduce do |condition1, condition2|
condition1.and(condition2)
end
}.reduce { |a, e| a.and(e) }
where(where_conditions)
end

where(where_conditions)
end

def where_object_changes(args = {})
raise ArgumentError, 'expected to receive a Hash' unless args.is_a?(Hash)

if columns_hash['object_changes'].type == :jsonb
args.each { |field, value| args[field] = [value] }
where_conditions = "object_changes @> '#{args.to_json}'::jsonb"
where("object_changes @> ?", args.to_json)
elsif columns_hash['object'].type == :json
where_conditions = args.map do |field, value|
"((object_changes->>'#{field}' ILIKE '[#{value.to_json},%') " +
"OR (object_changes->>'#{field}' ILIKE '[%,#{value.to_json}]%'))"
predicates = []
values = []
args.each do |field, value|
predicates.push(
"((object_changes->>? ILIKE '[?,%') OR (object_changes->>? ILIKE '[%,?]%'))"
)
values.concat([field, value, field, value])
end
where_conditions = where_conditions.join(" AND ")
sql = predicates.join(" and ")
where(sql, *values)
else
arel_field = arel_table[:object_changes]

where_conditions = args.map do |field, value|
where_conditions = args.map { |field, value|
PaperTrail.serializer.where_object_changes_condition(arel_field, field, value)
end.reduce do |condition1, condition2|
condition1.and(condition2)
end
}.reduce { |a, e| a.and(e) }
where(where_conditions)
end

where(where_conditions)
end

def primary_key_is_int?
Expand Down

0 comments on commit 3c47da9

Please sign in to comment.