Finish removing the cleanup_callbacks hack from tests

Instead of changing the configuration of Fluxor on the fly, which
requires callback "cleanup", simply define a few models with the
desired configurations.
This commit is contained in:
Jared Beck 2017-02-05 20:57:54 -05:00
parent 530523c8ea
commit e2af8adb21
9 changed files with 131 additions and 208 deletions

View File

@ -118,6 +118,22 @@ DB=postgres bundle exec rake
DB=postgres bundle exec appraisal ar-5.0 rake
```
## Editing the migration
After editing `test/dummy/db/migrate/20110208155312_set_up_test_tables.rb` ..
```
cd test/dummy
export BUNDLE_GEMFILE=../../gemfiles/ar_5.0.gemfile
RAILS_ENV=test bundle exec rake db:environment:set db:drop db:create db:migrate
RAILS_ENV=foo bundle exec rake db:environment:set db:drop db:create db:migrate
RAILS_ENV=bar bundle exec rake db:environment:set db:drop db:create db:migrate
unset BUNDLE_GEMFILE
cd ../..
```
Don't forget to commit changes to `schema.rb`.
## Releases
1. Set the version in lib/paper_trail/version_number.rb

View File

@ -0,0 +1,6 @@
module On
class Create < ActiveRecord::Base
self.table_name = :on_create
has_paper_trail on: [:create]
end
end

View File

@ -0,0 +1,6 @@
module On
class Destroy < ActiveRecord::Base
self.table_name = :on_destroy
has_paper_trail on: [:destroy]
end
end

View File

@ -0,0 +1,6 @@
module On
class EmptyArray < ActiveRecord::Base
self.table_name = :on_empty_array
has_paper_trail on: []
end
end

View File

@ -0,0 +1,6 @@
module On
class Update < ActiveRecord::Base
self.table_name = :on_update
has_paper_trail on: [:update]
end
end

View File

@ -9,6 +9,22 @@ class SetUpTestTables < ActiveRecord::Migration
TEXT_BYTES = 1_073_741_823
def up
create_table :on_create, force: true do |t|
t.string :name, null: false
end
create_table :on_destroy, force: true do |t|
t.string :name, null: false
end
create_table :on_empty_array, force: true do |t|
t.string :name, null: false
end
create_table :on_update, force: true do |t|
t.string :name, null: false
end
# Classes: Vehicle, Car, Truck
create_table :vehicles, force: true do |t|
t.string :name, null: false
@ -292,51 +308,8 @@ class SetUpTestTables < ActiveRecord::Migration
end
def down
drop_table :bar_habtms_foo_habtms
drop_table :foo_habtms
drop_table :bar_habtms
drop_table :citations
drop_table :quotations
drop_table :animals
drop_table :skippers
drop_table :not_on_updates
drop_table :posts
drop_table :songs
drop_table :editors
drop_table :editorships
drop_table :people
drop_table :authorships
drop_table :books
drop_table :articles
drop_table :whatchamajiggers
drop_table :fluxors
drop_table :wotsits
remove_index :post_versions, column: [:item_type, :item_id]
drop_table :post_versions
remove_index :versions, column: [:item_type, :item_id]
drop_table :versions
if JsonVersion.table_exists?
remove_index :json_versions, column: [:item_type, :item_id]
drop_table :json_versions
end
drop_table :widgets
drop_table :documents
drop_table :legacy_widgets
drop_table :things
drop_table :translations
drop_table :gadgets
drop_table :customers
drop_table :orders
drop_table :line_items
drop_table :fruits
drop_table :boolits
drop_table :chapters
drop_table :sections
drop_table :paragraphs
remove_index :version_associations, column: [:version_id]
remove_index :version_associations, name: "index_version_associations_on_foreign_key"
drop_table :version_associations
drop_table :callback_modifiers
# Not actually irreversible, but there is no need to maintain this method.
raise ActiveRecord::IrreversibleMigration
end
private

View File

@ -1,4 +1,3 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
@ -37,10 +36,9 @@ ActiveRecord::Schema.define(version: 20110208155312) do
t.string "whodunnit"
t.text "object"
t.datetime "created_at"
t.index ["item_type", "item_id"], name: "index_banana_versions_on_item_type_and_item_id"
end
add_index "banana_versions", ["item_type", "item_id"], name: "index_banana_versions_on_item_type_and_item_id"
create_table "bananas", force: :cascade do |t|
t.datetime "created_at"
t.datetime "updated_at"
@ -53,11 +51,10 @@ ActiveRecord::Schema.define(version: 20110208155312) do
create_table "bar_habtms_foo_habtms", id: false, force: :cascade do |t|
t.integer "foo_habtm_id"
t.integer "bar_habtm_id"
t.index ["bar_habtm_id"], name: "index_bar_habtms_foo_habtms_on_bar_habtm_id"
t.index ["foo_habtm_id"], name: "index_bar_habtms_foo_habtms_on_foo_habtm_id"
end
add_index "bar_habtms_foo_habtms", ["bar_habtm_id"], name: "index_bar_habtms_foo_habtms_on_bar_habtm_id"
add_index "bar_habtms_foo_habtms", ["foo_habtm_id"], name: "index_bar_habtms_foo_habtms_on_foo_habtm_id"
create_table "books", force: :cascade do |t|
t.string "title"
end
@ -87,15 +84,14 @@ ActiveRecord::Schema.define(version: 20110208155312) do
t.string "whodunnit"
t.text "object"
t.datetime "created_at"
t.index ["item_type", "item_id"], name: "idx_cust_pk_item"
end
add_index "custom_primary_key_record_versions", ["item_type", "item_id"], name: "idx_cust_pk_item"
create_table "custom_primary_key_records", id: false, force: :cascade do |t|
t.string "uuid"
create_table "custom_primary_key_records", primary_key: "uuid", id: :string, force: :cascade do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.index ["uuid"], name: "sqlite_autoindex_custom_primary_key_records_1", unique: true
end
create_table "customers", force: :cascade do |t|
@ -151,6 +147,22 @@ ActiveRecord::Schema.define(version: 20110208155312) do
t.datetime "updated_at"
end
create_table "on_create", force: :cascade do |t|
t.string "name", null: false
end
create_table "on_destroy", force: :cascade do |t|
t.string "name", null: false
end
create_table "on_empty_array", force: :cascade do |t|
t.string "name", null: false
end
create_table "on_update", force: :cascade do |t|
t.string "name", null: false
end
create_table "orders", force: :cascade do |t|
t.integer "customer_id"
t.string "order_date"
@ -175,10 +187,9 @@ ActiveRecord::Schema.define(version: 20110208155312) do
t.datetime "created_at"
t.string "ip"
t.string "user_agent"
t.index ["item_type", "item_id"], name: "index_post_versions_on_item_type_and_item_id"
end
add_index "post_versions", ["item_type", "item_id"], name: "index_post_versions_on_item_type_and_item_id"
create_table "post_with_statuses", force: :cascade do |t|
t.integer "status"
t.datetime "created_at", null: false
@ -232,11 +243,10 @@ ActiveRecord::Schema.define(version: 20110208155312) do
t.integer "version_id"
t.string "foreign_key_name", null: false
t.integer "foreign_key_id"
t.index ["foreign_key_name", "foreign_key_id"], name: "index_version_associations_on_foreign_key"
t.index ["version_id"], name: "index_version_associations_on_version_id"
end
add_index "version_associations", ["foreign_key_name", "foreign_key_id"], name: "index_version_associations_on_foreign_key"
add_index "version_associations", ["version_id"], name: "index_version_associations_on_version_id"
create_table "versions", force: :cascade do |t|
t.string "item_type", null: false
t.integer "item_id", null: false
@ -253,10 +263,9 @@ ActiveRecord::Schema.define(version: 20110208155312) do
t.string "title"
t.string "ip"
t.string "user_agent"
t.index ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id"
end
add_index "versions", ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id"
create_table "whatchamajiggers", force: :cascade do |t|
t.string "owner_type"
t.integer "owner_id"

View File

@ -98,21 +98,3 @@ def params_wrapper(args)
args
end
end
# Deprecated
module CleanupCallbacks
# Deprecated
def cleanup_callbacks(target, type)
original_callbacks = nil
setup do
original_callbacks = target.send(:get_callbacks, type).deep_dup
end
teardown do
if original_callbacks
target.send(:set_callbacks, type, original_callbacks)
end
end
end
end

View File

@ -2,8 +2,6 @@ require "test_helper"
require "time_travel_helper"
class HasPaperTrailModelTest < ActiveSupport::TestCase
extend CleanupCallbacks
context "A record with defined 'only' and 'ignore' attributes" do
setup { @article = Article.create }
@ -1269,114 +1267,62 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
context "The `on` option" do
context "on create" do
cleanup_callbacks(Fluxor, :create)
cleanup_callbacks(Fluxor, :update)
cleanup_callbacks(Fluxor, :destroy)
cleanup_callbacks(Fluxor, :save)
setup do
Fluxor.instance_eval <<-END
has_paper_trail :on => [:create]
END
@fluxor = Fluxor.create
@fluxor.update_attributes name: "blah"
@fluxor.destroy
end
should "only have a version for the create event" do
assert_equal 1, @fluxor.versions.length
assert_equal "create", @fluxor.versions.last.event
record = ::On::Create.create(name: "Alice")
record.update_attributes name: "blah"
record.destroy
assert_equal 1, record.versions.length
assert_equal "create", record.versions.last.event
end
end
context "on update" do
cleanup_callbacks(Fluxor, :create)
cleanup_callbacks(Fluxor, :update)
cleanup_callbacks(Fluxor, :destroy)
cleanup_callbacks(Fluxor, :save)
setup do
Fluxor.instance_eval <<-END
has_paper_trail :on => [:update]
END
@fluxor = Fluxor.create
@fluxor.update_attributes name: "blah"
@fluxor.destroy
end
should "only have a version for the update event" do
assert_equal 1, @fluxor.versions.length
assert_equal "update", @fluxor.versions.last.event
record = ::On::Update.create(name: "Alice")
record.update_attributes name: "blah"
record.destroy
assert_equal 1, record.versions.length
assert_equal "update", record.versions.last.event
end
end
context "on destroy" do
cleanup_callbacks(Fluxor, :create)
cleanup_callbacks(Fluxor, :update)
cleanup_callbacks(Fluxor, :destroy)
cleanup_callbacks(Fluxor, :save)
setup do
Fluxor.instance_eval <<-END
has_paper_trail :on => [:destroy]
END
@fluxor = Fluxor.create
@fluxor.update_attributes name: "blah"
@fluxor.destroy
end
should "only have a version for the destroy event" do
assert_equal 1, @fluxor.versions.length
assert_equal "destroy", @fluxor.versions.last.event
record = ::On::Destroy.create(name: "Alice")
record.update_attributes name: "blah"
record.destroy
assert_equal 1, record.versions.length
assert_equal "destroy", record.versions.last.event
end
end
context "on []" do
cleanup_callbacks(Fluxor, :create)
cleanup_callbacks(Fluxor, :update)
cleanup_callbacks(Fluxor, :destroy)
cleanup_callbacks(Fluxor, :save)
setup do
Fluxor.instance_eval <<-END
has_paper_trail :on => []
END
@fluxor = Fluxor.create
@fluxor.update_attributes name: "blah"
@record = ::On::EmptyArray.create(name: "Alice")
@record.update_attributes name: "blah"
end
teardown do
@fluxor.destroy
@record.destroy
end
should "not have any versions" do
assert_equal 0, @fluxor.versions.length
assert_equal 0, @record.versions.length
end
should "still respond to touch_with_version" do
@fluxor.paper_trail.touch_with_version
assert_equal 1, @fluxor.versions.length
@record.paper_trail.touch_with_version
assert_equal 1, @record.versions.length
end
end
context "allows a symbol to be passed" do
cleanup_callbacks(Fluxor, :create)
cleanup_callbacks(Fluxor, :update)
cleanup_callbacks(Fluxor, :destroy)
cleanup_callbacks(Fluxor, :save)
setup do
Fluxor.instance_eval <<-END
has_paper_trail :on => :create
END
@fluxor = Fluxor.create
@fluxor.update_attributes name: "blah"
@fluxor.destroy
end
should "only have a version for hte create event" do
assert_equal 1, @fluxor.versions.length
assert_equal "create", @fluxor.versions.last.event
record = ::On::Create.create(name: "Alice")
record.update_attributes name: "blah"
record.destroy
assert_equal 1, record.versions.length
assert_equal "create", record.versions.last.event
end
end
end
@ -1419,65 +1365,38 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
context "custom events" do
context "on create" do
cleanup_callbacks(Fluxor, :create)
cleanup_callbacks(Fluxor, :update)
cleanup_callbacks(Fluxor, :destroy)
cleanup_callbacks(Fluxor, :save)
setup do
Fluxor.instance_eval <<-END
has_paper_trail :on => [:create]
END
@fluxor = Fluxor.new.tap { |model| model.paper_trail_event = "created" }
@fluxor.update_attributes name: "blah"
@fluxor.destroy
end
should "only have a version for the created event" do
assert_equal 1, @fluxor.versions.length
assert_equal "created", @fluxor.versions.last.event
record = ::On::Create.new.tap { |model|
model.paper_trail_event = "created"
}
record.update_attributes name: "blah"
record.destroy
assert_equal 1, record.versions.length
assert_equal "created", record.versions.last.event
end
end
context "on update" do
cleanup_callbacks(Fluxor, :create)
cleanup_callbacks(Fluxor, :update)
cleanup_callbacks(Fluxor, :destroy)
cleanup_callbacks(Fluxor, :save)
setup do
Fluxor.instance_eval <<-END
has_paper_trail :on => [:update]
END
@fluxor = Fluxor.create.tap { |model| model.paper_trail_event = "name_updated" }
@fluxor.update_attributes name: "blah"
@fluxor.destroy
end
should "only have a version for the name_updated event" do
assert_equal 1, @fluxor.versions.length
assert_equal "name_updated", @fluxor.versions.last.event
record = ::On::Update.create(name: "Alice").tap { |model|
model.paper_trail_event = "name_updated"
}
record.update_attributes name: "blah"
record.destroy
assert_equal 1, record.versions.length
assert_equal "name_updated", record.versions.last.event
end
end
context "on destroy" do
cleanup_callbacks(Fluxor, :create)
cleanup_callbacks(Fluxor, :update)
cleanup_callbacks(Fluxor, :destroy)
cleanup_callbacks(Fluxor, :save)
setup do
Fluxor.instance_eval <<-END
has_paper_trail :on => [:destroy]
END
@fluxor = Fluxor.create.tap { |model| model.paper_trail_event = "destroyed" }
@fluxor.update_attributes name: "blah"
@fluxor.destroy
end
should "only have a version for the destroy event" do
assert_equal 1, @fluxor.versions.length
assert_equal "destroyed", @fluxor.versions.last.event
record = ::On::Destroy.create(name: "Alice").tap { |model|
model.paper_trail_event = "destroyed"
}
record.update_attributes name: "blah"
record.destroy
assert_equal 1, record.versions.length
assert_equal "destroyed", record.versions.last.event
end
end
end