Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).

### Added

- None
- [#1459](https://github.com/paper-trail-gem/paper_trail/issues/1459)
`rails generate paper_trail:install` now creates a `paper_trail.rb` initializer
that disables versioning on touch events by default.

### Fixed

Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ by adding a controller callback.
end
```

1. Review the default initializer that was created by ```rails generate paper_trail:install```
at `config/initializers/paper_trail.rb`.

### 1.c. Basic Usage

Your models now have a `versions` method which returns the "paper trail" of
Expand Down Expand Up @@ -1152,7 +1155,8 @@ Be advised that redefining an association is an undocumented feature of Rails.
### 5.c. Generators

PaperTrail has one generator, `paper_trail:install`. It writes, but does not
run, a migration file. The migration creates the `versions` table.
run, a migration file. The migration creates the `versions` table. The generator
also creates an initializer file `config/initializers/paper_trail.rb`.

#### Reference

Expand Down
5 changes: 5 additions & 0 deletions lib/generators/paper_trail/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class InstallGenerator < MigrationGenerator
desc "Generates (but does not run) a migration to add a versions table. " \
"See section 5.c. Generators in README.md for more information."

def create_initializer_file
copy_file "paper_trail.rb", "config/initializers/paper_trail.rb"
end

def create_migration_file
add_paper_trail_migration(
"create_versions",
Expand All @@ -41,6 +45,7 @@ def create_migration_file
if options.with_changes?
add_paper_trail_migration("add_object_changes_to_versions")
end
create_initializer_file
end

private
Expand Down
31 changes: 31 additions & 0 deletions lib/generators/paper_trail/install/templates/paper_trail.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

# This file is automatically generated by `rails generate paper_trail:install`
# and goes in `YourApp/config/initializers/`.
# Each model can override most of these global settings on a per-model basis.

## Enable/disable PaperTrail globally
PaperTrail.config.enabled = true

## Track versions on create, update, & destroy, but not touch events to reduce version table size.
## Add `touch` below to track touch events, or comment out (default behaviour tracks all events).
PaperTrail.config.has_paper_trail_defaults = {
on: %i[create update destroy]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i like the generation of this file but i don't think the config generated here should differ from the existing defaults. i realize this goes against part of the intent of your pr but it results in an implicit change to the defaults for anyone starting from scratch which feels inconsistent to me.

}

## Limit the number of versions saved per record.
## The version limit does not apply to `create` events
## Default is nil (no limit)
# PaperTrail.config.version_limit = 3

## Define error handling behaviour.
## options:
### :legacy # Raise on create, log on update/delete (Default)
### :log # only log the error
### :exception # raise exception
### :silent # do nothing
# PaperTrail.config.version_error_behavior = :legacy

## See the readme for other important configuration steps such as setting whodunnit,
## and enabling/disabling paper_trail in tests.
## https://github.com/paper-trail-gem/paper_trail#readme
15 changes: 15 additions & 0 deletions spec/generators/paper_trail/install_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@
}
)
end

it "generates the initializer file" do
expect(destination_root).to(
have_structure {
directory("config") {
directory("initializers") {
file("paper_trail.rb") {
contains "# This file is automatically generated"
contains "PaperTrail.config.enabled = true"
}
}
}
}
)
end
end

describe "`--with-changes` option set to `true`" do
Expand Down