paper_trail 4.2.0 → 7.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/generators/paper_trail/install_generator.rb +91 -17
- data/lib/generators/paper_trail/templates/add_object_changes_to_versions.rb.erb +12 -0
- data/lib/generators/paper_trail/templates/{add_transaction_id_column_to_versions.rb → add_transaction_id_column_to_versions.rb.erb} +3 -1
- data/lib/generators/paper_trail/templates/create_version_associations.rb.erb +22 -0
- data/lib/generators/paper_trail/templates/{create_versions.rb → create_versions.rb.erb} +9 -7
- data/lib/paper_trail.rb +180 -148
- data/lib/paper_trail/attribute_serializers/README.md +10 -0
- data/lib/paper_trail/attribute_serializers/cast_attribute_serializer.rb +80 -0
- data/lib/paper_trail/attribute_serializers/legacy_active_record_shim.rb +48 -0
- data/lib/paper_trail/attribute_serializers/object_attribute.rb +39 -0
- data/lib/paper_trail/attribute_serializers/object_changes_attribute.rb +42 -0
- data/lib/paper_trail/cleaner.rb +16 -10
- data/lib/paper_trail/config.rb +28 -27
- data/lib/paper_trail/frameworks/active_record/models/paper_trail/version.rb +5 -1
- data/lib/paper_trail/frameworks/active_record/models/paper_trail/version_association.rb +6 -2
- data/lib/paper_trail/frameworks/cucumber.rb +1 -0
- data/lib/paper_trail/frameworks/rails.rb +2 -7
- data/lib/paper_trail/frameworks/rails/controller.rb +20 -18
- data/lib/paper_trail/frameworks/rails/engine.rb +6 -1
- data/lib/paper_trail/frameworks/rspec.rb +17 -6
- data/lib/paper_trail/frameworks/rspec/helpers.rb +3 -1
- data/lib/paper_trail/has_paper_trail.rb +25 -503
- data/lib/paper_trail/model_config.rb +207 -0
- data/lib/paper_trail/queries/versions/where_object.rb +60 -0
- data/lib/paper_trail/queries/versions/where_object_changes.rb +68 -0
- data/lib/paper_trail/record_history.rb +2 -12
- data/lib/paper_trail/record_trail.rb +573 -0
- data/lib/paper_trail/reifier.rb +164 -215
- data/lib/paper_trail/reifiers/belongs_to.rb +48 -0
- data/lib/paper_trail/reifiers/has_and_belongs_to_many.rb +50 -0
- data/lib/paper_trail/reifiers/has_many.rb +110 -0
- data/lib/paper_trail/reifiers/has_many_through.rb +90 -0
- data/lib/paper_trail/reifiers/has_one.rb +76 -0
- data/lib/paper_trail/serializers/json.rb +16 -7
- data/lib/paper_trail/serializers/yaml.rb +9 -13
- data/lib/paper_trail/version_association_concern.rb +3 -5
- data/lib/paper_trail/version_concern.rb +138 -111
- data/lib/paper_trail/version_number.rb +10 -9
- metadata +95 -327
- data/.gitignore +0 -22
- data/.rspec +0 -2
- data/.travis.yml +0 -41
- data/CHANGELOG.md +0 -362
- data/CONTRIBUTING.md +0 -84
- data/Gemfile +0 -2
- data/MIT-LICENSE +0 -20
- data/README.md +0 -1535
- data/Rakefile +0 -30
- data/doc/bug_report_template.rb +0 -65
- data/gemfiles/ar3.gemfile +0 -61
- data/lib/generators/paper_trail/templates/add_object_changes_to_versions.rb +0 -10
- data/lib/generators/paper_trail/templates/create_version_associations.rb +0 -17
- data/lib/paper_trail/attributes_serialization.rb +0 -89
- data/lib/paper_trail/frameworks/sinatra.rb +0 -38
- data/paper_trail.gemspec +0 -59
- data/spec/generators/install_generator_spec.rb +0 -67
- data/spec/models/animal_spec.rb +0 -36
- data/spec/models/boolit_spec.rb +0 -48
- data/spec/models/callback_modifier_spec.rb +0 -96
- data/spec/models/fluxor_spec.rb +0 -19
- data/spec/models/gadget_spec.rb +0 -70
- data/spec/models/joined_version_spec.rb +0 -47
- data/spec/models/json_version_spec.rb +0 -103
- data/spec/models/kitchen/banana_spec.rb +0 -14
- data/spec/models/not_on_update_spec.rb +0 -19
- data/spec/models/post_with_status_spec.rb +0 -17
- data/spec/models/skipper_spec.rb +0 -46
- data/spec/models/thing_spec.rb +0 -11
- data/spec/models/version_spec.rb +0 -239
- data/spec/models/widget_spec.rb +0 -298
- data/spec/modules/paper_trail_spec.rb +0 -27
- data/spec/modules/version_concern_spec.rb +0 -32
- data/spec/modules/version_number_spec.rb +0 -44
- data/spec/paper_trail/config_spec.rb +0 -52
- data/spec/paper_trail_spec.rb +0 -66
- data/spec/rails_helper.rb +0 -34
- data/spec/requests/articles_spec.rb +0 -30
- data/spec/spec_helper.rb +0 -95
- data/spec/support/alt_db_init.rb +0 -59
- data/test/custom_json_serializer.rb +0 -13
- data/test/dummy/Rakefile +0 -7
- data/test/dummy/app/controllers/application_controller.rb +0 -20
- data/test/dummy/app/controllers/articles_controller.rb +0 -17
- data/test/dummy/app/controllers/test_controller.rb +0 -5
- data/test/dummy/app/controllers/widgets_controller.rb +0 -31
- data/test/dummy/app/helpers/application_helper.rb +0 -2
- data/test/dummy/app/models/animal.rb +0 -6
- data/test/dummy/app/models/article.rb +0 -16
- data/test/dummy/app/models/authorship.rb +0 -5
- data/test/dummy/app/models/book.rb +0 -9
- data/test/dummy/app/models/boolit.rb +0 -4
- data/test/dummy/app/models/callback_modifier.rb +0 -45
- data/test/dummy/app/models/cat.rb +0 -2
- data/test/dummy/app/models/chapter.rb +0 -9
- data/test/dummy/app/models/citation.rb +0 -5
- data/test/dummy/app/models/customer.rb +0 -4
- data/test/dummy/app/models/document.rb +0 -4
- data/test/dummy/app/models/dog.rb +0 -2
- data/test/dummy/app/models/editor.rb +0 -4
- data/test/dummy/app/models/editorship.rb +0 -5
- data/test/dummy/app/models/elephant.rb +0 -3
- data/test/dummy/app/models/fluxor.rb +0 -3
- data/test/dummy/app/models/foo_widget.rb +0 -2
- data/test/dummy/app/models/fruit.rb +0 -5
- data/test/dummy/app/models/gadget.rb +0 -3
- data/test/dummy/app/models/kitchen/banana.rb +0 -5
- data/test/dummy/app/models/legacy_widget.rb +0 -4
- data/test/dummy/app/models/line_item.rb +0 -4
- data/test/dummy/app/models/not_on_update.rb +0 -4
- data/test/dummy/app/models/order.rb +0 -5
- data/test/dummy/app/models/paragraph.rb +0 -5
- data/test/dummy/app/models/person.rb +0 -38
- data/test/dummy/app/models/post.rb +0 -3
- data/test/dummy/app/models/post_with_status.rb +0 -8
- data/test/dummy/app/models/protected_widget.rb +0 -3
- data/test/dummy/app/models/quotation.rb +0 -5
- data/test/dummy/app/models/section.rb +0 -6
- data/test/dummy/app/models/skipper.rb +0 -6
- data/test/dummy/app/models/song.rb +0 -32
- data/test/dummy/app/models/thing.rb +0 -3
- data/test/dummy/app/models/translation.rb +0 -4
- data/test/dummy/app/models/whatchamajigger.rb +0 -4
- data/test/dummy/app/models/widget.rb +0 -15
- data/test/dummy/app/models/wotsit.rb +0 -8
- data/test/dummy/app/versions/joined_version.rb +0 -5
- data/test/dummy/app/versions/json_version.rb +0 -3
- data/test/dummy/app/versions/kitchen/banana_version.rb +0 -5
- data/test/dummy/app/versions/post_version.rb +0 -3
- data/test/dummy/app/views/layouts/application.html.erb +0 -14
- data/test/dummy/config.ru +0 -4
- data/test/dummy/config/application.rb +0 -69
- data/test/dummy/config/boot.rb +0 -10
- data/test/dummy/config/database.mysql.yml +0 -19
- data/test/dummy/config/database.postgres.yml +0 -15
- data/test/dummy/config/database.sqlite.yml +0 -15
- data/test/dummy/config/environment.rb +0 -5
- data/test/dummy/config/environments/development.rb +0 -40
- data/test/dummy/config/environments/production.rb +0 -73
- data/test/dummy/config/environments/test.rb +0 -41
- data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/test/dummy/config/initializers/inflections.rb +0 -10
- data/test/dummy/config/initializers/mime_types.rb +0 -5
- data/test/dummy/config/initializers/paper_trail.rb +0 -10
- data/test/dummy/config/initializers/secret_token.rb +0 -7
- data/test/dummy/config/initializers/session_store.rb +0 -8
- data/test/dummy/config/locales/en.yml +0 -5
- data/test/dummy/config/routes.rb +0 -4
- data/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb +0 -287
- data/test/dummy/db/schema.rb +0 -246
- data/test/dummy/script/rails +0 -6
- data/test/functional/controller_test.rb +0 -91
- data/test/functional/enabled_for_controller_test.rb +0 -29
- data/test/functional/modular_sinatra_test.rb +0 -48
- data/test/functional/sinatra_test.rb +0 -49
- data/test/functional/thread_safety_test.rb +0 -48
- data/test/paper_trail_test.rb +0 -38
- data/test/test_helper.rb +0 -105
- data/test/time_travel_helper.rb +0 -15
- data/test/unit/associations_test.rb +0 -726
- data/test/unit/cleaner_test.rb +0 -182
- data/test/unit/inheritance_column_test.rb +0 -43
- data/test/unit/model_test.rb +0 -1373
- data/test/unit/protected_attrs_test.rb +0 -47
- data/test/unit/serializer_test.rb +0 -117
- data/test/unit/serializers/json_test.rb +0 -88
- data/test/unit/serializers/mixin_json_test.rb +0 -36
- data/test/unit/serializers/mixin_yaml_test.rb +0 -49
- data/test/unit/serializers/yaml_test.rb +0 -52
- data/test/unit/timestamp_test.rb +0 -43
- data/test/unit/version_test.rb +0 -119
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4882c535d132836d08cb122573826e8325286d5e
|
4
|
+
data.tar.gz: 629910ced4c4c38c6c2f6b5e1c695c2ca9c62057
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 042dfb1f7c98f3cb8e2fd153e28751282296e8e80a533a3f8a0afa06364c2bf6f7752b3500bd402b663d8b06acd574b8f8384a3cc2624f8633b7294398b416a9
|
7
|
+
data.tar.gz: 20c950e1a32aead04e4baacfb9be6d2a2694f0eaf626f6111b456907eac4ae2b3f7f4f8f13dff18294504680fb88af0a46f2ed4a36808faf8fd741d2af2f4ee5
|
@@ -1,40 +1,114 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "rails/generators"
|
2
|
+
require "rails/generators/active_record"
|
3
3
|
|
4
4
|
module PaperTrail
|
5
|
+
# Installs PaperTrail in a rails app.
|
5
6
|
class InstallGenerator < ::Rails::Generators::Base
|
6
7
|
include ::Rails::Generators::Migration
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
# Class names of MySQL adapters.
|
10
|
+
# - `MysqlAdapter` - Used by gems: `mysql`, `activerecord-jdbcmysql-adapter`.
|
11
|
+
# - `Mysql2Adapter` - Used by `mysql2` gem.
|
12
|
+
MYSQL_ADAPTERS = [
|
13
|
+
"ActiveRecord::ConnectionAdapters::MysqlAdapter",
|
14
|
+
"ActiveRecord::ConnectionAdapters::Mysql2Adapter"
|
15
|
+
].freeze
|
13
16
|
|
14
|
-
|
17
|
+
source_root File.expand_path("../templates", __FILE__)
|
18
|
+
class_option(
|
19
|
+
:with_changes,
|
20
|
+
type: :boolean,
|
21
|
+
default: false,
|
22
|
+
desc: "Store changeset (diff) with each version"
|
23
|
+
)
|
24
|
+
class_option(
|
25
|
+
:with_associations,
|
26
|
+
type: :boolean,
|
27
|
+
default: false,
|
28
|
+
desc: "Store transactional IDs to support association restoration"
|
29
|
+
)
|
30
|
+
|
31
|
+
desc "Generates (but does not run) a migration to add a versions table." \
|
32
|
+
" Also generates an initializer file for configuring PaperTrail"
|
15
33
|
|
16
34
|
def create_migration_file
|
17
|
-
add_paper_trail_migration(
|
18
|
-
add_paper_trail_migration(
|
35
|
+
add_paper_trail_migration("create_versions")
|
36
|
+
add_paper_trail_migration("add_object_changes_to_versions") if options.with_changes?
|
19
37
|
if options.with_associations?
|
20
|
-
add_paper_trail_migration(
|
21
|
-
add_paper_trail_migration(
|
38
|
+
add_paper_trail_migration("create_version_associations")
|
39
|
+
add_paper_trail_migration("add_transaction_id_column_to_versions")
|
22
40
|
end
|
23
41
|
end
|
24
42
|
|
43
|
+
def create_initializer
|
44
|
+
create_file(
|
45
|
+
"config/initializers/paper_trail.rb",
|
46
|
+
"PaperTrail.config.track_associations = #{!!options.with_associations?}"
|
47
|
+
)
|
48
|
+
end
|
49
|
+
|
25
50
|
def self.next_migration_number(dirname)
|
26
51
|
::ActiveRecord::Generators::Base.next_migration_number(dirname)
|
27
52
|
end
|
28
53
|
|
29
54
|
protected
|
55
|
+
|
30
56
|
def add_paper_trail_migration(template)
|
31
|
-
migration_dir = File.expand_path(
|
57
|
+
migration_dir = File.expand_path("db/migrate")
|
58
|
+
if self.class.migration_exists?(migration_dir, template)
|
59
|
+
::Kernel.warn "Migration already exists: #{template}"
|
60
|
+
else
|
61
|
+
migration_template(
|
62
|
+
"#{template}.rb.erb",
|
63
|
+
"db/migrate/#{template}.rb",
|
64
|
+
item_type_options: item_type_options,
|
65
|
+
migration_version: migration_version,
|
66
|
+
versions_table_options: versions_table_options
|
67
|
+
)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
# MySQL 5.6 utf8mb4 limit is 191 chars for keys used in indexes.
|
74
|
+
# See https://github.com/airblade/paper_trail/issues/651
|
75
|
+
def item_type_options
|
76
|
+
opt = { null: false }
|
77
|
+
opt[:limit] = 191 if mysql?
|
78
|
+
", #{opt}"
|
79
|
+
end
|
80
|
+
|
81
|
+
def migration_version
|
82
|
+
major = ActiveRecord::VERSION::MAJOR
|
83
|
+
if major >= 5
|
84
|
+
"[#{major}.#{ActiveRecord::VERSION::MINOR}]"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def mysql?
|
89
|
+
MYSQL_ADAPTERS.include?(ActiveRecord::Base.connection.class.name)
|
90
|
+
end
|
32
91
|
|
33
|
-
|
34
|
-
|
92
|
+
# Even modern versions of MySQL still use `latin1` as the default character
|
93
|
+
# encoding. Many users are not aware of this, and run into trouble when they
|
94
|
+
# try to use PaperTrail in apps that otherwise tend to use UTF-8. Postgres, by
|
95
|
+
# comparison, uses UTF-8 except in the unusual case where the OS is configured
|
96
|
+
# with a custom locale.
|
97
|
+
#
|
98
|
+
# - https://dev.mysql.com/doc/refman/5.7/en/charset-applications.html
|
99
|
+
# - http://www.postgresql.org/docs/9.4/static/multibyte.html
|
100
|
+
#
|
101
|
+
# Furthermore, MySQL's original implementation of UTF-8 was flawed, and had
|
102
|
+
# to be fixed later by introducing a new charset, `utf8mb4`.
|
103
|
+
#
|
104
|
+
# - https://mathiasbynens.be/notes/mysql-utf8mb4
|
105
|
+
# - https://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html
|
106
|
+
#
|
107
|
+
def versions_table_options
|
108
|
+
if mysql?
|
109
|
+
', { options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci" }'
|
35
110
|
else
|
36
|
-
|
37
|
-
" Please check your migrations directory before re-running")
|
111
|
+
""
|
38
112
|
end
|
39
113
|
end
|
40
114
|
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# This migration adds the optional `object_changes` column, in which PaperTrail
|
2
|
+
# will store the `changes` diff for each update event. See the readme for
|
3
|
+
# details.
|
4
|
+
class AddObjectChangesToVersions < ActiveRecord::Migration<%= migration_version %>
|
5
|
+
# The largest text column available in all supported RDBMS.
|
6
|
+
# See `create_versions.rb` for details.
|
7
|
+
TEXT_BYTES = 1_073_741_823
|
8
|
+
|
9
|
+
def change
|
10
|
+
add_column :versions, :object_changes, :text, limit: TEXT_BYTES
|
11
|
+
end
|
12
|
+
end
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# This migration and CreateVersionAssociations provide the necessary
|
2
|
+
# schema for tracking associations.
|
3
|
+
class AddTransactionIdColumnToVersions < ActiveRecord::Migration<%= migration_version %>
|
2
4
|
def self.up
|
3
5
|
add_column :versions, :transaction_id, :integer
|
4
6
|
add_index :versions, [:transaction_id]
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# This migration and AddTransactionIdColumnToVersions provide the necessary
|
2
|
+
# schema for tracking associations.
|
3
|
+
class CreateVersionAssociations < ActiveRecord::Migration<%= migration_version %>
|
4
|
+
def self.up
|
5
|
+
create_table :version_associations do |t|
|
6
|
+
t.integer :version_id
|
7
|
+
t.string :foreign_key_name, null: false
|
8
|
+
t.integer :foreign_key_id
|
9
|
+
end
|
10
|
+
add_index :version_associations, [:version_id]
|
11
|
+
add_index :version_associations,
|
12
|
+
%i(foreign_key_name foreign_key_id),
|
13
|
+
name: "index_version_associations_on_foreign_key"
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.down
|
17
|
+
remove_index :version_associations, [:version_id]
|
18
|
+
remove_index :version_associations,
|
19
|
+
name: "index_version_associations_on_foreign_key"
|
20
|
+
drop_table :version_associations
|
21
|
+
end
|
22
|
+
end
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# This migration creates the `versions` table, the only schema PT requires.
|
2
|
+
# All other migrations PT provides are optional.
|
3
|
+
class CreateVersions < ActiveRecord::Migration<%= migration_version %>
|
2
4
|
|
3
5
|
# The largest text column available in all supported RDBMS is
|
4
6
|
# 1024^3 - 1 bytes, roughly one gibibyte. We specify a size
|
@@ -7,12 +9,12 @@ class CreateVersions < ActiveRecord::Migration
|
|
7
9
|
TEXT_BYTES = 1_073_741_823
|
8
10
|
|
9
11
|
def change
|
10
|
-
create_table :versions do |t|
|
11
|
-
t.string :item_type
|
12
|
-
t.integer :item_id, :
|
13
|
-
t.string :event, :
|
12
|
+
create_table :versions<%= versions_table_options %> do |t|
|
13
|
+
t.string :item_type<%= item_type_options %>
|
14
|
+
t.integer :item_id, null: false
|
15
|
+
t.string :event, null: false
|
14
16
|
t.string :whodunnit
|
15
|
-
t.text :object,
|
17
|
+
t.text :object, limit: TEXT_BYTES
|
16
18
|
|
17
19
|
# Known issue in MySQL: fractional second precision
|
18
20
|
# -------------------------------------------------
|
@@ -29,6 +31,6 @@ class CreateVersions < ActiveRecord::Migration
|
|
29
31
|
#
|
30
32
|
t.datetime :created_at
|
31
33
|
end
|
32
|
-
add_index :versions,
|
34
|
+
add_index :versions, %i(item_type item_id)
|
33
35
|
end
|
34
36
|
end
|
data/lib/paper_trail.rb
CHANGED
@@ -1,151 +1,184 @@
|
|
1
|
-
require
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
1
|
+
require "request_store"
|
2
|
+
require "paper_trail/cleaner"
|
3
|
+
require "paper_trail/config"
|
4
|
+
require "paper_trail/has_paper_trail"
|
5
|
+
require "paper_trail/record_history"
|
6
|
+
require "paper_trail/reifier"
|
7
|
+
require "paper_trail/version_association_concern"
|
8
|
+
require "paper_trail/version_concern"
|
9
|
+
require "paper_trail/version_number"
|
10
|
+
require "paper_trail/serializers/json"
|
11
|
+
require "paper_trail/serializers/yaml"
|
12
|
+
|
13
|
+
# An ActiveRecord extension that tracks changes to your models, for auditing or
|
14
|
+
# versioning.
|
13
15
|
module PaperTrail
|
14
16
|
extend PaperTrail::Cleaner
|
15
17
|
|
16
|
-
# Switches PaperTrail on or off.
|
17
|
-
def self.enabled=(value)
|
18
|
-
PaperTrail.config.enabled = value
|
19
|
-
end
|
20
|
-
|
21
|
-
# Returns `true` if PaperTrail is on, `false` otherwise.
|
22
|
-
# PaperTrail is enabled by default.
|
23
|
-
def self.enabled?
|
24
|
-
!!PaperTrail.config.enabled
|
25
|
-
end
|
26
|
-
|
27
|
-
def self.serialized_attributes?
|
28
|
-
ActiveSupport::Deprecation.warn(
|
29
|
-
"PaperTrail.serialized_attributes? is deprecated without replacement " +
|
30
|
-
"and always returns false."
|
31
|
-
)
|
32
|
-
false
|
33
|
-
end
|
34
|
-
|
35
|
-
# Sets whether PaperTrail is enabled or disabled for the current request.
|
36
|
-
def self.enabled_for_controller=(value)
|
37
|
-
paper_trail_store[:request_enabled_for_controller] = value
|
38
|
-
end
|
39
|
-
|
40
|
-
# Returns `true` if PaperTrail is enabled for the request, `false` otherwise.
|
41
|
-
#
|
42
|
-
# See `PaperTrail::Rails::Controller#paper_trail_enabled_for_controller`.
|
43
|
-
def self.enabled_for_controller?
|
44
|
-
!!paper_trail_store[:request_enabled_for_controller]
|
45
|
-
end
|
46
|
-
|
47
|
-
# Sets whether PaperTrail is enabled or disabled for this model in the
|
48
|
-
# current request.
|
49
|
-
def self.enabled_for_model(model, value)
|
50
|
-
paper_trail_store[:"enabled_for_#{model}"] = value
|
51
|
-
end
|
52
|
-
|
53
|
-
# Returns `true` if PaperTrail is enabled for this model in the current
|
54
|
-
# request, `false` otherwise.
|
55
|
-
def self.enabled_for_model?(model)
|
56
|
-
!!paper_trail_store.fetch(:"enabled_for_#{model}", true)
|
57
|
-
end
|
58
|
-
|
59
|
-
# Set the field which records when a version was created.
|
60
|
-
def self.timestamp_field=(field_name)
|
61
|
-
PaperTrail.config.timestamp_field = field_name
|
62
|
-
end
|
63
|
-
|
64
|
-
# Returns the field which records when a version was created.
|
65
|
-
def self.timestamp_field
|
66
|
-
PaperTrail.config.timestamp_field
|
67
|
-
end
|
68
|
-
|
69
|
-
# Sets who is responsible for any changes that occur. You would normally use
|
70
|
-
# this in a migration or on the console, when working with models directly.
|
71
|
-
# In a controller it is set automatically to the `current_user`.
|
72
|
-
def self.whodunnit=(value)
|
73
|
-
paper_trail_store[:whodunnit] = value
|
74
|
-
end
|
75
|
-
|
76
|
-
# Returns who is reponsible for any changes that occur.
|
77
|
-
def self.whodunnit
|
78
|
-
paper_trail_store[:whodunnit]
|
79
|
-
end
|
80
|
-
|
81
|
-
# Sets any information from the controller that you want PaperTrail to
|
82
|
-
# store. By default this is set automatically by a before filter.
|
83
|
-
def self.controller_info=(value)
|
84
|
-
paper_trail_store[:controller_info] = value
|
85
|
-
end
|
86
|
-
|
87
|
-
# Returns any information from the controller that you want
|
88
|
-
# PaperTrail to store.
|
89
|
-
#
|
90
|
-
# See `PaperTrail::Rails::Controller#info_for_paper_trail`.
|
91
|
-
def self.controller_info
|
92
|
-
paper_trail_store[:controller_info]
|
93
|
-
end
|
94
|
-
|
95
|
-
# Getter and Setter for PaperTrail Serializer
|
96
|
-
def self.serializer=(value)
|
97
|
-
PaperTrail.config.serializer = value
|
98
|
-
end
|
99
|
-
|
100
|
-
def self.serializer
|
101
|
-
PaperTrail.config.serializer
|
102
|
-
end
|
103
|
-
|
104
|
-
def self.active_record_protected_attributes?
|
105
|
-
@active_record_protected_attributes ||= ::ActiveRecord::VERSION::MAJOR < 4 ||
|
106
|
-
!!defined?(ProtectedAttributes)
|
107
|
-
end
|
108
|
-
|
109
|
-
def self.transaction?
|
110
|
-
::ActiveRecord::Base.connection.open_transactions > 0
|
111
|
-
end
|
112
|
-
|
113
|
-
def self.transaction_id
|
114
|
-
paper_trail_store[:transaction_id]
|
115
|
-
end
|
116
|
-
|
117
|
-
def self.transaction_id=(id)
|
118
|
-
paper_trail_store[:transaction_id] = id
|
119
|
-
end
|
120
|
-
|
121
|
-
private
|
122
|
-
|
123
|
-
# Thread-safe hash to hold PaperTrail's data. Initializing with needed
|
124
|
-
# default values.
|
125
|
-
def self.paper_trail_store
|
126
|
-
RequestStore.store[:paper_trail] ||= { :request_enabled_for_controller => true }
|
127
|
-
end
|
128
|
-
|
129
|
-
# Returns PaperTrail's configuration object.
|
130
|
-
def self.config
|
131
|
-
@config ||= PaperTrail::Config.instance
|
132
|
-
yield @config if block_given?
|
133
|
-
@config
|
134
|
-
end
|
135
|
-
|
136
18
|
class << self
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
#
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
#
|
19
|
+
# @api private
|
20
|
+
def clear_transaction_id
|
21
|
+
self.transaction_id = nil
|
22
|
+
end
|
23
|
+
|
24
|
+
# Switches PaperTrail on or off.
|
25
|
+
# @api public
|
26
|
+
def enabled=(value)
|
27
|
+
PaperTrail.config.enabled = value
|
28
|
+
end
|
29
|
+
|
30
|
+
# Returns `true` if PaperTrail is on, `false` otherwise.
|
31
|
+
# PaperTrail is enabled by default.
|
32
|
+
# @api public
|
33
|
+
def enabled?
|
34
|
+
!!PaperTrail.config.enabled
|
35
|
+
end
|
36
|
+
|
37
|
+
# Sets whether PaperTrail is enabled or disabled for the current request.
|
38
|
+
# @api public
|
39
|
+
def enabled_for_controller=(value)
|
40
|
+
paper_trail_store[:request_enabled_for_controller] = value
|
41
|
+
end
|
42
|
+
|
43
|
+
# Returns `true` if PaperTrail is enabled for the request, `false` otherwise.
|
44
|
+
#
|
45
|
+
# See `PaperTrail::Rails::Controller#paper_trail_enabled_for_controller`.
|
46
|
+
# @api public
|
47
|
+
def enabled_for_controller?
|
48
|
+
!!paper_trail_store[:request_enabled_for_controller]
|
49
|
+
end
|
50
|
+
|
51
|
+
# Sets whether PaperTrail is enabled or disabled for this model in the
|
52
|
+
# current request.
|
53
|
+
# @api public
|
54
|
+
def enabled_for_model(model, value)
|
55
|
+
paper_trail_store[:"enabled_for_#{model}"] = value
|
56
|
+
end
|
57
|
+
|
58
|
+
# Returns `true` if PaperTrail is enabled for this model in the current
|
59
|
+
# request, `false` otherwise.
|
60
|
+
# @api public
|
61
|
+
def enabled_for_model?(model)
|
62
|
+
!!paper_trail_store.fetch(:"enabled_for_#{model}", true)
|
63
|
+
end
|
64
|
+
|
65
|
+
# Returns a `::Gem::Version`, convenient for comparisons. This is
|
66
|
+
# recommended over `::PaperTrail::VERSION::STRING`.
|
67
|
+
# @api public
|
68
|
+
def gem_version
|
69
|
+
::Gem::Version.new(VERSION::STRING)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Set the field which records when a version was created.
|
73
|
+
# @api public
|
74
|
+
def timestamp_field=(_field_name)
|
75
|
+
raise(
|
76
|
+
"PaperTrail.timestamp_field= has been removed, without replacement. " \
|
77
|
+
"It is no longer configurable. The timestamp field in the versions table " \
|
78
|
+
"must now be named created_at."
|
79
|
+
)
|
80
|
+
end
|
81
|
+
|
82
|
+
# Sets who is responsible for any changes that occur. You would normally use
|
83
|
+
# this in a migration or on the console, when working with models directly.
|
84
|
+
# In a controller it is set automatically to the `current_user`.
|
85
|
+
# @api public
|
86
|
+
def whodunnit=(value)
|
87
|
+
paper_trail_store[:whodunnit] = value
|
88
|
+
end
|
89
|
+
|
90
|
+
# If nothing passed, returns who is reponsible for any changes that occur.
|
91
|
+
#
|
92
|
+
# PaperTrail.whodunnit = "someone"
|
93
|
+
# PaperTrail.whodunnit # => "someone"
|
94
|
+
#
|
95
|
+
# If value and block passed, set this value as whodunnit for the duration of the block
|
96
|
+
#
|
97
|
+
# PaperTrail.whodunnit("me") do
|
98
|
+
# puts PaperTrail.whodunnit # => "me"
|
99
|
+
# end
|
100
|
+
#
|
101
|
+
# @api public
|
102
|
+
def whodunnit(value = nil)
|
103
|
+
if value
|
104
|
+
raise ArgumentError, "no block given" unless block_given?
|
105
|
+
|
106
|
+
previous_whodunnit = paper_trail_store[:whodunnit]
|
107
|
+
paper_trail_store[:whodunnit] = value
|
108
|
+
|
109
|
+
begin
|
110
|
+
yield
|
111
|
+
ensure
|
112
|
+
paper_trail_store[:whodunnit] = previous_whodunnit
|
113
|
+
end
|
114
|
+
elsif paper_trail_store[:whodunnit].respond_to?(:call)
|
115
|
+
paper_trail_store[:whodunnit].call
|
116
|
+
else
|
117
|
+
paper_trail_store[:whodunnit]
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
# Sets any information from the controller that you want PaperTrail to
|
122
|
+
# store. By default this is set automatically by a before filter.
|
123
|
+
# @api public
|
124
|
+
def controller_info=(value)
|
125
|
+
paper_trail_store[:controller_info] = value
|
126
|
+
end
|
127
|
+
|
128
|
+
# Returns any information from the controller that you want
|
129
|
+
# PaperTrail to store.
|
130
|
+
#
|
131
|
+
# See `PaperTrail::Rails::Controller#info_for_paper_trail`.
|
132
|
+
# @api public
|
133
|
+
def controller_info
|
134
|
+
paper_trail_store[:controller_info]
|
135
|
+
end
|
136
|
+
|
137
|
+
# Getter and Setter for PaperTrail Serializer
|
138
|
+
# @api public
|
139
|
+
def serializer=(value)
|
140
|
+
PaperTrail.config.serializer = value
|
141
|
+
end
|
142
|
+
|
143
|
+
# @api public
|
144
|
+
def serializer
|
145
|
+
PaperTrail.config.serializer
|
146
|
+
end
|
147
|
+
|
148
|
+
# @api public
|
149
|
+
def transaction?
|
150
|
+
::ActiveRecord::Base.connection.open_transactions > 0
|
151
|
+
end
|
152
|
+
|
153
|
+
# @api public
|
154
|
+
def transaction_id
|
155
|
+
paper_trail_store[:transaction_id]
|
156
|
+
end
|
157
|
+
|
158
|
+
# @api public
|
159
|
+
def transaction_id=(id)
|
160
|
+
paper_trail_store[:transaction_id] = id
|
161
|
+
end
|
162
|
+
|
163
|
+
# Thread-safe hash to hold PaperTrail's data. Initializing with needed
|
164
|
+
# default values.
|
165
|
+
# @api private
|
166
|
+
def paper_trail_store
|
167
|
+
RequestStore.store[:paper_trail] ||= { request_enabled_for_controller: true }
|
168
|
+
end
|
169
|
+
|
170
|
+
# Returns PaperTrail's configuration object.
|
171
|
+
# @api private
|
172
|
+
def config
|
173
|
+
@config ||= PaperTrail::Config.instance
|
174
|
+
yield @config if block_given?
|
175
|
+
@config
|
176
|
+
end
|
177
|
+
alias configure config
|
178
|
+
|
179
|
+
def version
|
180
|
+
VERSION::STRING
|
181
|
+
end
|
149
182
|
end
|
150
183
|
end
|
151
184
|
|
@@ -154,9 +187,8 @@ ActiveSupport.on_load(:active_record) do
|
|
154
187
|
end
|
155
188
|
|
156
189
|
# Require frameworks
|
157
|
-
|
158
|
-
|
159
|
-
require 'paper_trail/frameworks/rails'
|
190
|
+
if defined?(::Rails) && ActiveRecord::VERSION::STRING >= "3.2"
|
191
|
+
require "paper_trail/frameworks/rails"
|
160
192
|
else
|
161
|
-
require
|
193
|
+
require "paper_trail/frameworks/active_record"
|
162
194
|
end
|