Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate versioning into AR touch method #1063

Merged
merged 3 commits into from
Mar 16, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/paper_trail/model_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ def on_update
@model_class.paper_trail_options[:on] << :update
end

# Adds a callback that records a version after a "touch" event.
# @api public
def on_touch
@model_class.after_touch { |r|
r.paper_trail.record_update(force: true, in_after_callback: true)
}
end

# Set up `@model_class` for PaperTrail. Installs callbacks, associations,
# "class attributes", instance methods, and more.
# @api private
Expand Down Expand Up @@ -196,6 +204,8 @@ def setup_callbacks_from_options(options_on = [])
options_on.each do |event|
public_send("on_#{event}")
end

public_send("on_touch")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can just call on_touch here, right? The public_send is not necessary.

end

def setup_habtm_change_callbacks(assoc)
Expand Down
8 changes: 8 additions & 0 deletions spec/models/widget_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,14 @@
end
end

describe "touch", versioning: true do
it "creates a version" do
expect { widget.touch }.to change {
widget.versions.count
}.by(+1)
end
end

describe ".paper_trail.update_columns", versioning: true do
it "creates a version record" do
widget = Widget.create
Expand Down
6 changes: 3 additions & 3 deletions spec/paper_trail/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@
context "and then updated without any changes" do
before { @widget.touch }

it "not have a new version" do
expect(@widget.versions.length).to(eq(1))
it "to have two previous versions" do
expect(@widget.versions.length).to(eq(2))
end
end

context "and then updated with changes" do
before { @widget.update_attributes(name: "Harry") }

it "have two previous versions" do
it "have three previous versions" do
expect(@widget.versions.length).to(eq(2))
end

Expand Down