Module: Useful::ErbHelpers::Links
- Includes:
- Common
- Defined in:
- lib/useful/erb_helpers/links.rb
Constant Summary collapse
- ABSOLUTE_LINK_PATH =
/\Ahttp[s]*:\/\//
Constants included from Common
Class Method Summary collapse
Instance Method Summary collapse
-
#image_tag(source, options = {}) ⇒ Object
helper to emulate ‘image_tag’ EX : image_tag ‘logo.jpg’ => <img src=“/images/logo.jpg” /> EX : image_tag ‘/better/logo.jpg’ => <img src=“/better/logo.jpg” />.
-
#javascript_include_tag(*args) ⇒ Object
helper to emulate ‘javascript_include_tag’ EX : javascript_include_tag ‘app’ => <script src=“/js/app.js” type=“text/javascript” /> EX : javascript_include_tag [‘app’, ‘jquery’] => <script src=“/js/app.js” type=“text/javascript” /> => <script src=“/js/jquery.js” type=“text/javascript” />.
-
#javascript_tag(options = {}) ⇒ Object
helper to emulate ‘javascript_tag’.
-
#link_to(*args) ⇒ Object
helper to emulate action view’s ‘link_to’ EX : link_to “google.com” => <a href=“google.com”>google.com</a> EX : link_to “google”, “google.com” => <a href=“google.com”>google</a> EX : link_to “google”, “google.com”, :class => “awesome” => <a href=“google.com” class=“awesome”>google</a> EX : link_to “google.com”, :popup => true => <a href=“google.com” onclick=“javascript: window.open(this.href); return false;”>google.com</a> EX : link_to “google.com”, :popup => [‘new_window_name’, ‘height=300,width=600’] => <a href=“google.com” onclick=“javascript: window.open(this.href,‘new_window_name’,‘height=300,width=600’); return false;”>google.com</a> EX : link_to “google.com”, :confirm => “Are you sure?” => <a href=“google.com” onclick=“javascript: return confirm(‘Are you sure?’);”>google.com</a> EX : link_to “google.com”, :confirm => “Are you sure?”, :popup => true => <a href=“google.com” onclick=“javascript: if (confirm(‘Are you sure?’)) { window.open(this.href); }; return false;”>google.com</a>.
- #link_to_function(content, function, opts = {}) ⇒ Object
-
#mail_to(*args) ⇒ Object
helper for generating a mailto: EX: mail_to “[email protected]” # => <a href=“[email protected]”>[email protected]</a> EX: mail_to “[email protected]”, nil, :replace_at => “at”, :replace_dot => “dot”, :class => “email” # => <a href=“[email protected]” class=“email”>me_at_domain_dot_com</a> EX: mail_to “[email protected]”, nil, :replace_at => “ [at]”, :replace_dot => “ [dot] ”, :disabled => true # => me [at] domain [dot] com EX: mail_to “[email protected]”, “My email”, :cc => “[email protected]”, :subject => “This is an example email” # => <a href=“[email protected][email protected]&subject=This%20is%20an%20example%20email”>My email</a>.
-
#stylesheet_link_tag(*args) ⇒ Object
helper to emulate activesupport’s ‘stylesheet_link_tag’ EX : stylesheet_link_tag ‘default’ => <link rel=“stylesheet” type=“text/css” media=“all” href=“/stylesheets/default.css”> EX : stylesheet_link_tag ‘default’, :timestamp => true => <link rel=“stylesheet” type=“text/css” media=“screen” href=“/stylesheets/default.css?12345678”> EX : stylesheet_link_tag ‘default’, :media => ‘screen’ => <link rel=“stylesheet” type=“text/css” media=“screen” href=“/stylesheets/default.css”> EX : stylesheet_link_tag ‘default’, ‘test’, :media => ‘screen’ => <link rel=“stylesheet” type=“text/css” media=“screen” href=“/stylesheets/default.css”> => <link rel=“stylesheet” type=“text/css” media=“screen” href=“/stylesheets/test.css”> EX : stylesheet_link_tag [‘default’, ‘test’], :media => ‘screen’ => <link rel=“stylesheet” type=“text/css” media=“screen” href=“/stylesheets/default.css”> => <link rel=“stylesheet” type=“text/css” media=“screen” href=“/stylesheets/test.css”> EX : stylesheet_link_tag ‘default’, ‘/other_sheets/other’, ‘/other_sheets/another.css’ => <link rel=“stylesheet” type=“text/css” media=“screen” href=“/stylesheets/default.css”> => <link rel=“stylesheet” type=“text/css” media=“screen” href=“/other_sheets/other.css”> => <link rel=“stylesheet” type=“text/css” media=“screen” href=“/other_sheets/another.css”>.
Methods included from Common
#erb_helper_clear_output_buffer
Class Method Details
.included(receiver) ⇒ Object
131 132 133 |
# File 'lib/useful/erb_helpers/links.rb', line 131 def self.included(receiver) receiver.send :include, Useful::ErbHelpers::Tags end |
Instance Method Details
#image_tag(source, options = {}) ⇒ Object
helper to emulate ‘image_tag’ EX : image_tag ‘logo.jpg’
=> <img src="/images/logo.jpg" />
EX : image_tag ‘/better/logo.jpg’
=> <img src="/better/logo.jpg" />
69 70 71 72 73 74 75 76 |
# File 'lib/useful/erb_helpers/links.rb', line 69 def image_tag(source,={}) [:src] = if source =~ ABSOLUTE_LINK_PATH source else ['/'].include?(source[0..0]) ? source : "/images/#{source}" end tag(:img, ) end |
#javascript_include_tag(*args) ⇒ Object
helper to emulate ‘javascript_include_tag’ EX : javascript_include_tag ‘app’
=> <script src="/js/app.js" type="text/javascript" />
EX : javascript_include_tag [‘app’, ‘jquery’]
=> <script src="/js/app.js" type="text/javascript" />
=> <script src="/js/jquery.js" type="text/javascript" />
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/useful/erb_helpers/links.rb', line 120 def javascript_include_tag(*args) srcs, = (args) [:type] ||= "text/javascript" = .delete(:timestamp) Array(srcs).collect do |src| #TODO: write sinatra helper to auto set env with Sinatra::Application.environment.to_s [:src] = build_src_href(src, :default_path => "javascripts", :extension => ".js", :timestamp => ) tag(:script, ) { '' } end.join("\n") end |
#javascript_tag(options = {}) ⇒ Object
helper to emulate ‘javascript_tag’
109 110 111 112 |
# File 'lib/useful/erb_helpers/links.rb', line 109 def javascript_tag(={}) [:type] ||= "text/javascript" tag(:script, ) { yield } end |
#link_to(*args) ⇒ Object
helper to emulate action view’s ‘link_to’ EX : link_to “google.com”
> <a href=“google.com”>google.com</a>
EX : link_to “google”, “google.com”
> <a href=“google.com”>google</a>
EX : link_to “google”, “google.com”, :class => “awesome”
> <a href=“google.com” class=“awesome”>google</a>
EX : link_to “google.com”, :popup => true
> <a href=“google.com” onclick=“javascript: window.open(this.href); return false;”>google.com</a>
EX : link_to “google.com”, :popup => [‘new_window_name’, ‘height=300,width=600’]
> <a href=“google.com” onclick=“javascript: window.open(this.href,‘new_window_name’,‘height=300,width=600’); return false;”>google.com</a>
EX : link_to “google.com”, :confirm => “Are you sure?”
> <a href=“google.com” onclick=“javascript: return confirm(‘Are you sure?’);”>google.com</a>
EX : link_to “google.com”, :confirm => “Are you sure?”, :popup => true
> <a href=“google.com” onclick=“javascript: if (confirm(‘Are you sure?’)) { window.open(this.href); }; return false;”>google.com</a>
30 31 32 33 34 35 |
# File 'lib/useful/erb_helpers/links.rb', line 30 def link_to(*args) content, href, = link_content_href_opts(args) .update :href => href () tag(:a, ) { content } end |
#link_to_function(content, function, opts = {}) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/useful/erb_helpers/links.rb', line 57 def link_to_function(content, function, opts={}) opts ||= {} opts[:href] ||= 'javascript: void(0);' opts[:onclick] = "javascript: #{function}; return false;" link_to content, opts[:href], opts end |
#mail_to(*args) ⇒ Object
helper for generating a mailto: EX: mail_to “[email protected]”
# => <a href="mailto:[email protected]">[email protected]</a>
EX: mail_to “[email protected]”, nil, :replace_at => “at”, :replace_dot => “dot”, :class => “email”
# => <a href="mailto:[email protected]" class="email">me_at_domain_dot_com</a>
EX: mail_to “[email protected]”, nil, :replace_at => “ [at]”, :replace_dot => “ [dot] ”, :disabled => true
# => me [at] domain [dot] com
EX: mail_to “[email protected]”, “My email”, :cc => “[email protected]”, :subject => “This is an example email”
# => <a href="mailto:[email protected][email protected]&subject=This%20is%20an%20example%20email">My email</a>
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/useful/erb_helpers/links.rb', line 46 def mail_to(*args) content, email, = link_content_href_opts(args) email_args = [:cc, :bcc, :subject, :body].inject({}) do |args, arg| args[arg] = .delete(arg) if .has_key?(arg) args end content.gsub!(/@/, .delete(:replace_at)) if .has_key?(:replace_at) content.gsub!(/\./, .delete(:replace_dot)) if .has_key?(:replace_dot) .delete(:disabled) ? content : link_to(content, "mailto: #{email}#{email_args.to_http_query_str}", ) end |
#stylesheet_link_tag(*args) ⇒ Object
helper to emulate activesupport’s ‘stylesheet_link_tag’ EX : stylesheet_link_tag ‘default’
=> <link rel="stylesheet" type="text/css" media="all" href="/stylesheets/default.css">
EX : stylesheet_link_tag ‘default’, :timestamp => true
=> <link rel="stylesheet" type="text/css" media="screen" href="/stylesheets/default.css?12345678">
EX : stylesheet_link_tag ‘default’, :media => ‘screen’
=> <link rel="stylesheet" type="text/css" media="screen" href="/stylesheets/default.css">
EX : stylesheet_link_tag ‘default’, ‘test’, :media => ‘screen’
=> <link rel="stylesheet" type="text/css" media="screen" href="/stylesheets/default.css">
=> <link rel="stylesheet" type="text/css" media="screen" href="/stylesheets/test.css">
EX : stylesheet_link_tag [‘default’, ‘test’], :media => ‘screen’
=> <link rel="stylesheet" type="text/css" media="screen" href="/stylesheets/default.css">
=> <link rel="stylesheet" type="text/css" media="screen" href="/stylesheets/test.css">
EX : stylesheet_link_tag ‘default’, ‘/other_sheets/other’, ‘/other_sheets/another.css’
=> <link rel="stylesheet" type="text/css" media="screen" href="/stylesheets/default.css">
=> <link rel="stylesheet" type="text/css" media="screen" href="/other_sheets/other.css">
=> <link rel="stylesheet" type="text/css" media="screen" href="/other_sheets/another.css">
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/useful/erb_helpers/links.rb', line 95 def stylesheet_link_tag(*args) srcs, = (args) [:rel] ||= "stylesheet" [:type] ||= "text/css" [:media] ||= "all" = .delete(:timestamp) Array(srcs).collect do |src| #TODO: write sinatra helper to auto set env with Sinatra::Application.environment.to_s [:href] = build_src_href(src, :default_path => "stylesheets", :extension => ".css", :timestamp => ) tag(:link, ) end.join("\n") end |