35

using a method :layout_for_namespace I set my app's layout depending on whether I am in frontend or backend, as the backend is using an namespace "admin".

I could not find a pretty way to find out which namespace I am, the only way I found is by parsing the string from params[:controller]. Of course that's easy, seems to be fail-safe and working good. But I am just wondering if there's a better, prepared, way to do this. Does anyone know?

Currently I am just using the following method:

def is_backend_namespace?
  params[:controller].index("admin/") == 0
end

Thanks in advance

Arne

arnekolja
  • 1,687
  • 5
  • 22
  • 29

9 Answers9

45

You can use:

self.class.parent == Admin
William Wong Garay
  • 1,921
  • 18
  • 14
  • 1
    this answer should be first one! – mmike Apr 27 '16 at 09:03
  • 9
    As many have pointed out this doesn't work for classes that have multiple levels of namespacing. However if you are just checking to see if you are in such a namespace (like the OP kind of was getting at) then you can use `controller.class.parents.include?(Admin)` and get at the similar thing without having to resort to string parsing – trcarden Jul 30 '16 at 17:07
  • @trcarden I get this error with that solution: `NameError Exception: undefined local variable or method 'controller'`. I'm on Rails 5.1. `self.class.parents` works and is equivalent. – stwr667 Jun 15 '20 at 06:00
27

Outside the controller (e.g. in the views), use controller.class.name. You can turn this into a helper method like this:

module ApplicationHelper
  def admin?
    controller.class.name.split("::").first=="Admin"
  end
end
Ryenski
  • 9,582
  • 3
  • 43
  • 47
20

In both the controller and the views, you can parse controller_path, eg.:

namespace = controller_path.split('/').first
KenB
  • 6,587
  • 2
  • 35
  • 31
  • this gives `'foo'` back if I say `'foo'.split('/').first` . A solution would be best if namespace is `nil` when checking a non-namespaced controller name – Cristian Oct 15 '12 at 13:30
11

Not much more elegant, but it uses the class instead of the params hash. I am not aware of a "prepared" way to do this without some parsing.

self.class.to_s.split("::").first=="Admin"
johnmcaliley
  • 11,015
  • 2
  • 42
  • 47
  • Hi. That's not what I meant, sorry. The controller's name is something else, "admin" is the namespace. It's Admin::MyController, so params[:controller] gives "admin/my_controller" or something, thats where I check if it is the admin namespace. Using controller_name I would have to do just the same, but I'd like to know a way to not _parse_ it but get back the namespace only. – arnekolja Nov 20 '10 at 12:19
  • My fault, I didn't read the question closely. You clearly state namespace and not controller name. Answer updated with an alternative. – johnmcaliley Nov 20 '10 at 18:49
  • 2
    More simply `controller.class.name.start_with?('Admin')`. – Patrick Berkeley Oct 02 '13 at 01:12
10

Setting the namespace in application controller:

path = self.controller_path.split('/')
@namespace = path.second ? path.first : nil
Danny
  • 415
  • 4
  • 6
10

None of these solutions consider a constant with multiple parent modules. For instance:

A::B::C

As of Rails 3.2.x you can simply:

"A::B::C".deconstantize #=> "A::B"

As of Rails 3.1.x you can:

constant_name = "A::B::C"
constant_name.gsub( "::#{constant_name.demodulize}", '' )

This is because #demodulize is the opposite of #deconstantize:

"A::B::C".demodulize #=> "C"

If you really need to do this manually, try this:

constant_name = "A::B::C"
constant_name.split( '::' )[0,constant_name.split( '::' ).length-1]
Jason Harrelson
  • 2,683
  • 1
  • 16
  • 10
7

In Rails 6, the controller class does not seem to have a namespace method on it.

The solution that seemed cleanest and worked for me in a view was: controller.class.module_parent

Specifically, if your namespace is Admin:: and you wanted 'admin', you'd do: controller.class.module_parent.to_s.downcase

staxim
  • 1,328
  • 14
  • 15
  • 1
    Seems like module_parent is not available yet in Rails 6.0.3.2. So, I use `controller.class.parent` – wrystr Aug 04 '20 at 06:32
2

Rails 6

Accessing the namespace in the view?

do not use: controller.namespace.parent == Admin

the parent method will be removed in Rails 6.1

DEPRECATION WARNING: `Module#parent` has been renamed to `module_parent`. `parent` is deprecated and will be removed in Rails 6.1.

use module_parent instead:

controller.namespace.module_parent == Admin

Sami Birnbaum
  • 773
  • 8
  • 20
0

In Rails 6 you can do:

controller.class.module_parent

to get the parent module. Haven't tried it with multiple nestings. But for one you can convert to a symbol like this:

controller.class.module_parent.to_s.underscore.to_sym
Randomtheories
  • 1,220
  • 18
  • 22