How to make a model or a model decorator aware of its controller in Rails?

Is a decorator, like Draper, a good place to store the controller and routes associated with a given model?

I would like to be able to pass one or several model objects to a generic view, and have the view automatically generate links to the actions associated with the objects. Like this:

link_to object.public_send(attribute),
        { :controller => object.controller_path,
          :action     => :show,
          :id         => object.id }

Thank you for any suggestions of what would be a common practice.

I have also posted this question on SO: decorator - How to make a model aware of its controller in Rails? - Stack Overflow.

I will answer myself: i think a decorator is not a good place to store an associated controller, a decorator should only know about model data and HTML markup. I am still looking for a good solution.

You could check out http://objectsonrails.com/ and GitHub - objects-on-rails/display-case: An implementation of the Exhibit pattern, as described in Objects on Rails - as it says, it brings together the model and context (which could be a controller)

brett

In your example, the controller path seems highly relevant to generating HTML markup…

–Matt Jones

Matt, i do not agree: if my application changes the host, or i decide to change route names or controller names, the link urls will change, but html tags or model behavior will not. I think controller awareness should be added on a different level, not in a decorator (single responsibility principle).

-Alexey.

Thanks, i’ll look into it.

-Alexey.

I do not know if i will be able to apply it to my problem, but this tutorial is very interesting and helpful, thanks.

  • A.