Skip to content

Commit 0c79418

Browse files
Automatically accept incoming changes which only differ in case
1 parent b6979e3 commit 0c79418

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

app/models/concerns/pending_changes_concern.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,32 @@ module PendingChangesConcern
66
included { attribute :pending_changes, :jsonb, default: {} }
77

88
def stage_changes(attributes)
9+
need_save = false
10+
911
new_pending_changes =
1012
attributes.each_with_object({}) do |(attr, new_value), staged_changes|
1113
current_value = public_send(attr)
12-
staged_changes[attr.to_s] = new_value if new_value != current_value
14+
15+
if normalise_for_comparison(new_value) ==
16+
normalise_for_comparison(current_value)
17+
if new_value != current_value
18+
public_send("#{attr}=", new_value)
19+
need_save = true
20+
end
21+
else
22+
staged_changes[attr.to_s] = new_value
23+
end
1324
end
1425

15-
if new_pending_changes.any?
26+
if new_pending_changes.any? || need_save
1627
update!(pending_changes: pending_changes.merge(new_pending_changes))
1728
end
1829
end
1930

31+
def normalise_for_comparison(value)
32+
value.respond_to?(:downcase) ? value.downcase : value
33+
end
34+
2035
def with_pending_changes
2136
return self if pending_changes.blank?
2237

0 commit comments

Comments
 (0)