2014-06-05 11:06:51 -04:00
|
|
|
# Require core library
|
|
|
|
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
|
|
|
# ActiveRecord 5 drops support for serialized attributes; for previous
|
|
|
|
# versions of ActiveRecord it is supported, we have a config option
|
|
|
|
# to enable it within PaperTrail.
|
|
|
|
def self.serialized_attributes?
|
|
|
|
!!PaperTrail.config.serialized_attributes && ::ActiveRecord::VERSION::MAJOR < 5
|
|
|
|
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
|
|
|
|
2014-02-11 16:01:44 -05:00
|
|
|
# Sets whether PaperTrail is enabled or disabled for this model in the current request.
|
|
|
|
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
|
|
|
|
|
|
|
|
# Returns `true` if PaperTrail is enabled for this model in the current request, `false` otherwise.
|
|
|
|
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
|
|
|
|
|
2010-03-19 10:19:01 -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
|
|
|
|
|
|
|
|
# Sets any information from the controller that you want PaperTrail
|
|
|
|
# to store. By default this is set automatically by a before filter.
|
|
|
|
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?
|
2014-01-08 14:17:42 -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?
|
2014-12-04 07:04:37 -05: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
|
|
|
|
|
|
|
|
# Thread-safe hash to hold PaperTrail's data.
|
2011-04-06 20:21:57 -04:00
|
|
|
# Initializing with needed default values.
|
2010-03-19 14:53:49 -04:00
|
|
|
def self.paper_trail_store
|
2013-08-13 17:10:41 -04:00
|
|
|
Thread.current[:paper_trail] ||= { :request_enabled_for_controller => true }
|
2010-03-19 14:53:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns PaperTrail's configuration object.
|
|
|
|
def self.config
|
|
|
|
@@config ||= PaperTrail::Config.instance
|
2009-05-27 11:21:20 -04:00
|
|
|
end
|
2013-01-21 10:38:43 -05:00
|
|
|
|
2013-01-07 14:44:12 -05:00
|
|
|
def self.configure
|
|
|
|
yield config
|
|
|
|
end
|
2009-05-27 11:21:20 -04:00
|
|
|
end
|
2011-02-08 12:16:35 -05:00
|
|
|
|
2013-10-02 17:04:45 -04:00
|
|
|
# Ensure `ProtectedAttributes` gem gets required if it is available before the `Version` class gets loaded in
|
|
|
|
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'
|
2013-10-03 10:06:24 -04:00
|
|
|
rescue LoadError; end # will rescue if `ProtectedAttributes` gem is not available
|
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'
|
2014-06-05 11:06:51 -04:00
|
|
|
if defined? Rails
|
|
|
|
require 'paper_trail/frameworks/rails'
|
|
|
|
else
|
|
|
|
require 'paper_trail/frameworks/active_record'
|
|
|
|
end
|