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.
Files changed (171) hide show
  1. checksums.yaml +4 -4
  2. data/lib/generators/paper_trail/install_generator.rb +91 -17
  3. data/lib/generators/paper_trail/templates/add_object_changes_to_versions.rb.erb +12 -0
  4. data/lib/generators/paper_trail/templates/{add_transaction_id_column_to_versions.rb → add_transaction_id_column_to_versions.rb.erb} +3 -1
  5. data/lib/generators/paper_trail/templates/create_version_associations.rb.erb +22 -0
  6. data/lib/generators/paper_trail/templates/{create_versions.rb → create_versions.rb.erb} +9 -7
  7. data/lib/paper_trail.rb +180 -148
  8. data/lib/paper_trail/attribute_serializers/README.md +10 -0
  9. data/lib/paper_trail/attribute_serializers/cast_attribute_serializer.rb +80 -0
  10. data/lib/paper_trail/attribute_serializers/legacy_active_record_shim.rb +48 -0
  11. data/lib/paper_trail/attribute_serializers/object_attribute.rb +39 -0
  12. data/lib/paper_trail/attribute_serializers/object_changes_attribute.rb +42 -0
  13. data/lib/paper_trail/cleaner.rb +16 -10
  14. data/lib/paper_trail/config.rb +28 -27
  15. data/lib/paper_trail/frameworks/active_record/models/paper_trail/version.rb +5 -1
  16. data/lib/paper_trail/frameworks/active_record/models/paper_trail/version_association.rb +6 -2
  17. data/lib/paper_trail/frameworks/cucumber.rb +1 -0
  18. data/lib/paper_trail/frameworks/rails.rb +2 -7
  19. data/lib/paper_trail/frameworks/rails/controller.rb +20 -18
  20. data/lib/paper_trail/frameworks/rails/engine.rb +6 -1
  21. data/lib/paper_trail/frameworks/rspec.rb +17 -6
  22. data/lib/paper_trail/frameworks/rspec/helpers.rb +3 -1
  23. data/lib/paper_trail/has_paper_trail.rb +25 -503
  24. data/lib/paper_trail/model_config.rb +207 -0
  25. data/lib/paper_trail/queries/versions/where_object.rb +60 -0
  26. data/lib/paper_trail/queries/versions/where_object_changes.rb +68 -0
  27. data/lib/paper_trail/record_history.rb +2 -12
  28. data/lib/paper_trail/record_trail.rb +573 -0
  29. data/lib/paper_trail/reifier.rb +164 -215
  30. data/lib/paper_trail/reifiers/belongs_to.rb +48 -0
  31. data/lib/paper_trail/reifiers/has_and_belongs_to_many.rb +50 -0
  32. data/lib/paper_trail/reifiers/has_many.rb +110 -0
  33. data/lib/paper_trail/reifiers/has_many_through.rb +90 -0
  34. data/lib/paper_trail/reifiers/has_one.rb +76 -0
  35. data/lib/paper_trail/serializers/json.rb +16 -7
  36. data/lib/paper_trail/serializers/yaml.rb +9 -13
  37. data/lib/paper_trail/version_association_concern.rb +3 -5
  38. data/lib/paper_trail/version_concern.rb +138 -111
  39. data/lib/paper_trail/version_number.rb +10 -9
  40. metadata +95 -327
  41. data/.gitignore +0 -22
  42. data/.rspec +0 -2
  43. data/.travis.yml +0 -41
  44. data/CHANGELOG.md +0 -362
  45. data/CONTRIBUTING.md +0 -84
  46. data/Gemfile +0 -2
  47. data/MIT-LICENSE +0 -20
  48. data/README.md +0 -1535
  49. data/Rakefile +0 -30
  50. data/doc/bug_report_template.rb +0 -65
  51. data/gemfiles/ar3.gemfile +0 -61
  52. data/lib/generators/paper_trail/templates/add_object_changes_to_versions.rb +0 -10
  53. data/lib/generators/paper_trail/templates/create_version_associations.rb +0 -17
  54. data/lib/paper_trail/attributes_serialization.rb +0 -89
  55. data/lib/paper_trail/frameworks/sinatra.rb +0 -38
  56. data/paper_trail.gemspec +0 -59
  57. data/spec/generators/install_generator_spec.rb +0 -67
  58. data/spec/models/animal_spec.rb +0 -36
  59. data/spec/models/boolit_spec.rb +0 -48
  60. data/spec/models/callback_modifier_spec.rb +0 -96
  61. data/spec/models/fluxor_spec.rb +0 -19
  62. data/spec/models/gadget_spec.rb +0 -70
  63. data/spec/models/joined_version_spec.rb +0 -47
  64. data/spec/models/json_version_spec.rb +0 -103
  65. data/spec/models/kitchen/banana_spec.rb +0 -14
  66. data/spec/models/not_on_update_spec.rb +0 -19
  67. data/spec/models/post_with_status_spec.rb +0 -17
  68. data/spec/models/skipper_spec.rb +0 -46
  69. data/spec/models/thing_spec.rb +0 -11
  70. data/spec/models/version_spec.rb +0 -239
  71. data/spec/models/widget_spec.rb +0 -298
  72. data/spec/modules/paper_trail_spec.rb +0 -27
  73. data/spec/modules/version_concern_spec.rb +0 -32
  74. data/spec/modules/version_number_spec.rb +0 -44
  75. data/spec/paper_trail/config_spec.rb +0 -52
  76. data/spec/paper_trail_spec.rb +0 -66
  77. data/spec/rails_helper.rb +0 -34
  78. data/spec/requests/articles_spec.rb +0 -30
  79. data/spec/spec_helper.rb +0 -95
  80. data/spec/support/alt_db_init.rb +0 -59
  81. data/test/custom_json_serializer.rb +0 -13
  82. data/test/dummy/Rakefile +0 -7
  83. data/test/dummy/app/controllers/application_controller.rb +0 -20
  84. data/test/dummy/app/controllers/articles_controller.rb +0 -17
  85. data/test/dummy/app/controllers/test_controller.rb +0 -5
  86. data/test/dummy/app/controllers/widgets_controller.rb +0 -31
  87. data/test/dummy/app/helpers/application_helper.rb +0 -2
  88. data/test/dummy/app/models/animal.rb +0 -6
  89. data/test/dummy/app/models/article.rb +0 -16
  90. data/test/dummy/app/models/authorship.rb +0 -5
  91. data/test/dummy/app/models/book.rb +0 -9
  92. data/test/dummy/app/models/boolit.rb +0 -4
  93. data/test/dummy/app/models/callback_modifier.rb +0 -45
  94. data/test/dummy/app/models/cat.rb +0 -2
  95. data/test/dummy/app/models/chapter.rb +0 -9
  96. data/test/dummy/app/models/citation.rb +0 -5
  97. data/test/dummy/app/models/customer.rb +0 -4
  98. data/test/dummy/app/models/document.rb +0 -4
  99. data/test/dummy/app/models/dog.rb +0 -2
  100. data/test/dummy/app/models/editor.rb +0 -4
  101. data/test/dummy/app/models/editorship.rb +0 -5
  102. data/test/dummy/app/models/elephant.rb +0 -3
  103. data/test/dummy/app/models/fluxor.rb +0 -3
  104. data/test/dummy/app/models/foo_widget.rb +0 -2
  105. data/test/dummy/app/models/fruit.rb +0 -5
  106. data/test/dummy/app/models/gadget.rb +0 -3
  107. data/test/dummy/app/models/kitchen/banana.rb +0 -5
  108. data/test/dummy/app/models/legacy_widget.rb +0 -4
  109. data/test/dummy/app/models/line_item.rb +0 -4
  110. data/test/dummy/app/models/not_on_update.rb +0 -4
  111. data/test/dummy/app/models/order.rb +0 -5
  112. data/test/dummy/app/models/paragraph.rb +0 -5
  113. data/test/dummy/app/models/person.rb +0 -38
  114. data/test/dummy/app/models/post.rb +0 -3
  115. data/test/dummy/app/models/post_with_status.rb +0 -8
  116. data/test/dummy/app/models/protected_widget.rb +0 -3
  117. data/test/dummy/app/models/quotation.rb +0 -5
  118. data/test/dummy/app/models/section.rb +0 -6
  119. data/test/dummy/app/models/skipper.rb +0 -6
  120. data/test/dummy/app/models/song.rb +0 -32
  121. data/test/dummy/app/models/thing.rb +0 -3
  122. data/test/dummy/app/models/translation.rb +0 -4
  123. data/test/dummy/app/models/whatchamajigger.rb +0 -4
  124. data/test/dummy/app/models/widget.rb +0 -15
  125. data/test/dummy/app/models/wotsit.rb +0 -8
  126. data/test/dummy/app/versions/joined_version.rb +0 -5
  127. data/test/dummy/app/versions/json_version.rb +0 -3
  128. data/test/dummy/app/versions/kitchen/banana_version.rb +0 -5
  129. data/test/dummy/app/versions/post_version.rb +0 -3
  130. data/test/dummy/app/views/layouts/application.html.erb +0 -14
  131. data/test/dummy/config.ru +0 -4
  132. data/test/dummy/config/application.rb +0 -69
  133. data/test/dummy/config/boot.rb +0 -10
  134. data/test/dummy/config/database.mysql.yml +0 -19
  135. data/test/dummy/config/database.postgres.yml +0 -15
  136. data/test/dummy/config/database.sqlite.yml +0 -15
  137. data/test/dummy/config/environment.rb +0 -5
  138. data/test/dummy/config/environments/development.rb +0 -40
  139. data/test/dummy/config/environments/production.rb +0 -73
  140. data/test/dummy/config/environments/test.rb +0 -41
  141. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  142. data/test/dummy/config/initializers/inflections.rb +0 -10
  143. data/test/dummy/config/initializers/mime_types.rb +0 -5
  144. data/test/dummy/config/initializers/paper_trail.rb +0 -10
  145. data/test/dummy/config/initializers/secret_token.rb +0 -7
  146. data/test/dummy/config/initializers/session_store.rb +0 -8
  147. data/test/dummy/config/locales/en.yml +0 -5
  148. data/test/dummy/config/routes.rb +0 -4
  149. data/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb +0 -287
  150. data/test/dummy/db/schema.rb +0 -246
  151. data/test/dummy/script/rails +0 -6
  152. data/test/functional/controller_test.rb +0 -91
  153. data/test/functional/enabled_for_controller_test.rb +0 -29
  154. data/test/functional/modular_sinatra_test.rb +0 -48
  155. data/test/functional/sinatra_test.rb +0 -49
  156. data/test/functional/thread_safety_test.rb +0 -48
  157. data/test/paper_trail_test.rb +0 -38
  158. data/test/test_helper.rb +0 -105
  159. data/test/time_travel_helper.rb +0 -15
  160. data/test/unit/associations_test.rb +0 -726
  161. data/test/unit/cleaner_test.rb +0 -182
  162. data/test/unit/inheritance_column_test.rb +0 -43
  163. data/test/unit/model_test.rb +0 -1373
  164. data/test/unit/protected_attrs_test.rb +0 -47
  165. data/test/unit/serializer_test.rb +0 -117
  166. data/test/unit/serializers/json_test.rb +0 -88
  167. data/test/unit/serializers/mixin_json_test.rb +0 -36
  168. data/test/unit/serializers/mixin_yaml_test.rb +0 -49
  169. data/test/unit/serializers/yaml_test.rb +0 -52
  170. data/test/unit/timestamp_test.rb +0 -43
  171. data/test/unit/version_test.rb +0 -119
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e7937d44654d4f77422ffe61d4bc2d81b7ed4026
4
- data.tar.gz: 9ec1f8bc013795818775e58e1cd9c33e5dd36499
3
+ metadata.gz: 4882c535d132836d08cb122573826e8325286d5e
4
+ data.tar.gz: 629910ced4c4c38c6c2f6b5e1c695c2ca9c62057
5
5
  SHA512:
