2014-06-05 15:06:51 +00: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 23:13:58 +00:00
|
|
|
|
2013-10-02 17:48:20 +00:00
|
|
|
# Require serializers
|
2014-06-05 15:06:51 +00: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 15:21:20 +00:00
|
|
|
|
|
|
|
module PaperTrail
|
2013-03-21 20:40:15 +00:00
|
|
|
extend PaperTrail::Cleaner
|
2009-05-27 15:21:20 +00:00
|
|
|
|
2010-03-19 15:21:07 +00: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
|
|
|
|
|
2013-11-16 23:39:08 +00: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 13:30:40 +00:00
|
|
|
# Returns `true` if PaperTrail is enabled for the request, `false` otherwise.
|
|
|
|
#
|
2013-11-16 23:39:08 +00:00
|
|
|
# See `PaperTrail::Rails::Controller#paper_trail_enabled_for_controller`.
|
2011-04-05 08:08:54 +00:00
|
|
|
def self.enabled_for_controller?
|
|
|
|
!!paper_trail_store[:request_enabled_for_controller]
|
2011-03-18 08:50:34 +00:00
|
|
|
end
|
2011-04-04 16:44:41 +00:00
|
|
|
|
2014-02-11 21:01:44 +00: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 19:02:34 +00:00
|
|
|
paper_trail_store[:"enabled_for_#{model}"] = value
|
2014-02-11 21:01:44 +00: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 19:02:34 +00:00
|
|
|
!!paper_trail_store.fetch(:"enabled_for_#{model}", true)
|
2014-02-11 21:01:44 +00:00
|
|
|
end
|
|
|
|
|
2012-03-12 11:14:47 +00:00
|
|
|
# Set the field which records when a version was created.
|
2012-02-01 19:47:06 +00:00
|
|
|
def self.timestamp_field=(field_name)
|
|
|
|
PaperTrail.config.timestamp_field = field_name
|
|
|
|
end
|
|
|
|
|
2012-03-12 11:14:47 +00:00
|
|
|
# Returns the field which records when a version was created.
|
2012-02-01 19:47:06 +00:00
|
|
|
def self.timestamp_field
|
|
|
|
PaperTrail.config.timestamp_field
|
|
|
|
end
|
|
|
|
|
2010-03-19 14:19:01 +00: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 15:21:20 +00:00
|
|
|
def self.whodunnit=(value)
|
2010-03-19 18:53:49 +00:00
|
|
|
paper_trail_store[:whodunnit] = value
|
|
|
|
end
|
|
|
|
|
2013-11-16 23:39:08 +00:00
|
|
|
# Returns who is reponsible for any changes that occur.
|
|
|
|
def self.whodunnit
|
|
|
|
paper_trail_store[:whodunnit]
|
2010-03-19 18:53:49 +00: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 23:39:08 +00: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 15:19:19 +00:00
|
|
|
# Getter and Setter for PaperTrail Serializer
|
|
|
|
def self.serializer=(value)
|
|
|
|
PaperTrail.config.serializer = value
|
|
|
|
end
|
|
|
|
|
2012-07-04 03:22:52 +00:00
|
|
|
def self.serializer
|
|
|
|
PaperTrail.config.serializer
|
|
|
|
end
|
2010-03-19 18:53:49 +00:00
|
|
|
|
2013-08-07 14:54:19 +00:00
|
|
|
def self.active_record_protected_attributes?
|
2014-01-08 19:17:42 +00:00
|
|
|
@active_record_protected_attributes ||= ::ActiveRecord::VERSION::MAJOR < 4 || !!defined?(ProtectedAttributes)
|
2013-08-07 14:54:19 +00:00
|
|
|
end
|
|
|
|
|
2010-03-19 18:53:49 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
# Thread-safe hash to hold PaperTrail's data.
|
2011-04-07 00:21:57 +00:00
|
|
|
# Initializing with needed default values.
|
2010-03-19 18:53:49 +00:00
|
|
|
def self.paper_trail_store
|
2013-08-13 21:10:41 +00:00
|
|
|
Thread.current[:paper_trail] ||= { :request_enabled_for_controller => true }
|
2010-03-19 18:53:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns PaperTrail's configuration object.
|
|
|
|
def self.config
|
|
|
|
@@config ||= PaperTrail::Config.instance
|
2009-05-27 15:21:20 +00:00
|
|
|
end
|
2013-01-21 15:38:43 +00:00
|
|
|
|
2013-01-07 19:44:12 +00:00
|
|
|
def self.configure
|
|
|
|
yield config
|
|
|
|
end
|
2009-05-27 15:21:20 +00:00
|
|
|
end
|
2011-02-08 17:16:35 +00:00
|
|
|
|
2013-10-02 21:04:45 +00: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 14:06:24 +00:00
|
|
|
PaperTrail.send(:remove_instance_variable, :@active_record_protected_attributes)
|
2013-10-02 21:04:45 +00:00
|
|
|
begin
|
|
|
|
require 'protected_attributes'
|
2013-10-03 14:06:24 +00:00
|
|
|
rescue LoadError; end # will rescue if `ProtectedAttributes` gem is not available
|
2013-10-02 21:04:45 +00:00
|
|
|
end
|
|
|
|
|
2014-04-02 21:05:20 +00:00
|
|
|
ActiveSupport.on_load(:active_record) do
|
|
|
|
include PaperTrail::Model
|
|
|
|
end
|
|
|
|
|
2013-10-02 21:04:45 +00:00
|
|
|
# Require frameworks
|
2013-10-02 17:48:20 +00:00
|
|
|
require 'paper_trail/frameworks/sinatra'
|
2014-06-05 15:06:51 +00:00
|
|
|
if defined? Rails
|
|
|
|
require 'paper_trail/frameworks/rails'
|
|
|
|
else
|
|
|
|
require 'paper_trail/frameworks/active_record'
|
|
|
|
end
|
2014-07-17 18:46:34 +00:00
|
|
|
require 'paper_trail/frameworks/rspec' if defined? RSpec::Core
|
2013-10-02 17:48:20 +00:00
|
|
|
require 'paper_trail/frameworks/cucumber' if defined? World
|