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

Instance Method Details

#article(options = {}, &block) ⇒ Object



126
127
128
# File 'lib/swiss_knife/helpers.rb', line 126

def article(options = {}, &block)
  wrapper(:article, options, &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(options = {}, &block)
  action_name = ACTION_ALIASES[controller.action_name] || controller.action_name
  controller_name_for_css_class = controller_name.gsub(/\_/, "-")

  options = {
    :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(options)

  options[:class] << (" " + options.delete(:append_class).to_s) if options[:append_class]

  (:body, capture(&block), options).html_safe
end

#dispatcher_tagObject

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, options = {}, &block)
  legend = t(legend, :default => legend.to_s)

  (:fieldset, options) do
    body = (:legend, legend)
    body << yield.to_s.html_safe
    body
  end
end

#flash_messagesObject

Display flash messages.

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 flash_messages
  safe_buffer do |html|
    flash.each do |name, message|
      html << (:p, message, :class => "message #{name}")
      flash.discard(name)
    end
  end
end


122
123
124
# File 'lib/swiss_knife/helpers.rb', line 122

def footer(options = {}, &block)
  wrapper(:footer, options, &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, options = {})
  email = Digest::MD5.hexdigest(email) unless email =~ /^[a-z0-9]{32}$/i
  options.reverse_merge!(
    :rating  => :g,
    :default => "gravatar.jpg",
    :ssl     => request.ssl?,
    :size    => 32
  )

  params = {
    :r => options[:rating],
    :d => DEFAULT_GRAVATARS.include?(options[:default]) ?
      options[:default] : compute_public_path(options[:default], "images", nil, true),
    :s => options[:size]
  }

  url = String.new.tap do |s|
    s << (options[: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 => options[:alt], :title => options[:title] }
end

#header(options = {}, &block) ⇒ Object



118
119
120
# File 'lib/swiss_knife/helpers.rb', line 118

def header(options = {}, &block)
  wrapper(:header, options, &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)
  options = args.extract_options!

  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, options)
      elsif bundle
        bundle.each do |file|
          html << javascript_include_tag(file.to_s.html_safe, options)
        end
      else
        html << javascript_include_tag(name.to_s.html_safe, options)
      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(options = {}, &block)
  wrapper(:div, options.merge(:id => "main"), &block)
end

#page(options = {}, &block) ⇒ Object



106
107
108
# File 'lib/swiss_knife/helpers.rb', line 106

def page(options = {}, &block)
  wrapper(:div, options.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(options = {}, &block)
  wrapper(:section, options, &block)
end


114
115
116
# File 'lib/swiss_knife/helpers.rb', line 114

def sidebar(options = {}, &block)
  wrapper(:sidebar, options, &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)
  options = args.extract_options!

  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", options)
      elsif bundle
        bundle.each do |file|
          html << stylesheet_link_tag("#{file}", options)
        end
      else
        html << stylesheet_link_tag("#{name}", options)
      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, options = {})
  options.reverse_merge!(:button => :"swiss_knife.submit", :cancel => :"swiss_knife.cancel")

  safe_buffer do |html|
    html << submit_tag(t(options[:button], :default => options[:button]), :class => "button")
    html << " "
    html << link_to(t(options[:cancel], :default => options[: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, options = {}, &block)
  (tag, capture(&block), options)
end