From 8b81f3a383aeeb851b018bdf446d6af5f568253e Mon Sep 17 00:00:00 2001 From: Dominic Cleal Date: Mon, 27 Mar 2017 15:44:19 +0100 Subject: [PATCH 1/2] Test Ruby on Rails 5.1, Ruby 2.4 --- .travis.yml | 8 ++++++++ Appraisals | 4 ++++ README.md | 3 ++- gemfiles/rails51.gemfile | 7 +++++++ spec/audited/sweeper_spec.rb | 2 +- spec/support/active_record/models.rb | 1 + .../postgres/1_change_audited_changes_type_to_json.rb | 3 ++- .../postgres/2_change_audited_changes_type_to_jsonb.rb | 3 ++- test/upgrade_generator_test.rb | 6 +++++- 9 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 gemfiles/rails51.gemfile diff --git a/.travis.yml b/.travis.yml index 9c90cde74..41e163a1a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ rvm: - 2.1 - 2.2.4 - 2.3.1 + - 2.4.1 - ruby-head env: - DB=SQLITE @@ -16,12 +17,19 @@ gemfile: - gemfiles/rails41.gemfile - gemfiles/rails42.gemfile - gemfiles/rails50.gemfile + - gemfiles/rails51.gemfile matrix: allow_failures: - rvm: ruby-head exclude: - rvm: 2.1 gemfile: gemfiles/rails50.gemfile + - rvm: 2.1 + gemfile: gemfiles/rails51.gemfile + - rvm: 2.4.1 + gemfile: gemfiles/rails40.gemfile + - rvm: 2.4.1 + gemfile: gemfiles/rails41.gemfile fast_finish: true branches: only: diff --git a/Appraisals b/Appraisals index e905f2a94..1c47eedcd 100644 --- a/Appraisals +++ b/Appraisals @@ -17,3 +17,7 @@ end appraise 'rails50' do gem 'rails', '~> 5.0.0' end + +appraise 'rails51' do + gem 'rails', '>= 5.1.0.rc1', '< 5.2' +end diff --git a/README.md b/README.md index be404bb0a..37a7fc536 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Audited [![Build Status](https://secure.travis-ci.org/collectiveidea/audited.svg **Audited** (previously acts_as_audited) is an ORM extension that logs all changes to your models. Audited can also record who made those changes, save comments and associate models related to the changes. -Audited currently (4.x) works with Rails 5.0 and 4.2. It may work with 4.1 and 4.0, but this is not guaranteed. +Audited currently (4.x) works with Rails 5.1, 5.0 and 4.2. It may work with 4.1 and 4.0, but this is not guaranteed. For Rails 3, use gem version 3.0 or see the [3.0-stable branch](https://github.com/collectiveidea/audited/tree/3.0-stable). @@ -14,6 +14,7 @@ Audited supports and is [tested against](http://travis-ci.org/collectiveidea/aud * 2.1.5 * 2.2.4 * 2.3.1 +* 2.4.1 Audited may work just fine with a Ruby version not listed above, but we can't guarantee that it will. If you'd like to maintain a Ruby that isn't listed, please let us know with a [pull request](https://github.com/collectiveidea/audited/pulls). diff --git a/gemfiles/rails51.gemfile b/gemfiles/rails51.gemfile new file mode 100644 index 000000000..935e22322 --- /dev/null +++ b/gemfiles/rails51.gemfile @@ -0,0 +1,7 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "rails", ">= 5.1.0.rc1", "< 5.2" + +gemspec :name => "audited", :path => "../" diff --git a/spec/audited/sweeper_spec.rb b/spec/audited/sweeper_spec.rb index e5a78bc03..cb5fd6113 100644 --- a/spec/audited/sweeper_spec.rb +++ b/spec/audited/sweeper_spec.rb @@ -76,7 +76,7 @@ def update controller.send(:current_user=, user) expect { - put :update, id: 123 + put :update, Rails::VERSION::MAJOR == 4 ? {id: 123} : {params: {id: 123}} }.to_not change( Audited::Audit, :count ) end end diff --git a/spec/support/active_record/models.rb b/spec/support/active_record/models.rb index 90125746b..f02d9d577 100644 --- a/spec/support/active_record/models.rb +++ b/spec/support/active_record/models.rb @@ -15,6 +15,7 @@ def name=(val) class UserOnlyPassword < ::ActiveRecord::Base self.table_name = :users + attribute :non_column_attr if Rails.version >= '5.1' audited allow_mass_assignment: true, only: :password end diff --git a/spec/support/active_record/postgres/1_change_audited_changes_type_to_json.rb b/spec/support/active_record/postgres/1_change_audited_changes_type_to_json.rb index 789b7577c..26af16a30 100644 --- a/spec/support/active_record/postgres/1_change_audited_changes_type_to_json.rb +++ b/spec/support/active_record/postgres/1_change_audited_changes_type_to_json.rb @@ -1,4 +1,5 @@ -class ChangeAuditedChangesTypeToJson < ActiveRecord::Migration +parent = Rails::VERSION::MAJOR == 4 ? ActiveRecord::Migration : ActiveRecord::Migration[4.2] +class ChangeAuditedChangesTypeToJson < parent def self.up remove_column :audits, :audited_changes add_column :audits, :audited_changes, :json diff --git a/spec/support/active_record/postgres/2_change_audited_changes_type_to_jsonb.rb b/spec/support/active_record/postgres/2_change_audited_changes_type_to_jsonb.rb index c8d543f0d..711e100ac 100644 --- a/spec/support/active_record/postgres/2_change_audited_changes_type_to_jsonb.rb +++ b/spec/support/active_record/postgres/2_change_audited_changes_type_to_jsonb.rb @@ -1,4 +1,5 @@ -class ChangeAuditedChangesTypeToJsonb < ActiveRecord::Migration +parent = Rails::VERSION::MAJOR == 4 ? ActiveRecord::Migration : ActiveRecord::Migration[4.2] +class ChangeAuditedChangesTypeToJsonb < parent def self.up remove_column :audits, :audited_changes add_column :audits, :audited_changes, :jsonb diff --git a/test/upgrade_generator_test.rb b/test/upgrade_generator_test.rb index c00cffefa..3b4243dd2 100644 --- a/test/upgrade_generator_test.rb +++ b/test/upgrade_generator_test.rb @@ -6,7 +6,11 @@ class UpgradeGeneratorTest < Rails::Generators::TestCase destination File.expand_path('../../tmp', __FILE__) setup :prepare_destination tests Audited::Generators::UpgradeGenerator - self.use_transactional_fixtures = false + if Rails::VERSION::MAJOR == 4 + self.use_transactional_fixtures = false + else + self.use_transactional_tests = false + end test "should add 'comment' to audits table" do load_schema 1 From 8d5d7135478431d6a3e72345013039517e9db94e Mon Sep 17 00:00:00 2001 From: Dominic Cleal Date: Mon, 27 Mar 2017 17:32:06 +0100 Subject: [PATCH 2/2] Update generators to use versioned AR::Migration parent --- lib/generators/audited/install_generator.rb | 2 ++ lib/generators/audited/migration_helper.rb | 9 +++++++++ .../audited/templates/add_association_to_audits.rb | 2 +- .../audited/templates/add_comment_to_audits.rb | 2 +- .../audited/templates/add_remote_address_to_audits.rb | 2 +- .../audited/templates/add_request_uuid_to_audits.rb | 2 +- lib/generators/audited/templates/install.rb | 2 +- .../templates/rename_association_to_associated.rb | 2 +- .../templates/rename_changes_to_audited_changes.rb | 2 +- .../audited/templates/rename_parent_to_association.rb | 2 +- lib/generators/audited/upgrade_generator.rb | 2 ++ test/install_generator_test.rb | 9 +++++++++ test/upgrade_generator_test.rb | 11 +++++++++++ 13 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 lib/generators/audited/migration_helper.rb diff --git a/lib/generators/audited/install_generator.rb b/lib/generators/audited/install_generator.rb index c7b018977..19a3e3259 100644 --- a/lib/generators/audited/install_generator.rb +++ b/lib/generators/audited/install_generator.rb @@ -3,11 +3,13 @@ require 'active_record' require 'rails/generators/active_record' require 'generators/audited/migration' +require 'generators/audited/migration_helper' module Audited module Generators class InstallGenerator < Rails::Generators::Base include Rails::Generators::Migration + include Audited::Generators::MigrationHelper extend Audited::Generators::Migration class_option :audited_changes_column_type, type: :string, default: "text", required: false diff --git a/lib/generators/audited/migration_helper.rb b/lib/generators/audited/migration_helper.rb new file mode 100644 index 000000000..37cac7cc1 --- /dev/null +++ b/lib/generators/audited/migration_helper.rb @@ -0,0 +1,9 @@ +module Audited + module Generators + module MigrationHelper + def migration_parent + Rails::VERSION::MAJOR == 4 ? 'ActiveRecord::Migration' : "ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]" + end + end + end +end diff --git a/lib/generators/audited/templates/add_association_to_audits.rb b/lib/generators/audited/templates/add_association_to_audits.rb index fd4a94b8b..b949b4649 100644 --- a/lib/generators/audited/templates/add_association_to_audits.rb +++ b/lib/generators/audited/templates/add_association_to_audits.rb @@ -1,4 +1,4 @@ -class <%= migration_class_name %> < ActiveRecord::Migration +class <%= migration_class_name %> < <%= migration_parent %> def self.up add_column :audits, :association_id, :integer add_column :audits, :association_type, :string diff --git a/lib/generators/audited/templates/add_comment_to_audits.rb b/lib/generators/audited/templates/add_comment_to_audits.rb index 1fd5703f0..b20b0abea 100644 --- a/lib/generators/audited/templates/add_comment_to_audits.rb +++ b/lib/generators/audited/templates/add_comment_to_audits.rb @@ -1,4 +1,4 @@ -class <%= migration_class_name %> < ActiveRecord::Migration +class <%= migration_class_name %> < <%= migration_parent %> def self.up add_column :audits, :comment, :string end diff --git a/lib/generators/audited/templates/add_remote_address_to_audits.rb b/lib/generators/audited/templates/add_remote_address_to_audits.rb index 58ab5f9be..4cd3f50dc 100644 --- a/lib/generators/audited/templates/add_remote_address_to_audits.rb +++ b/lib/generators/audited/templates/add_remote_address_to_audits.rb @@ -1,4 +1,4 @@ -class <%= migration_class_name %> < ActiveRecord::Migration +class <%= migration_class_name %> < <%= migration_parent %> def self.up add_column :audits, :remote_address, :string end diff --git a/lib/generators/audited/templates/add_request_uuid_to_audits.rb b/lib/generators/audited/templates/add_request_uuid_to_audits.rb index a58a48328..d7c9113fb 100644 --- a/lib/generators/audited/templates/add_request_uuid_to_audits.rb +++ b/lib/generators/audited/templates/add_request_uuid_to_audits.rb @@ -1,4 +1,4 @@ -class <%= migration_class_name %> < ActiveRecord::Migration +class <%= migration_class_name %> < <%= migration_parent %> def self.up add_column :audits, :request_uuid, :string add_index :audits, :request_uuid diff --git a/lib/generators/audited/templates/install.rb b/lib/generators/audited/templates/install.rb index d8a6fca7a..260655f64 100644 --- a/lib/generators/audited/templates/install.rb +++ b/lib/generators/audited/templates/install.rb @@ -1,4 +1,4 @@ -class <%= migration_class_name %> < ActiveRecord::Migration +class <%= migration_class_name %> < <%= migration_parent %> def self.up create_table :audits, :force => true do |t| t.column :auditable_id, :integer diff --git a/lib/generators/audited/templates/rename_association_to_associated.rb b/lib/generators/audited/templates/rename_association_to_associated.rb index a4a2d8e0d..206e0a5de 100644 --- a/lib/generators/audited/templates/rename_association_to_associated.rb +++ b/lib/generators/audited/templates/rename_association_to_associated.rb @@ -1,4 +1,4 @@ -class <%= migration_class_name %> < ActiveRecord::Migration +class <%= migration_class_name %> < <%= migration_parent %> def self.up if index_exists? :audits, [:association_id, :association_type], :name => 'association_index' remove_index :audits, :name => 'association_index' diff --git a/lib/generators/audited/templates/rename_changes_to_audited_changes.rb b/lib/generators/audited/templates/rename_changes_to_audited_changes.rb index 8e21c6f2f..ec7c7b6e1 100644 --- a/lib/generators/audited/templates/rename_changes_to_audited_changes.rb +++ b/lib/generators/audited/templates/rename_changes_to_audited_changes.rb @@ -1,4 +1,4 @@ -class <%= migration_class_name %> < ActiveRecord::Migration +class <%= migration_class_name %> < <%= migration_parent %> def self.up rename_column :audits, :changes, :audited_changes end diff --git a/lib/generators/audited/templates/rename_parent_to_association.rb b/lib/generators/audited/templates/rename_parent_to_association.rb index f29b2e2c0..8c5d63dc1 100644 --- a/lib/generators/audited/templates/rename_parent_to_association.rb +++ b/lib/generators/audited/templates/rename_parent_to_association.rb @@ -1,4 +1,4 @@ -class <%= migration_class_name %> < ActiveRecord::Migration +class <%= migration_class_name %> < <%= migration_parent %> def self.up rename_column :audits, :auditable_parent_id, :association_id rename_column :audits, :auditable_parent_type, :association_type diff --git a/lib/generators/audited/upgrade_generator.rb b/lib/generators/audited/upgrade_generator.rb index 14d066662..35d328c13 100644 --- a/lib/generators/audited/upgrade_generator.rb +++ b/lib/generators/audited/upgrade_generator.rb @@ -3,11 +3,13 @@ require 'active_record' require 'rails/generators/active_record' require 'generators/audited/migration' +require 'generators/audited/migration_helper' module Audited module Generators class UpgradeGenerator < Rails::Generators::Base include Rails::Generators::Migration + include Audited::Generators::MigrationHelper extend Audited::Generators::Migration source_root File.expand_path("../templates", __FILE__) diff --git a/test/install_generator_test.rb b/test/install_generator_test.rb index 3b9f7b35e..8f17ac121 100644 --- a/test/install_generator_test.rb +++ b/test/install_generator_test.rb @@ -33,4 +33,13 @@ class InstallGeneratorTest < Rails::Generators::TestCase assert_includes(content, 't.column :audited_changes, :json') end end + + test "generate migration with correct AR migration parent" do + run_generator + + assert_migration "db/migrate/install_audited.rb" do |content| + parent = Rails::VERSION::MAJOR == 4 ? 'ActiveRecord::Migration' : "ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]" + assert_includes(content, "class InstallAudited < #{parent}\n") + end + end end diff --git a/test/upgrade_generator_test.rb b/test/upgrade_generator_test.rb index 3b4243dd2..53ada067c 100644 --- a/test/upgrade_generator_test.rb +++ b/test/upgrade_generator_test.rb @@ -78,4 +78,15 @@ class UpgradeGeneratorTest < Rails::Generators::TestCase assert_match(/add_index :audits, :request_uuid/, content) end end + + test "generate migration with correct AR migration parent" do + load_schema 1 + + run_generator %w(upgrade) + + assert_migration "db/migrate/add_comment_to_audits.rb" do |content| + parent = Rails::VERSION::MAJOR == 4 ? 'ActiveRecord::Migration' : "ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]" + assert_includes(content, "class AddCommentToAudits < #{parent}\n") + end + end end