Last active
October 20, 2015 10:35
-
-
Save Bodacious/d0be4af281a3bd806780 to your computer and use it in GitHub Desktop.
Creating a "namespace" for Rails controllers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module NamespaceableController | |
| extend ActiveSupport::Concern | |
| included do | |
| helper_method :namespace | |
| end | |
| private | |
| # Returns this class's namespace as a String | |
| def namespace | |
| self.class.namespace | |
| end | |
| module ClassMethods | |
| # Call this in your controller's _Base_ class to set a constant namespace used | |
| # through each of the sub-classes. This sets the layout for the controller and it's | |
| # sub-classes, as well as providing a class and instance method named `namespace` | |
| # to access the current namespace name. If the current controller's namespace is | |
| # not set, will fallback on the superclass's namespace. This _might_ cause issues | |
| # if multiple layers of inheritance are used. | |
| # | |
| # name - A String with the name of the current namespace (default: nil) | |
| # | |
| # Examples | |
| # | |
| # class Admins::BaseClass < ApplicationClass | |
| # | |
| # namespace "admins" | |
| # | |
| # end | |
| # | |
| # Returns a String | |
| def namespace(name = nil) | |
| if name | |
| layout(name) | |
| @namespace = name | |
| else | |
| defined?(@namespace) ? @namespace : superclass.instance_variable_get("@namespace") | |
| end | |
| end | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@CodeMeister - Have a look at this...