1. Skip to navigation
  2. Skip to content

Streamlined Support for Namespaced Controllers

Recently I’ve been spending a lot of time with Streamlined. The product is coming along quite well and recently a plugin version was released. One of the things that bugged me was lack of support for namespaced controllers. There’s a lot of controversary about namespaced controllers within the Rails community, but I’ve never had an issue with them and they help to provide good separation with larger projects. In the case of Streamlined, I’m using namespaced controllers for all of my administration pages. This results in a url like:

http://blog.railsconsulting.com/admin/categories

In addition to good separation namespaced controllers also insulate my own controllers from stepping on / or getting stepped on an acts_as_streamlined controller.

Anyway, enough justification. Correcting the namespaced issue is pretty simple. The problem is in the lib/streamlined_controller.rb file. The issues is how the streamlined code is arriving at the model name. Around line 404 in the file there is the following line:

@model_name ||= Inflector.singularize(self.class.name.chomp("Controller"))

What’s going on here is that in the original code it is taking the controller class name and getting rid of Controller from the end of it. In the case of a controller named UsersController this will result in a model name of User, which is correct. In the case of a namespaced controller named Admin::UserController the code results in a model named AdminUser, which obviously is not correct.

By changing it to read the following it will correct the problem:

@model_name ||= Inflector.classify(self.class.controller_name)

By referencing the internal method controller_name we will get back the correct controller name, sans the namespace, which in turn results in the correct model name.