paper_trail 4.0.2 → 10.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (170) hide show
  1. checksums.yaml +5 -5
  2. data/lib/generators/paper_trail/install/USAGE +3 -0
  3. data/lib/generators/paper_trail/install/install_generator.rb +71 -0
  4. data/lib/generators/paper_trail/install/templates/add_object_changes_to_versions.rb.erb +12 -0
  5. data/lib/generators/paper_trail/install/templates/create_versions.rb.erb +36 -0
  6. data/lib/generators/paper_trail/migration_generator.rb +37 -0
  7. data/lib/generators/paper_trail/update_item_subtype/USAGE +4 -0
  8. data/lib/generators/paper_trail/update_item_subtype/templates/update_versions_for_item_subtype.rb.erb +85 -0
  9. data/lib/generators/paper_trail/update_item_subtype/update_item_subtype_generator.rb +17 -0
  10. data/lib/paper_trail.rb +138 -146
  11. data/lib/paper_trail/attribute_serializers/README.md +10 -0
  12. data/lib/paper_trail/attribute_serializers/attribute_serializer_factory.rb +27 -0
  13. data/lib/paper_trail/attribute_serializers/cast_attribute_serializer.rb +83 -0
  14. data/lib/paper_trail/attribute_serializers/object_attribute.rb +41 -0
  15. data/lib/paper_trail/attribute_serializers/object_changes_attribute.rb +44 -0
  16. data/lib/paper_trail/cleaner.rb +24 -9
  17. data/lib/paper_trail/compatibility.rb +51 -0
  18. data/lib/paper_trail/config.rb +55 -33
  19. data/lib/paper_trail/events/base.rb +320 -0
  20. data/lib/paper_trail/events/create.rb +32 -0
  21. data/lib/paper_trail/events/destroy.rb +42 -0
  22. data/lib/paper_trail/events/update.rb +60 -0
  23. data/lib/paper_trail/frameworks/active_record.rb +2 -1
  24. data/lib/paper_trail/frameworks/active_record/models/paper_trail/version.rb +10 -1
  25. data/lib/paper_trail/frameworks/cucumber.rb +6 -3
  26. data/lib/paper_trail/frameworks/rails.rb +3 -6
  27. data/lib/paper_trail/frameworks/rails/controller.rb +41 -10
  28. data/lib/paper_trail/frameworks/rails/engine.rb +39 -1
  29. data/lib/paper_trail/frameworks/rspec.rb +22 -9
  30. data/lib/paper_trail/frameworks/rspec/helpers.rb +5 -1
  31. data/lib/paper_trail/has_paper_trail.rb +37 -500
  32. data/lib/paper_trail/model_config.rb +256 -0
  33. data/lib/paper_trail/queries/versions/where_object.rb +65 -0
  34. data/lib/paper_trail/queries/versions/where_object_changes.rb +75 -0
  35. data/lib/paper_trail/record_history.rb +51 -0
  36. data/lib/paper_trail/record_trail.rb +300 -0
  37. data/lib/paper_trail/reifier.rb +132 -0
  38. data/lib/paper_trail/request.rb +166 -0
  39. data/lib/paper_trail/serializers/json.rb +13 -14
  40. data/lib/paper_trail/serializers/yaml.rb +21 -15
  41. data/lib/paper_trail/type_serializers/postgres_array_serializer.rb +48 -0
  42. data/lib/paper_trail/version_concern.rb +223 -329
  43. data/lib/paper_trail/version_number.rb +14 -9
  44. metadata +99 -320
  45. data/.gitignore +0 -21
  46. data/.rspec +0 -2
  47. data/.travis.yml +0 -39
  48. data/CHANGELOG.md +0 -319
  49. data/CONTRIBUTING.md +0 -11
  50. data/Gemfile +0 -2
  51. data/MIT-LICENSE +0 -20
  52. data/README.md +0 -1476
  53. data/Rakefile +0 -30
  54. data/gemfiles/3.0.gemfile +0 -60
  55. data/lib/generators/paper_trail/USAGE +0 -2
  56. data/lib/generators/paper_trail/install_generator.rb +0 -41
  57. data/lib/generators/paper_trail/templates/add_object_changes_to_versions.rb +0 -10
  58. data/lib/generators/paper_trail/templates/add_transaction_id_column_to_versions.rb +0 -11
  59. data/lib/generators/paper_trail/templates/create_version_associations.rb +0 -17
  60. data/lib/generators/paper_trail/templates/create_versions.rb +0 -20
  61. data/lib/paper_trail/frameworks/active_record/models/paper_trail/version_association.rb +0 -7
  62. data/lib/paper_trail/frameworks/sinatra.rb +0 -38
  63. data/lib/paper_trail/version_association_concern.rb +0 -13
  64. data/paper_trail.gemspec +0 -62
  65. data/spec/generators/install_generator_spec.rb +0 -67
  66. data/spec/models/animal_spec.rb +0 -19
  67. data/spec/models/boolit_spec.rb +0 -48
  68. data/spec/models/fluxor_spec.rb +0 -19
  69. data/spec/models/gadget_spec.rb +0 -70
  70. data/spec/models/joined_version_spec.rb +0 -47
  71. data/spec/models/json_version_spec.rb +0 -100
  72. data/spec/models/kitchen/banana_spec.rb +0 -14
  73. data/spec/models/not_on_update_spec.rb +0 -19
  74. data/spec/models/post_with_status_spec.rb +0 -17
  75. data/spec/models/skipper_spec.rb +0 -46
  76. data/spec/models/thing_spec.rb +0 -11
  77. data/spec/models/version_spec.rb +0 -239
  78. data/spec/models/widget_spec.rb +0 -298
  79. data/spec/modules/paper_trail_spec.rb +0 -27
  80. data/spec/modules/version_concern_spec.rb +0 -32
  81. data/spec/modules/version_number_spec.rb +0 -44
  82. data/spec/paper_trail_spec.rb +0 -66
  83. data/spec/rails_helper.rb +0 -34
  84. data/spec/requests/articles_spec.rb +0 -30
  85. data/spec/spec_helper.rb +0 -89
  86. data/spec/support/alt_db_init.rb +0 -59
  87. data/test/custom_json_serializer.rb +0 -13
  88. data/test/dummy/Rakefile +0 -7
  89. data/test/dummy/app/controllers/application_controller.rb +0 -20
  90. data/test/dummy/app/controllers/articles_controller.rb +0 -17
  91. data/test/dummy/app/controllers/test_controller.rb +0 -5
  92. data/test/dummy/app/controllers/widgets_controller.rb +0 -31
  93. data/test/dummy/app/helpers/application_helper.rb +0 -2
  94. data/test/dummy/app/models/animal.rb +0 -6
  95. data/test/dummy/app/models/article.rb +0 -16
  96. data/test/dummy/app/models/authorship.rb +0 -5
  97. data/test/dummy/app/models/book.rb +0 -9
  98. data/test/dummy/app/models/boolit.rb +0 -4
  99. data/test/dummy/app/models/cat.rb +0 -2
  100. data/test/dummy/app/models/customer.rb +0 -4
  101. data/test/dummy/app/models/document.rb +0 -4
  102. data/test/dummy/app/models/dog.rb +0 -2
  103. data/test/dummy/app/models/editor.rb +0 -4
  104. data/test/dummy/app/models/editorship.rb +0 -5
  105. data/test/dummy/app/models/elephant.rb +0 -3
  106. data/test/dummy/app/models/fluxor.rb +0 -3
  107. data/test/dummy/app/models/foo_widget.rb +0 -2
  108. data/test/dummy/app/models/fruit.rb +0 -5
  109. data/test/dummy/app/models/gadget.rb +0 -3
  110. data/test/dummy/app/models/kitchen/banana.rb +0 -5
  111. data/test/dummy/app/models/legacy_widget.rb +0 -4
  112. data/test/dummy/app/models/line_item.rb +0 -4
  113. data/test/dummy/app/models/not_on_update.rb +0 -4
  114. data/test/dummy/app/models/order.rb +0 -5
  115. data/test/dummy/app/models/person.rb +0 -38
  116. data/test/dummy/app/models/post.rb +0 -3
  117. data/test/dummy/app/models/post_with_status.rb +0 -8
  118. data/test/dummy/app/models/protected_widget.rb +0 -3
  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 -8
  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 -254
  150. data/test/dummy/db/schema.rb +0 -219
  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 -69
  159. data/test/time_travel_helper.rb +0 -15
  160. data/test/unit/cleaner_test.rb +0 -182
  161. data/test/unit/inheritance_column_test.rb +0 -43
  162. data/test/unit/model_test.rb +0 -1905
  163. data/test/unit/protected_attrs_test.rb +0 -46
  164. data/test/unit/serializer_test.rb +0 -117
  165. data/test/unit/serializers/json_test.rb +0 -88
  166. data/test/unit/serializers/mixin_json_test.rb +0 -36
  167. data/test/unit/serializers/mixin_yaml_test.rb +0 -49
  168. data/test/unit/serializers/yaml_test.rb +0 -52
  169. data/test/unit/timestamp_test.rb +0 -43
  170. data/test/unit/version_test.rb +0 -101
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 8a92875e074ba53155be9597997716e75f2dfa33
4
- data.tar.gz: b87bb9cad5f094f61957c287e4a27db3cff0d5d1
2
+ SHA256:
3
+ metadata.gz: a08a7c456a492933cc36762913040e14bad8f468fc08889580faf9e86ade7fea
4
+ data.tar.gz: 19711dc6c6e4a438a0a97819c572df610d52db0f5c9aeba0a39eb1f44f3909c0
5
5
  SHA512:
