0
votes

I am really confused about rails namespaces. I tried to create my own admin namespace so added namespace to routes, this works good. Then i added folder admin into controllers. Admin::Controller this is how my controllers in that folder looks.

but here comes the problem. How can i separate Helpers? rails automatically loads all helpers. I disabled that in config but now it wont load it manually like module Admin::ApplicationHelper.

How about next things what needs to be separated? Like i18N, sessions, flashes? Is there a tutorial for this problem? Im using Rails 4. Thanks for advices

2
If you really want to separate these namespaces and functionality completely I would suggest building an Engine. What you are asking for is exactly what Rails Engines are designed for. - engineersmnky

2 Answers

0
votes

You only namespace controllers, views, models, and helpers, not everything else you mentioned. If you disabled autoloading helpers you'll have to manually require each one that you need:

require 'admin/admin_helper'

class Admin::Controller < ActionController::Base
  ... code ...

Same goes for any other helper such as application_helper, etc. Everything else, sessions, flashes, i18n and so on are merely methods from ActionController::Base that all controller's inherit. There's no namespacing these.

Going back to the helpers question: you namespace them the same way you namespace the controllers:

# app/helpers/admin/admin_helper.rb

module Admin::AdminHelper
  ... code ...
end

And then just require it in your admin controllers if you need to. I'd keep autoloading helpers enabled in order to forego having to require them everywhere.

0
votes

As you've noticed rails defaults to including all helpers into all views. You can turn this off by adding

config.application_controller.include_all_helpers = false

This will result in only ApplicationHelper and the controller's helper being included. Adding

helper :foo

To a controller would result in FooHelper being included in addition to the defaults. If there are helpers that should be loaded for all of the admin controllers then add this to their base class. If you need anything more than this then consider using a rails engine (with the isolate_namespaces option turned on)