2016-03-05 17:07:32 -05:00
|
|
|
require "request_store"
|
2015-02-03 16:28:43 -05:00
|
|
|
|
2015-11-05 16:18:25 -05:00
|
|
|
# Require files in lib/paper_trail, but not its subdirectories.
|
2016-03-05 17:07:32 -05:00
|
|
|
Dir[File.join(File.dirname(__FILE__), "paper_trail", "*.rb")].each do |file|
|
|
|
|
require File.join("paper_trail", File.basename(file, ".rb"))
|
2014-06-05 11:06:51 -04:00
|
|
|
end
|
2013-01-22 18:13:58 -05:00
|
|
|
|
2013-10-02 13:48:20 -04:00
|
|
|
# Require serializers
|
2016-03-05 17:07:32 -05:00
|
|
|
Dir[File.join(File.dirname(__FILE__), "paper_trail", "serializers", "*.rb")].each do |file|
|
|
|
|
require File.join("paper_trail", "serializers", File.basename(file, ".rb"))
|
2014-06-05 11:06:51 -04:00
|
|
|
end
|
2009-05-27 11:21:20 -04:00
|
|
|
|
2016-04-09 01:08:34 -04:00
|
|
|
# An ActiveRecord extension that tracks changes to your models, for auditing or
|
|
|
|
# versioning.
|
2009-05-27 11:21:20 -04:00
|
|
|
module PaperTrail
|
2013-03-21 16:40:15 -04:00
|
|
|
extend PaperTrail::Cleaner
|
2009-05-27 11:21:20 -04:00
|
|
|
|
2015-02-26 23:37:16 -05:00
|
|
|
class << self
|
2016-02-15 19:58:23 -05:00
|
|
|
# Switches PaperTrail on or off.
|
|
|
|
# @api public
|
|
|
|
def enabled=(value)
|
|
|
|
PaperTrail.config.enabled = value
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns `true` if PaperTrail is on, `false` otherwise.
|
|
|
|
# PaperTrail is enabled by default.
|
|
|
|
# @api public
|
|
|
|
def enabled?
|
|
|
|
!!PaperTrail.config.enabled
|
|
|
|
end
|
|
|
|
|
|
|
|
def serialized_attributes?
|
|
|
|
ActiveSupport::Deprecation.warn(
|
|
|
|
"PaperTrail.serialized_attributes? is deprecated without replacement " +
|
|
|
|
"and always returns false."
|
|
|
|
)
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sets whether PaperTrail is enabled or disabled for the current request.
|
|
|
|
# @api public
|
|
|
|
def enabled_for_controller=(value)
|
|
|
|
paper_trail_store[:request_enabled_for_controller] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns `true` if PaperTrail is enabled for the request, `false` otherwise.
|
|
|
|
#
|
|
|
|
# See `PaperTrail::Rails::Controller#paper_trail_enabled_for_controller`.
|
|
|
|
# @api public
|
|
|
|
def enabled_for_controller?
|
|
|
|
!!paper_trail_store[:request_enabled_for_controller]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sets whether PaperTrail is enabled or disabled for this model in the
|
|
|
|
# current request.
|
|
|
|
# @api public
|
|
|
|
def enabled_for_model(model, value)
|
|
|
|
paper_trail_store[:"enabled_for_#{model}"] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns `true` if PaperTrail is enabled for this model in the current
|
|
|
|
# request, `false` otherwise.
|
|
|
|
# @api public
|
|
|
|
def enabled_for_model?(model)
|
|
|
|
!!paper_trail_store.fetch(:"enabled_for_#{model}", true)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set the field which records when a version was created.
|
|
|
|
# @api public
|
|
|
|
def timestamp_field=(field_name)
|
|
|
|
PaperTrail.config.timestamp_field = field_name
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the field which records when a version was created.
|
|
|
|
# @api public
|
|
|
|
def timestamp_field
|
|
|
|
PaperTrail.config.timestamp_field
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sets who is responsible for any changes that occur. You would normally use
|
|
|
|
# this in a migration or on the console, when working with models directly.
|
|
|
|
# In a controller it is set automatically to the `current_user`.
|
|
|
|
# @api public
|
|
|
|
def whodunnit=(value)
|
|
|
|
paper_trail_store[:whodunnit] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns who is reponsible for any changes that occur.
|
|
|
|
# @api public
|
|
|
|
def whodunnit
|
|
|
|
paper_trail_store[:whodunnit]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sets any information from the controller that you want PaperTrail to
|
|
|
|
# store. By default this is set automatically by a before filter.
|
|
|
|
# @api public
|
|
|
|
def controller_info=(value)
|
|
|
|
paper_trail_store[:controller_info] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns any information from the controller that you want
|
|
|
|
# PaperTrail to store.
|
|
|
|
#
|
|
|
|
# See `PaperTrail::Rails::Controller#info_for_paper_trail`.
|
|
|
|
# @api public
|
|
|
|
def controller_info
|
|
|
|
paper_trail_store[:controller_info]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Getter and Setter for PaperTrail Serializer
|
|
|
|
# @api public
|
|
|
|
def serializer=(value)
|
|
|
|
PaperTrail.config.serializer = value
|
|
|
|
end
|
|
|
|
|
|
|
|
# @api public
|
|
|
|
def serializer
|
|
|
|
PaperTrail.config.serializer
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a boolean indicating whether "protected attibutes" should be
|
|
|
|
# configured, e.g. attr_accessible, mass_assignment_sanitizer,
|
|
|
|
# whitelist_attributes, etc.
|
|
|
|
# @api public
|
|
|
|
def active_record_protected_attributes?
|
|
|
|
@active_record_protected_attributes ||= ::ActiveRecord::VERSION::MAJOR < 4 ||
|
|
|
|
!!defined?(ProtectedAttributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
# @api public
|
|
|
|
def transaction?
|
|
|
|
::ActiveRecord::Base.connection.open_transactions > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
# @api public
|
|
|
|
def transaction_id
|
|
|
|
paper_trail_store[:transaction_id]
|
|
|
|
end
|
|
|
|
|
|
|
|
# @api public
|
|
|
|
def transaction_id=(id)
|
|
|
|
paper_trail_store[:transaction_id] = id
|
|
|
|
end
|
|
|
|
|
|
|
|
# Thread-safe hash to hold PaperTrail's data. Initializing with needed
|
|
|
|
# default values.
|
|
|
|
# @api private
|
|
|
|
def paper_trail_store
|
|
|
|
RequestStore.store[:paper_trail] ||= { request_enabled_for_controller: true }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns PaperTrail's configuration object.
|
|
|
|
# @api private
|
|
|
|
def config
|
|
|
|
@config ||= PaperTrail::Config.instance
|
|
|
|
yield @config if block_given?
|
|
|
|
@config
|
|
|
|
end
|
2016-03-31 04:57:32 -04:00
|
|
|
alias configure config
|
2016-04-08 23:32:09 -04:00
|
|
|
|
|
|
|
def version
|
|
|
|
VERSION::STRING
|
|
|
|
end
|
2013-01-07 14:44:12 -05:00
|
|
|
end
|
2009-05-27 11:21:20 -04:00
|
|
|
end
|
2011-02-08 12:16:35 -05:00
|
|
|
|
2016-02-15 19:53:13 -05:00
|
|
|
# If available, ensure that the `protected_attributes` gem is loaded
|
|
|
|
# before the `Version` class.
|
2013-10-02 17:04:45 -04:00
|
|
|
unless PaperTrail.active_record_protected_attributes?
|
2013-10-03 10:06:24 -04:00
|
|
|
PaperTrail.send(:remove_instance_variable, :@active_record_protected_attributes)
|
2013-10-02 17:04:45 -04:00
|
|
|
begin
|
2016-03-05 17:07:32 -05:00
|
|
|
require "protected_attributes"
|
2016-02-15 19:53:13 -05:00
|
|
|
rescue LoadError # rubocop:disable Lint/HandleExceptions
|
|
|
|
# In case `protected_attributes` gem is not available.
|
2015-08-03 16:45:42 -04:00
|
|
|
end
|
2013-10-02 17:04:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Require frameworks
|
2016-03-05 17:07:32 -05:00
|
|
|
require "paper_trail/frameworks/sinatra"
|
|
|
|
if defined?(::Rails) && ActiveRecord::VERSION::STRING >= "3.2"
|
|
|
|
require "paper_trail/frameworks/rails"
|
2014-06-05 11:06:51 -04:00
|
|
|
else
|
2016-03-05 17:07:32 -05:00
|
|
|
require "paper_trail/frameworks/active_record"
|
2014-06-05 11:06:51 -04:00
|
|
|
end
|