Skip to content

Instantly share code, notes, and snippets.

@marxarelli
Created September 14, 2011 18:53
Show Gist options
  • Save marxarelli/1217427 to your computer and use it in GitHub Desktop.
Save marxarelli/1217427 to your computer and use it in GitHub Desktop.
Controller mixin for removing the first level of view/layout namespace from a controller's path.
require 'active_support/concern'
module Filaments
# Extends controllers to leave off the `filaments/` prefix from view paths.
#
# The Filaments controllers are meant to be subclassed by the host
# application when necessary to override default behavior and the default
# view-path resolution prefix would lead to an annoying reshuffling of
# related views every time this subclassing is done.
#
# Consider the following development.
# - Host application uses `Filaments::UsersController` to handle `users/*`
# routes.
# - Views are implemented in host app under `app/views/filaments/users/*`.
# - Developer implements functionality that requires subclassing
# `Filaments::UsersController`, creating `::UsersController`.
# - Views must now be moved from `app/views/filaments/users` to
# `app/views/users`.
# - All views must be refactored, changing `filaments/users/*`
# view or partial paths to just `users/*`.
#
# The latter two steps are annoying and would lead to superfluous merge
# conflicts. For these reasons, this override of `controller_path` is necessary.
module LocalViewResolution
extend ActiveSupport::Concern
included do
# This is a necessary evil, due to AbstractController::Layouts creating
# the _layout method when the controller class is defined (before this
# module can be include and before our controller_path implementation
# can take effect on layouts).
_write_layout_method if respond_to?(:_write_layout_method)
end
module ClassMethods #:nodoc:
# Returns the controller path, without a leading `module_name/` namespace.
#
def controller_path
if (names = name.split('::')).length > 1
module_name = names.first.underscore
(path = super) && path[module_name.length + 1, path.length]
else
super
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment