fix a bug when has_many :through does not have paper_trail enabled

This commit is contained in:
Ben Li 2014-11-04 15:52:31 +11:00
parent aba6ef5241
commit 0f89d37623
7 changed files with 53 additions and 1 deletions

View File

@ -337,6 +337,7 @@ module PaperTrail
# This must be called after the direct has_manys have been reified (reify_has_many_directly)
def reify_has_many_through(associations, model, options = {})
associations.each do |assoc|
next unless assoc.klass.paper_trail_enabled_for_model?
through_collection = model.send(assoc.options[:through])
collection_keys = through_collection.map { |through_model| through_model.send(assoc.foreign_key) }

View File

@ -1,5 +1,9 @@
class Book < ActiveRecord::Base
has_many :authorships, :dependent => :destroy
has_many :authors, :through => :authorships, :source => :person
has_many :editorships, :dependent => :destroy
has_many :editors, :through => :editorships
has_paper_trail
end

View File

@ -0,0 +1,4 @@
# to demonstrate a has_through association that does not have paper_trail enabled
class Editor < ActiveRecord::Base
has_many :editorships, :dependent => :destroy
end

View File

@ -0,0 +1,5 @@
class Editorship < ActiveRecord::Base
belongs_to :book
belongs_to :editor
has_paper_trail
end

View File

@ -93,6 +93,15 @@ class SetUpTestTables < ActiveRecord::Migration
t.string :time_zone
end
create_table :editorships, :force => true do |t|
t.integer :book_id
t.integer :editor_id
end
create_table :editors, :force => true do |t|
t.string :name
end
create_table :songs, :force => true do |t|
t.integer :length
end
@ -152,6 +161,8 @@ class SetUpTestTables < ActiveRecord::Migration
drop_table :animals
drop_table :posts
drop_table :songs
drop_table :editors
drop_table :editorships
drop_table :people
drop_table :authorships
drop_table :books
@ -167,6 +178,9 @@ class SetUpTestTables < ActiveRecord::Migration
drop_table :legacy_widgets
drop_table :translations
drop_table :gadgets
drop_table :customers
drop_table :orders
drop_table :line_items
remove_index :version_associations, :column => [:version_id]
remove_index :version_associations, :name => 'index_version_associations_on_foreign_key'
drop_table :version_associations

View File

@ -42,6 +42,15 @@ ActiveRecord::Schema.define(version: 20110208155312) do
t.string "name"
end
create_table "editors", force: true do |t|
t.string "name"
end
create_table "editorships", force: true do |t|
t.integer "book_id"
t.integer "editor_id"
end
create_table "fluxors", force: true do |t|
t.integer "widget_id"
t.string "name"
@ -114,7 +123,7 @@ ActiveRecord::Schema.define(version: 20110208155312) do
t.integer "foreign_key_id"
end
add_index "version_associations", ["foreign_key_name", "foreign_key_id"], name: "index_on_foreign_key_name_and foreign_key_id"
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: true do |t|

View File

@ -1710,5 +1710,20 @@ class HasPaperTrailModelTransactionalTest < ActiveSupport::TestCase
end
end
end
context 'updated before the associated without paper_trail was created' do
setup do
@book.update_attributes! :title => 'book_1'
@book.editors.create! :name => 'editor_0'
end
context 'when reified' do
setup { @book_0 = @book.versions.last.reify(:has_many => true) }
should 'see the live association' do
assert_equal ['editor_0'], @book_0.editors.map(&:name)
end
end
end
end
end