rails_utils 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +16 -1
- data/lib/rails_utils.rb +29 -1
- data/lib/rails_utils/version.rb +1 -1
- data/test/rails_utils_test.rb +42 -2
- metadata +14 -14
data/README.markdown
CHANGED
@@ -56,6 +56,21 @@ By looking at the compiled JavaScript output, it should be apparent on how you s
|
|
56
56
|
|
57
57
|
As similar to `page_class`, `create` is mapped to `new` and `update` is mapped to `edit`.
|
58
58
|
|
59
|
+
## #`flash_messages`
|
60
|
+
|
61
|
+
This helper method prints Rails flash messages with classes that correspond to Bootstrap's convention.
|
62
|
+
|
63
|
+
Just invoke `flash_messages` anywhere within `layout/application`.
|
64
|
+
|
65
|
+
= flash_messages
|
66
|
+
|
67
|
+
Suppose there's a `flash[:success]`, you should see:
|
68
|
+
|
69
|
+
<div class="alert alert-success fade in">
|
70
|
+
<button class="close" data-dismiss-alert="alert" type="button">x</button>
|
71
|
+
<p>flash is success</p>
|
72
|
+
</div>
|
73
|
+
|
59
74
|
## Installation
|
60
75
|
|
61
76
|
gem install rails_utils
|
@@ -66,7 +81,7 @@ Minitest-ed. To run all tests, just run `rake` or `rake test`.
|
|
66
81
|
|
67
82
|
## Author
|
68
83
|
|
69
|
-
Rails
|
84
|
+
Rails Utils is maintained by [Winston Teo](mailto:winstonyw+googlevisualr@gmail.com).
|
70
85
|
|
71
86
|
Who is Winston Teo? [You should follow Winston on Twitter](http://www.twitter.com/winstonyw), or find out more on [WinstonYW](http://www.winstonyw.com) and [LinkedIn](http://sg.linkedin.com/in/winstonyw).
|
72
87
|
|
data/lib/rails_utils.rb
CHANGED
@@ -18,6 +18,20 @@ module RailsUtils
|
|
18
18
|
JS
|
19
19
|
end
|
20
20
|
|
21
|
+
def flash_messages
|
22
|
+
html = ""
|
23
|
+
flash.each do |key, message|
|
24
|
+
html <<
|
25
|
+
content_tag(:div, class: "#{flash_class(key)} fade in") do
|
26
|
+
content = ""
|
27
|
+
content << content_tag(:button, "x", type: "button", class: "close", "data-dismiss-alert" => "alert")
|
28
|
+
content << content_tag(:p, message)
|
29
|
+
content.html_safe
|
30
|
+
end
|
31
|
+
end
|
32
|
+
html
|
33
|
+
end
|
34
|
+
|
21
35
|
private
|
22
36
|
|
23
37
|
def page_controller_class
|
@@ -28,8 +42,22 @@ module RailsUtils
|
|
28
42
|
class_mappings = { "create" => "new", "update" => "edit" }
|
29
43
|
class_mappings[controller.action_name] || controller.action_name
|
30
44
|
end
|
45
|
+
|
46
|
+
def flash_class(key)
|
47
|
+
case key
|
48
|
+
when :success
|
49
|
+
"alert alert-success"
|
50
|
+
when :notice
|
51
|
+
"alert alert-info"
|
52
|
+
when :error
|
53
|
+
"alert alert-error"
|
54
|
+
when :alert
|
55
|
+
"alert alert-error"
|
56
|
+
else
|
57
|
+
"alert alert-#{key}"
|
58
|
+
end
|
59
|
+
end
|
31
60
|
end
|
32
61
|
end
|
33
62
|
|
34
|
-
|
35
63
|
ActionView::Base.send :include, RailsUtils::ActionViewExtensions
|
data/lib/rails_utils/version.rb
CHANGED
data/test/rails_utils_test.rb
CHANGED
@@ -1,11 +1,14 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
describe "RailsUtils::ActionViewExtensions" do
|
4
|
-
|
5
4
|
let(:controller) { ActionController::Base.new }
|
5
|
+
let(:request) { ActionDispatch::Request.new(flash: {}) }
|
6
6
|
let(:view) { ActionView::Base.new }
|
7
7
|
|
8
|
-
before
|
8
|
+
before do
|
9
|
+
controller.request = request
|
10
|
+
view.controller = controller
|
11
|
+
end
|
9
12
|
|
10
13
|
describe "#page_class" do
|
11
14
|
# controller_name, action_name, expected
|
@@ -70,4 +73,41 @@ describe "RailsUtils::ActionViewExtensions" do
|
|
70
73
|
end
|
71
74
|
end
|
72
75
|
|
76
|
+
describe "#flash_messages" do
|
77
|
+
def set_flash(key, message)
|
78
|
+
controller.flash[key] = message
|
79
|
+
end
|
80
|
+
|
81
|
+
[
|
82
|
+
[ :success , /alert alert-success/, "flash is success" ],
|
83
|
+
[ :notice , /alert alert-info/ , "flash is notice" ],
|
84
|
+
[ :error , /alert alert-error/ , "flash is error" ],
|
85
|
+
[ :alert , /alert alert-error/ , "flash is alert" ],
|
86
|
+
[ :custom , /alert alert-custom/ , "flash is custom" ],
|
87
|
+
].each do |key, expected_class, expected_message|
|
88
|
+
describe "when flash contains #{key} key" do
|
89
|
+
before { set_flash key, expected_message }
|
90
|
+
|
91
|
+
it "prints class '#{expected_class}'" do
|
92
|
+
view.flash_messages.must_match expected_class
|
93
|
+
end
|
94
|
+
|
95
|
+
it "prints message '#{expected_message}'" do
|
96
|
+
view.flash_messages.must_match expected_message
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
describe "when bootstrap is present" do
|
102
|
+
it "can fade in and out" do
|
103
|
+
set_flash :alert , "not important"
|
104
|
+
view.flash_messages.must_match /fade in/
|
105
|
+
end
|
106
|
+
|
107
|
+
it "can be dismissed" do
|
108
|
+
set_flash :alert , "not important"
|
109
|
+
view.flash_messages.must_match /data-dismiss-alert=.*alert/
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
73
113
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,22 +9,22 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-04-
|
12
|
+
date: 2013-04-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &70237572439820 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
-
- -
|
19
|
+
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: 3.2
|
21
|
+
version: '3.2'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70237572439820
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: minitest
|
27
|
-
requirement: &
|
27
|
+
requirement: &70237572439300 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 4.7.0
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70237572439300
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: turn
|
38
|
-
requirement: &
|
38
|
+
requirement: &70237572437360 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 0.9.6
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70237572437360
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: mocha
|
49
|
-
requirement: &
|
49
|
+
requirement: &70237572436900 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: 0.13.3
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70237572436900
|
58
58
|
description: Rails helpers based on opinionated project practices. Currently useful
|
59
59
|
for structuring CSS and JS.
|
60
60
|
email:
|
@@ -112,7 +112,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
112
112
|
version: '0'
|
113
113
|
segments:
|
114
114
|
- 0
|
115
|
-
hash:
|
115
|
+
hash: -410381614727265590
|
116
116
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
117
|
none: false
|
118
118
|
requirements:
|
@@ -121,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
121
121
|
version: '0'
|
122
122
|
segments:
|
123
123
|
- 0
|
124
|
-
hash:
|
124
|
+
hash: -410381614727265590
|
125
125
|
requirements: []
|
126
126
|
rubyforge_project:
|
127
127
|
rubygems_version: 1.8.17
|