Skip to content

Commit 3c47da9

Browse files
committed
Bind parameters in where_object{,_changes}
[Fixes #696]
1 parent c97eb17 commit 3c47da9

1 file changed

Lines changed: 24 additions & 23 deletions

File tree

lib/paper_trail/version_concern.rb

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -110,48 +110,49 @@ def where_object(args = {})
110110
raise ArgumentError, 'expected to receive a Hash' unless args.is_a?(Hash)
111111

112112
if columns_hash['object'].type == :jsonb
113-
where_conditions = "object @> '#{args.to_json}'::jsonb"
113+
where("object @> ?", args.to_json)
114114
elsif columns_hash['object'].type == :json
115-
where_conditions = args.map do |field, value|
116-
"object->>'#{field}' = '#{value}'"
115+
predicates = []
116+
values = []
117+
args.each do |field, value|
118+
predicates.push "object->>? = ?"
119+
values.concat([field, value])
117120
end
118-
where_conditions = where_conditions.join(" AND ")
121+
sql = predicates.join(" and ")
122+
where(sql, *values)
119123
else
120124
arel_field = arel_table[:object]
121-
122-
where_conditions = args.map do |field, value|
125+
where_conditions = args.map { |field, value|
123126
PaperTrail.serializer.where_object_condition(arel_field, field, value)
124-
end.reduce do |condition1, condition2|
125-
condition1.and(condition2)
126-
end
127+
}.reduce { |a, e| a.and(e) }
128+
where(where_conditions)
127129
end
128-
129-
where(where_conditions)
130130
end
131131

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

135135
if columns_hash['object_changes'].type == :jsonb
136136
args.each { |field, value| args[field] = [value] }
137-
where_conditions = "object_changes @> '#{args.to_json}'::jsonb"
137+
where("object_changes @> ?", args.to_json)
138138
elsif columns_hash['object'].type == :json
139-
where_conditions = args.map do |field, value|
140-
"((object_changes->>'#{field}' ILIKE '[#{value.to_json},%') " +
141-
"OR (object_changes->>'#{field}' ILIKE '[%,#{value.to_json}]%'))"
139+
predicates = []
140+
values = []
141+
args.each do |field, value|
142+
predicates.push(
143+
"((object_changes->>? ILIKE '[?,%') OR (object_changes->>? ILIKE '[%,?]%'))"
144+
)
145+
values.concat([field, value, field, value])
142146
end
143-
where_conditions = where_conditions.join(" AND ")
147+
sql = predicates.join(" and ")
148+
where(sql, *values)
144149
else
145150
arel_field = arel_table[:object_changes]
146-
147-
where_conditions = args.map do |field, value|
151+
where_conditions = args.map { |field, value|
148152
PaperTrail.serializer.where_object_changes_condition(arel_field, field, value)
149-
end.reduce do |condition1, condition2|
150-
condition1.and(condition2)
151-
end
153+
}.reduce { |a, e| a.and(e) }
154+
where(where_conditions)
152155
end
153-
154-
where(where_conditions)
155156
end
156157

157158
def primary_key_is_int?

0 commit comments

Comments
 (0)