Skip to content

Commit

Permalink
Disallow empty saved_changes in camel_trail (#307)
Browse files Browse the repository at this point in the history
Allowing `camel_trail` to record changes that are empty (`{}`) should
not be possible. If no changes are present, return. This prevents
useless records from being created.

---------

Co-authored-by: Carlos Palhares <[email protected]>
  • Loading branch information
thestephenmarshall and xjunior authored Feb 19, 2025
1 parent acb6c69 commit 1b6dd58
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/camel_trail/docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

## [1.1.0] - 2025-02-18

- Drop rails 6.0 support and adds Rails 7.0 to CI [#309](https://github.com/powerhome/power-tools/pull/309)
- Prevents creating records with empty `source_changes` [#307](https://github.com/powerhome/power-tools/pull/307)
- Support keyword arguments for serialization [#308](https://github.com/powerhome/power-tools/pull/308)
- Drop rails 6.0 support and adds Rails 7.0 to CI [#309](https://github.com/powerhome/power-tools/pull/309)

## [1.0.0] - 2024-02-28

Expand Down
3 changes: 3 additions & 0 deletions packages/camel_trail/lib/camel_trail/recordable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def history_options(source_changes: nil)
def __record_changes
activity = new_record? ? :created : :updated
yield

return if saved_changes.blank?

CamelTrail.record!(self, activity, __camel_trail_source_changes,
CamelTrail::Config.current_session_user_id&.call)
end
Expand Down
9 changes: 9 additions & 0 deletions packages/camel_trail/spec/camel_trail/recordable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,13 @@
# Changed negative price to 0
expect(last_truck_history.source_changes).to include("price" => [nil, 0])
end

it "does not save empty source_changes" do
allow_any_instance_of(Truck).to receive(:saved_changes).and_return({})

truck = Truck.create! name: "I am empty on the inside"
last_truck_history = CamelTrail.for(truck)

expect(last_truck_history).to eql []
end
end

0 comments on commit 1b6dd58

Please sign in to comment.