diff --git a/.travis.yml b/.travis.yml index d1479f5..ba7c27a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,8 @@ language: ruby rvm: - 1.9.3 - 2.0.0 - - 2.1.0 + - 2.1 + - 2.2 branches: only: diff --git a/Gemfile b/Gemfile index c80ee36..b4e2a20 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,3 @@ -source "http://rubygems.org" +source "https://rubygems.org" gemspec diff --git a/Gemfile.lock b/Gemfile.lock index b7ae5bc..6546602 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -5,7 +5,7 @@ PATH rails (>= 3.2) GEM - remote: http://rubygems.org/ + remote: https://rubygems.org/ specs: actionmailer (4.0.5) actionpack (= 4.0.5) diff --git a/README.markdown b/README.md similarity index 100% rename from README.markdown rename to README.md diff --git a/lib/rails_utils.rb b/lib/rails_utils.rb index 53279dd..91dbbcb 100644 --- a/lib/rails_utils.rb +++ b/lib/rails_utils.rb @@ -33,24 +33,18 @@ def javascript_initialization JS end - def flash_messages - html = "" - flash.each do |key, message| - html << - content_tag(:div, class: "#{flash_class(key.to_sym)} fade in") do - content = "" - content << content_tag(:button, "x", type: "button", class: "close", "data-dismiss" => "alert") - content << message - content.html_safe - end - end - html.html_safe + def flash_messages(options = {}) + flash.collect do |key, message| + next if message.blank? + + content_tag(:div, content_tag(:button, "x", type: "button", class: "close", "data-dismiss" => "alert") + message, class: "#{flash_class(key)} fade in #{options[:class]}") + end.join("\n").html_safe end private def flash_class(key) - case key + case key.to_sym when :success "alert alert-success" when :notice diff --git a/test/rails_utils_test.rb b/test/rails_utils_test.rb index db29edc..2dc0e8c 100644 --- a/test/rails_utils_test.rb +++ b/test/rails_utils_test.rb @@ -1,9 +1,9 @@ require 'test_helper' describe "RailsUtils::ActionViewExtensions" do - let(:controller) { ActionController::Base.new } - let(:request) { ActionDispatch::Request.new(flash: {}) } - let(:view) { ActionView::Base.new } + let(:controller) { ActionController::Base.new } + let(:request) { ActionDispatch::Request.new(flash: {}) } + let(:view) { ActionView::Base.new } before do controller.request = request @@ -37,14 +37,14 @@ describe "#page_action_class" do # action_name, expected [ - [ "index" , "index" ], - [ "show" , "show" ], - [ "new" , "new" ], - [ "create" , "new" ], - [ "edit" , "edit" ], - [ "update" , "edit" ], - [ "destroy", "destroy" ], - [ "custom" , "custom" ], + [ "index" , "index" ], + [ "show" , "show" ], + [ "new" , "new" ], + [ "create" , "new" ], + [ "edit" , "edit" ], + [ "update" , "edit" ], + [ "destroy", "destroy" ], + [ "custom" , "custom" ], ].each do |action_name, expected| describe "when ##{action_name}" do before { controller.stubs(:action_name).returns(action_name) } @@ -79,7 +79,7 @@ end describe 'when translation is missing' do - let(:action_name) { "random" } + let(:action_name) { "random" } let(:default_translation) { "#{controller_name.capitalize} #{action_name.capitalize}" } it "combines page_controller_class and page_action_class" do @@ -88,22 +88,22 @@ end describe 'when translation is avaiable' do - let(:action_name) { 'show' } + let(:action_name) { "show" } - before { I18n.backend.store_translations("en", {controller_name.to_sym => {action_name.to_sym => {title: "An awesome title"}}}) } + before { I18n.backend.store_translations("en", { controller_name.to_sym => { action_name.to_sym => { title: "An awesome title" } }}) } - it 'translates page title' do - view.page_title.must_equal 'An awesome title' + it "translates page title" do + view.page_title.must_equal "An awesome title" end end - describe 'when translation is avaiable + interpolations' do - let(:action_name) { 'show' } + describe "when translation is avaiable + interpolations" do + let(:action_name) { "show" } - before { I18n.backend.store_translations("en", {controller_name.to_sym => {action_name.to_sym => {title: "An awesome title, %{name}"}}}) } + before { I18n.backend.store_translations("en", { controller_name.to_sym => { action_name.to_sym => { title: "An awesome title, %{name}" } }}) } - it 'translates page title' do - view.page_title(name: 'bro').must_equal 'An awesome title, bro' + it "translates page title" do + view.page_title(name: "bro").must_equal "An awesome title, bro" end end end @@ -118,7 +118,7 @@ end describe "when controller name and action name are standard" do - let(:action_name) { "custom" } + let(:action_name) { "custom" } it "invokes application" do view.javascript_initialization.must_match "Dummy.init();" @@ -131,7 +131,7 @@ end describe "when action name is create" do - let(:action_name) { "create" } + let(:action_name) { "create" } it "replaces create with new" do view.javascript_initialization.must_match "Dummy.#{controller_name}.init_new();" @@ -139,7 +139,7 @@ end describe "when action name is update" do - let(:action_name) { "update" } + let(:action_name) { "update" } it "replaces update with create" do view.javascript_initialization.must_match "Dummy.#{controller_name}.init_edit();" @@ -157,9 +157,11 @@ def set_flash(key, message) # alert-error is for Bootstrap 2.3.2 [ [ :success , /alert alert-success/ , "flash is success" ], + [ "success", /alert alert-success/ , "flash is success" ], [ :notice , /alert alert-info/ , "flash is notice" ], [ "notice" , /alert alert-info/ , "flash is notice" ], [ :error , /alert alert-danger alert-error/ , "flash is error" ], + [ "error" , /alert alert-danger alert-error/ , "flash is error" ], [ :alert , /alert alert-danger alert-error/ , "flash is alert" ], [ "alert" , /alert alert-danger alert-error/ , "flash is alert" ], [ :custom , /alert alert-custom/ , "flash is custom" ], @@ -180,12 +182,12 @@ def set_flash(key, message) describe "when bootstrap is present" do it "can fade in and out" do - set_flash :alert , "not important" + set_flash :alert, "not important" view.flash_messages.must_match /fade in/ end it "can be dismissed" do - set_flash :alert , "not important" + set_flash :alert, "not important" view.flash_messages.must_match /data-dismiss=.*alert/ end end