-
Notifications
You must be signed in to change notification settings - Fork 901
/
Copy pathpaper_trail.rb
127 lines (103 loc) · 3.7 KB
/
paper_trail.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
require 'paper_trail/config'
require 'paper_trail/has_paper_trail'
require 'paper_trail/cleaner'
require 'paper_trail/version_number'
# Require serializers
Dir[File.join(File.dirname(__FILE__), 'paper_trail', 'serializers', '*.rb')].each { |file| require file }
module PaperTrail
extend PaperTrail::Cleaner
# 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
# 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
# Returns `true` if PaperTrail is enabled for the request, `false` otherwise.
#
# See `PaperTrail::Rails::Controller#paper_trail_enabled_for_controller`.
def self.enabled_for_controller?
!!paper_trail_store[:request_enabled_for_controller]
end
# Set the field which records when a version was created.
def self.timestamp_field=(field_name)
PaperTrail.config.timestamp_field = field_name
end
# Returns the field which records when a version was created.
def self.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`.
def self.whodunnit=(value)
paper_trail_store[:whodunnit] = value
end
# Returns who is reponsible for any changes that occur.
def self.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.
def self.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`.
def self.controller_info
paper_trail_store[:controller_info]
end
# Getter and Setter for PaperTrail Serializer
def self.serializer=(value)
PaperTrail.config.serializer = value
end
def self.serializer
PaperTrail.config.serializer
end
def self.active_record_protected_attributes?
@active_record_protected_attributes ||= ::ActiveRecord::VERSION::STRING.to_f < 4.0 || !!defined?(ProtectedAttributes)
end
private
# Thread-safe hash to hold PaperTrail's data.
# Initializing with needed default values.
def self.paper_trail_store
Thread.current[:paper_trail] ||= { :request_enabled_for_controller => true }
end
# Returns PaperTrail's configuration object.
def self.config
@@config ||= PaperTrail::Config.instance
end
def self.configure
yield config
end
end
# Ensure `ProtectedAttributes` gem gets required if it is available before the `Version` class gets loaded in
unless PaperTrail.active_record_protected_attributes?
PaperTrail.send(:remove_instance_variable, :@active_record_protected_attributes)
begin
require 'protected_attributes'
rescue LoadError; end # will rescue if `ProtectedAttributes` gem is not available
end
require 'paper_trail/version'
# Require frameworks
require 'paper_trail/frameworks/rails'
require 'paper_trail/frameworks/sinatra'
require 'paper_trail/frameworks/rspec' if defined? RSpec
require 'paper_trail/frameworks/cucumber' if defined? World
ActiveSupport.on_load(:active_record) do
include PaperTrail::Model
end
if defined?(ActionController)
ActiveSupport.on_load(:action_controller) do
include PaperTrail::Rails::Controller
end
end