2015-02-03 16:28:43 -05:00
|
|
|
require 'request_store'
|
|
|
|
|
2015-11-05 16:18:25 -05:00
|
|
|
# Require files in lib/paper_trail, but not its subdirectories.
|
2014-06-05 11:06:51 -04:00
|
|
|
Dir[File.join(File.dirname(__FILE__), 'paper_trail', '*.rb')].each do |file|
|
|
|
|
require File.join('paper_trail', File.basename(file, '.rb'))
|
|
|
|
end
|
2013-01-22 18:13:58 -05:00
|
|
|
|
2013-10-02 13:48:20 -04:00
|
|
|
# Require serializers
|
2014-06-05 11:06:51 -04:00
|
|
|
Dir[File.join(File.dirname(__FILE__), 'paper_trail', 'serializers', '*.rb')].each do |file|
|
|
|
|
require File.join('paper_trail', 'serializers', File.basename(file, '.rb'))
|
|
|
|
end
|
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
|
|
|
|
2010-03-19 11:21:07 -04:00
|
|
|
# Switches PaperTrail on or off.
|
|
|
|
def self.enabled=(value)
|
|
|
|
PaperTrail.config.enabled = value
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns `true` if PaperTrail is on, `false` otherwise.
|
|
|
|
# PaperTrail is enabled by default.
|
|
|
|
def self.enabled?
|
|
|
|
!!PaperTrail.config.enabled
|
|
|
|
end
|
|
|
|
|
2014-12-29 14:38:32 -05:00
|
|
|
def self.serialized_attributes?
|
2015-12-22 11:32:50 -05:00
|
|
|
ActiveSupport::Deprecation.warn(
|
|
|
|
"PaperTrail.serialized_attributes? is deprecated without replacement " +
|
|
|
|
"and always returns false."
|
|
|
|
)
|
|
|
|
false
|
2014-12-29 14:38:32 -05:00
|
|
|
end
|
|
|
|
|
2013-11-16 18:39:08 -05:00
|
|
|
# Sets whether PaperTrail is enabled or disabled for the current request.
|
|
|
|
def self.enabled_for_controller=(value)
|
|
|
|
paper_trail_store[:request_enabled_for_controller] = value
|
|
|
|
end
|
|
|
|
|
2011-04-06 09:30:40 -04:00
|
|
|
# Returns `true` if PaperTrail is enabled for the request, `false` otherwise.
|
|
|
|
#
|
2013-11-16 18:39:08 -05:00
|
|
|
# See `PaperTrail::Rails::Controller#paper_trail_enabled_for_controller`.
|
2011-04-05 04:08:54 -04:00
|
|
|
def self.enabled_for_controller?
|
|
|
|
!!paper_trail_store[:request_enabled_for_controller]
|
2011-03-18 04:50:34 -04:00
|
|
|
end
|
2011-04-04 12:44:41 -04:00
|
|
|
|
2015-08-03 16:45:42 -04:00
|
|
|
# Sets whether PaperTrail is enabled or disabled for this model in the
|
|
|
|
# current request.
|
2014-02-11 16:01:44 -05:00
|
|
|
def self.enabled_for_model(model, value)
|
2014-03-12 15:02:34 -04:00
|
|
|
paper_trail_store[:"enabled_for_#{model}"] = value
|
2014-02-11 16:01:44 -05:00
|
|
|
end
|
|
|
|
|
2015-08-03 16:45:42 -04:00
|
|
|
# Returns `true` if PaperTrail is enabled for this model in the current
|
|
|
|
# request, `false` otherwise.
|
2014-02-11 16:01:44 -05:00
|
|
|
def self.enabled_for_model?(model)
|
2014-03-12 15:02:34 -04:00
|
|
|
!!paper_trail_store.fetch(:"enabled_for_#{model}", true)
|
2014-02-11 16:01:44 -05:00
|
|
|
end
|
|
|
|
|
2012-03-12 07:14:47 -04:00
|
|
|
# Set the field which records when a version was created.
|
2012-02-01 14:47:06 -05:00
|
|
|
def self.timestamp_field=(field_name)
|
|
|
|
PaperTrail.config.timestamp_field = field_name
|
|
|
|
end
|
|
|
|
|
2012-03-12 07:14:47 -04:00
|
|
|
# Returns the field which records when a version was created.
|
2012-02-01 14:47:06 -05:00
|
|
|
def self.timestamp_field
|
|
|
|
PaperTrail.config.timestamp_field
|
|
|
|
end
|
|
|
|
|
2015-08-03 16:45:42 -04:00
|
|
|
# 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`.
|
2009-05-27 11:21:20 -04:00
|
|
|
def self.whodunnit=(value)
|
2010-03-19 14:53:49 -04:00
|
|
|
paper_trail_store[:whodunnit] = value
|
|
|
|
end
|
|
|
|
|
2013-11-16 18:39:08 -05:00
|
|
|
# Returns who is reponsible for any changes that occur.
|
|
|
|
def self.whodunnit
|
|
|
|
paper_trail_store[:whodunnit]
|
2010-03-19 14:53:49 -04:00
|
|
|
end
|
|
|
|
|
2015-08-03 16:45:42 -04:00
|
|
|
# Sets any information from the controller that you want PaperTrail to
|
|
|
|
# store. By default this is set automatically by a before filter.
|
2010-03-19 14:53:49 -04:00
|
|
|
def self.controller_info=(value)
|
|
|
|
paper_trail_store[:controller_info] = value
|
|
|
|
end
|
|
|
|
|
2013-11-16 18:39:08 -05:00
|
|
|
# Returns any information from the controller that you want
|
|
|
|
# PaperTrail to store.
|
|
|
|
#
|
|
|
|
# See `PaperTrail::Rails::Controller#info_for_paper_trail`.
|
|
|
|
def self.controller_info
|
|
|
|
paper_trail_store[:controller_info]
|
|
|
|
end
|
|
|
|
|
2013-01-04 10:19:19 -05:00
|
|
|
# Getter and Setter for PaperTrail Serializer
|
|
|
|
def self.serializer=(value)
|
|
|
|
PaperTrail.config.serializer = value
|
|
|
|
end
|
|
|
|
|
2012-07-03 23:22:52 -04:00
|
|
|
def self.serializer
|
|
|
|
PaperTrail.config.serializer
|
|
|
|
end
|
2010-03-19 14:53:49 -04:00
|
|
|
|
2013-08-07 10:54:19 -04:00
|
|
|
def self.active_record_protected_attributes?
|
2015-11-01 23:56:53 -05:00
|
|
|
@active_record_protected_attributes ||= ::ActiveRecord::VERSION::MAJOR < 4 ||
|
|
|
|
!!defined?(ProtectedAttributes)
|
2013-08-07 10:54:19 -04:00
|
|
|
end
|
|
|
|
|
2014-02-25 09:02:36 -05:00
|
|
|
def self.transaction?
|
2015-06-11 00:00:38 -04:00
|
|
|
::ActiveRecord::Base.connection.open_transactions > 0
|
2014-02-25 09:02:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.transaction_id
|
|
|
|
paper_trail_store[:transaction_id]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.transaction_id=(id)
|
|
|
|
paper_trail_store[:transaction_id] = id
|
|
|
|
end
|
|
|
|
|
2010-03-19 14:53:49 -04:00
|
|
|
private
|
|
|
|
|
2015-08-03 16:45:42 -04:00
|
|
|
# Thread-safe hash to hold PaperTrail's data. Initializing with needed
|
|
|
|
# default values.
|
2010-03-19 14:53:49 -04:00
|
|
|
def self.paper_trail_store
|
2015-11-01 23:57:31 -05:00
|
|
|
RequestStore.store[:paper_trail] ||= { request_enabled_for_controller: true }
|
2010-03-19 14:53:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns PaperTrail's configuration object.
|
|
|
|
def self.config
|
2015-11-01 21:36:19 -05:00
|
|
|
@config ||= PaperTrail::Config.instance
|
|
|
|
yield @config if block_given?
|
|
|
|
@config
|
2009-05-27 11:21:20 -04:00
|
|
|
end
|
2013-01-21 10:38:43 -05:00
|
|
|
|
2015-02-26 23:37:16 -05:00
|
|
|
class << self
|
|
|
|
alias_method :configure, :config
|
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
|
|
|
|
2015-08-03 16:45:42 -04:00
|
|
|
# Ensure `ProtectedAttributes` gem gets required if it is available before the
|
|
|
|
# `Version` class gets loaded in.
|
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
|
|
|
|
require 'protected_attributes'
|
2015-08-03 16:45:42 -04:00
|
|
|
rescue LoadError
|
|
|
|
# In case `ProtectedAttributes` gem is not available.
|
|
|
|
end
|
2013-10-02 17:04:45 -04:00
|
|
|
end
|
|
|
|
|
2014-04-02 17:05:20 -04:00
|
|
|
ActiveSupport.on_load(:active_record) do
|
|
|
|
include PaperTrail::Model
|
|
|
|
end
|
|
|
|
|
2013-10-02 17:04:45 -04:00
|
|
|
# Require frameworks
|
2013-10-02 13:48:20 -04:00
|
|
|
require 'paper_trail/frameworks/sinatra'
|
2015-07-23 14:16:12 -04:00
|
|
|
if defined?(::Rails) && ActiveRecord::VERSION::STRING >= '3.2'
|
2014-06-05 11:06:51 -04:00
|
|
|
require 'paper_trail/frameworks/rails'
|
|
|
|
else
|
|
|
|
require 'paper_trail/frameworks/active_record'
|
|
|
|
end
|