6
- metadata.gz: f5e16e1229742e460df8c0d1c6315a8207653f756b5df16d88b82e17ae07530260a0dab228afc894aeb3dcc765a7aa15f2b1b46f9366364abbba1bcb12ed61f5
7
- data.tar.gz: e5a3ad55b824efa44f70924a47e840ccc16733308298d66eb1de4ba07010eff61ba1ad6a54c50288c18b60d92462d94d025bbd1d14bf4f496349c1a39e43ea89
6
+ metadata.gz: b39e89605a2b03889976070f0871f9dcd07544b97b6e0105b78035b5fb1ec030c7d5438dc0fe9fb16cccc8e3624cd920d37bd73066f8a4f3892bf356fa088e85
7
+ data.tar.gz: 56567a718ea4e605d32ffc2b71835b188f6cefab8dd7111b7abcdfc6d9e5b843e8468312ed4dfbc87f74607c1cd4a197ae2c11a9cb44ee2dd16a37a11303e6ad
@@ -0,0 +1,3 @@
1
+ Description:
2
+ Generates (but does not run) a migration to add a versions table. Also generates an initializer
3
+ file for configuring PaperTrail. See section 5.c. Generators in README.md for more information.
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../migration_generator"
4
+
5
+ module PaperTrail
6
+ # Installs PaperTrail in a rails app.
7
+ class InstallGenerator < MigrationGenerator
8
+ # Class names of MySQL adapters.
9
+ # - `MysqlAdapter` - Used by gems: `mysql`, `activerecord-jdbcmysql-adapter`.
10
+ # - `Mysql2Adapter` - Used by `mysql2` gem.
11
+ MYSQL_ADAPTERS = [
12
+ "ActiveRecord::ConnectionAdapters::MysqlAdapter",
13
+ "ActiveRecord::ConnectionAdapters::Mysql2Adapter"
14
+ ].freeze
15
+
16
+ source_root File.expand_path("templates", __dir__)
17
+ class_option(
18
+ :with_changes,
19
+ type: :boolean,
20
+ default: false,
21
+ desc: "Store changeset (diff) with each version"
22
+ )
23
+
24
+ desc "Generates (but does not run) a migration to add a versions table." \
25
+ " See section 5.c. Generators in README.md for more information."
26
+
27
+ def create_migration_file
28
+ add_paper_trail_migration("create_versions",
29
+ item_type_options: item_type_options,
30
+ versions_table_options: versions_table_options)
31
+ add_paper_trail_migration("add_object_changes_to_versions") if options.with_changes?
32
+ end
33
+
34
+ private
35
+
36
+ # MySQL 5.6 utf8mb4 limit is 191 chars for keys used in indexes.
37
+ # See https://github.com/paper-trail-gem/paper_trail/issues/651
38
+ def item_type_options
39
+ opt = { null: false }
40
+ opt[:limit] = 191 if mysql?
41
+ ", #{opt}"
42
+ end
43
+
44
+ def mysql?
45
+ MYSQL_ADAPTERS.include?(ActiveRecord::Base.connection.class.name)
46
+ end
47
+
48
+ # Even modern versions of MySQL still use `latin1` as the default character
49
+ # encoding. Many users are not aware of this, and run into trouble when they
50
+ # try to use PaperTrail in apps that otherwise tend to use UTF-8. Postgres, by
51
+ # comparison, uses UTF-8 except in the unusual case where the OS is configured
52
+ # with a custom locale.
53
+ #
54
+ # - https://dev.mysql.com/doc/refman/5.7/en/charset-applications.html
55
+ # - http://www.postgresql.org/docs/9.4/static/multibyte.html
56
+ #
57
+ # Furthermore, MySQL's original implementation of UTF-8 was flawed, and had
58
+ # to be fixed later by introducing a new charset, `utf8mb4`.
59
+ #
60
+ # - https://mathiasbynens.be/notes/mysql-utf8mb4
61
+ # - https://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html
62
+ #
63
+ def versions_table_options
64
+ if mysql?
65
+ ', { options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci" }'
66
+ else
67
+ ""
68
+ end
69
+ end
70
+ end
71
+ 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
@@ -0,0 +1,36 @@
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 %>
4
+
5
+ # The largest text column available in all supported RDBMS is
6
+ # 1024^3 - 1 bytes, roughly one gibibyte. We specify a size
7
+ # so that MySQL will use `longtext` instead of `text`. Otherwise,
8
+ # when serializing very large objects, `text` might not be big enough.
9
+ TEXT_BYTES = 1_073_741_823
10
+
11
+ def change
12
+ create_table :versions<%= versions_table_options %> do |t|
13
+ t.string :item_type<%= item_type_options %>
14
+ t.integer :item_id, null: false, limit: 8
15
+ t.string :event, null: false
16
+ t.string :whodunnit
17
+ t.text :object, limit: TEXT_BYTES
18
+
19
+ # Known issue in MySQL: fractional second precision
20
+ # -------------------------------------------------
21
+ #
22
+ # MySQL timestamp columns do not support fractional seconds unless
23
+ # defined with "fractional seconds precision". MySQL users should manually
24
+ # add fractional seconds precision to this migration, specifically, to
25
+ # the `created_at` column.
26
+ # (https://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html)
27
+ #
28
+ # MySQL users should also upgrade to at least rails 4.2, which is the first
29
+ # version of ActiveRecord with support for fractional seconds in MySQL.
30
+ # (https://github.com/rails/rails/pull/14359)
31
+ #
32
+ t.datetime :created_at
33
+ end
34
+ add_index :versions, %i(item_type item_id)
35
+ end
36
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators"
4
+ require "rails/generators/active_record"
5
+
6
+ module PaperTrail
7
+ # Basic structure to support a generator that builds a migration
8
+ class MigrationGenerator < ::Rails::Generators::Base
9
+ include ::Rails::Generators::Migration
10
+
11
+ def self.next_migration_number(dirname)
12
+ ::ActiveRecord::Generators::Base.next_migration_number(dirname)
13
+ end
14
+
15
+ protected
16
+
17
+ def add_paper_trail_migration(template, extra_options = {})
18
+ migration_dir = File.expand_path("db/migrate")
19
+ if self.class.migration_exists?(migration_dir, template)
20
+ ::Kernel.warn "Migration already exists: #{template}"
21
+ else
22
+ migration_template(
23
+ "#{template}.rb.erb",
24
+ "db/migrate/#{template}.rb",
25
+ { migration_version: migration_version }.merge(extra_options)
26
+ )
27
+ end
28
+ end
29
+
30
+ def migration_version
31
+ major = ActiveRecord::VERSION::MAJOR
32
+ if major >= 5
33
+ "[#{major}.#{ActiveRecord::VERSION::MINOR}]"
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,4 @@
1
+ Description:
2
+ Generates (but does not run) a migration to update item_type for STI entries
3
+ in an existing versions table. See section 5.c. Generators in README.md for
4
+ more information.
@@ -0,0 +1,85 @@
1
+ # This migration updates existing `versions` that have `item_type` that refers to
2
+ # the base_class, and changes them to refer to the subclass instead.
3
+ class UpdateVersionsForItemSubtype < ActiveRecord::Migration<%= migration_version %>
4
+ include ActionView::Helpers::TextHelper
5
+ def up
6
+ <%=
7
+ # Returns class, column, range
8
+ def self.parse_custom_entry(text)
9
+ parts = text.split("):")
10
+ range = parts.last.split("..").map(&:to_i)
11
+ range = Range.new(range.first, range.last)
12
+ parts.first.split("(") + [range]
13
+ end
14
+ # Running:
15
+ # rails g paper_trail:update_item_subtype Animal(species):1..4 Plant(genus):42..1337
16
+ # results in:
17
+ # # Versions of item_type "Animal" with IDs between 1 and 4 will be updated based on `species`
18
+ # # Versions of item_type "Plant" with IDs between 42 and 1337 will be updated based on `genus`
19
+ # hints = {"Animal"=>{1..4=>"species"}, "Plant"=>{42..1337=>"genus"}}
20
+ hint_descriptions = ""
21
+ hints = args.inject(Hash.new{|h, k| h[k] = {}}) do |s, v|
22
+ klass, column, range = parse_custom_entry(v)
23
+ hint_descriptions << " # Versions of item_type \"#{klass}\" with IDs between #{
24
+ range.first} and #{range.last} will be updated based on \`#{column}\`\n"
25
+ s[klass][range] = column
26
+ s
27
+ end
28
+
29
+ unless hints.empty?
30
+ "#{hint_descriptions} hints = #{hints.inspect}\n"
31
+ end
32
+ %>
33
+ # Find all ActiveRecord models mentioned in existing versions
34
+ changes = Hash.new { |h, k| h[k] = [] }
35
+ model_names = PaperTrail::Version.select(:item_type).distinct
36
+ model_names.map(&:item_type).each do |model_name|
37
+ hint = hints[model_name] if defined?(hints)
38
+ begin
39
+ klass = model_name.constantize
40
+ # Actually implements an inheritance_column? (Usually "type")
41
+ has_inheritance_column = klass.columns.map(&:name).include?(klass.inheritance_column)
42
+ # Find domain of types stored in PaperTrail versions
43
+ PaperTrail::Version.where(item_type: model_name, item_subtype: nil).select(:id, :object, :object_changes).each do |obj|
44
+ if (object_detail = PaperTrail.serializer.load(obj.object || obj.object_changes))
45
+ is_found = false
46
+ subtype_name = nil
47
+ hint&.each do |k, v|
48
+ if k === obj.id && (subtype_name = object_detail[v])
49
+ break
50
+ end
51
+ end
52
+ if subtype_name.nil? && has_inheritance_column
53
+ subtype_name = object_detail[klass.inheritance_column]
54
+ end
55
+ if subtype_name
56
+ subtype_name = subtype_name.last if subtype_name.is_a?(Array)
57
+ if subtype_name != model_name
58
+ changes[subtype_name] << obj.id
59
+ end
60
+ end
61
+ end
62
+ end
63
+ rescue NameError => ex
64
+ say "Skipping reference to #{model_name}", subitem: true
65
+ end
66
+ end
67
+ changes.each do |k, v|
68
+ # Update in blocks of up to 100 at a time
69
+ block_of_ids = []
70
+ id_count = 0
71
+ num_updated = 0
72
+ v.sort.each do |id|
73
+ block_of_ids << id
74
+ if (id_count += 1) % 100 == 0
75
+ num_updated += PaperTrail::Version.where(id: block_of_ids).update_all(item_subtype: k)
76
+ block_of_ids = []
77
+ end
78
+ end
79
+ num_updated += PaperTrail::Version.where(id: block_of_ids).update_all(item_subtype: k)
80
+ if num_updated > 0
81
+ say "Associated #{pluralize(num_updated, 'record')} to #{k}", subitem: true
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../migration_generator"
4
+
5
+ module PaperTrail
6
+ # Updates STI entries for PaperTrail
7
+ class UpdateItemSubtypeGenerator < MigrationGenerator
8
+ source_root File.expand_path("templates", __dir__)
9
+
10
+ desc "Generates (but does not run) a migration to update item_subtype for STI entries in an "\
11
+ "existing versions table."
12
+
13
+ def create_migration_file
14
+ add_paper_trail_migration("update_versions_for_item_subtype", sti_type_options: options)
15
+ end
16
+ end
17
+ end
@@ -1,160 +1,152 @@
1
- require 'request_store'
2
-
3
- # Require core library
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
+ # frozen_string_literal: true
2
+
3
+ # AR does not require all of AS, but PT does. PT uses core_ext like
4
+ # `String#squish`, so we require `active_support/all`. Instead of eagerly
5
+ # loading all of AS here, we could put specific `require`s in only the various
6
+ # PT files that need them, but this seems easier to troubleshoot, though it may
7
+ # add a few milliseconds to rails boot time. If that becomes a pain point, we
8
+ # can revisit this decision.
9
+ require "active_support/all"
10
+
11
+ # AR is required for, eg. has_paper_trail.rb, so we could put this `require` in
12
+ # all of those files, but it seems easier to troubleshoot if we just make sure
13
+ # AR is loaded here before loading *any* of PT. See discussion of
14
+ # performance/simplicity tradeoff for activesupport above.
15
+ require "active_record"
16
+
17
+ require "request_store"
18
+ require "paper_trail/cleaner"
19
+ require "paper_trail/compatibility"
20
+ require "paper_trail/config"
21
+ require "paper_trail/has_paper_trail"
22
+ require "paper_trail/record_history"
23
+ require "paper_trail/reifier"
24
+ require "paper_trail/request"
25
+ require "paper_trail/version_concern"
26
+ require "paper_trail/version_number"
27
+ require "paper_trail/serializers/json"
28
+ require "paper_trail/serializers/yaml"
29
+
30
+ # An ActiveRecord extension that tracks changes to your models, for auditing or
31
+ # versioning.
13
32
  module PaperTrail
