| File: | lib/Railsish/ViewHelpers.pm |
| Coverage: | 55.6% |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package Railsish::ViewHelpers; | ||||||
| 2 | 4 4 4 | 33 11 41 | use strict; | ||||
| 3 | 4 4 4 | 40 9 51 | use warnings; | ||||
| 4 | 4 4 4 | 248 15 78 | use Railsish::CoreHelpers; | ||||
| 5 | 4 4 4 | 352 19 85 | use HTML::Entities; | ||||
| 6 | |||||||
| 7 | 4 4 4 | 51 10 48 | use Exporter::Lite; | ||||
| 8 | our @EXPORT = qw(render_stickies stylesheet_link_tag javascript_include_tag link_to); | ||||||
| 9 | |||||||
| 10 | sub stylesheet_link_tag { | ||||||
| 11 | 0 | 0 | 0 | my (@css) = @_; | |||
| 12 | |||||||
| 13 | 0 | 0 | my $out = ""; | ||||
| 14 | 0 | 0 | for my $css (@css) { | ||||
| 15 | 0 | 0 | my $uri; | ||||
| 16 | 0 | 0 | if ($css =~ /^http:/) { | ||||
| 17 | 0 | 0 | $uri = $css; | ||||
| 18 | } | ||||||
| 19 | else { | ||||||
| 20 | 0 | 0 | my $dir = app_root("/public/stylesheets"); | ||||
| 21 | |||||||
| 22 | 0 | 0 | my $file = "${dir}/${css}.css"; | ||||
| 23 | 0 | 0 | $file = "${dir}/${css}" unless -f $file; | ||||
| 24 | 0 | 0 | $file .= "?" . (stat($file))[9]; | ||||
| 25 | 0 | 0 | $uri = $file; | ||||
| 26 | 0 | 0 | $uri =~ s/^$dir/\/stylesheets/; | ||||
| 27 | } | ||||||
| 28 | |||||||
| 29 | 0 | 0 | if ($uri) { | ||||
| 30 | 0 | 0 | $out .= qq{<link href="$uri" media="all" rel="stylesheet" type="text/css">\n} | ||||
| 31 | } | ||||||
| 32 | } | ||||||
| 33 | 0 | 0 | return $out; | ||||
| 34 | }; | ||||||
| 35 | |||||||
| 36 | sub javascript_include_tag { | ||||||
| 37 | 0 | 0 | 0 | my @sources = @_; | |||
| 38 | 0 | 0 | my $out = ""; | ||||
| 39 | 0 | 0 | for my $source (@sources) { | ||||
| 40 | 0 | 0 | my $uri; | ||||
| 41 | 0 | 0 | if ($source =~ /^\w+:\/\//) { | ||||
| 42 | 0 | 0 | $uri = $source; | ||||
| 43 | } | ||||||
| 44 | else { | ||||||
| 45 | 0 | 0 | $uri = $source; | ||||
| 46 | 0 | 0 | $uri .= ".js" if $source !~ /\./; | ||||
| 47 | 0 | 0 | $uri = "/javascripts/$uri" if $source !~ /\//; | ||||
| 48 | } | ||||||
| 49 | |||||||
| 50 | 0 | 0 | $out .= qq{<script type="text/javascript" src="$uri"></script>\n}; | ||||
| 51 | } | ||||||
| 52 | 6 | 38 | return $out; | ||||
| 53 | } | ||||||
| 54 | |||||||
| 55 | sub link_to { | ||||||
| 56 | 6 | 0 | 19 | my ($label, $url, %attr) = @_; | |||
| 57 | 6 | 19 | my $attr = ""; | ||||
| 58 | 6 | 13 | if (%attr) { | ||||
| 59 | 6 2 4 | 41 7 12 | $attr = qq{ $_="@{[ encode_entities($attr{$_}, '<>&"') ]}"} for keys %attr; | ||||
| 60 | } | ||||||
| 61 | 4 4 | 22 26 | qq{<a href="$url"$attr>@{[ encode_entities($label, '<>&') ]}</a>}; | ||||
| 62 | } | ||||||
| 63 | |||||||
| 64 | 4 4 4 | 358 21 15 | use Railsish::ControllerHelpers (); | ||||
| 65 | |||||||
| 66 | sub render_stickies { | ||||||
| 67 | 6 | 0 | 41 | my $out = ""; | |||
| 68 | 6 | 96 | if (@Railsish::ControllerHelpers::notice_stickies > 0) { | ||||
| 69 | 3 | 29 | $out = '<div id="notice_stickies" class="message notice">'; | ||||
| 70 | 3 | 15 | for (@Railsish::ControllerHelpers::notice_stickies) { | ||||
| 71 | 3 | 32 | $out .= "<p>" . $_->{text} . "</p>"; | ||||
| 72 | } | ||||||
| 73 | 3 | 6 | $out .= "</div>"; | ||||
| 74 | } | ||||||
| 75 | |||||||
| 76 | 3 | 23 | @Railsish::ControllerHelpers::notice_stickies = (); | ||||
| 77 | 3 | 21 | return $out; | ||||
| 78 | } | ||||||
| 79 | |||||||
| 80 | 1; | ||||||