2010-04-27 12:25:08 -04:00
|
|
|
require 'singleton'
|
2009-05-27 11:21:20 -04:00
|
|
|
require 'yaml'
|
2010-10-12 08:15:56 -04:00
|
|
|
|
2010-03-19 11:21:07 -04:00
|
|
|
require 'paper_trail/config'
|
2010-03-19 13:21:16 -04:00
|
|
|
require 'paper_trail/controller'
|
2009-05-27 11:21:20 -04:00
|
|
|
require 'paper_trail/has_paper_trail'
|
|
|
|
require 'paper_trail/version'
|
|
|
|
|
2010-03-19 13:21:16 -04:00
|
|
|
# PaperTrail's module methods can be called in both models and controllers.
|
2009-05-27 11:21:20 -04:00
|
|
|
module PaperTrail
|
|
|
|
|
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
|
|
|
|
|
2011-04-06 09:30:40 -04:00
|
|
|
# Returns `true` if PaperTrail is enabled for the request, `false` otherwise.
|
|
|
|
#
|
|
|
|
# See `PaperTrail::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
|
|
|
|
2011-04-06 09:30:40 -04:00
|
|
|
# Sets whether PaperTrail is enabled or disabled for the current request.
|
2011-04-05 04:08:54 -04:00
|
|
|
def self.enabled_for_controller=(value)
|
|
|
|
paper_trail_store[:request_enabled_for_controller] = value
|
2011-03-18 06:05:57 -04:00
|
|
|
end
|
2011-04-04 12:44:41 -04:00
|
|
|
|
2010-03-19 11:21:07 -04:00
|
|
|
# Returns who is reponsible for any changes that occur.
|
2009-05-27 11:21:20 -04:00
|
|
|
def self.whodunnit
|
2010-03-19 14:53:49 -04:00
|
|
|
paper_trail_store[:whodunnit]
|
2009-05-27 11:21:20 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
# Returns any information from the controller that you want
|
|
|
|
# PaperTrail to store.
|
|
|
|
#
|
|
|
|
# See `PaperTrail::Controller#info_for_paper_trail`.
|
|
|
|
def self.controller_info
|
|
|
|
paper_trail_store[:controller_info]
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2011-04-06 20:21:57 -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
|
|
|
|
|
|
|
|
end
|
2011-02-08 12:16:35 -05:00
|
|
|
|
|
|
|
|
|
|
|
ActiveSupport.on_load(:active_record) do
|
|
|
|
include PaperTrail::Model
|
|
|
|
end
|
|
|
|
|
|
|
|
ActiveSupport.on_load(:action_controller) do
|
|
|
|
include PaperTrail::Controller
|
|
|
|
end
|