Module: MizugumoLinkHelper

Includes:
ActionView::Helpers::UrlHelper
Defined in:
lib/mizugumo_link_helper.rb

Instance Method Summary collapse

Instance Method Details



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
57
58
59
60
# File 'lib/mizugumo_link_helper.rb', line 27

def degradable_form_for_link(*args, &block)
  if block_given?
    return degradable_form_for_link(capture(&block), *args)
  else
    contents = args.shift
    if contents =~ /<img.+src=['"](\S+?)['"]/
      submit_element = image_submit_tag($1)
      title = "block was passed"
    else
      title = contents
      submit_element = button_tag(title, :type => 'submit')
    end
  end
  options       = args[0] || {}
  html_options  = args[1]
  action        = url_for(options)
  # debugger
  method = html_options[:method]

  # debugger

  cssclass      = [ 'mizugumo_graceful_form' ]
  cssclass      << html_options[:class] unless html_options[:class].blank?
  html_options = convert_options_to_data_attributes(options, html_options)
  html_options.merge!({:action => action, :method => :post, :title => title, :class => cssclass})
  html_options.delete('rel')
  html_options.delete('class')

  (:form, html_options) do
    hidden_field_tag("_method", method) +
    hidden_field_tag("authenticity_token", session[:_csrf_token]) +
    submit_element
  end
end

Redefined link_to to provide Mizugumo-style graceful degradation. If a :method option was provided, this replaces the link with a form and specifies a class so that MizugumoScript will convert it to the expected link on Javascript-enabled browsers.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mizugumo_link_helper.rb', line 11

def link_to(*args, &block)
  if block_given?
    options       = args[0] || {}
    html_options  = args[1]
  else
    options       = args[1] || {}
    html_options  = args[2]
  end

  if html_options and html_options[:method]
    degradable_form_for_link(*args, &block)
  else
    super(*args, &block)
  end
end