From 221580de68e5c7984b7496903ded33e7e065b424 Mon Sep 17 00:00:00 2001 From: Ilya Vassilevsky Date: Wed, 4 Apr 2018 20:39:18 +0400 Subject: [PATCH] Remove CLI options in favor of manual migration editing --- CHANGELOG.md | 4 ++ README.md | 15 +++++-- lib/generators/audited/install_generator.rb | 3 -- lib/generators/audited/templates/install.rb | 4 +- test/install_generator_test.rb | 45 --------------------- 5 files changed, 17 insertions(+), 54 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b51535d4..0fc4b6424 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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, diff --git a/README.md b/README.md index 2ca981858..4b32ce44b 100644 --- a/README.md +++ b/README.md @@ -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 @@ -49,7 +57,6 @@ $ rake db:migrate Upgrading will only make changes if changes are needed. - ## Usage Simply call `audited` on your models: diff --git a/lib/generators/audited/install_generator.rb b/lib/generators/audited/install_generator.rb index bbc5c49c5..2162c0720 100644 --- a/lib/generators/audited/install_generator.rb +++ b/lib/generators/audited/install_generator.rb @@ -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 diff --git a/lib/generators/audited/templates/install.rb b/lib/generators/audited/templates/install.rb index 1d43f093c..d29ff0dc6 100644 --- a/lib/generators/audited/templates/install.rb +++ b/lib/generators/audited/templates/install.rb @@ -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 diff --git a/test/install_generator_test.rb b/test/install_generator_test.rb index 7d3c1bd26..1ea6090ca 100644 --- a/test/install_generator_test.rb +++ b/test/install_generator_test.rb @@ -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