Created
June 19, 2012 12:56
-
-
Save kredmer/2954021 to your computer and use it in GitHub Desktop.
Wrong access of options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def belongs_to_with_test_methods(name, options={}, &block) | |
belongs_to_without_test_methods(name, options, &block) | |
refl = reflections[name] | |
id_method = refl.options[:primary_key] || refl.klass.primary_key | |
if options[:polymorphic] | |
# TODO: the class lookup in _is? below is incomplete; a polymorphic association to an STI base class | |
# will fail to match an object of a derived type | |
# (ie X belongs_to Y (polymorphic), Z is a subclass of Y; @x.y_is?(some_z) will never pass) | |
class_eval %{ | |
--------> binding.pry | |
def #{name}_is?(target) | |
#target.class.name == self.#{refl.options[:foreign_type]} && target.#{id_method} == self.#{refl.foreign_key} | |
target.class.name == self.#{refl.foreign_type} && target.#{id_method} == self.#{refl.foreign_key} | |
end | |
def #{name}_changed? | |
# #{refl.foreign_key}_changed? || #{refl.options[:foreign_type]}_changed? | |
#{refl.foreign_key}_changed? || #{refl.foreign_type}_changed? | |
end | |
} | |
else | |
id_method = refl.options[:primary_key] || refl.klass.primary_key | |
class_eval %{ | |
def #{name}_is?(target) | |
our_id = self.#{refl.foreign_key} | |
# if our_id is nil, only return true if target is nil | |
return target.nil? unless our_id | |
target.class <= ::#{refl.klass.name} && target.#{id_method} == our_id | |
end | |
def #{name}_changed? | |
#{refl.foreign_key}_changed? | |
end | |
} | |
end | |
end | |
[1] pry(UserAddon)> refl | |
=> #<ActiveRecord::Reflection::AssociationReflection:0xbc90198 | |
@active_record= | |
UserAddon(id: integer, myapikey: string, myurl: string, created_at: datetime, updated_at: datetime, contact_id: integer, addon_id: integer, addon_type: string), | |
@class_name="Addon", | |
@collection=false, | |
@foreign_key="addon_id", | |
@foreign_type="addon_type", | |
@klass= | |
Addon(id: integer, title: string, body: text, bodylong: text, url: string, created_at: datetime, updated_at: datetime, what: string), | |
@macro=:belongs_to, | |
@name=:addon, | |
@options={:polymorphic=>true}, | |
@plural_name="addons"> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment