Module: PaperTrail

Extended by:
Cleaner
Defined in:
lib/paper_trail.rb,
lib/paper_trail/config.rb,
lib/paper_trail/cleaner.rb,
lib/paper_trail/reifier.rb,
lib/paper_trail/model_config.rb,
lib/paper_trail/record_trail.rb,
lib/paper_trail/record_history.rb,
lib/paper_trail/version_number.rb,
lib/paper_trail/has_paper_trail.rb,
lib/paper_trail/version_concern.rb,
lib/paper_trail/reifiers/has_one.rb,
lib/paper_trail/serializers/json.rb,
lib/paper_trail/serializers/yaml.rb,
lib/paper_trail/reifiers/has_many.rb,
lib/paper_trail/frameworks/cucumber.rb,
lib/paper_trail/reifiers/belongs_to.rb,
lib/paper_trail/frameworks/rails/engine.rb,
lib/paper_trail/frameworks/rspec/helpers.rb,
lib/paper_trail/reifiers/has_many_through.rb,
lib/paper_trail/frameworks/rails/controller.rb,
lib/paper_trail/version_association_concern.rb,
lib/generators/paper_trail/install_generator.rb,
lib/paper_trail/queries/versions/where_object.rb,
lib/paper_trail/reifiers/has_and_belongs_to_many.rb,
lib/paper_trail/queries/versions/where_object_changes.rb,
lib/paper_trail/attribute_serializers/object_attribute.rb,
lib/paper_trail/type_serializers/postgres_array_serializer.rb,
lib/paper_trail/attribute_serializers/object_changes_attribute.rb,
lib/paper_trail/attribute_serializers/cast_attribute_serializer.rb,
lib/paper_trail/attribute_serializers/attribute_serializer_factory.rb,
lib/paper_trail/frameworks/active_record/models/paper_trail/version.rb,
lib/paper_trail/frameworks/active_record/models/paper_trail/version_association.rb

Overview

An ActiveRecord extension that tracks changes to your models, for auditing or versioning.

Defined Under Namespace

Modules: AttributeSerializers, Cleaner, Cucumber, Model, Queries, RSpec, Rails, Reifier, Reifiers, Serializers, TypeSerializers, VERSION, VersionAssociationConcern, VersionConcern Classes: Config, InstallGenerator, ModelConfig, RecordHistory, RecordTrail, Version, VersionAssociation

Constant Summary collapse

E_RAILS_NOT_LOADED =
"  PaperTrail has been loaded too early, before rails is loaded. This can\n  happen when another gem defines the ::Rails namespace, then PT is loaded,\n  all before rails is loaded. You may want to reorder your Gemfile, or defer\n  the loading of PT by using `require: false` and a manual require elsewhere.\n".squish.freeze
E_TIMESTAMP_FIELD_CONFIG =
"  PaperTrail.timestamp_field= has been removed, without replacement. It is no\n  longer configurable. The timestamp column in the versions table must now be\n  named created_at.\n".squish.freeze

Class Method Summary collapse

Methods included from Cleaner

clean_versions!

Class Method Details

.clear_transaction_idObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



33
34
35
# File 'lib/paper_trail.rb', line 33

def clear_transaction_id
  self.transaction_id = nil
end

.config {|@config| ... } ⇒ Object Also known as: configure

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns PaperTrail’s configuration object.

Yields:



181
182
183
184
185
# File 'lib/paper_trail.rb', line 181

def config
  @config ||= PaperTrail::Config.instance
  yield @config if block_given?
  @config
end

.controller_infoObject

Returns any information from the controller that you want PaperTrail to store.

See ‘PaperTrail::Rails::Controller#info_for_paper_trail`.



142
143
144
# File 'lib/paper_trail.rb', line 142

def controller_info
  paper_trail_store[:controller_info]
end

.controller_info=(value) ⇒ Object

Sets any information from the controller that you want PaperTrail to store. By default this is set automatically by a before filter.



133
134
135
# File 'lib/paper_trail.rb', line 133

def controller_info=(value)
  paper_trail_store[:controller_info] = value
end

.enabled=(value) ⇒ Object

Switches PaperTrail on or off.



39
40
41
# File 'lib/paper_trail.rb', line 39

def enabled=(value)
  PaperTrail.config.enabled = value
end

.enabled?Boolean

Returns true if PaperTrail is on, false otherwise. PaperTrail is enabled by default.

Returns:

  • (Boolean)


46
47
48
# File 'lib/paper_trail.rb', line 46

def enabled?
  !!PaperTrail.config.enabled
end

.enabled_for_controller=(value) ⇒ Object

Sets whether PaperTrail is enabled or disabled for the current request.



52
53
54
# File 'lib/paper_trail.rb', line 52

def enabled_for_controller=(value)
  paper_trail_store[:request_enabled_for_controller] = value
end

.enabled_for_controller?Boolean

Returns true if PaperTrail is enabled for the request, false otherwise.

See ‘PaperTrail::Rails::Controller#paper_trail_enabled_for_controller`.

Returns:

  • (Boolean)


60
61
62
# File 'lib/paper_trail.rb', line 60

def enabled_for_controller?
  !!paper_trail_store[:request_enabled_for_controller]
end

.enabled_for_model(model, value) ⇒ Object

Sets whether PaperTrail is enabled or disabled for this model in the current request.



67
68
69
# File 'lib/paper_trail.rb', line 67

def enabled_for_model(model, value)
  paper_trail_store[:"enabled_for_#{model}"] = value
end

.enabled_for_model?(model) ⇒ Boolean

Returns true if PaperTrail is enabled for this model in the current request, false otherwise.

Returns:

  • (Boolean)


74
75
76
# File 'lib/paper_trail.rb', line 74

def enabled_for_model?(model)
  !!paper_trail_store.fetch(:"enabled_for_#{model}", true)
end

.gem_versionObject

Returns a ::Gem::Version, convenient for comparisons. This is recommended over ::PaperTrail::VERSION::STRING.



81
82
83
# File 'lib/paper_trail.rb', line 81

def gem_version
  ::Gem::Version.new(VERSION::STRING)
end

.paper_trail_storeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Thread-safe hash to hold PaperTrail’s data. Initializing with needed default values.



175
176
177
# File 'lib/paper_trail.rb', line 175

def paper_trail_store
  RequestStore.store[:paper_trail] ||= { request_enabled_for_controller: true }
end

.serializerObject



153
154
155
# File 'lib/paper_trail.rb', line 153

def serializer
  PaperTrail.config.serializer
end

.serializer=(value) ⇒ Object

Getter and Setter for PaperTrail Serializer



148
149
150
# File 'lib/paper_trail.rb', line 148

def serializer=(value)
  PaperTrail.config.serializer = value
end

.timestamp_field=(_field_name) ⇒ Object

Set the field which records when a version was created.



87
88
89
# File 'lib/paper_trail.rb', line 87

def timestamp_field=(_field_name)
  raise(E_TIMESTAMP_FIELD_CONFIG)
end

.transaction?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/paper_trail.rb', line 158

def transaction?
  ::ActiveRecord::Base.connection.open_transactions > 0
end

.transaction_idObject



163
164
165
# File 'lib/paper_trail.rb', line 163

def transaction_id
  paper_trail_store[:transaction_id]
end

.transaction_id=(id) ⇒ Object



168
169
170
# File 'lib/paper_trail.rb', line 168

def transaction_id=(id)
  paper_trail_store[:transaction_id] = id
end

.versionObject



188
189
190
# File 'lib/paper_trail.rb', line 188

def version
  VERSION::STRING
end

.whodunnit(value = nil) ⇒ Object

If nothing passed, returns who is reponsible for any changes that occur.

PaperTrail.whodunnit = "someone"
PaperTrail.whodunnit # => "someone"

If value and block passed, set this value as whodunnit for the duration of the block

PaperTrail.whodunnit("me") do
  puts PaperTrail.whodunnit # => "me"
end


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/paper_trail.rb', line 111

def whodunnit(value = nil)
  if value
    raise ArgumentError, "no block given" unless block_given?

    previous_whodunnit = paper_trail_store[:whodunnit]
    paper_trail_store[:whodunnit] = value

    begin
      yield
    ensure
      paper_trail_store[:whodunnit] = previous_whodunnit
    end
  elsif paper_trail_store[:whodunnit].respond_to?(:call)
    paper_trail_store[:whodunnit].call
  else
    paper_trail_store[:whodunnit]
  end
end

.whodunnit=(value) ⇒ Object

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.



95
96
97
# File 'lib/paper_trail.rb', line 95

def whodunnit=(value)
  paper_trail_store[:whodunnit] = value
end