module Rad module ViewRoutingHelper inherit AbstractRoutingHelper # # links # # 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 # 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 def link_to *args, &block # content content = if block capture(&block) else args.shift end # url, html_options if args.first.is_a? String args.size.must_be.in 1..2 url, html_options = args html_options ||= {} else if url = special_url(args.first) args.size.must_be <= 2 html_options = args[1] || {} else html_options = if args[-1].is_a?(Hash) and args[-1].is_a?(Hash) args.pop else {} end args << {} unless args[-1].is_a?(Hash) url = url_for(*args) end end # add javascript add_js_link_options! url, html_options tag :a, content, html_options.merge(href: url) end protected # TODO3 refactor to use data-remote, data-method, data-confirm 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 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 end end