14
- extend PaperTrail::Cleaner
15
-
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
- # ActiveRecord 5 drops support for serialized attributes; for previous
28
- # versions of ActiveRecord it is supported, we have a config option
29
- # to enable it within PaperTrail.
30
- def self.serialized_attributes?
31
- !!PaperTrail.config.serialized_attributes && ::ActiveRecord::VERSION::MAJOR < 5
32
- end
33
-
34
- # Sets whether PaperTrail is enabled or disabled for the current request.
35
- def self.enabled_for_controller=(value)
36
- paper_trail_store[:request_enabled_for_controller] = value
37
- end
38
-
39
- # Returns `true` if PaperTrail is enabled for the request, `false` otherwise.
40
- #
41
- # See `PaperTrail::Rails::Controller#paper_trail_enabled_for_controller`.
42
- def self.enabled_for_controller?
43
- !!paper_trail_store[:request_enabled_for_controller]
44
- end
45
-
46
- # Sets whether PaperTrail is enabled or disabled for this model in the
47
- # current request.
48
- def self.enabled_for_model(model, value)
49
- paper_trail_store[:"enabled_for_#{model}"] = value
50
- end
51
-
52
- # Returns `true` if PaperTrail is enabled for this model in the current
53
- # request, `false` otherwise.
54
- def self.enabled_for_model?(model)
55
- !!paper_trail_store.fetch(:"enabled_for_#{model}", true)
56
- end
57
-
58
- # Set the field which records when a version was created.
59
- def self.timestamp_field=(field_name)
60
- PaperTrail.config.timestamp_field = field_name
61
- end
62
-
63
- # Returns the field which records when a version was created.
64
- def self.timestamp_field
65
- PaperTrail.config.timestamp_field
66
- end
67
-
68
- # Sets who is responsible for any changes that occur. You would normally use
69
- # this in a migration or on the console, when working with models directly.
70
- # In a controller it is set automatically to the `current_user`.
71
- def self.whodunnit=(value)
72
- paper_trail_store[:whodunnit] = value
73
- end
74
-
75
- # Returns who is reponsible for any changes that occur.
76
- def self.whodunnit
77
- paper_trail_store[:whodunnit]
78
- end
33
+ E_RAILS_NOT_LOADED = <<-EOS.squish.freeze
34
+ PaperTrail has been loaded too early, before rails is loaded. This can
35
+ happen when another gem defines the ::Rails namespace, then PT is loaded,
36
+ all before rails is loaded. You may want to reorder your Gemfile, or defer
37
+ the loading of PT by using `require: false` and a manual require elsewhere.
38
+ EOS
39
+ E_TIMESTAMP_FIELD_CONFIG = <<-EOS.squish.freeze
40
+ PaperTrail.timestamp_field= has been removed, without replacement. It is no
41
+ longer configurable. The timestamp column in the versions table must now be
42
+ named created_at.
43
+ EOS
79
44
 
