Skip to content

Commit

Permalink
allow circular dependencies in polymorphic_type_override
Browse files Browse the repository at this point in the history
test plan:
  * run specs

Change-Id: Ie6f8be9c23c76ff2ab499641c65ba5425e4a453b
Reviewed-on: https://gerrit.instructure.com/29563
Tested-by: Jenkins <[email protected]>
Reviewed-by: Jon Willesen <[email protected]>
Product-Review: Raphael Weiner <[email protected]>
QA-Review: Raphael Weiner <[email protected]>
  • Loading branch information
zachpendleton authored and Raphael Weiner committed Feb 4, 2014
1 parent c7ef846 commit 3e6f3ed
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app/models/cloned_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion app/models/content_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def initialize( alignment )
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

Expand Down
4 changes: 2 additions & 2 deletions app/models/learning_outcome_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 2 additions & 10 deletions app/models/polymorphic_type_override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ def self.included(base)

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
Expand All @@ -27,11 +25,5 @@ def override_polymorphic_types(polymorphic_type_mappings)
super attr
end
end

private

def validate_classes!(klasses)
klasses.map(&:constantize)
end
end
end
end
16 changes: 3 additions & 13 deletions spec/models/polymorphic_type_override_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
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
Expand All @@ -41,8 +41,8 @@ class ContentTag
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
Expand All @@ -61,15 +61,5 @@ class ContentTag
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
2 changes: 1 addition & 1 deletion vendor/plugins/simply_versioned/lib/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 3e6f3ed

Please sign in to comment.