Reputation: 349
i have a set of objects, some of which have an organization_id attribute, and others of which delegate organization_id.
for example:
class Department
belongs_to :organization
end
class Program
belongs_to :department
delegate :organization_id, to: :department
end
class Role
belongs_to :organization
end
class UsersRole
belongs_to :role
delegate :organization_id, to: :role
end
i would like a means by which to programmatically determine which class of object the organization_id is being delegated to so that i could do something like the following
def find_organization_id_source(object)
object.organization_id.method_im_hoping_exists
end
$ find_organization_id_source(@program)
>> Department
$ find_organization_id_source(@users_role)
>> Role
Upvotes: 2
Views: 144
Reputation: 66
def is_delegated?(object, delegated_attribute)
object.attributes.include? delegated_attribute
end
It won't tell you where you're getting the value from but it should tell you if you've gotten it through delegation.
Upvotes: 0
Reputation: 18762
It is not possible. As can be seen in source of active_support/core_ext/module/delegation.rb
, Rails creates a new method that will internally invoke the delegated method.
Upvotes: 4