Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Flash messages / Bootstrap
# app/helpers/application_helper.rb
def twitterized_type(type)
case type
when :alert
"alert-block"
when :error
"alert-error"
when :notice
"alert-info"
when :success
"alert-success"
else
type.to_s
end
end
# app/views/layouts/application.html.haml
=render :partial => "shared/flash_messages", :locals => {:flash => flash}
# app/views/shared/_flash_messages.html/haml
- flash.each do |type, message|
%div.alert-message.fade.in{ "data-alert" => "alert", :class => twitterized_type(type) }
%a.close{:href => "#"} ×
%p
= message

levifig commented Oct 10, 2011

Would be nice to see this in ERb… :)
Thanks anyway! ;)

thejspr commented Jan 15, 2012

Given you twitterized_type method always returns a string representation of the input, you could use type.to_s instead.

Just missing a good 'ol spec test 👇

require 'spec_helper'

describe ApplicationHelper do
  context "instance methods" do
    describe "#twitterized_type" do
      let(:twitter_flashes) do
        {
          :alert => 'alert-block',
          :error => 'alert-error',
          :notice => 'alert-info',
          :success => 'alert-success'
        }
      end

      it "returns the twitter-bootstrap version for each flash type" do
        twitter_flashes.each do |flash_type, twitter_class|
          helper.twitterized_type(flash_type).should == twitter_class
        end
      end
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment