Module: PostEmAll::ViewHelpers

Defined in:
lib/post-em-all.rb

Instance Method Summary collapse

Instance Method Details



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
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/post-em-all.rb', line 66

def link_to(*args, &block)
  if block_given?
    options      = args.first || {}
    html_options = args.second
    link_to(capture(&block), options, html_options)
  else
    name         = args[0]
    options      = args[1] || {}
    html_options = args[2] || {}
    prevent_post = html_options.delete(:post)===false
    if !prevent_post && post?
      is_get = html_options.empty? || !html_options.keys.include?(:method) || html_options[:method] == :get
      html_options[:method] = :post if is_get
      html_options = convert_options_to_data_attributes(options, html_options)      
      url = url_for(options)
      if is_get && !url.match(/^\w+\:/)
        if url =~ /^https?\:\/\//
          #url
          y url
          host, path = url.match(/^(https?\:\/\/.*?\/)(.*)/).to_a[1..2]
          url = "#{host}get/#{path}"
        else
          # path
          url = "/get#{url}"
        end
      end
    else
      html_options = convert_options_to_data_attributes(options, html_options)      
      url = url_for(options)
    end

    href = html_options['href']
    tag_options = tag_options(html_options)

    href_attr = "href=\"#{html_escape(url)}\"" unless href
    "<a #{href_attr}#{tag_options}>#{html_escape(name || url)}</a>".html_safe
  end
end