Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch/minor updates #5

Merged
merged 5 commits into from
Oct 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ language: ruby
rvm:
- 1.9.3
- 2.0.0
- 2.1.0
- 2.1
- 2.2

branches:
only:
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
source "http://rubygems.org"
source "https://rubygems.org"

gemspec
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
File renamed without changes.
20 changes: 7 additions & 13 deletions lib/rails_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 28 additions & 26 deletions test/rails_utils_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) }
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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();"
Expand All @@ -131,15 +131,15 @@
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();"
end
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();"
Expand All @@ -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" ],
Expand All @@ -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
Expand Down