80
- # Sets any information from the controller that you want PaperTrail to
81
- # store. By default this is set automatically by a before filter.
82
- def self.controller_info=(value)
83
- paper_trail_store[:controller_info] = value
84
- end
85
-
86
- # Returns any information from the controller that you want
87
- # PaperTrail to store.
88
- #
89
- # See `PaperTrail::Rails::Controller#info_for_paper_trail`.
90
- def self.controller_info
91
- paper_trail_store[:controller_info]
92
- end
93
-
94
- # Getter and Setter for PaperTrail Serializer
95
- def self.serializer=(value)
96
- PaperTrail.config.serializer = value
97
- end
98
-
99
- def self.serializer
100
- PaperTrail.config.serializer
101
- end
102
-
103
- def self.active_record_protected_attributes?
104
- @active_record_protected_attributes ||= ::ActiveRecord::VERSION::MAJOR < 4 || !!defined?(ProtectedAttributes)
105
- end
106
-
107
- def self.transaction?
108
- ::ActiveRecord::Base.connection.open_transactions > 0
109
- end
110
-
111
- def self.transaction_id
112
- paper_trail_store[:transaction_id]
113
- end
114
-
115
- def self.transaction_id=(id)
116
- paper_trail_store[:transaction_id] = id
117
- end
118
-
119
- private
120
-
121
- # Thread-safe hash to hold PaperTrail's data. Initializing with needed
122
- # default values.
123
- def self.paper_trail_store
124
- RequestStore.store[:paper_trail] ||= { :request_enabled_for_controller => true }
125
- end
126
-
127
- # Returns PaperTrail's configuration object.
128
- def self.config
129
- @@config ||= PaperTrail::Config.instance
130
- yield @@config if block_given?
131
- @@config
132
- end
45
+ extend PaperTrail::Cleaner
133
46
 
