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

Support for belongs_to whodunnit #1481

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 1 addition & 10 deletions lib/paper_trail/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def enabled_for_model?(model)
def with(options)
return unless block_given?
validate_public_options(options)
before = to_h
before = store.dup
merge(options)
yield
ensure
Expand Down Expand Up @@ -132,15 +132,6 @@ def store
}
end

# Returns a deep copy of the internal hash from our RequestStore. Keys are
# all symbols. Values are mostly primitives, but whodunnit can be a Proc.
# We cannot use Marshal.dump here because it doesn't support Proc. It is
# unclear exactly how `deep_dup` handles a Proc, but it doesn't complain.
# @api private
def to_h
store.deep_dup
end

# Provide a helpful error message if someone has a typo in one of their
# option keys. We don't validate option values here. That's traditionally
# been handled with casting (`to_s`, `!!`) in the accessor method.
Expand Down
5 changes: 5 additions & 0 deletions spec/dummy_app/app/models/planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class Planet < ApplicationRecord
has_paper_trail versions: { class_name: "PlanetVersion" }
end
4 changes: 4 additions & 0 deletions spec/dummy_app/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class User < ApplicationRecord
end
7 changes: 7 additions & 0 deletions spec/dummy_app/app/versions/planet_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class PlanetVersion < PaperTrail::Version
self.table_name = :planet_versions

belongs_to :whodunnit, class_name: "User", optional: true
end
19 changes: 19 additions & 0 deletions spec/dummy_app/db/migrate/20110208155312_set_up_test_tables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ def up
t.timestamps null: true, limit: 6
end

create_table :users, force: true do |t|
t.string :name, null: false
t.timestamps null: true, limit: 6
end

create_table :planets, force: true do |t|
t.string :name, null: false
t.timestamps null: true, limit: 6
end

create_table :planet_versions, force: true do |t|
t.string :item_type, null: false
t.integer :item_id, null: false
t.string :event, null: false
t.references :whodunnit, null: true, foreign_key: { to_table: :users }
t.text :object
t.datetime :created_at, limit: 6
end

if ENV["DB"] == "postgres"
create_table :postgres_users, force: true do |t|
t.string :name
Expand Down
18 changes: 18 additions & 0 deletions spec/models/planet_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Planet, type: :model do
let!(:user) { User.create!(name: FFaker::Name.name) }

def create_and_update_planet
PaperTrail.request(whodunnit: user) do
planet = PaperTrail.request(whodunnit: nil) do
Planet.create!(name: "The Earth")
end
planet.update!(name: "Earth")
end
end

it { expect { create_and_update_planet }.not_to change(User, :count).from(1) }
end