Module: Merb::Slices::ControllerMixin::ClassMethods

Defined in:
lib/merb-slices/controller_mixin.rb

Instance Method Summary collapse

Instance Method Details

#controller_for_slice(slice_module = nil, options = {}) ⇒ Object

Setup a controller to reference a slice and its template roots

This method is available to any class inheriting from Merb::AbstractController; it enabled correct location of templates, as well as access to the slice module.

Examples:

controller_for_slice # uses current module

controller_for_slice SliceMod # defaults to :view templates and no subdirectory

controller_for_slice :templates_for => :mailer, :path => ‘views’ # for Merb::Mailer

controller_for_slice SliceMod, :templates_for => :mailer, :path => ‘views’ # for Merb::Mailer

Parameters:

  • slice_module (#to_s) (defaults to: nil)

    The slice module to use; defaults to current module.

  • options (Hash) (defaults to: {})

    Optional parameters to set which component path is used (defaults to :view) and the :path option lets you specify a subdirectory of that component path. When :layout is set, then this is used instead of the config’s :layout setting.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/merb-slices/controller_mixin.rb', line 63

def controller_for_slice(slice_module = nil, options = {})
  options, slice_module = slice_module.merge(options), nil if slice_module.is_a?(Hash)
  slice_module ||= self.name.split('::').first
  options[:templates_for] = :view unless options.key?(:templates_for)
  if slice_mod = Merb::Slices[slice_module.to_s]
    # Include the instance methods
    unless self.kind_of?(Merb::Slices::ControllerMixin::MixinMethods)
      self.send(:extend, Merb::Slices::ControllerMixin::MixinMethods)
    end
    # Reference this controller's slice module
    self.class_inheritable_accessor :slice, :instance_writer => false
    self.slice = slice_mod
    # Setup template roots
    if options[:templates_for]
      self._template_root  = join_template_path(slice_mod.dir_for(options[:templates_for]), options[:path])
      self._template_roots = []
      # app-level app/views directory for shared and fallback views, layouts and partials
      self._template_roots << [join_template_path(Merb.dir_for(options[:templates_for]), options[:path]), :_template_location] if Merb.dir_for(options[:templates_for])
      # slice-level app/views for the standard supplied views
      self._template_roots << [self._template_root, :_slice_template_location] 
      # app-level slices/<slice>/app/views for specific overrides
      self._template_roots << [join_template_path(slice_mod.app_dir_for(options[:templates_for]), options[:path]), :_slice_template_location]
      # additional template roots for specific overrides (optional)
      self._template_roots += Array(options[:template_roots]) if options[:template_roots]
    end
    # Set the layout for this slice controller
    layout_for_slice(options[:layout])
  end
end