Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).
`rails generate paper_trail:install CommentVersion` created `comment_versions` table
- `rails generate paper_trail:update_item_subtype` now supports custom version classes via
`--version-class-name` option, e.g. `--version-class-name=CommentVersion`
- [#1459](https://github.com/paper-trail-gem/paper_trail/issues/1459)
`rails generate paper_trail:install` now creates a `paper_trail.rb` initializer

### 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`.

You can provide a custom version table name e.g., for having multiple version tables. You will still need setup a custom Version class and configure it to use the custom table. See [6.a. Custom Version Classes](#6a-custom-version-classes).

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 @@ -31,6 +31,10 @@ class InstallGenerator < MigrationGenerator
"Can be customized by providing a Version class name. " \
"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
# Use the table_name to create the proper migration filename
add_paper_trail_migration(
Expand All @@ -43,6 +47,7 @@ def create_migration_file
if options.with_changes?
add_paper_trail_migration("add_object_changes_to_#{table_name}")
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

## Default behaviour tracks versions on all events (create, update, destroy, and touch).
## You may wish to remove touch events to reduce version table size.
PaperTrail.config.has_paper_trail_defaults = {
on: %i[create update destroy touch]
}

## 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 @@ -63,6 +63,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