paper_trail-rails 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Changelog.md +0 -0
- data/Gemfile +6 -0
- data/License +18 -0
- data/Rakefile +6 -0
- data/Readme.md +35 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/paper_trail/rails/configuration.rb +32 -0
- data/lib/paper_trail/rails/general.rb +30 -0
- data/lib/paper_trail/rails/migration_extensions.rb +16 -0
- data/lib/paper_trail/rails/paper_trail_extensions.rb +24 -0
- data/lib/paper_trail/rails/railtie.rb +45 -0
- data/lib/paper_trail/rails/version.rb +7 -0
- data/lib/paper_trail/rails.rb +53 -0
- data/paper_trail-rails.gemspec +35 -0
- metadata +135 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3c2a7d0c232080ed191377bd3b70aebdbbf5df8561d29846b79c6bfaf245c840
|
4
|
+
data.tar.gz: d85a090ed4de3c94eef116bfe09a9b8a6ef33a61bcf02b0c13e8f9811e66a6c3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ea9485746cd967d7e878bb81891a848148c943dd0ec45eb1a1e975fa42e8de149661bc5b33a44b001d3d2014a224ff98e95b32c408c2165e92864860b5443fb8
|
7
|
+
data.tar.gz: ae08cbb579ad278720936d0e9b4057a4c63c9271dc311f17d85777ce544751a4955cf7c563c90eefb2899caf60c6913ee81be1dc339b332c6546e818583359e2
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Changelog.md
ADDED
File without changes
|
data/Gemfile
ADDED
data/License
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Copyright (c) 2018 Tyler Rick
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
4
|
+
associated documentation files (the "Software"), to deal in the Software without restriction,
|
5
|
+
including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
6
|
+
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
7
|
+
furnished to do so, subject to the following conditions:
|
8
|
+
|
9
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial
|
10
|
+
portions of the Software.
|
11
|
+
|
12
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
13
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
14
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
15
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
16
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
17
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
18
|
+
SOFTWARE.
|
data/Rakefile
ADDED
data/Readme.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# PaperTrail::Rails
|
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
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'paper_trail-rails'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install paper_trail-rails
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/TylerRick/paper_trail-rails.
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "paper_trail/rails"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module PaperTrail
|
2
|
+
module Rails
|
3
|
+
class Configuration
|
4
|
+
def initialize
|
5
|
+
self.user_filter_for_console = :itself
|
6
|
+
self.paper_trail_user_for_test_console = ->(users) {
|
7
|
+
users.admins.last
|
8
|
+
}
|
9
|
+
|
10
|
+
self.ask_reason = true
|
11
|
+
self.require_reason = false
|
12
|
+
end
|
13
|
+
|
14
|
+
# Filter users with this proc. For example:
|
15
|
+
# ->(users) { users.admins) }
|
16
|
+
attr_accessor :user_filter_for_console
|
17
|
+
|
18
|
+
# A proc that returns a user if you ever run `rails console` in test
|
19
|
+
# 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
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# General helpers that aren't really specific to this project
|
2
|
+
|
3
|
+
module PaperTrail
|
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]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
other_values_prompt = " (#{other_values_prompt})" if other_values_prompt.present?
|
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
|
24
|
+
end
|
25
|
+
end
|
26
|
+
user
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module PaperTrail
|
2
|
+
module Rails
|
3
|
+
module RecordMigrationNameInVersion
|
4
|
+
def exec_migration(conn, direction)
|
5
|
+
PaperTrail.update_metadata(
|
6
|
+
command: "rails db:migrate: #{self.name} (#{direction})"
|
7
|
+
)
|
8
|
+
super
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
ActiveRecord::Migration.class_eval do
|
15
|
+
prepend PaperTrail::Rails::RecordMigrationNameInVersion
|
16
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
PaperTrail.class_eval do
|
2
|
+
|
3
|
+
unless methods.include?(:with_metadata)
|
4
|
+
# Adds additional metadata to versions created within the given block. This will be merged with
|
5
|
+
# (and may override) any metadata already set by your controller's info_for_paper_trail.
|
6
|
+
#
|
7
|
+
# Example:
|
8
|
+
# PaperTrail.with_metadata(reason: 'Update for ...')
|
9
|
+
#
|
10
|
+
def self.with_metadata(metadata)
|
11
|
+
merged_metadata = (::PaperTrail.request.controller_info || {}).merge(metadata)
|
12
|
+
PaperTrail.request(controller_info: merged_metadata) do
|
13
|
+
yield
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
unless methods.include?(:update_metadata)
|
19
|
+
def self.update_metadata(metadata)
|
20
|
+
merged_metadata = (::PaperTrail.request.controller_info || {}).merge(metadata)
|
21
|
+
PaperTrail.request.controller_info = merged_metadata
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# paper_trail gem also provides a Railtie (Engine) in this require. It seems to
|
2
|
+
# work to just add initializers to that existing Railtie. If needed, though, we
|
3
|
+
# could define a different one, like "Railtie".
|
4
|
+
require 'rails'
|
5
|
+
require 'paper_trail/frameworks/rails/engine'
|
6
|
+
|
7
|
+
module PaperTrail
|
8
|
+
module Rails
|
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
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'paper_trail'
|
2
|
+
|
3
|
+
require_relative 'rails/version'
|
4
|
+
require_relative 'rails/general'
|
5
|
+
require_relative 'rails/paper_trail_extensions'
|
6
|
+
require_relative 'rails/configuration'
|
7
|
+
require_relative 'rails/migration_extensions'
|
8
|
+
|
9
|
+
module PaperTrail
|
10
|
+
module Rails
|
11
|
+
class << self
|
12
|
+
def configuration
|
13
|
+
@configuration ||= Configuration.new
|
14
|
+
end
|
15
|
+
alias_method :config, :configuration
|
16
|
+
|
17
|
+
def configure
|
18
|
+
yield config
|
19
|
+
end
|
20
|
+
|
21
|
+
# Store some metadata about where the change came from, even for rake tasks, etc.
|
22
|
+
def set_default_paper_trail_metadata
|
23
|
+
PaperTrail.update_metadata(
|
24
|
+
command: "#{File.basename($PROGRAM_NAME)} #{ARGV.join ' '}",
|
25
|
+
source_location: caller.find { |line|
|
26
|
+
line.starts_with? ::Rails.root.to_s and
|
27
|
+
!line.starts_with? __FILE__
|
28
|
+
}
|
29
|
+
)
|
30
|
+
end
|
31
|
+
|
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'"
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
def get_paper_trail_reason(required: false)
|
41
|
+
reason = nil
|
42
|
+
until reason.present? do
|
43
|
+
print "What is the reason for this change? "
|
44
|
+
reason = gets.chomp
|
45
|
+
break unless required
|
46
|
+
end
|
47
|
+
reason
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
require_relative 'rails/railtie'
|
@@ -0,0 +1,35 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "paper_trail/rails/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "paper_trail-rails"
|
8
|
+
spec.version = PaperTrail::Rails.version
|
9
|
+
spec.authors = ["Tyler Rick"]
|
10
|
+
spec.email = ["tyler@tylerrick.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Integrate with rails console and migrations. In rails console, ask who is making change and why. Records the command.}
|
13
|
+
spec.description = spec.summary
|
14
|
+
spec.homepage = "https://github.com/TylerRick/paper_trail-rails"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
18
|
+
spec.metadata["changelog_uri"] = "#{spec.metadata["source_code_uri"]}/blob/master/Changelog.md"
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_dependency "paper_trail"
|
30
|
+
spec.add_dependency "rails"
|
31
|
+
|
32
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: paper_trail-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tyler Rick
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-05-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: paper_trail
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
description: Integrate with rails console and migrations. In rails console, ask who
|
84
|
+
is making change and why. Records the command.
|
85
|
+
email:
|
86
|
+
- tyler@tylerrick.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".travis.yml"
|
94
|
+
- Changelog.md
|
95
|
+
- Gemfile
|
96
|
+
- License
|
97
|
+
- Rakefile
|
98
|
+
- Readme.md
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- lib/paper_trail/rails.rb
|
102
|
+
- lib/paper_trail/rails/configuration.rb
|
103
|
+
- lib/paper_trail/rails/general.rb
|
104
|
+
- lib/paper_trail/rails/migration_extensions.rb
|
105
|
+
- lib/paper_trail/rails/paper_trail_extensions.rb
|
106
|
+
- lib/paper_trail/rails/railtie.rb
|
107
|
+
- lib/paper_trail/rails/version.rb
|
108
|
+
- paper_trail-rails.gemspec
|
109
|
+
homepage: https://github.com/TylerRick/paper_trail-rails
|
110
|
+
licenses: []
|
111
|
+
metadata:
|
112
|
+
homepage_uri: https://github.com/TylerRick/paper_trail-rails
|
113
|
+
source_code_uri: https://github.com/TylerRick/paper_trail-rails
|
114
|
+
changelog_uri: https://github.com/TylerRick/paper_trail-rails/blob/master/Changelog.md
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
requirements: []
|
130
|
+
rubygems_version: 3.0.1
|
131
|
+
signing_key:
|
132
|
+
specification_version: 4
|
133
|
+
summary: Integrate with rails console and migrations. In rails console, ask who is
|
134
|
+
making change and why. Records the command.
|
135
|
+
test_files: []
|