I discovered an interesting issue with Merb's Controller#resource
today. I posted about this to the merb list, but it seemed relevant
for DM as well. The issue is this:

  resource(Company.first, :teams)

works fine but

  resource(@team.company, :teams)

fails to find the route.

After some digging, I discovered the problem. To find the route,
#resource uses the object's class name as part of the route key.
Company.first is of class Company, and @team.company is of class
DataMapper::Associations::ManyToOne::Proxy. The interesting thing is
that calling inspect on each of them gives the same results, since
Proxy overrides #kind_of?, so this problem took me a while to find.

I suppose DataMapper::Associations::ManyToOne::Proxy should forward
#class as well, unless there is another solution anyone could propose.
Is there a different method that #resource could be using to turn the
class into a key?

The workaround for getting the right class is:

  company = Company.get!(team.company_id)

(Another workaround in Merb is to use named URLs with the #url method
and avoid this problem
entirely, but I want to be consistent and use #resource for all
resource
links.)

My colleague suggested another interesting workaround that Merb could
implement:

class Object
  def real_class_name
    class.to_s
  end
end

class Proxy
  def real_class_name
    parent.class.to_s
  end
end

And then #resource would use real_class_name instead of class_name.

..tony..
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to