Skip to content

Commit

Permalink
Remove CLI options in favor of manual migration editing
Browse files Browse the repository at this point in the history
  • Loading branch information
vassilevsky committed Aug 31, 2018
1 parent 7eb075e commit 221580d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 54 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

Breaking changes

- removed `--audited-changes-column-type` and `--audited-user-id-column-type`
command-line options of `audited:install` generator
in favor of manual editing of the generated migration file
[#434](https://github.com/collectiveidea/audited/pull/434)
- removed block support for `Audit.reconstruct_attributes`
[#437](https://github.com/collectiveidea/audited/pull/437)
- removed `audited_columns`, `non_audited_columns`, `auditing_enabled=` instance methods,
Expand Down
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,22 @@ Add the gem to your Gemfile:
gem "audited", "~> 4.7"
```

Then, from your Rails app directory, create the `audits` table:
Then, from your Rails app directory, create a migration that will add an `audits` table:

```bash
$ rails generate audited:install
$ rake db:migrate
```

If you're using PostgreSQL, then you can use `rails generate audited:install --audited-changes-column-type jsonb` (or `json`) to store audit changes natively with its JSON column types. If you're using something other than integer primary keys (e.g. UUID) for your User model, then you can use `rails generate audited:install --audited-user-id-column-type uuid` to customize the `audits` table `user_id` column type.
You can edit the generated migration and change some column types to better suit your database:

* **id** columns (`auditable_id`, `associated_id`, `user_id`): their types must match those in the models you are going to audit
* `audited_changes`: the type can alternatively be set to `:json` or `:jsonb` (in PostgreSQL databases)

After that, create the table by running the migration:

```bash
$ rake db:migrate
```

#### Upgrading

Expand All @@ -49,7 +57,6 @@ $ rake db:migrate

Upgrading will only make changes if changes are needed.


## Usage

Simply call `audited` on your models:
Expand Down
3 changes: 0 additions & 3 deletions lib/generators/audited/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ class InstallGenerator < Rails::Generators::Base
include Audited::Generators::MigrationHelper
extend Audited::Generators::Migration

class_option :audited_changes_column_type, type: :string, default: "text", required: false
class_option :audited_user_id_column_type, type: :string, default: "integer", required: false

source_root File.expand_path("../templates", __FILE__)

def copy_migration
Expand Down
4 changes: 2 additions & 2 deletions lib/generators/audited/templates/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ def self.up
t.column :auditable_type, :string
t.column :associated_id, :integer
t.column :associated_type, :string
t.column :user_id, :<%= options[:audited_user_id_column_type] %>
t.column :user_id, :integer
t.column :user_type, :string
t.column :username, :string
t.column :action, :string
t.column :audited_changes, :<%= options[:audited_changes_column_type] %>
t.column :audited_changes, :text
t.column :version, :integer, :default => 0
t.column :comment, :string
t.column :remote_address, :string
Expand Down
45 changes: 0 additions & 45 deletions test/install_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,6 @@ class InstallGeneratorTest < Rails::Generators::TestCase
setup :prepare_destination
tests Audited::Generators::InstallGenerator

test "generate migration with 'text' type for audited_changes column" do
run_generator

assert_migration "db/migrate/install_audited.rb" do |content|
assert_includes(content, 'class InstallAudited')
assert_includes(content, 't.column :audited_changes, :text')
end
end

test "generate migration with 'jsonb' type for audited_changes column" do
run_generator %w(--audited-changes-column-type jsonb)

assert_migration "db/migrate/install_audited.rb" do |content|
assert_includes(content, 'class InstallAudited')
assert_includes(content, 't.column :audited_changes, :jsonb')
end
end

test "generate migration with 'json' type for audited_changes column" do
run_generator %w(--audited-changes-column-type json)

assert_migration "db/migrate/install_audited.rb" do |content|
assert_includes(content, 'class InstallAudited')
assert_includes(content, 't.column :audited_changes, :json')
end
end

test "generate migration with 'string' type for user_id column" do
run_generator %w(--audited-user-id-column-type string)

assert_migration "db/migrate/install_audited.rb" do |content|
assert_includes(content, 'class InstallAudited')
assert_includes(content, 't.column :user_id, :string')
end
end

test "generate migration with 'uuid' type for user_id column" do
run_generator %w(--audited-user-id-column-type uuid)

assert_migration "db/migrate/install_audited.rb" do |content|
assert_includes(content, 'class InstallAudited')
assert_includes(content, 't.column :user_id, :uuid')
end
end

test "generate migration with correct AR migration parent" do
run_generator

Expand Down

0 comments on commit 221580d

Please sign in to comment.