Module: BivouacHelpers::HtmlView

Defined in:
lib/bivouac/helpers/view/goh/html.rb

Constant Summary collapse

@@javascript_default_sources =
%w{prototype.js scriptaculous.js}

Instance Method Summary collapse

Instance Method Details

#image_path(source) ⇒ Object

Computes the path to an image asset in the public images directory. Full paths from the document root will be passed through. Used internally by image_tag to build the image path.

image_path("edit")                                         # => /public/images/edit
image_path("edit.png")                                     # => /public/images/edit.png
image_path("icons/edit.png")                               # => /public/images/icons/edit.png
image_path("/icons/edit.png")                              # => /icons/edit.png
image_path("http://www.railsapplication.com/img/edit.png") # => http://www.railsapplication.com/img/edit.png


98
99
100
101
102
103
104
# File 'lib/bivouac/helpers/view/goh/html.rb', line 98

def image_path( source )
  if /^http:\/\//.match( source ) || /^\//.match( source )
    return source
  else
    return "/public/images/#{source}"
  end
end

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

Returns an html image tag for the source. The source must be a file that exists in your public images directory (public/images). You can add html attributes using the options. If no alt text is given, the file name part of the source is used (capitalized and without the extension)

image_tag("icon.png")  # =>
  <img src="/public/images/icon.png" alt="Icon" />

image_tag("icon.png", :alt => "Edit Entry")  # =>
  <img src="/public/images/icon.png" alt="Edit Entry" />


82
83
84
85
86
87
# File 'lib/bivouac/helpers/view/goh/html.rb', line 82

def image_tag( source, options = {} )
  options[:src] = "/public/images/#{source}"
  options[:alt] ||= File.basename(options[:src], '.*').split('.').first.capitalize
  
  img( options )
end

#javascript_include_tag(*sources) ⇒ Object

Returns an html script tag for each of the sources provided. You can pass in the filename (.js extension is optional) of javascript files that exist in your public/javascripts directory for inclusion into the current page. To include the Prototype and Scriptaculous javascript libraries in your application, pass :defaults as the source.

javascript_include_tag "xmlhr" # =>
  <script type="text/javascript" src="/public/javascripts/xmlhr.js"></script>

javascript_include_tag "common.javascript", "elsewhere/cools" # =>
  <script type="text/javascript" src="/public/javascripts/common.javascript"></script>
  <script type="text/javascript" src="/public/javascripts/elsewhere/cools.js"></script>


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bivouac/helpers/view/goh/html.rb', line 43

def javascript_include_tag( *sources )
  options = sources.last.is_a?(Hash) ? sources.pop : { }
  options[:type] = "text/javascript"
  ie_only = options.delete( :ieOnly ) || []
  
  if sources.include?(:defaults) 
    sources = sources[0..(sources.index(:defaults))] + 
      @@javascript_default_sources.dup + 
      sources[(sources.index(:defaults) + 1)..sources.length]
  
    sources.delete(:defaults) 
  end
  sources.collect do |file|
    file = file.to_s
    file << ".js" if File.extname(file).blank?
    options[:src] = "/public/javascripts/#{file}"
    script( options ) {}
  end
  ie_only.collect do |file|
    file = file.to_s
    file << ".js" if File.extname(file).blank?
    options[:src] = "/public/javascripts/#{file}"
    text "<!--[if IE]>"
    script( options ) {}
    text "<![endif]-->"
  end
end

Creates a link tag of the given name using address has the href for the link

The options will accept a hash of html attributes for the link tag. It also accepts 2 modifiers that specialize the link behavior.

  • :confirm => 'question?': This will add a JavaScript confirm prompt with the question specified. If the user accepts, the link is processed normally, otherwise no action is taken.

  • :popup => true || array of window options: This will force the link to open in a popup window. By passing true, a default browser window will be opened with the URL. You can also specify an array of options that are passed-thru to JavaScripts window.open method.

You can mix and match the options.

link_to "Documentation", "http://camping.rubyforge.org/", :confirm => "Are you sure?"
link_to "Help", R( Help ), :popup => true
link_to image_tag( "thumb.png" ), "/public/images/fullsize.png", :popup => ['My house', 'height=300,width=600']


125
126
127
128
129
130
# File 'lib/bivouac/helpers/view/goh/html.rb', line 125

def link_to(name, address, options = {})
  options = javascript_options( options )
  options[:href] = address
  
  a( options ) do; name; end
end

Returns a stylesheet link tag for the sources specified as arguments. If you don’t specify an extension, .css will be appended automatically. You can modify the link attributes by passing a hash as the last argument.

stylesheet_link_tag "style" # =>
  <link href="/public/stylesheets/style.css" media="screen" rel="Stylesheet" type="text/css" />

stylesheet_link_tag "style", :media => "all" # =>
  <link href="/public/stylesheets/style.css" media="all" rel="Stylesheet" type="text/css" />

stylesheet_link_tag "random.styles", "/css/stylish" # =>
  <link href="/public/stylesheets/random.styles" media="screen" rel="Stylesheet" type="text/css" />
  <link href="/public/stylesheets/css/stylish.css" media="screen" rel="Stylesheet" type="text/css" />


20
21
22
23
24
25
26
27
28
29
# File 'lib/bivouac/helpers/view/goh/html.rb', line 20

def stylesheet_link_tag( *data )
  options = data.last.is_a?(Hash) ? data.pop : { }
  options[:rel]   = "Stylesheet"
  options[:type]  = "text/css"
  data.collect do |file|
    file << ".css" if File.extname(file).blank?
    options[:href] = "/public/stylesheets/#{file}"
    link( options )
  end
end