Module: SwissKnife::Helpers
- Defined in:
- lib/swiss_knife/helpers.rb
Constant Summary collapse
- ACTION_ALIASES =
{ "create" => "new", "update" => "edit", "remove" => "destroy" }
- DEFAULT_GRAVATARS =
[:mm, :identicon, :monsterid, :wavatar, :retro, 404]
- GRAVATAR_URLS =
{ :http => "http://www.gravatar.com/avatar/", :https => "https://secure.gravatar.com/avatar/" }
Instance Method Summary collapse
- #article(options = {}, &block) ⇒ Object
-
#body(options = {}, &block) ⇒ Object
Wrap the content in a <BODY> tag.
-
#dispatcher_tag ⇒ Object
Create a meta tag for github.com/fnando/dispatcher-js.
- #fieldset(legend, options = {}, &block) ⇒ Object
-
#flash_messages ⇒ Object
Display flash messages.
- #footer(options = {}, &block) ⇒ Object
-
#gravatar_tag(email, options = {}) ⇒ Object
Display gravatar images.
- #header(options = {}, &block) ⇒ Object
- #javascript_includes(*args) ⇒ Object
-
#jquery_script_tag(version = "1.6.3") ⇒ Object
Load jQuery from Google’s CDN and if the world is collapsing and Google is down, load it locally.
- #main(options = {}, &block) ⇒ Object
- #page(options = {}, &block) ⇒ Object
-
#safe_buffer(&block) ⇒ Object
Return a string marked as safe html.
- #section(options = {}, &block) ⇒ Object
- #sidebar(options = {}, &block) ⇒ Object
- #stylesheet_includes(*args) ⇒ Object
-
#submit_or_cancel(url, options = {}) ⇒ Object
Create a submit button with a cancel link besides.
- #wrapper(tag, options = {}, &block) ⇒ Object
Instance Method Details
#article(options = {}, &block) ⇒ Object
126 127 128 |
# File 'lib/swiss_knife/helpers.rb', line 126 def article( = {}, &block) wrapper(:article, , &block) end |
#body(options = {}, &block) ⇒ Object
Wrap the content in a <BODY> tag.
<%= body do %>
<h1>Hi there!</h1>
<% end %>
This will set some attributes like id
and class
, with controller and action name.
You can set additional CSS classes by using the :append_class
option.
body(:append_class => "theme-red") { ...some content }
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/swiss_knife/helpers.rb', line 87 def body( = {}, &block) action_name = ACTION_ALIASES[controller.action_name] || controller.action_name controller_name_for_css_class = controller_name.gsub(/\_/, "-") = { :id => "#{controller_name_for_css_class}-page", :class => [ "#{controller_name_for_css_class}-#{action_name}", "#{controller_name_for_css_class}-#{controller.action_name}", I18n.locale, Rails.env ].uniq.join(" ") }.merge() [:class] << (" " + .delete(:append_class).to_s) if [:append_class] content_tag(:body, capture(&block), ).html_safe end |
#dispatcher_tag ⇒ Object
Create a meta tag for github.com/fnando/dispatcher-js.
<%= dispatcher %>
70 71 72 |
# File 'lib/swiss_knife/helpers.rb', line 70 def dispatcher_tag %[<meta name="page" content="#{controller_name}##{controller.action_name}" />].html_safe end |
#fieldset(legend, options = {}, &block) ⇒ Object
178 179 180 181 182 183 184 185 186 |
# File 'lib/swiss_knife/helpers.rb', line 178 def fieldset(legend, = {}, &block) legend = t(legend, :default => legend.to_s) content_tag(:fieldset, ) do body = content_tag(:legend, legend) body << yield.to_s.html_safe body end end |
#flash_messages ⇒ Object
Display flash messages.
#=> '<p class="message alert">This is just an alert!</p>
57 58 59 60 61 62 63 64 |
# File 'lib/swiss_knife/helpers.rb', line 57 def safe_buffer do |html| flash.each do |name, | html << content_tag(:p, , :class => "message #{name}") flash.discard(name) end end end |
#footer(options = {}, &block) ⇒ Object
122 123 124 |
# File 'lib/swiss_knife/helpers.rb', line 122 def ( = {}, &block) wrapper(:footer, , &block) end |
#gravatar_tag(email, options = {}) ⇒ Object
Display gravatar images.
<%= gravatar "cb5d9e9095cd41b636764a85e57ade4b" %>
<%= gravatar user.email, :alt => user.name, :size => 80 %>
<%= gravatar user.email, :title => user.name %>
<%= gravatar user.email, :rating => :g %>
<%= gravatar user.email, :default => :mm %>
<%= gravatar user.email, :default => "gravatar.jpg" %>
<%= gravatar user.email, :ssl => true %>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/swiss_knife/helpers.rb', line 26 def gravatar_tag(email, = {}) email = Digest::MD5.hexdigest(email) unless email =~ /^[a-z0-9]{32}$/i .reverse_merge!( :rating => :g, :default => "gravatar.jpg", :ssl => request.ssl?, :size => 32 ) params = { :r => [:rating], :d => DEFAULT_GRAVATARS.include?([:default]) ? [:default] : compute_public_path([:default], "images", nil, true), :s => [:size] } url = String.new.tap do |s| s << ([:ssl] ? GRAVATAR_URLS[:https] : GRAVATAR_URLS[:http]) s << email.to_s << ".jpg" s << "?" s << params.collect {|k, v| v.to_s.to_query(k) }.join("&") end image_tag url, { :class => "gravatar", :alt => [:alt], :title => [:title] } end |
#header(options = {}, &block) ⇒ Object
118 119 120 |
# File 'lib/swiss_knife/helpers.rb', line 118 def header( = {}, &block) wrapper(:header, , &block) end |
#javascript_includes(*args) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/swiss_knife/helpers.rb', line 138 def javascript_includes(*args) = args. safe_buffer do |html| args.each do |name| bundle = SwissKnife::Assets.config["javascripts"][name.to_s] rescue nil if SwissKnife::Assets.merge? && bundle html << javascript_include_tag("#{name}_packaged".html_safe, ) elsif bundle bundle.each do |file| html << javascript_include_tag(file.to_s.html_safe, ) end else html << javascript_include_tag(name.to_s.html_safe, ) end end end end |
#jquery_script_tag(version = "1.6.3") ⇒ Object
Load jQuery from Google’s CDN and if the world is collapsing and Google is down, load it locally.
jquery_script_tag #=> Default to 1.6.3
jquery_script_tag("1.4.2")
The local version should be placed at public/javascripts/jquery-%{version}.min.js
.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/swiss_knife/helpers.rb', line 214 def jquery_script_tag(version = "1.6.3") target = respond_to?(:compute_public_path) ? self : asset_paths local_path = target.__send__(:compute_public_path, "jquery-#{version}.min.js", "javascripts") remote_path = "#{request.protocol}ajax.googleapis.com/ajax/libs/jquery/#{version}/jquery.min.js" safe_buffer do %[ <script type="text/javascript" src="#{remote_path}"></script> <script type="text/javascript"> if (typeof jQuery === "undefined") { document.write(unescape("%3Cscript src='#{local_path}' type='text/javascript'%3E%3C/script%3E")); }; </script> ] end end |
#main(options = {}, &block) ⇒ Object
110 111 112 |
# File 'lib/swiss_knife/helpers.rb', line 110 def main( = {}, &block) wrapper(:div, .merge(:id => "main"), &block) end |
#page(options = {}, &block) ⇒ Object
106 107 108 |
# File 'lib/swiss_knife/helpers.rb', line 106 def page( = {}, &block) wrapper(:div, .merge(:id => "page"), &block) end |
#safe_buffer(&block) ⇒ Object
Return a string marked as safe html.
Change the buffer object when block expects one argument.
safe_buffer {|html| html << "<p>Some content</p>"}
Use block’s return when block expects no argument.
safe_buffer { "<p>Some content</p>" }
Get a safe buffer object.
buffer = safe_buffer
245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/swiss_knife/helpers.rb', line 245 def safe_buffer(&block) buffer = ActiveSupport::SafeBuffer.new if block_given? if block.arity == 1 yield buffer else buffer = yield(buffer) end end buffer.html_safe end |
#section(options = {}, &block) ⇒ Object
130 131 132 |
# File 'lib/swiss_knife/helpers.rb', line 130 def section( = {}, &block) wrapper(:section, , &block) end |
#sidebar(options = {}, &block) ⇒ Object
114 115 116 |
# File 'lib/swiss_knife/helpers.rb', line 114 def ( = {}, &block) wrapper(:sidebar, , &block) end |
#stylesheet_includes(*args) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/swiss_knife/helpers.rb', line 158 def stylesheet_includes(*args) = args. safe_buffer do |html| args.each do |name| bundle = SwissKnife::Assets.config["stylesheets"][name.to_s] rescue nil if SwissKnife::Assets.merge? && bundle html << stylesheet_link_tag("#{name}_packaged", ) elsif bundle bundle.each do |file| html << stylesheet_link_tag("#{file}", ) end else html << stylesheet_link_tag("#{name}", ) end end end end |
#submit_or_cancel(url, options = {}) ⇒ Object
Create a submit button with a cancel link besides.
submit_or_cancel root_path
submit_or_cancel root_path, :button => "Save"
submit_or_cancel root_path, :button => :"some.scope.for.button"
submit_or_cancel root_path, :cancel => "Go back"
submit_or_cancel root_path, :cancel => :"some.scope.for.link"
196 197 198 199 200 201 202 203 204 |
# File 'lib/swiss_knife/helpers.rb', line 196 def submit_or_cancel(url, = {}) .reverse_merge!(:button => :"swiss_knife.submit", :cancel => :"swiss_knife.cancel") safe_buffer do |html| html << submit_tag(t([:button], :default => [:button]), :class => "button") html << " " html << link_to(t([:cancel], :default => [:cancel]), url, :class => "cancel") end end |
#wrapper(tag, options = {}, &block) ⇒ Object
134 135 136 |
# File 'lib/swiss_knife/helpers.rb', line 134 def wrapper(tag, = {}, &block) content_tag(tag, capture(&block), ) end |