Module: Merb::Slices::ControllerMixin::MixinMethods::InstanceMethods

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

Instance Method Summary collapse

Instance Method Details

#sliceModule

Reference this controller’s slice module directly.

Returns:

  • (Module)

    A slice module.



122
# File 'lib/merb-slices/controller_mixin.rb', line 122

def slice; self.class.slice; end

#slice_url(*args) ⇒ String

Generate a url - takes the slice’s :path_prefix into account.

Examples:

slice_url(:controller => ‘foo’, :action => ‘bar’)

slice_url(:awesome, :format => ‘html’)

slice_url(:forum, :posts, :format => ‘xml’)

Parameters:

  • *args (Array[Symbol,Hash])

    There are several possibilities regarding arguments:

    • when passing a Hash only, the :default route of the current slice will be used

    • when a single Symbol is passed, it’s used as the route name, while the slice itself will be the current one

    • when two Symbols are passed, the first will be the slice name, the second will be the route name

    • a Hash with additional params can optionally be passed

Returns:

  • (String)

    A uri based on the requested slice.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/merb-slices/controller_mixin.rb', line 141

def slice_url(*args)
  opts = args.last.is_a?(Hash) ? args.pop : {}
  slice_name, route_name = if args[0].is_a?(Symbol) && args[1].is_a?(Symbol)
    [args.shift, args.shift] # other slice identifier, route name
  elsif args[0].is_a?(Symbol)
    [slice.identifier_sym, args.shift] # self, route name
  else
    [slice.identifier_sym, :default] # self, default route
  end
  
  routes = Merb::Slices.named_routes[slice_name]
  unless routes && route = routes[route_name]
    raise Merb::Router::GenerationError, "Named route not found: #{route_name}"
  end
  
  args.push(opts)
  route.generate(args, params)
end