From dc55fc10f1961dd1cbcda328dbecac557e6f0f4d Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Wed, 14 Oct 2015 08:45:24 -0400 Subject: [PATCH] Support associations (no has_and_belongs_to) --- app/controllers/admin/versions_controller.rb | 2 +- ...51014121823_create_version_associations.rb | 17 +++++++++++++++ ...4_add_transaction_id_column_to_versions.rb | 11 ++++++++++ db/schema.rb | 21 ++++++++++++++----- 4 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20151014121823_create_version_associations.rb create mode 100644 db/migrate/20151014121824_add_transaction_id_column_to_versions.rb diff --git a/app/controllers/admin/versions_controller.rb b/app/controllers/admin/versions_controller.rb index 83ec1c9..d16a86d 100644 --- a/app/controllers/admin/versions_controller.rb +++ b/app/controllers/admin/versions_controller.rb @@ -11,6 +11,6 @@ class Admin::VersionsController < Admin::BaseController private def set_version - @version = PaperTrail::Version.find(params[:id]).reify + @version = PaperTrail::Version.find(params[:id]).reify(has_one: true) end end diff --git a/db/migrate/20151014121823_create_version_associations.rb b/db/migrate/20151014121823_create_version_associations.rb new file mode 100644 index 0000000..12c8f06 --- /dev/null +++ b/db/migrate/20151014121823_create_version_associations.rb @@ -0,0 +1,17 @@ +class CreateVersionAssociations < ActiveRecord::Migration + def self.up + create_table :version_associations do |t| + t.integer :version_id + t.string :foreign_key_name, :null => false + t.integer :foreign_key_id + end + add_index :version_associations, [:version_id] + add_index :version_associations, [:foreign_key_name, :foreign_key_id], :name => 'index_version_associations_on_foreign_key' + end + + def self.down + remove_index :version_associations, [:version_id] + remove_index :version_associations, :name => 'index_version_associations_on_foreign_key' + drop_table :version_associations + end +end \ No newline at end of file diff --git a/db/migrate/20151014121824_add_transaction_id_column_to_versions.rb b/db/migrate/20151014121824_add_transaction_id_column_to_versions.rb new file mode 100644 index 0000000..67e0e22 --- /dev/null +++ b/db/migrate/20151014121824_add_transaction_id_column_to_versions.rb @@ -0,0 +1,11 @@ +class AddTransactionIdColumnToVersions < ActiveRecord::Migration + def self.up + add_column :versions, :transaction_id, :integer + add_index :versions, [:transaction_id] + end + + def self.down + remove_index :versions, [:transaction_id] + remove_column :versions, :transaction_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 81bdf6a..33c88fa 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20151009204428) do +ActiveRecord::Schema.define(version: 20151014121824) do create_table "awaken_types", force: :cascade do |t| t.string "name" @@ -134,15 +134,26 @@ ActiveRecord::Schema.define(version: 20151009204428) do add_index "users_roles", ["user_id", "role_id"], name: "index_users_roles_on_user_id_and_role_id" + create_table "version_associations", force: :cascade do |t| + t.integer "version_id" + t.string "foreign_key_name", null: false + t.integer "foreign_key_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 - t.string "event", null: false + t.string "item_type", null: false + t.integer "item_id", null: false + t.string "event", null: false t.string "whodunnit" - t.text "object", limit: 1073741823 + t.text "object", limit: 1073741823 t.datetime "created_at" + t.integer "transaction_id" end add_index "versions", ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id" + add_index "versions", ["transaction_id"], name: "index_versions_on_transaction_id" end