Skip to content

Commit

Permalink
Merge pull request #985 from hired/optional-item
Browse files Browse the repository at this point in the history
Support belongs_to_required_by_default
  • Loading branch information
jaredbeck committed Aug 31, 2017
2 parents 0e95bda + 29623cf commit 935999d
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 15 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).

### Fixed

- None
- [#985](https://github.com/airblade/paper_trail/pull/985) - Fix RecordInvalid error on nil item
association when belongs_to_required_by_default is enabled.

## 7.1.1 (2017-08-18)

Expand Down
6 changes: 5 additions & 1 deletion lib/paper_trail/version_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ module VersionConcern
extend ::ActiveSupport::Concern

included do
belongs_to :item, polymorphic: true
if ActiveRecord.gem_version >= Gem::Version.new("5.0")
belongs_to :item, polymorphic: true, optional: true
else
belongs_to :item, polymorphic: true
end

# Since the test suite has test coverage for this, we want to declare
# the association when the test suite is running. This makes it pass when
Expand Down
11 changes: 8 additions & 3 deletions spec/dummy_app/app/models/callback_modifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ class BeforeDestroyModifier < CallbackModifier
paper_trail.on_destroy :before
end

class AfterDestroyModifier < CallbackModifier
has_paper_trail on: []
paper_trail.on_destroy :after
if ActiveRecord.gem_version < Gem::Version.new("5") ||
!ActiveRecord::Base.belongs_to_required_by_default

class AfterDestroyModifier < CallbackModifier
has_paper_trail on: []
paper_trail.on_destroy :after
end

end

class NoArgDestroyModifier < CallbackModifier
Expand Down
6 changes: 5 additions & 1 deletion spec/dummy_app/app/models/fluxor.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
class Fluxor < ActiveRecord::Base
belongs_to :widget
if ActiveRecord.gem_version >= Gem::Version.new("5.0")
belongs_to :widget, optional: true
else
belongs_to :widget
end
end
8 changes: 7 additions & 1 deletion spec/dummy_app/app/models/person.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
class Person < ActiveRecord::Base
has_many :authorships, foreign_key: :author_id, dependent: :destroy
has_many :books, through: :authorships
belongs_to :mentor, class_name: "Person", foreign_key: :mentor_id

if ActiveRecord.gem_version >= Gem::Version.new("5.0")
belongs_to :mentor, class_name: "Person", foreign_key: :mentor_id, optional: true
else
belongs_to :mentor, class_name: "Person", foreign_key: :mentor_id
end

has_paper_trail

# Convert strings to TimeZone objects when assigned
Expand Down
7 changes: 6 additions & 1 deletion spec/dummy_app/app/models/whatchamajigger.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
class Whatchamajigger < ActiveRecord::Base
has_paper_trail
belongs_to :owner, polymorphic: true

if ActiveRecord.gem_version >= Gem::Version.new("5.0")
belongs_to :owner, polymorphic: true, optional: true
else
belongs_to :owner, polymorphic: true
end
end
7 changes: 6 additions & 1 deletion spec/dummy_app/app/models/wotsit.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
class Wotsit < ActiveRecord::Base
has_paper_trail
belongs_to :widget

if ActiveRecord.gem_version >= Gem::Version.new("5.0")
belongs_to :widget, optional: true
else
belongs_to :widget
end

def created_on
created_at.to_date
Expand Down
7 changes: 6 additions & 1 deletion spec/dummy_app/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ class Application < Rails::Application
if v >= Gem::Version.new("4.2") && v < Gem::Version.new("5.0.0.beta1")
config.active_record.raise_in_transactional_callbacks = true
end
if v >= Gem::Version.new("5.0.0.beta1")
if v >= Gem::Version.new("5.0.0.beta1") && v < Gem::Version.new("5.1")
config.active_record.belongs_to_required_by_default = true
config.active_record.time_zone_aware_types = [:datetime]
end
if v >= Gem::Version.new("5.1")
config.load_defaults "5.1"
config.active_record.time_zone_aware_types = [:datetime]
end
end
Expand Down
15 changes: 10 additions & 5 deletions spec/models/callback_modifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@
end
end

context "when :after" do
it "creates the version after destroy" do
modifier = AfterDestroyModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.test_destroy
expect(modifier.versions.last.reify).to be_flagged_deleted
if ActiveRecord.gem_version < Gem::Version.new("5") ||
!ActiveRecord::Base.belongs_to_required_by_default

context "when :after" do
it "creates the version after destroy" do
modifier = AfterDestroyModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.test_destroy
expect(modifier.versions.last.reify).to be_flagged_deleted
end
end

end

context "when no argument" do
Expand Down

0 comments on commit 935999d

Please sign in to comment.