Module: Crystal::ViewUrlHelper

Defined in:
lib/crystal/html/view_helpers/view_url_helper.rb

Instance Method Summary collapse

Instance Method Details

TODO3 refactor to use data-remote, data-method, data-confirm



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
61
62
63
64
# File 'lib/crystal/html/view_helpers/view_url_helper.rb', line 36

def add_js_link_options! url, html_options
  confirm, remote, method = html_options.delete("confirm"), html_options.delete('remote'), 
    html_options.delete("method")
    
  remote ||= config.remote_link_formats!.include? url.marks.format.to_s

  onclick = if remote
    method ||= :get
    %{$(this).link_to({method: '#{method}', ajax: true}); return false;}
  elsif method
    %{$(this).link_to({method: '#{method}'}); return false;}
  else
    nil
  end

  # if onclick and ActionController::Base.defer_static_scripts?
  #   html_options['class'] ||= ""
  # end

  if confirm 
    onclick = if onclick
      "if(confirm(#{confirm.js_escape})){#{onclick}}; return false;"
    else
      "return confirm(#{confirm.js_escape});"
    end
  end

  html_options["onclick"] = onclick if onclick        
end

links



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/crystal/html/view_helpers/view_url_helper.rb', line 6

def link_to *args, &block
  # parse args
  content = if block
    capture(&block)
  else
    args.shift
  end
  
  special = nil
  html_options = if args.last.is_a?(Hash)
    if args[-2].is_a?(Hash)
      args.pop.stringify_keys!
    else
      {}
    end          
  else        
    args << {}
    {}
  end            
  
  # build url
  url = special_url(*args) || url_for(*args)
  
  # add javascript
  add_js_link_options! url, html_options
  
  tag :a, content, html_options.merge('href' => url)
end