Close
Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

PIVOTAL LABS
Standup 10/7/2008 – Namespaces, class names, and PostGRES

Ask for Help

When using a namespaced controller, it’s hard to get url_for to work with it. Why is that?

  • The controller option for url_for attempts to apply the namespace of whatever controller context it’s inside. So if you have an Admin::MyController class, here’s what you would need to do for url_fors to this controller:

    • from within an Admin controller class: :controller => "my"

    • from outside an Admin controller class: :controller => "admin/my"

    • for a partial that is used all over the app: :controller => "/admin/my" seems safest
  • Most of us now avoid using url_for-style hashes for our links and URL references. Named routes are a lot more dependable.

  • It is possible to generate a namespaced URL from a model reference, if you are careful.

    • This approach assumes that your controller class names correspond to your model class names.
    • If your model is @model, and your controller is Admin::ModelsController, you can use a helper like the following:

       form_for [:admin, @model] do |f| ... end
      
    • This approach can limit your design, if you rely too heavily on this convention.

Interesting Things

How to read your class’s name

While working with the UltraSphinx plugin, Davis and Brandon learned that Class.name didn’t always give them the right results. Some of their classes had a class method override what Class.name returned.

Class.class_name came closer, but returns the base class for STI (single table inheritance) classes.

Class.to_s was their most reliable option.

Testing your app on both MySQL and PostGRES

David S. is working on a plugin that allows you to test your environment on multiple databases. Since we’re starting to have more projects using PostGRES, we’re uncovering situations where some of our common code makes too many assumptions about running on MySQL.

Slutty namespaces

A couple of projects ran into mysterious issues where some of their namespaced controller classes would not load when run from rake.

The culprit was that their namespace module names were the same name as their app models. The lesson – never have a module with the same name as one of your other unrelated classes. (No, I’m not talking about inner classes, which can be fine).

A prime example of this is an Admin::* namespace coexisting with an Admin model. This will cause strange problems depending on what order the classes are loaded. One recommendation is to pluralize your namespaces. In this case, your namespace would be Admins::*.

Another suggestion would be to use an extended noun form for your namespace (Administration::*). Whatever convention you use, stick to it and avoid name collisions.

Comments
Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *