Module Mack::ViewHelpers::LinkHelpers
In: lib/mack/view_helpers/link_helpers.rb

Methods

Included Modules

Mack::Assets

Public Instance methods

Used in views to create href links. It takes link_text, url, and a Hash that gets added to the href as options.

Examples:

   a("http://www.mackframework.com") # => <a href="http://www.mackframework.com">http://www.mackframework.com</a>
   a("Mack", :href => "http://www.mackframework.com") # => <a href="http://www.mackframework.com">Mack</a>
   a("Mack", :href => "http://www.mackframework.com", :target => "_blank") # => <a href="http://www.mackframework.com" target="_blank">Mack</a>
   a("Mack", :href => "http://www.mackframework.com", :target => "_blank", :rel => :nofollow) # => <a href="http://www.mackframework.com" target="_blank" rel="nofollow">Mack</a>

If you pass in :method as an option it will be a JavaScript form that will post to the specified link with the methd specified.

   a("Mack", :href => "http://www.mackframework.com", :method => :delete)

If you use the :method option you can also pass in a :confirm option. The :confirm option will generate a javascript confirmation window. If ‘OK’ is selected the the form will submit. If ‘cancel’ is selected, then nothing will happen. This is extremely useful for ‘delete’ type of links.

   a("Mack", :href => "http://www.mackframework.com", :method => :delete, :confirm => "Are you sure?")

[Source]

     # File lib/mack/view_helpers/link_helpers.rb, line 85
 85:       def a(link_text, options = {})
 86:         options = {:href => link_text}.merge(options)
 87:         if options[:method]
 88:           meth = nil
 89:           confirm = nil
 90:         
 91:           meth = %{var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var s = document.createElement('input'); s.setAttribute('type', 'hidden'); s.setAttribute('name', '_method'); s.setAttribute('value', '#{options[:method]}'); f.appendChild(s); var s2 = document.createElement('input'); s2.setAttribute('type', 'hidden'); s2.setAttribute('name', '__authenticity_token'); s2.setAttribute('value', '#{Mack::Utils::AuthenticityTokenDispenser.instance.dispense_token(session.id)}'); f.appendChild(s2); f.submit()}
 92:           options.delete(:method)
 93:         
 94:           if options[:confirm]
 95:             confirm = %{if (confirm('#{options[:confirm]}'))}
 96:             options.delete(:confirm)
 97:           end
 98:         
 99:           options[:onclick] = (confirm ? (confirm + " { ") : "") << meth << (confirm ? (" } ") : "") << ";return false;"
100:         end
101:         content_tag(:a, options, link_text)
102:       end

[Source]

     # File lib/mack/view_helpers/link_helpers.rb, line 185
185:       def assets_bundle(names)
186:         names = [names].flatten
187:         names.collect { |s| s.to_s }
188:         arr = []
189:         names.each do |name|
190:           arr << javascript(name) if assets_mgr.has_group?(name, :javascripts)
191:           arr << stylesheet(name) if assets_mgr.has_group?(name, :stylesheets)
192:         end
193:         return arr.join("\n")
194:       end

Generate Javascript tag (<script src="/javascripts/foo.js?1241225" type="text/javascript" />)

If distributed_site_domain is specified, it will be used.

[Source]

     # File lib/mack/view_helpers/link_helpers.rb, line 145
145:       def javascript(files, options = {})
146:         files = [files].flatten
147:         files = resolve_bundle('javascripts', files)                
148:         
149:         link = ""
150:         files.each do |name|
151:           file_name = !name.to_s.end_with?(".js") ? "#{name}.js" : "#{name}"
152:           resource = "/javascripts/#{file_name}"
153:           link += "<script src=\"#{get_resource_root(resource)}#{resource}?#{configatron.mack.assets.stamp}\" type=\"text/javascript\"></script>\n"
154:         end
155:         return link
156:       end

Wraps an image tag with a link tag.

Examples:

  <%= link_image_to("/images/foo.jpg", "#" %> # => <a href="#"><img src="/images/foo.jpg"></a>

[Source]

     # File lib/mack/view_helpers/link_helpers.rb, line 108
108:       def link_image_to(image_url, url, image_options = {}, html_options = {})
109:         link_to(img(image_url, image_options), url, html_options)
110:       end

This is just an alias to the a method

Examples:

  <%= link_to("http://www.mackframework.com") %> # => <a href="http://www.mackframework.com">http://www.mackframework.com</a>
  <%= link_to("Mack", "http://www.mackframework.com") %> # => <a href="http://www.mackframework.com">Mack</a>
  <%= link_to("Mack", "http://www.mackframework.com", :target => "_blank") %> # => <a href="http://www.mackframework.com" target="_blank">Mack</a>
  <%= link_to("Mack", "http://www.mackframework.com", :target => "_blank", :rel => :nofollow) %> # => <a href="http://www.mackframework.com" target="_blank" rel="nofollow">Mack</a>

If you pass in :method as an option it will be a JavaScript form that will post to the specified link with the methd specified.

  <%= link_to("Mack", "http://www.mackframework.com", :method => :delete) %>

If you use the :method option you can also pass in a :confirm option. The :confirm option will generate a javascript confirmation window. If ‘OK’ is selected the the form will submit. If ‘cancel’ is selected, then nothing will happen. This is extremely useful for ‘delete’ type of links.

  <%= link_to("Mack", "http://www.mackframework.com", :method => :delete, :confirm => "Are you sure?") %>

[Source]

    # File lib/mack/view_helpers/link_helpers.rb, line 46
46:       def link_to(link_text, url = link_text, html_options = {})
47:         options = {:href => url}.merge(html_options)
48:         a(link_text, options)
49:       end

Only creates a link if the expression is true, otherwise, it passes back the text.

[Source]

    # File lib/mack/view_helpers/link_helpers.rb, line 58
58:       def link_to_if(expr, text, link = text, options = {})
59:         if expr
60:           return link_to(text, link, options)
61:         end
62:         return text
63:       end

Only creates a link unless the expression is true, otherwise, it passes back the text.

[Source]

    # File lib/mack/view_helpers/link_helpers.rb, line 66
66:       def link_to_unless(expr, text, link = text, options = {})
67:         link_to_if(!expr, text, link, options)
68:       end

If the current page matches the link requested, then it will only return the text. If the current page does not match the link requested then link_to is call.

[Source]

    # File lib/mack/view_helpers/link_helpers.rb, line 53
53:       def link_unless_current(text, link = text, options = {})
54:         link_to_unless((link == request.fullpath), text, link, options)
55:       end

Builds a mailto href. By default it will generate JavaScript to help prevent phishing. To turn this off pass in the option :format => :plain

  mail_to("Saul Frami", "frami.saul@klocko.ca") # =>
  <script>document.write(String.fromCharCode(60,97,32,104,114,101,
  102,61,34,109,97,105,108,116,111,58,102,114,97,109,105,46,115,97,117,
  108,64,107,108,111,99,107,111,46,99,97,34,62,83,97,117,108,32,70,114,97,109,105,60,47,97,62));</script>

[Source]

     # File lib/mack/view_helpers/link_helpers.rb, line 120
120:       def mail_to(text, email_address = nil, options = {})
121:         email_address = text if email_address.blank?
122:         options = {:format => :js}.merge(options)
123:         format = options[:format]
124:         options - [:format]
125:         link = link_to(text, "mailto:#{email_address}", options)
126:         if format == :js
127:           y = ''
128:           link.size.times {y << 'a'}
129:           js_link = "<script>"
130:           c_code = []
131:           link.each_byte {|c| c_code << c}
132:           js_link << "document.write(String.fromCharCode(#{c_code.join(",")}));"
133:           js_link << "</script>"
134:           return js_link
135:         else
136:           return link
137:         end
138:       end

Generates a javascript popup window. It will create the javascript needed for the window, as well as the href to call it.

Example:

  popup('click here', 'http://www.example.com', {:toolbar => :yes, :name => :example_window}, {:alt => 'hello'}) # =>
  <script>
    function popup_r8b3edqgpbhm3zkthlgp(u) {
      window.open(u, 'example_window', "height=400,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=no,toolbar=yes,width=500");
    }
  </script>
  <a alt="hello" href="javascript:popup_r8b3edqgpbhm3zkthlgp('http://www.example.com')">click here</a>

[Source]

    # File lib/mack/view_helpers/link_helpers.rb, line 17
17:       def popup(link_text, url = link_text, popup_options = {}, html_options = {})
18:         m = String.randomize(20).downcase
19:         popup_options = {:menubar => :no, :width => 500, :height => 400, :toolbar => :no, :scrollbars => :yes, :resizable => :yes, :titlebar => :no, :status => :no, :location => :no, :name => m}.merge(popup_options)
20:         window_name = popup_options[:name]
21:         popup_options.delete(:name)
22:         %{
23: <script>
24:   function popup_#{m}(u) {
25:     window.open(u, '#{window_name}', "#{popup_options.join("%s=%s", ",")}");
26:   }
27: </script>
28: #{link_to(link_text, "javascript:popup_#{m}('#{url}')", html_options)}
29:         }.strip
30:       end

Generate Stylesheet tag If distributed_site_domain is specified, then it will use it as the host of the css file example: stylesheet("foo") => <link href="/stylesheets/scaffold.css" media="screen" rel="stylesheet" type="text/css" />

distributed_site_domain is set to ‘localhost:3001’ then, stylesheet("foo") will generate <link href="localhost:3001/stylesheets/scaffold.css" media="screen" rel="stylesheet" type="text/css" />

Supported options are: :media, :rel, and :type

[Source]

     # File lib/mack/view_helpers/link_helpers.rb, line 170
170:       def stylesheet(files, options = {})
171:         files = [files].flatten
172:         files = resolve_bundle('stylesheets', files)
173:         options = {:media => 'screen', :rel => 'stylesheet', :type => 'text/css'}.merge(options)
174:         
175:         link = ""
176:         files.each do |name|
177:           file_name = !name.to_s.end_with?(".css") ? "#{name}.css" : "#{name}"
178:           resource = "/stylesheets/#{file_name}"
179:           link += "<link href=\"#{get_resource_root(resource)}#{resource}?#{configatron.mack.assets.stamp}\" media=\"#{options[:media]}\" rel=\"#{options[:rel]}\" type=\"#{options[:type]}\" />\n"
180:         end
181:         
182:         return link
183:       end

[Validate]