Skip to content

Instantly share code, notes, and snippets.

@Bodacious
Last active October 20, 2015 10:35
Show Gist options
  • Select an option

  • Save Bodacious/d0be4af281a3bd806780 to your computer and use it in GitHub Desktop.

Select an option

Save Bodacious/d0be4af281a3bd806780 to your computer and use it in GitHub Desktop.
Creating a "namespace" for Rails controllers
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
@Bodacious

Copy link
Copy Markdown
Author

@CodeMeister - Have a look at this...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment