fix a bug when has_many :through does not have paper_trail enabled
This commit is contained in:
parent
aba6ef5241
commit
0f89d37623
|
@ -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) }
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -0,0 +1,5 @@
|
|||
class Editorship < ActiveRecord::Base
|
||||
belongs_to :book
|
||||
belongs_to :editor
|
||||
has_paper_trail
|
||||
end
|
|
@ -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
|
||||
|
|
|
@ -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|
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue