Skip to content

Instantly share code, notes, and snippets.

@mariozig
Created October 3, 2011 01:46
Show Gist options
  • Save mariozig/ed25e8fd0a5b0ee24c4e to your computer and use it in GitHub Desktop.
Save mariozig/ed25e8fd0a5b0ee24c4e to your computer and use it in GitHub Desktop.
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
Copy link

levifig commented Oct 10, 2011

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

@thejspr
Copy link

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.

@mariozig
Copy link
Author

@codenamev
Copy link

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