134
47
  class << self
135
- alias_method :configure, :config
136
- end
137
- end
138
-
139
- # Ensure `ProtectedAttributes` gem gets required if it is available before the
140
- # `Version` class gets loaded in.
141
- unless PaperTrail.active_record_protected_attributes?
142
- PaperTrail.send(:remove_instance_variable, :@active_record_protected_attributes)
143
- begin
144
- require 'protected_attributes'
145
- rescue LoadError
146
- # In case `ProtectedAttributes` gem is not available.
48
+ # Switches PaperTrail on or off, for all threads.
49
+ # @api public
50
+ def enabled=(value)
51
+ PaperTrail.config.enabled = value
52
+ end
53
+
54
+ # Returns `true` if PaperTrail is on, `false` otherwise. This is the
55
+ # on/off switch that affects all threads. Enabled by default.
56
+ # @api public
57
+ def enabled?
58
+ !!PaperTrail.config.enabled
59
+ end
60
+
61
+ # Returns PaperTrail's `::Gem::Version`, convenient for comparisons. This is
62
+ # recommended over `::PaperTrail::VERSION::STRING`.
63
+ #
64
+ # Added in 7.0.0
65
+ #
66
+ # @api public
67
+ def gem_version
68
+ ::Gem::Version.new(VERSION::STRING)
69
+ end
70
+
71
+ # Set variables for the current request, eg. whodunnit.
72
+ #
73
+ # All request-level variables are now managed here, as of PT 9. Having the
74
+ # word "request" right there in your application code will remind you that
75
+ # these variables only affect the current request, not all threads.
76
+ #
77
+ # Given a block, temporarily sets the given `options`, executes the block,
78
+ # and returns the value of the block.
79
+ #
80
+ # Without a block, this currently just returns `PaperTrail::Request`.
81
+ # However, please do not use `PaperTrail::Request` directly. Currently,
82
+ # `Request` is a `Module`, but in the future it is quite possible we may
83
+ # make it a `Class`. If we make such a choice, we will not provide any
84
+ # warning and will not treat it as a breaking change. You've been warned :)
85
+ #
86
+ # @api public
87
+ def request(options = nil, &block)
88
+ if options.nil? && !block_given?
89
+ Request
90
+ else
91
+ Request.with(options, &block)
92
+ end
93
+ end
94
+
95
+ # Set the field which records when a version was created.
96
+ # @api public
97
+ def timestamp_field=(_field_name)
98
+ raise(E_TIMESTAMP_FIELD_CONFIG)
99
+ end
100
+
101
+ # Set the PaperTrail serializer. This setting affects all threads.
102
+ # @api public
103
+ def serializer=(value)
104
+ PaperTrail.config.serializer = value
105
+ end
106
+
107
+ # Get the PaperTrail serializer used by all threads.
108
+ # @api public
109
+ def serializer
110
+ PaperTrail.config.serializer
111
+ end
112
+
113
+ # Returns PaperTrail's global configuration object, a singleton. These
114
+ # settings affect all threads.
115
+ # @api private
116
+ def config
117
+ @config ||= PaperTrail::Config.instance
118
+ yield @config if block_given?
119
+ @config
120
+ end
121
+ alias configure config
122
+
123
+ def version
124
+ VERSION::STRING
125
+ end
147
126
  end
