paper_trail-rails 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3c2a7d0c232080ed191377bd3b70aebdbbf5df8561d29846b79c6bfaf245c840
4
- data.tar.gz: d85a090ed4de3c94eef116bfe09a9b8a6ef33a61bcf02b0c13e8f9811e66a6c3
3
+ metadata.gz: 70c683fd54f3d91ce5136dc79ccf3cae67564beebaed69d6fc94590588b2f9b2
4
+ data.tar.gz: 6b75131fbe1e81b1cb40943ec9148a52357310ed7e6d505b0bde16f2bb126d14
5
5
  SHA512:
6
- metadata.gz: ea9485746cd967d7e878bb81891a848148c943dd0ec45eb1a1e975fa42e8de149661bc5b33a44b001d3d2014a224ff98e95b32c408c2165e92864860b5443fb8
7
- data.tar.gz: ae08cbb579ad278720936d0e9b4057a4c63c9271dc311f17d85777ce544751a4955cf7c563c90eefb2899caf60c6913ee81be1dc339b332c6546e818583359e2
6
+ metadata.gz: ffa6a5dbfa8c843e651a27394f51d7515498a0c486dc890e136f2c57cfcb832f932d931bdbe260efb4b758d02cf1d812ea8e3de7298443aaee69872fa55f3fbf
7
+ data.tar.gz: faeb455308c26c59e0de58849563ac8fafe70b22b57f49fc7f2f844c9d32312646ca6d6f7ac1acb59e3552af4465dbadbe889c55db02c7a750134c7c1f7a33d1
data/Changelog.md CHANGED
@@ -0,0 +1,15 @@
1
+ ## 0.2.0 (2019-05-23)
2
+
3
+ ### Breaking changes
4
+ - Moved config specific to rails console under `config.console`
5
+ - Rename `PaperTrail::Rails.set_default_paper_trail_metadata` to `PaperTrail::Rails.set_default_metadata`
6
+ - Rename `PaperTrail::Rails.select_paper_trail_user_or_system` to `PaperTrail::Rails.select_user`
7
+ - and others
8
+
9
+ ### Added
10
+ - Automatically reset reason so it doesn't keep assuming and using the same reason for all changes you make in this console session.(Can be disabled with `config.console.auto_reset_reason = false`)
11
+ - Add `config.console.ask_for_user`, `config.console.require_user`, and `config.console.auto_reset_user`
12
+
13
+ ## 0.1.0 (2019-05-22)
14
+
15
+ Initial release
data/Readme.md CHANGED
@@ -1,8 +1,22 @@
1
1
  # PaperTrail::Rails
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/paper_trail/rails`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ [![Gem Version][1]][2]
4
+
5
+ An extension to [PaperTrail](https://github.com/paper-trail-gem/paper_trail)
6
+ that adds some useful automatic integrations with Rails:
7
+
8
+ - Automatically record which migration made the change in the version record (in
9
+ the `command` attribute) when migrations make changes to data.
10
+ - Automatically record `rails console` as the command in the version record (in
11
+ the `command` attribute).
12
+ - Automatically record the source location that initiated the change so you can
13
+ figure out what caused caused the change later when inspecting a version
14
+ record
15
+ - (Optional) Never forget to set the actor (whodunnit) when making changes in the `rails
16
+ console`. It will prompt you to choose an actor user as soon as you try to
17
+ make a change that would record a PaperTrail Version. (Enter `system` if you
18
+ don't want any particular user recorded as the actor.)
19
+ - (Optional) Automatically ask for a reason whenever you make a change
6
20
 
7
21
  ## Installation
8
22
 
@@ -16,13 +30,40 @@ And then execute:
16
30
 
17
31
  $ bundle
18
32
 
19
- Or install it yourself as:
33
+ Add and run this migration:
34
+ ```
35
+ class VersionsAddSourceLocationCommandReason < ActiveRecord::Migration[5.2]
36
+ def change
37
+ change_table :versions do |t|
38
+ t.text :source_location
39
+ t.text :command
40
+ t.text :reason
41
+ end
42
+ change_table :versions do |t|
43
+ t.index :command
44
+ t.index :reason
45
+ end
46
+ end
47
+ end
48
+ ```
49
+
50
+ ## Configuration
20
51
 
21
- $ gem install paper_trail-rails
52
+ Add to `config/initializers/paper_trail.rb` and change as needed:
22
53
 
23
- ## Usage
54
+ ```ruby
55
+ PaperTrail::Rails.configure do |config|
56
+ config.console.ask_for_user = true
57
+ config.console.require_user = false
58
+ config.console.auto_reset_user = false
59
+
60
+ config.console.ask_for_reason = true
61
+ config.console.require_reason = false
62
+ config.console.auto_reset_reason = true
24
63
 
