Swiss Knife

Lots of Rails 3 helpers that I use on my own projects.

Usage

dispatcher_tag

If you’re using github.com/fnando/dispatcher-js, this helper will create the meta tag.

<%= dispatcher_tag %>

body(&block)

Create the body tag setting some attributes:

<%= body do %>
<% end %>

Will be converted to

<body id="sample-page" class="sample-index en">
</body>

Set any attribute by providing a hash:

<%= body :id => "foo", :class => "bar", :onload => "init();" do %>
<% end %>

If you just want to append more classes, use the options :append_class:

<%= body :append_class => do %>
<% end %>

flash_messages

Display all flash messages in <p class="message #{type}"></p> tags. So if you set messages like

flash[:notice] = "Notice"

you can add this to your view

<%= flash_messages %>

and the helper will output

<p class="message notice">Notice</p>

Block wrappers

Just hiding some HTML.

<%= main do %>
  <!-- Wrap the content into a div#main tag -->
<% end %>

<%= sidebar do %>
  <!-- Wrap the content into a sidebar tag -->
<% end %>

There’s also page, section, article, header, and footer wrappers. You can set other attributes, like CSS classes:

<%= main :class => "rounded" do %>
<% end %>

Assets (JavaScripts & Stylesheets)

There are two helpers that merge files like Assets Packager.

<%= javascript_includes :base %>
<%= stylesheet_includes :base %>

You need to create a config/assets.yml file like this:

javascripts:
  base:
    - jquery
    - rails
    - application
stylesheets:
  base:
    - reset

To merge files, you have to run a rake task:

$ rake assets:merge

fieldset

A fieldset helper with I18n support.

<%= fieldset "labels.user.create" do %>
  <!-- Some html -->
<% end %>

page_title

Set page title right from I18n files.

en:
  titles:
    users:
      new: "Sign up"
      show: "%{name}'s Profile"

You can set interpolation options:

page_title :name => @user.name

To display page title:

<%= page_title %>

The are some action aliases:

create => new
update => edit
remove => destroy

gravatar_tag

gravatar_tag user.email
gravatar_tag "098f6bcd4621d373cade4e832627b4f6"
gravatar_tag user.email, :size => 80
gravatar_tag user.email, :rating => :x

# Predefined default images provided by Gravatar. Can be
# :mm, :identicon, :monsterid, :wavatar, :retro, 404
gravatar_tag user.email, :default => :mm

gravatar_tag user.email, :default => "gravatar.png"
gravatar_tag user.email, :ssl => true
gravatar_tag user.email, :alt => user.name
gravatar_tag user.email, :title => user.name

submit_or_cancel

submit_or_cancel root_path
submit_or_cancel root_path :button => "Save"
submit_or_cancel root_path :button => :"some.scope.for.save"
submit_or_cancel root_path :cancel => "Go back"
submit_or_cancel root_path :cancel => :"some.scope.for.go_back"

jquery_script_tag

Load jQuery from Google’s CDN, with fallback to a local version.

jquery_script_tag #=> Default to 1.4.4
jquery_script_tag("1.4.2")

The local version should be placed at public/javascripts/jquery-%{version}.min.js.

RSpec

Swiss Knife includes some RSpec matchers. To use them, add the following like to your spec_helper.rb file:

require "swiss_knife/rspec"

have_tag and have_node

The have_tag matcher uses Nokogiri, so you can use any CSS selector accepted by Nokogiri to filter elements.

html.should have_tag("p", "Hello world!")
html.should have_tag("p", :text => "Hello world!")
html.should have_tag("p", :count => 1)
html.should have_tag("p", :maximum => 3)
html.should have_tag("p", :minimum => 3)
html.should have_tag("form") do |form|
  form.should have_tag("input.submit", :count => 1)
end

If you’re inspecting some XML snippet, you can use the have_node matcher.

have_text

string.should have_text("Lorem ipsum")
string.should have_text(/Lorem ipsum/)

allow

record.should allow("[email protected]").as(:email)
record.should_not allow(nil, "").as(:email)

Maintainer

License

(The MIT License)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.