6
- metadata.gz: e546ad5e910cc2a08aeaaf0dca6754e05f3a89fbfb022b49a078025e09a623e359a552c2e52f3126c84662fad2cd804c34db709689211852ff43e32ff40db3a1
7
- data.tar.gz: 0ef05118041333567e076bc8407330917cc36ec5d23aec22fd6dced9367b10a466c4284eccddbb3f4d0675c4154165f24097eafe4acf0135e133c426a2b71d4b
6
+ metadata.gz: 042dfb1f7c98f3cb8e2fd153e28751282296e8e80a533a3f8a0afa06364c2bf6f7752b3500bd402b663d8b06acd574b8f8384a3cc2624f8633b7294398b416a9
7
+ data.tar.gz: 20c950e1a32aead04e4baacfb9be6d2a2694f0eaf626f6111b456907eac4ae2b3f7f4f8f13dff18294504680fb88af0a46f2ed4a36808faf8fd741d2af2f4ee5
@@ -1,40 +1,114 @@
1
- require 'rails/generators'
2
- require 'rails/generators/active_record'
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
- source_root File.expand_path('../templates', __FILE__)
9
- class_option :with_changes, :type => :boolean, :default => false,
10
- :desc => "Store changeset (diff) with each version"
11
- class_option :with_associations, :type => :boolean, :default => false,
12
- :desc => "Store transactional IDs to support association restoration"
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
- desc 'Generates (but does not run) a migration to add a versions table.'
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('create_versions')
18
- add_paper_trail_migration('add_object_changes_to_versions') if options.with_changes?
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('create_version_associations')
21
- add_paper_trail_migration('add_transaction_id_column_to_versions')
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('db/migrate')
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
- unless self.class.migration_exists?(migration_dir, template)
34
- migration_template "#{template}.rb", "db/migrate/#{template}.rb"
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
- warn("ALERT: Migration already exists named '#{template}'." +
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
- class AddTransactionIdColumnToVersions < ActiveRecord::Migration
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
- class CreateVersions < ActiveRecord::Migration
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, :null => false
12
- t.integer :item_id, :null => false
13
- t.string :event, :null => false
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, :limit => TEXT_BYTES
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, [:item_type, :item_id]
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 'request_store'
2
-
3
- # Require files in lib/paper_trail, but not its subdirectories.
4
- Dir[File.join(File.dirname(__FILE__), 'paper_trail', '*.rb')].each do |file|
5
- require File.join('paper_trail', File.basename(file, '.rb'))
6
- end
7
-
8
- # Require serializers
9
- Dir[File.join(File.dirname(__FILE__), 'paper_trail', 'serializers', '*.rb')].each do |file|
10
- require File.join('paper_trail', 'serializers', File.basename(file, '.rb'))
11
- end
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
- alias_method :configure, :config
138
- end
139
- end
140
-
141
- # Ensure `ProtectedAttributes` gem gets required if it is available before the
142
- # `Version` class gets loaded in.
143
- unless PaperTrail.active_record_protected_attributes?
144
- PaperTrail.send(:remove_instance_variable, :@active_record_protected_attributes)
145
- begin
146
- require 'protected_attributes'
147
- rescue LoadError
148
- # In case `ProtectedAttributes` gem is not available.
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
- require 'paper_trail/frameworks/sinatra'
158
- if defined?(::Rails) && ActiveRecord::VERSION::STRING >= '3.2'
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 'paper_trail/frameworks/active_record'
193
+ require "paper_trail/frameworks/active_record"
162
194
  end