Module: Useful::ErbHelpers::Links

Includes:
Common
Defined in:
lib/useful/erb_helpers/links.rb

Constant Summary collapse

/\Ahttp[s]*:\/\//

Constants included from Common

Common::OPTIONS

Class Method Summary collapse

Instance Method Summary collapse

Instance Method Details

#image_tag(source, options = {}) ⇒ Object

helper to emulate 'image_tag' EX : image_tag 'logo.jpg' => EX : image_tag '/better/logo.jpg' =>



69
70
71
72
73
74
75
76
# File 'lib/useful/erb_helpers/links.rb', line 69

def image_tag(source,options={})
  options[:src] = if source =~ ABSOLUTE_LINK_PATH
    source
  else
    ['/'].include?(source[0..0]) ? source : "/images/#{source}"
  end
  tag(:img, options)
end

#javascript_include_tag(*args) ⇒ Object

helper to emulate 'javascript_include_tag' EX : javascript_include_tag 'app' =>

helper to emulate 'javascript_tag'



109
110
111
112
# File 'lib/useful/erb_helpers/links.rb', line 109

def javascript_tag(options={})
  options[:type] ||= "text/javascript"
  tag(:script, options) { yield }
end

helper to emulate action view's 'link_to' EX : link_to "http://google.com" => http://google.com EX : link_to "google", "http://google.com" => google EX : link_to "google", "http://google.com", :class => "awesome" => google EX : link_to "http://google.com", :popup => true => http://google.com EX : link_to "http://google.com", :popup => ['new_window_name', 'height=300,width=600'] => http://google.com EX : link_to "http://google.com", :confirm => "Are you sure?" => http://google.com EX : link_to "http://google.com", :confirm => "Are you sure?", :popup => true => http://google.com



30
31
32
33
34
35
# File 'lib/useful/erb_helpers/links.rb', line 30

def link_to(*args)
  content, href, options = link_content_href_opts(args)
  options.update :href => href
  erb_helper_convert_options_to_javascript!(options)
  tag(:a, options) { content }
end


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]"

=> [email protected]

EX: mail_to "[email protected]", nil, :replace_at => "at", :replace_dot => "dot", :class => "email"

=>

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"

=> My email



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, options = link_content_href_opts(args)
  email_args = [:cc, :bcc, :subject, :body].inject({}) do |args, arg|
    args[arg] = options.delete(arg) if options.has_key?(arg)
    args
  end
  content.gsub!(/@/, options.delete(:replace_at)) if options.has_key?(:replace_at)
  content.gsub!(/\./, options.delete(:replace_dot)) if options.has_key?(:replace_dot)
  options.delete(:disabled) ? content : link_to(content, "mailto: #{email}#{email_args.to_http_query_str}", options)
end

helper to emulate activesupport's 'stylesheet_link_tag' EX : stylesheet_link_tag 'default' => EX : stylesheet_link_tag 'default', :timestamp => true => EX : stylesheet_link_tag 'default', :media => 'screen' => EX : stylesheet_link_tag 'default', 'test', :media => 'screen' => => EX : stylesheet_link_tag ['default', 'test'], :media => 'screen' => => EX : stylesheet_link_tag 'default', '/other_sheets/other', '/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, options = handle_srcs_options(args)
  options[:rel] ||= "stylesheet"
  options[:type] ||= "text/css"
  options[:media] ||=  "all"
  timestamp = options.delete(:timestamp)
  Array(srcs).collect do |src|
    #TODO: write sinatra helper to auto set env with Sinatra::Application.environment.to_s
    options[:href] = build_src_href(src, :default_path => "stylesheets", :extension => ".css", :timestamp => timestamp)
    tag(:link, options)
  end.join("\n")
end