allow circular dependencies in polymorphic_type_override

test plan:
  * run specs

Change-Id: Ie6f8be9c23c76ff2ab499641c65ba5425e4a453b
Reviewed-on: https://gerrit.instructure.com/29563
Tested-by: Jenkins <jenkins@instructure.com>
Reviewed-by: Jon Willesen <jonw@instructure.com>
Product-Review: Raphael Weiner <rweiner@pivotallabs.com>
QA-Review: Raphael Weiner <rweiner@pivotallabs.com>
This commit is contained in:
Zach Pendleton 2014-02-03 19:31:45 -07:00 committed by Raphael Weiner
parent c7ef846e86
commit 3e6f3edbca
6 changed files with 10 additions and 28 deletions

View File

@ -18,7 +18,7 @@
class ClonedItem < ActiveRecord::Base
include PolymorphicTypeOverride
override_polymorphic_types [type: 'original_item', from: 'Quiz', to: 'Quizzes::Quiz']
override_polymorphic_types [type: 'original_item_type', from: 'Quiz', to: 'Quizzes::Quiz']
belongs_to :original_item, :polymorphic => true
has_many :attachments, :order => 'id asc'

View File

@ -48,7 +48,7 @@ class ContentTag < ActiveRecord::Base
validates_as_url :url
include PolymorphicTypeOverride
override_polymorphic_types [type: 'content', from: 'Quiz', to: 'Quizzes::Quiz']
override_polymorphic_types [type: 'content_type', from: 'Quiz', to: 'Quizzes::Quiz']
acts_as_list :scope => :context_module

View File

@ -18,8 +18,8 @@
class LearningOutcomeResult < ActiveRecord::Base
include PolymorphicTypeOverride
override_polymorphic_types [{type: 'association', from: 'Quiz', to: 'Quizzes::Quiz'},
{type: 'associated_asset', from: 'Quiz', to: 'Quizzes::Quiz'}]
override_polymorphic_types [{type: 'association_type', from: 'Quiz', to: 'Quizzes::Quiz'},
{type: 'associated_asset_type', from: 'Quiz', to: 'Quizzes::Quiz'}]
belongs_to :user
belongs_to :learning_outcome

View File

@ -5,14 +5,12 @@ module PolymorphicTypeOverride
module ClassMethods
def override_polymorphic_types(polymorphic_type_mappings)
validate_classes!(polymorphic_type_mappings.map { |mapping| mapping[:to] })
read_attribute_override = Proc.new do |instance, attr|
matched = nil
polymorphic_type_mappings.each do |mapping|
original_class_name = mapping.fetch(:from)
new_class_name = mapping.fetch(:to)
attribute_to_override = "#{mapping.fetch(:type)}_type"
attribute_to_override = mapping.fetch(:type)
if attr == attribute_to_override && instance.instance_variable_get(:@attributes)[attribute_to_override] == original_class_name
matched = new_class_name
@ -27,11 +25,5 @@ module PolymorphicTypeOverride
super attr
end
end
private
def validate_classes!(klasses)
klasses.map(&:constantize)
end
end
end
end

View File

@ -24,7 +24,7 @@ describe PolymorphicTypeOverride do
it 'overrides a single old polymorphic type with a new one' do
class ContentTag
include PolymorphicTypeOverride
override_polymorphic_types [type: 'content', from: 'OldClassInDatabase', to: 'Quizzes::Quiz']
override_polymorphic_types [type: 'content_type', from: 'OldClassInDatabase', to: 'Quizzes::Quiz']
end
fizz_buzz = ContentTag.create! content: quiz_model, context: course_model
@ -41,8 +41,8 @@ describe PolymorphicTypeOverride do
it 'overrides multiple old polymorphic types with a new one' do
class ContentTag
extend PolymorphicTypeOverride
override_polymorphic_types [{type: 'content', from: 'OldClassInDatabase', to: 'Quizzes::Quiz'},
{type: 'context', from: 'AnotherOldClassInDatabase', to: 'Quizzes::Quiz'}]
override_polymorphic_types [{type: 'content_type', from: 'OldClassInDatabase', to: 'Quizzes::Quiz'},
{type: 'context_type', from: 'AnotherOldClassInDatabase', to: 'Quizzes::Quiz'}]
end
fizz_buzz = ContentTag.create! content: quiz_model, context: quiz_model
@ -61,15 +61,5 @@ describe PolymorphicTypeOverride do
updated_fizz_buzz.context_type.should == 'Quizzes::Quiz'
updated_fizz_buzz.context_id.should_not == 'Quizzes::Quiz'
end
it 'raises if new class does not exist' do
expect {
class ContentTag
extend PolymorphicTypeOverride
override_polymorphic_types [type: 'content', from: 'OldClassInDatabase', to: 'ThisClassDoesNotExist']
end
}.to raise_error NameError
end
end
end

View File

@ -17,7 +17,7 @@ class Version < ActiveRecord::Base #:nodoc:
# INSTRUCTURE: shims for quizzes namespacing
include PolymorphicTypeOverride
override_polymorphic_types [type: 'versionable', from: 'Quiz', to: 'Quizzes::Quiz']
override_polymorphic_types [type: 'versionable_type', from: 'Quiz', to: 'Quizzes::Quiz']
before_create :initialize_number