Module: Lolita::Controllers::UrlHelpers

Included in:
ActionView::RoutingUrlFor
Defined in:
lib/lolita/controllers/url_helpers.rb

Overview

This module is included in all controllers and views. UrlHelper provide methods to generate lolita resource paths. As all resources goes to one controller (by default), than it is very difficult to generate url with :controller and :action. There are four methods for path:

  • lolita_resources_path - goes to index action

  • lolita_resource_path - goes to create, destroy or show, it depends what kind of method is used

  • new_lolita_resource_path - goes to new action

  • edit_lolita_resource_path - goes to edit action

All of those methods accept mapping as a first argument and optional options that will be passed to path gererator. It is possible to pass only options, than current mapping will be used.

Example

# lets say, that current mapping is for posts
edit_lolita_resource_path(:id=>4) #=> /lolita/posts/4/edit
edit_lolita_resource_path(comment_mapping,:id=>1,:parent_id=>2) #=> /lolita/comments/1/edit?parent_id=2

For custom actions there are method #lolita_resource_name that make it easier to generate custom method. All other methods use that for real resource path method generation.

Class Method Summary collapse

Class Method Details

.included(model_klass) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lolita/controllers/url_helpers.rb', line 21

def self.included(model_klass)
  model_klass.class_eval do

    # Overrides url_for when controller or view uses this helper.
    # It catches hash options and replaces with lolita named route
    # Without this method routes always looks like /lolita/rest/[method]
    # ====Example
    #     # in routes.rb
    #     lolita_for :posts
    #     # GET /lolita/posts
    #     # in view
    #     url_for #=> /lolita/posts
    #     url_for(:controller=>"/posts",:action=>:index) #=> /posts
    #     # GET /posts
    #     url_for #=> /posts
    def url_for_with_lolita options = {}
      if request && options.is_a?(Hash) && !options[:use_route] && self.respond_to?(:lolita_mapping) && self.lolita_mapping
        controller = options[:controller].to_s
        if Lolita.mappings[lolita_mapping.name].controllers.values.include?(controller)
          resource_type = {
            :index => :lolita_resources_path,
            :new => :new_lolita_resource_path,
            :edit => :edit_lolita_resource_path,
            :create => :lolita_resources_path
          }
          action = (options[:action] || params[:action]).to_sym
          options = self.send(resource_type[action] || :lolita_resource_path,options)
        end
      end

      url_for_without_lolita(options)
    end
    alias_method_chain :url_for, :lolita

  end
end