Skip to content

Commit

Permalink
Enable Rails 5.1 defaults in dummy app
Browse files Browse the repository at this point in the history
This enables the belongs_to_required_by_default option.
Add "optional: true" where necessary to fix spec failures caused by this.
This will enable users of PaperTrail, who also use the Rails 5.1 defaults,
to not see exceptions caused by "item" being required by VersionConcern, such as when using soft deletion.
  • Loading branch information
Joel Hayhurst committed Aug 24, 2017
1 parent 0e95bda commit d404d7f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 5 deletions.
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
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
3 changes: 3 additions & 0 deletions spec/dummy_app/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class Application < Rails::Application
if v >= Gem::Version.new("5.0.0.beta1")
config.active_record.time_zone_aware_types = [:datetime]
end
if v >= Gem::Version.new("5.1")
config.load_defaults 5.1
end
end
end
end

0 comments on commit d404d7f

Please sign in to comment.