Skip to content

Commit

Permalink
re: #501; Add config option for tracking associations with a logical …
Browse files Browse the repository at this point in the history
…default
  • Loading branch information
batter committed Mar 25, 2015
1 parent b97b299 commit 5f8904e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/paper_trail/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Config
include Singleton
attr_accessor :enabled, :timestamp_field, :serializer, :version_limit
attr_reader :serialized_attributes
attr_writer :track_associations

def initialize
@enabled = true # Indicates whether PaperTrail is on or off.
Expand All @@ -27,5 +28,10 @@ def serialized_attributes=(value)
end
@serialized_attributes = value
end

def track_associations
@track_associations ||= PaperTrail::VersionAssociation.table_exists?
end
alias_method :track_associations?, :track_associations
end
end
2 changes: 1 addition & 1 deletion lib/paper_trail/has_paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def record_destroy

# saves associations if the join table for `VersionAssociation` exists
def save_associations(version)
return unless PaperTrail::VersionAssociation.table_exists?
return unless PaperTrail.config.track_associations?
self.class.reflect_on_all_associations(:belongs_to).each do |assoc|
assoc_version_args = {
:version_id => version.id,
Expand Down
2 changes: 1 addition & 1 deletion lib/paper_trail/version_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module VersionConcern
# association when the test suite is running. This makes it pass
# when DB is not initialized prior to test runs such as when we run on
# Travis CI (there won't be a db in `test/dummy/db/`)
if PaperTrail::VersionAssociation.table_exists? || ::Rails.env.test?
if PaperTrail.config.track_associations?
has_many :version_associations, :dependent => :destroy
end

Expand Down
3 changes: 3 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if ActiveRecord::Migration.respond_to?(:check_pending!)

# Turn on associations tracking when the test suite is run on Travis CI
PaperTrail.config.track_associations = true if ENV['TRAVIS']

RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"

Expand Down
3 changes: 3 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class ActiveSupport::TestCase
end
end

# Turn on associations tracking when the test suite is run on Travis CI
PaperTrail.config.track_associations = true if ENV['TRAVIS']

#
# Helpers
#
Expand Down

0 comments on commit 5f8904e

Please sign in to comment.