25
- TODO: Write usage instructions here
64
+ config.user_filter = ->(users) { users.admins }
65
+ end
66
+ ```
26
67
 
27
68
  ## Development
28
69
 
@@ -33,3 +74,6 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
33
74
  ## Contributing
34
75
 
35
76
  Bug reports and pull requests are welcome on GitHub at https://github.com/TylerRick/paper_trail-rails.
77
+
78
+ [1]: https://badge.fury.io/rb/paper_trail-rails.svg
79
+ [2]: https://rubygems.org/gems/paper_trail-rails
@@ -2,31 +2,27 @@ module PaperTrail
2
2
  module Rails
3
3
  class Configuration
4
4
  def initialize
5
- self.user_filter_for_console = :itself
6
- self.paper_trail_user_for_test_console = ->(users) {
5
+ config = self
6
+
7
+ config.select_user_filter = :itself
8
+ config.select_user_other_allowed_values = ['system', 'admin']
9
+ config.user_for_test = ->(users) {
7
10
  users.admins.last
8
11
  }
9
-
10
- self.ask_reason = true
11
- self.require_reason = false
12
12
  end
13
13
 
14
- # Filter users with this proc. For example:
14
+ # Filter proc to use to show a list of users in select_user helper. For example:
15
15
  # ->(users) { users.admins) }
16
- attr_accessor :user_filter_for_console
16
+ # or
17
+ # ->(users) { users.none) }
18
+ # Can be also be a symbol or anything that responds to to_proc.
19
+ attr_accessor :select_user_filter
20
+
21
+ attr_accessor :select_user_other_allowed_values
17
22
 
18
23
  # A proc that returns a user if you ever run `rails console` in test
19
24
  # environment (where you probably won't have any real data)
20
- attr_accessor :paper_trail_user_for_test_console
21
-
22
- attr_accessor :ask_reason
23
- attr_accessor :require_reason
24
-
25
- class << self
26
- def configure
27
- yield self
28
- end
29
- end
25
+ attr_accessor :user_for_test
30
26
  end
31
27
  end
32
28
  end
@@ -0,0 +1,102 @@
1
+ require_relative 'configuration'
2
+ require_relative 'railtie'
3
+
4
+ module PaperTrail
5
+ module Rails
6
+ module Console
7
+ class << self
8
+ end
9
+
10
+ class Configuration
11
+ def initialize
12
+ config = self
13
+
14
+ config.ask_for_user = true
15
+ config.require_user = false
16
+ config.auto_reset_user = false
17
+
18
+ config.ask_for_reason = true
19
+ config.require_reason = false
20
+ config.auto_reset_reason = true
21
+ end
22
+
23
+ attr_accessor :ask_for_user
24
+ attr_accessor :require_user
25
+ attr_accessor :auto_reset_user
26
+
27
+ attr_accessor :ask_for_reason
28
+ attr_accessor :require_reason
29
+ attr_accessor :auto_reset_reason
30
+
31
+ class << self
32
+ def configure
33
+ yield self
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ class Configuration
40
+ def console
41
+ @console ||= Console::Configuration.new
42
+ end
43
+ end
44
+
45
+ class Engine
46
+ initializer "paper_trail-rails.set_default_metadata" do |app|
47
+ # There's no way to set up deferred evaluation of PaperTrail.request.controller_info from
48
+ # here like we can with whodunnit, so abuse that property of PaperTrail.request.whodunnit to
49
+ # set other metadata. Reserve the whodunnit field for storing the actual name or id of the
50
+ # human who made the change.
51
+ PaperTrail.request.whodunnit = ->() {
52
+ PaperTrail::Rails.set_default_metadata
53
+ nil
54
+ }
55
+ end
56
+
57
+ console do
58
+ config = PaperTrail::Rails.config
59
+ PaperTrail.update_metadata command: "rails console"
60
+ PaperTrail.request.whodunnit = ->() {
61
+ PaperTrail::Rails.set_default_metadata
62
+ @paper_trail_whodunnit ||= (
63
+ if config.console.ask_for_user
64
+ if ::Rails.env.test?
65
+ user = config.console.user_for_test.(User.all)
66
+ else
67
+ puts "Before you make any changes... We need to know who is making the changes, to store in the PaperTrail version history."
68
+ user = PaperTrail::Rails.select_user(required: config.console.require_reason)
69
+ puts "Thank you, #{user}! Have a wonderful time!" if user
70
+ end
71
+ user.respond.id
72
+ end
73
+ )
74
+
75
+ if config.console.ask_for_reason
76
+ @paper_trail_reason ||= PaperTrail::Rails.get_reason(required: config.console.require_reason)
77
+ end
78
+ PaperTrail.update_metadata reason: @paper_trail_reason
79
+
80
+ @paper_trail_whodunnit
81
+ }
82
+
83
+
84
+ if config.console.auto_reset_user || config.console.auto_reset_reason
85
+ if defined?(Pry)
86
+ Pry.hooks.add_hook(:after_eval, "paper_trail-rails-auto_reset") do |output, binding, pry|
87
+ if config.console.auto_reset_user
88
+ @paper_trail_whodunnit = nil
89
+ end
90
+ if config.console.auto_reset_reason
91
+ @paper_trail_reason = nil
92
+ PaperTrail.update_metadata reason: @paper_trail_reason
93
+ end
94
+ end
95
+ else
96
+ raise "Pry is required in order to use `auto_reset`. Please add pry-rails to your Gemfile or disable this in the config."
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -2,28 +2,30 @@
2
2
 
3
3
  module PaperTrail
4
4
  module Rails
5
- class << self
6
- def select_user(filter: :itself, other_allowed_values: [], other_values_prompt: '')
7
- User.logger.silence do
8
- puts 'id. user'
9
- filter.to_proc.(User.all).default_order.each do |user|
10
- puts "%4s. %s" % [user.id, user.inspect]
5
+ module General
6
+ class << self
7
+ def select_user(filter: :itself, other_allowed_values: [], prompt: 'Please enter a User id', required: true)
8
+ User.logger.silence do
9
+ puts 'id. user'
10
+ filter.to_proc.(User.all).default_order.each do |user|
11
+ puts "%4s. %s" % [user.id, user.inspect]
12
+ end
11
13
  end
12
- end
13
- other_values_prompt = " (#{other_values_prompt})" if other_values_prompt.present?
14
14
 
15
- user = nil
16
- until user.present? do
17
- print "Please enter the id of one of the users above (or any valid User id)#{other_values_prompt}: "
18
- input = gets.chomp
19
- case input
20
- when *other_allowed_values
21
- user = input
22
- else
23
- user = User.find(input) rescue nil
15
+ user = nil
16
+ until user.present? do
17
+ print "#{prompt}: "
18
+ input = gets.chomp
19
+ case input
20
+ when *other_allowed_values
21
+ user = input
22
+ else
23
+ user = User.find(input) rescue nil
24
+ end
25
+ break unless required
24
26
  end
27
+ user
25
28
  end
26
- user
27
29
  end
28
30
  end
29
31
  end
@@ -15,6 +15,12 @@ PaperTrail.class_eval do
15
15
  end
16
16
  end
17
17
 
18
+ unless methods.include?(:reset_metadata)
19
+ def self.reset_metadata
20
+ PaperTrail.request.controller_info = nil
21
+ end
22
+ end
23
+
18
24
  unless methods.include?(:update_metadata)
19
25
  def self.update_metadata(metadata)
20
26
  merged_metadata = (::PaperTrail.request.controller_info || {}).merge(metadata)
@@ -7,39 +7,6 @@ require 'paper_trail/frameworks/rails/engine'
7
7
  module PaperTrail
8
8
  module Rails
9
9
  class Engine
10
- initializer "paper_trail-rails.set_default_paper_trail_metadata" do |app|
11
- # There's no way to set up deferred evaluation of PaperTrail.request.controller_info from
12
- # here like we can with whodunnit, so abuse that property of PaperTrail.request.whodunnit to
13
- # set other metadata. Reserve the whodunnit field for storing the actual name or id of the
14
- # human who made the change.
15
- PaperTrail.request.whodunnit = ->() {
16
- PaperTrail::Rails.set_default_paper_trail_metadata
17
- nil
18
- }
19
- end
20
-
21
- console do
22
- config = PaperTrail::Rails.config
23
- PaperTrail.update_metadata command: "rails console"
24
- PaperTrail.request.whodunnit = ->() {
25
- PaperTrail::Rails.set_default_paper_trail_metadata
26
- @paper_trail_whodunnit ||= (
27
- if ::Rails.env.test?
28
- user = config.paper_trail_user_for_test_console.(User.all)
29
- else
30
- puts "Before you make any changes... We need to know who is making the changes, to store in the PaperTrail version history."
31
- user = PaperTrail::Rails.select_paper_trail_user_or_system
32
- puts "Thank you, #{user}! Have a wonderful time!"
33
- end
34
- user.respond.id
35
- )
36
- if config.ask_reason
37
- @paper_trail_reason ||= PaperTrail::Rails.get_paper_trail_reason(required: config.require_reason)
38
- end
39
- PaperTrail.update_metadata reason: @paper_trail_reason
40
- @paper_trail_whodunnit
41
- }
42
- end
43
10
  end
44
11
  end
45
12
  end
@@ -1,7 +1,7 @@
1
1
  module PaperTrail
2
2
  module Rails
3
3
  def self.version
4
- "0.1.0"
4
+ "0.2.0"
5
5
  end
6
6
  end
7
7
  end
@@ -1,12 +1,18 @@
1
1
  require 'paper_trail'
2
2
 
3
3
  require_relative 'rails/version'
4
- require_relative 'rails/general'
5
- require_relative 'rails/paper_trail_extensions'
4
+
5
+ # (In alphabetical order)
6
6
  require_relative 'rails/configuration'
7
+ require_relative 'rails/console'
8
+ require_relative 'rails/general'
7
9
  require_relative 'rails/migration_extensions'
10
+ require_relative 'rails/paper_trail_extensions'
8
11
 
9
12
  module PaperTrail
13
+ # This is the main module for this gem. Can't make it a class because
14
+ # paper_trail gem already defines this module; otherwise I would have made
15
+ # this a class.
10
16
  module Rails
11
17
  class << self
12
18
  def configuration
@@ -18,8 +24,8 @@ module PaperTrail
18
24
  yield config
19
25
  end
20
26
 
21
- # Store some metadata about where the change came from, even for rake tasks, etc.
22
- def set_default_paper_trail_metadata
27
+ # Store some metadata about where the change came from
28
+ def set_default_metadata
23
29
  PaperTrail.update_metadata(
24
30
  command: "#{File.basename($PROGRAM_NAME)} #{ARGV.join ' '}",
25
31
  source_location: caller.find { |line|
@@ -29,15 +35,18 @@ module PaperTrail
29
35
  )
30
36
  end
31
37
 
32
- def select_paper_trail_user_or_system
33
- select_user(
34
- filter: config.user_filter_for_console,
35
- other_allowed_values: ['system'],
36
- other_values_prompt: "or 'system'"
38
+ def select_user(required: false)
39
+ other_allowed_values = config.select_user_other_allowed_values
40
+ other_values_prompt = " (or #{other_allowed_values.join(' or ')})" if other_allowed_values.present?
41
+ General.select_user(
42
+ filter: config.select_user_filter,
43
+ other_allowed_values: other_allowed_values,
44
+ prompt: "Please enter a User id#{other_values_prompt}",
45
+ required: required
37
46
  )
38
47
  end
39
48
 
40
- def get_paper_trail_reason(required: false)
49
+ def get_reason(required: false)
41
50
  reason = nil
42
51
  until reason.present? do
43
52
  print "What is the reason for this change? "
@@ -49,5 +58,3 @@ module PaperTrail
49
58
  end
50
59
  end
51
60
  end
52
-
53
- require_relative 'rails/railtie'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paper_trail-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Rick
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-22 00:00:00.000000000 Z
11
+ date: 2019-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: paper_trail
@@ -100,6 +100,7 @@ files:
100
100
  - bin/setup
101
101
  - lib/paper_trail/rails.rb
102
102
  - lib/paper_trail/rails/configuration.rb
103
+ - lib/paper_trail/rails/console.rb
103
104
  - lib/paper_trail/rails/general.rb
104
105
  - lib/paper_trail/rails/migration_extensions.rb
105
106
  - lib/paper_trail/rails/paper_trail_extensions.rb