148
127
  end
149
128
 
129
+ # We use the `on_load` "hook" instead of `ActiveRecord::Base.include` because we
130
+ # don't want to cause all of AR to be autloaded yet. See
131
+ # https://guides.rubyonrails.org/engines.html#what-are-on-load-hooks-questionmark
132
+ # to learn more about `on_load`.
150
133
  ActiveSupport.on_load(:active_record) do
151
134
  include PaperTrail::Model
152
135
  end
153
136
 
154
137
  # Require frameworks
155
- require 'paper_trail/frameworks/sinatra'
156
- if defined?(::Rails) && ActiveRecord::VERSION::STRING >= '3.2'
157
- require 'paper_trail/frameworks/rails'
138
+ if defined?(::Rails)
139
+ # Rails module is sometimes defined by gems like rails-html-sanitizer
140
+ # so we check for presence of Rails.application.
141
+ if defined?(::Rails.application)
142
+ require "paper_trail/frameworks/rails"
143
+ else
144
+ ::Kernel.warn(::PaperTrail::E_RAILS_NOT_LOADED)
145
+ end
158
146
  else
159
- require 'paper_trail/frameworks/active_record'
147
+ require "paper_trail/frameworks/active_record"
148
+ end
149
+
150
+ if defined?(::ActiveRecord)
151
+ ::PaperTrail::Compatibility.check_activerecord(::ActiveRecord.gem_version)
160
152
  end