rails/rails — pull request #7293 by Bertg (2012-08-08 21:18:09+00:00)
Title: Naming helpers should first check if passed object responds to model_name

Hi,

I refactored a piece of the `ActiveModel::Naming` module. In the part of the code that tries to find the `model_name` for the passed argument, I changed the implementation from "type based" (checking for Class) to "behavior based" (checking for method)

This change behaves almost 100% the same as the previous implementation.

* Makes the same assumptions as old code. If argument responds to `to_model`, then `.to_model.class.model_name` can be called.
* If argument it is not a `Class`, call `.class.model_name`
* This version however allows the argument to directly respond to `model_name` if it can.

This gives a (very minor) speed boost if a class is passed, but more importantly; it allows an instance to define its "name".

We use one class to store most of our data. However, how this class should behave, depends on it's configuration. Routing, human names, partials all depend on this configuration. By allowing the instance to respond to model_name, we can easily take advantage of all `ActiveModel` based code.