bh 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b513d3e005f5fc935272d9aacfe98b6ed4f37d0c
4
- data.tar.gz: a255d4c8284675835aa5ce890788608d42e0acbd
3
+ metadata.gz: e69edccfc681f4a4d24ba2d5212ab4f609b7f15e
4
+ data.tar.gz: e9cb987e0b6d6dbf58342169d5cb37088167a93f
5
5
  SHA512:
6
- metadata.gz: 0cc4f4eefbc96c5b8d4315b10799081413e85bd24f43308eee5f6b6342f788afc506df266a51d4388d123b6382e4efe78a5a8811beaba16824cb1e86340c053b
7
- data.tar.gz: e888e99e4d118adb87b0bcf86f02c4b82801d34b2a991df39a6728c3743633c0f48fa854057c8fdade9064d6054ad8fb8af877b096eebd0e15d1808024276165
6
+ metadata.gz: 4b20a81f7e2b445f9fc263b80c5a72f1f3a72caafaa562752129f72843bf67b0424d2ef6bfddee5c2d3abdb737bd926c70d31adcb50f2cd668fe85402b9c5c58
7
+ data.tar.gz: 891b720e5fb99e30003fc13410956750d862c71089fc3eb19e2ebe3147ae099d402f47084a5324f342c5dd0169eab2d8abcb180a7b047688d876f36368a39232
data/.gitignore CHANGED
@@ -15,3 +15,8 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --fail-fast
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ For more information about changelogs, check
6
+ [Keep a Changelog](http://keepachangelog.com) and
7
+ [Vandamme](http://tech-angels.github.io/vandamme).
8
+
9
+ ## 0.0.2 - 2014-08-15
10
+
11
+ * [FEATURE] Add `alert_box` helper
data/Gemfile CHANGED
@@ -1,3 +1,4 @@
1
1
  source 'https://rubygems.org'
2
+ gem 'pry'
2
3
  # Specify your gem's dependencies in bh.gemspec
3
4
  gemspec
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014 claudiob
1
+ Copyright (c) 2014 Fullscreen, Inc.
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -1,25 +1,186 @@
1
- # Bh
1
+ Bh - Bootstrap Helpers
2
+ ======================
2
3
 
3
- Bootstrap Helpers for Rails
4
+ Bh provides a set of powerful helpers that streamlines the use of
5
+ [Bootstrap components](http://getbootstrap.com/components) in Rails views.
4
6
 
5
- ## Installation
7
+ Bootstrap is a great framework, but requires to write many lines of HTML code
8
+ even for simple components.
9
+ For instance, you need to write the following HTML to show a _dismissible alert_:
6
10
 
7
- Add this line to your application's Gemfile:
11
+ ```html
12
+ <div class="alert alert-info alert-dismissible" role="alert">
13
+ <button type="button" class="close" data-dismiss="alert">
14
+ <span aria-hidden="true">&times;</span>
15
+ <span class="sr-only">Close</span>
16
+ </button>
17
+ You accepted the Terms of service.
18
+ </div>
19
+ ```
8
20
 
9
- gem 'bh'
21
+ Writing this for every _dismissible alert_ in a web site is cumbersome,
22
+ repetitive, and prone to errors.
10
23
 
11
- And then execute:
24
+ Bh offers a solution to this problem by means of a set of helper methods.
25
+ The example above can be with just one line of code:
12
26
 
13
- $ bundle
27
+ ```erb
28
+ <%= alert_box 'You accepted the Terms of service.', dismissible: true %>
29
+ ```
14
30
 
15
- Or install it yourself as:
31
+ `alert_box` is only of the helpers provided by Bh and described below.
32
+ You can use only the ones you need and even mix-and-match the "standard way"
33
+ of writing Bootstrap components (many HTML lines) with the "Bh way".
16
34
 
17
- $ gem install bh
35
+ How to install
36
+ ==============
18
37
 
19
- ## Contributing
38
+ Bh is meant to be included in Rails apps by adding this line to the Gemfile:
20
39
 
21
- 1. Fork it
22
- 2. Create your feature branch (`git checkout -b my-new-feature`)
23
- 3. Commit your changes (`git commit -am 'Add some feature'`)
24
- 4. Push to the branch (`git push origin my-new-feature`)
25
- 5. Create new Pull Request
40
+ gem 'bh', '~> 0.0.2'
41
+
42
+ Since the gem follows [Semantic Versioning](http://semver.org),
43
+ indicating the full version in your Gemfile (~> *major*.*minor*.*patch*)
44
+ guarantees that your project won’t occur in any error when you `bundle update`
45
+ and a new version of Bh is released.
46
+
47
+ Adding 'bh' to your Gemfile is all you need!
48
+ From now on, you will be able to use any of the following Bh helpers in your Rails views.
49
+
50
+ The `alert_box` helper
51
+ ======================
52
+
53
+ To include [Boostrap alert boxes](http://getbootstrap.com/components/#alerts)
54
+ in your Rails views, you can use the [alert_box](#doc) helper.
55
+ Here are some examples.
56
+
57
+ Basic alerts
58
+ ------------
59
+
60
+ ```erb
61
+ <%= alert_box 'You accepted the Terms of service.' %>
62
+ ```
63
+
64
+ will generate the HTML to render an "info" alert:
65
+
66
+ ```html
67
+ <div class="alert alert-info" role="alert">You accepted the Terms of service.</div>
68
+ ```
69
+
70
+ ![alert-basic](https://cloud.githubusercontent.com/assets/7408595/3936904/7c1c419c-24a7-11e4-9910-6452ab334c09.png)
71
+
72
+ Dismissible alerts
73
+ ------------------
74
+
75
+ ```erb
76
+ <%= alert_box 'You accepted the Terms of service.', dismissible: true %>
77
+ ```
78
+
79
+ will generate the HTML to render an alert with an '×' to close it:
80
+
81
+ ```html
82
+ <div class="alert alert-info" role="alert">
83
+ <button class="close" data-dismiss="alert" type="button">
84
+ <span aria-hidden="true">×</span><span class="sr-only">Close</span>
85
+ </button>
86
+ You accepted the Terms of service.
87
+ </div>
88
+ ```
89
+
90
+ ![alert-dismissible](https://cloud.githubusercontent.com/assets/7408595/3936901/7c107b00-24a7-11e4-8265-2752a630357a.png)
91
+
92
+ Contextual alerts
93
+ -----------------
94
+
95
+ ```erb
96
+ <%= alert_box 'You accepted the Terms of service.', context: :success %>
97
+ ```
98
+
99
+ will generate the HTML to render a "success" alert (green background):
100
+
101
+ ```html
102
+ <div class="alert alert-success" role="alert">You accepted the Terms of service.</div>
103
+ ```
104
+
105
+ Available contexts are `:success`, `:info` (default), `:warning`, `:danger`.
106
+
107
+ ![alert-success](https://cloud.githubusercontent.com/assets/7408595/3936902/7c182a44-24a7-11e4-9f8b-7c381662128b.png)
108
+
109
+ Priority alerts
110
+ ---------------
111
+
112
+ Since a common use of alert boxes in Rails applications is to display
113
+ [flash messages](http://api.rubyonrails.org/classes/ActionDispatch/Flash/FlashHash.html),
114
+ the `alert_box` helper accepts the priority of the flash message as an option.
115
+
116
+ ```erb
117
+ <%= alert_box 'You accepted the Terms of service.', priority: :notice %>
118
+ ```
119
+
120
+ will generate the HTML to render a dismissible "success" alert (green background):
121
+
122
+ ```html
123
+ <div class="alert alert-success" role="alert">
124
+ <button class="close" data-dismiss="alert" type="button">
125
+ <span aria-hidden="true">×</span><span class="sr-only">Close</span>
126
+ </button>
127
+ You accepted the Terms of service.
128
+ </div>
129
+ ```
130
+
131
+ Available priorities are `:alert`, `:notice`.
132
+
133
+ ![alert-success-dismissible](https://cloud.githubusercontent.com/assets/7408595/3936900/7c0bdbcc-24a7-11e4-9b49-93468b7dc738.png)
134
+
135
+ Complex alerts
136
+ --------------
137
+
138
+ ```erb
139
+ <%= alert_box context: :success, dismissible: true do %>
140
+ <strong>Thanks!</strong> You accepted the <%= link_to 'Terms of service', '/terms' %>.
141
+ <% end %>
142
+ ```
143
+
144
+ will generate the HTML to render a dismissible "success" alert that includes
145
+ highlighted text and appropriately styled links:
146
+
147
+ ```html
148
+ <div class="alert alert-success" role="alert">
149
+ <button class="close" data-dismiss="alert" type="button">
150
+ <span aria-hidden="true">×</span><span class="sr-only">Close</span>
151
+ </button>
152
+ <strong>Thanks!</strong> You accepted the <a href="/terms" class="alert-link">Terms of service</a>.
153
+ </div>
154
+ ```
155
+
156
+ ![alert-complex](https://cloud.githubusercontent.com/assets/7408595/3936903/7c1b70f0-24a7-11e4-95ee-11f6920ddd4d.png)
157
+
158
+
159
+ How to release new versions
160
+ ===========================
161
+
162
+ If you are a manager of this project, remember to upgrade the [Bh gem](http://rubygems.org/gems/bh)
163
+ whenever a new feature is added or a bug gets fixed.
164
+
165
+ Make sure all the tests are passing on [Travis CI](https://travis-ci.org/Fullscreen/bh),
166
+ document the changes in CHANGELOG.md and README.md, bump the version, then run
167
+
168
+ rake release
169
+
170
+ Remember that the bh gem follows [Semantic Versioning](http://semver.org).
171
+ Any new release that is fully backward-compatible should bump the *patch* version (0.0.x).
172
+ Any new version that breaks compatibility should bump the *minor* version (0.x.0)
173
+
174
+ How to contribute
175
+ =================
176
+
177
+ Bh needs your support!
178
+
179
+ If you find that a method is missing, fork the project, add the missing code,
180
+ write the appropriate tests, then submit a pull request, and it will gladly
181
+ be merged! If you need an inspiration, look at the TODO.md file.
182
+
183
+ To run the tests, simply type `rspec` on the command line.
184
+
185
+ Don’t hesitate to send code comments, issues or pull requests through GitHub
186
+ and to spread the love. Thanks! :)
data/Rakefile CHANGED
@@ -1 +1,11 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler'
2
+ Bundler.setup
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ require 'rspec/core/rake_task'
6
+ require 'rspec/core/version'
7
+
8
+ desc 'Run all examples'
9
+ RSpec::Core::RakeTask.new :spec
10
+
11
+ task default: [:spec]
data/TODO.md ADDED
@@ -0,0 +1,298 @@
1
+
2
+ Modals
3
+ ======
4
+
5
+ Use the `modal` helper to insert a button that will toggle any type of
6
+ [modal](http://getbootstrap.com/javascript/#modals) provided by Bootstrap.
7
+
8
+ Basic modals
9
+ ------------
10
+
11
+ ```erb
12
+ <%= modal 'Click to toggle a modal', 'I am the content of the modal' %>
13
+ ```
14
+
15
+ will generate the HTML to render a button that, when clicked, will toggle
16
+ a modal with some simple text:
17
+
18
+ ```html
19
+ <button class="btn btn-primary" data-toggle="modal" data-target="#myModal">
20
+ Click to toggle a modal
21
+ </button>
22
+ <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
23
+ <div class="modal-dialog">
24
+ <div class="modal-content">
25
+ <div class="modal-body">
26
+ I am the content of the modal
27
+ </div>
28
+ </div>
29
+ </div>
30
+ </div>
31
+ ```
32
+
33
+ Complex modals
34
+ --------------
35
+
36
+ ```erb
37
+ <%= modal 'Delete account', title: 'Confirm your action', dismissible: true, context: :danger, button_class: 'btn-sm', modal_class: 'modal-sm' do %>
38
+ You are about to delete your account. Would you like to proceed?
39
+ <%= footer do %>
40
+ <button class="btn btn-danger">Yes, delete my account</button>
41
+ <% end %>
42
+ <% end %>
43
+
44
+ will generate the HTML to render a small "danger" (red) button that, when
45
+ clicked, will toggle a small, dismissible modal with a title, some text, and a
46
+ footer with a call-to-action:
47
+
48
+ ```html
49
+ <button class="btn btn-danger btn-sm" data-toggle="modal" data-target="#myModal">
50
+ Delete account
51
+ </button>
52
+ <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
53
+ <div class="modal-dialog modal-sm">
54
+ <div class="modal-content">
55
+ <div class="modal-header">
56
+ <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
57
+ <h4 class="modal-title" id="mySmallModalLabel">Confirm your action</h4>
58
+ </div>
59
+ <div class="modal-body">
60
+ You are about to delete your account. Would you like to proceed?
61
+ </div>
62
+ <div class="modal-footer">
63
+ <button class="btn btn-danger">Yes, delete my account</button>
64
+ </div>
65
+ </div>
66
+ </div>
67
+ </div>
68
+ ```
69
+
70
+ Panels
71
+ ======
72
+
73
+ Use the `panel` helper to insert any type of
74
+ [panel](http://getbootstrap.com/components/#panels) provided by Bootstrap.
75
+
76
+ Basic panels
77
+ ------------
78
+
79
+ ```erb
80
+ <%= panel 'You have the right to remain silent...' %>
81
+ ```
82
+
83
+ will generate the HTML to render a basic panel:
84
+
85
+ ```html
86
+ <div class="panel panel-default">
87
+ <div class="panel-body">
88
+ You have the right to remain silent...
89
+ </div>
90
+ </div>
91
+ ```
92
+
93
+ Panels with title
94
+ -----------------
95
+
96
+ ```erb
97
+ <%= panel 'You have the right to remain silent...', title: 'Your rights' %>
98
+ ```
99
+
100
+ will generate the HTML to render a panel with a simple title header:
101
+
102
+ ```html
103
+ <div class="panel panel-default">
104
+ <div class="panel-heading">
105
+ <h2 class="panel-title">Your rights</h2>
106
+ </div>
107
+ <div class="panel-body">
108
+ You have the right to remain silent...
109
+ </div>
110
+ </div>
111
+ ```
112
+
113
+ Body and title can be passed as blocks if they are more than plain text:
114
+
115
+ ```erb
116
+ <%= panel do %>
117
+ <strong>Hey!</strong> You have the right to remain silent...
118
+ <%= title tag: :h3, do %>
119
+ <strong>Your rights</strong> (from our <%= link_to '/terms', 'Terms of service' %>)
120
+ <% end %>
121
+ <% end %>
122
+ ```
123
+
124
+ will generate
125
+
126
+ ```html
127
+ <div class="panel panel-info">
128
+ <div class="panel-heading">
129
+ <h3 class="panel-title">
130
+ <strong>Your rights</strong> (from our <a href="/terms">Terms of service</a>)
131
+ </h2>
132
+ </div>
133
+ <div class="panel-body">
134
+ <strong>Hey!</strong> You have the right to remain silent...
135
+ </div>
136
+ </div>
137
+ ```
138
+
139
+ Contextual panels
140
+ -----------------
141
+
142
+ ```erb
143
+ <%= panel 'You have the right to remain silent...', title: 'Your rights', context: :warning %>
144
+ ```
145
+
146
+ will generate the HTML to render a "warning" (orange border) panel:
147
+
148
+ ```html
149
+ <div class="panel panel-warning">
150
+ <div class="panel-heading">
151
+ <h2 class="panel-title">
152
+ Your rights
153
+ </h2>
154
+ </div>
155
+ <div class="panel-body">
156
+ You have the right to remain silent...
157
+ </div>
158
+ </div>
159
+ ```
160
+
161
+ Panels with footer
162
+ ------------------
163
+
164
+ ```erb
165
+ <%= panel do %>
166
+ You have the right to remain silent...
167
+ <%= footer do %>
168
+ <button class="btn btn-primary">Accept</button>
169
+ <% end %>
170
+ <% end %>
171
+ ```
172
+
173
+ will generate the HTML to render a panel with a button in the footer:
174
+
175
+ ```html
176
+ <div class="panel panel-default">
177
+ <div class="panel-body">
178
+ You have the right to remain silent...
179
+ </div>
180
+ <div class="panel-footer">
181
+ <button class="btn btn-primary">Accept</button>
182
+ </div>
183
+ </div>
184
+ ```
185
+
186
+ <!--
187
+ TODO
188
+ Both panel and modal should accept a title to override the header, or a header,
189
+ or none of them and then they should not have the header component at all.
190
+ For the size, though, they are different.
191
+ -->
192
+
193
+ Navbar
194
+ ======
195
+
196
+ Basic navbar
197
+ ------------
198
+
199
+ ```erb
200
+ <%= navbar do %>
201
+ <%= nav do %>
202
+ <%= link_to 'Posts', '/posts' %>
203
+ <%= link_to 'Users', '/users' %>
204
+ <% end %>
205
+ <% end %>
206
+ ```
207
+
208
+ will generate the HTML to render an default navbar with two links:
209
+
210
+ ```html
211
+ <nav class="navbar" role="navigation">
212
+ <div class="container">
213
+ <div class="navbar-header">
214
+ <button class="navbar-toggle" data-target="#bs-example-navbar-collapse-1" data-toggle="collapse" type="button">
215
+ <span class="sr-only">Toggle navigation</span>
216
+ <span class="icon-bar"></span>
217
+ <span class="icon-bar"></span>
218
+ <span class="icon-bar"></span>
219
+ </button>
220
+ </div>
221
+ <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
222
+ <ul class="nav navbar-nav">
223
+ <li><a href="/posts">Posts</a></li>
224
+ <li><a href="/users">Users</a></li>
225
+ </ul>
226
+ </div>
227
+ </div>
228
+ </nav>
229
+ ```
230
+
231
+
232
+ Complex navbar
233
+ --------------
234
+
235
+ <%= navbar fixed: :top, inverted: true do %>
236
+ <%= brand do %>
237
+ <%= link_to image_tag('logo.png', alt: 'Company Name'), '/' %>
238
+ <% end %>
239
+ <%= nav do %>
240
+ <%= link_to 'Posts', '/posts' %>
241
+ <%= link_to 'Users', '/users' %>
242
+ <% end %>
243
+ <%= nav align: :right do %>
244
+ <%= link_to 'Log in', '/login' %>
245
+ <% end %>
246
+ <% end %>
247
+
248
+ <!-- NOTE: to align a form to the right, it's not necessary to put it inside
249
+ a "nav align: :right", it's enough to have the form directly, but form_for
250
+ should be smart enough to add the 'navbar-form' class for better alignment -->
251
+
252
+ <style>body {padding-top: 70px}</style>
253
+ <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
254
+ <div class="container">
255
+ <div class="navbar-header">
256
+ <button class="navbar-toggle" data-target="#bs-example-navbar-collapse-1" data-toggle="collapse" type="button">
257
+ <span class="sr-only">Toggle navigation</span>
258
+ <span class="icon-bar"></span>
259
+ <span class="icon-bar"></span>
260
+ <span class="icon-bar"></span>
261
+ </button>
262
+ <a href="/" class="navbar-brand">
263
+ <img alt="Company Name" src="/logo.png">
264
+ </a>
265
+ </div>
266
+ <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
267
+ <ul class="nav navbar-nav">
268
+ <li class="active"><a href="/posts">Posts</a></li>
269
+ <li><a href="/users">Users</a></li>
270
+ </ul>
271
+ <ul class="nav navbar-nav navbar-right">
272
+ <li><a href="/login">Log in</a></li>
273
+ </ul>
274
+ </div>
275
+ </div>
276
+ </nav>
277
+
278
+ Forms
279
+ =====
280
+
281
+ <%= form_for %>
282
+ <%= button_to %>
283
+ <%= link_to %>
284
+
285
+ Buttons
286
+ =======
287
+
288
+ <!-- TODO: explain that button_to is smarter, same for form_for, link_to
289
+ they add extra-classes based on the context -->
290
+
291
+ Viewport meta tag
292
+ =================
293
+
294
+ <%= viewport_meta_tag %>
295
+
296
+ generates
297
+
298
+ <meta name="viewport" content="width=device-width, initial-scale=1">
data/bh.gemspec CHANGED
@@ -6,16 +6,28 @@ require 'bh/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "bh"
8
8
  spec.version = Bh::VERSION
9
- spec.authors = ["claudiob"]
10
- spec.email = ["claudiob@gmail.com"]
11
- spec.description = %q{Bootstrap Helpers}
12
- spec.summary = %q{Bootstrap Helpers for Rails}
13
- spec.homepage = "http://github.com/claudiob/bh"
9
+ spec.authors = ["Claudio Baccigalupo"]
10
+ spec.email = ["claudio@fullscreen.net"]
11
+ spec.description = %q{Bh - Bootstrap Helpers}
12
+ spec.summary = %q{Bh provides a set of powerful helpers that
13
+ streamlines the use of Bootstrap components in Rails views.}
14
+ spec.homepage = "http://github.com/Fullscreen/bh"
14
15
  spec.license = "MIT"
15
16
 
17
+ spec.required_ruby_version = '>= 1.9.3'
18
+
16
19
  spec.files = `git ls-files`.split($/)
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
21
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
22
  spec.require_paths = ["lib"]
19
23
 
20
- spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_dependency 'activesupport'
25
+ spec.add_dependency 'actionpack'
26
+
27
+ # For development / Code coverage / Documentation
28
+ spec.add_development_dependency 'bundler' #, '~> 1.0'
29
+ spec.add_development_dependency 'rspec' #, '~> 2.0'
30
+ spec.add_development_dependency 'rake' #, '~> 10.0'
31
+ spec.add_development_dependency 'yard' #, '~> 0.8.0'
32
+ spec.add_development_dependency 'coveralls' #, '~> 0.7.0'
21
33
  end
data/lib/bh.rb CHANGED
@@ -1,3 +1,5 @@
1
- module Bh
1
+ require 'bh/railtie' if defined?(Rails)
2
2
 
3
+ # Adds Bootstrap styles to Rails helpers
4
+ module Bh
3
5
  end
@@ -0,0 +1,83 @@
1
+ require 'action_view'
2
+
3
+ module Bh
4
+ module AlertHelper
5
+ include ActionView::Helpers::TagHelper # for content_tag
6
+ include ActionView::Context # for capture
7
+ include ActionView::Helpers::OutputSafetyHelper # for safe_join
8
+ # Returns an HTML block tag that follows the Bootstrap documentation
9
+ # on how to display *alert boxes*.
10
+ # Alert boxes provide contextual feedback messages for typical user
11
+ # actions with the handful of available and flexible alert messages.
12
+ # @see http://getbootstrap.com/components/#alerts
13
+ #
14
+ # The message to display in the alert can either be passed as the first
15
+ # parameter (in which case, the options are the second parameter), or as
16
+ # a block (in which case, the options are the first paramter).
17
+ # @example An alert with a plain-text message passed as the first parameter.
18
+ # alert 'User updated successfully', dismissible: true
19
+ # @example An alert with an HTML message passed as a block.
20
+ # alert_box dismissible: true do
21
+ # <strong>Hooray!</strong> User updated successfully
22
+ # end
23
+ #
24
+ # @return [String] an HTML block tag for an alert.
25
+ # @param [String] message_or_options_with_block the message to display in
26
+ # the alert.
27
+ # @param [Hash] options the options for the alert box.
28
+ # @option options [Boolean] :dismissible whether to display an '×' to the
29
+ # right of the alert than can be clicked to dismiss the alert.
30
+ # @option options [Symbol] :context the contextual class to apply to the
31
+ # alert depending on its importance. Can be :success, :info, :warning,
32
+ # or :danger. Defaults to :info.
33
+ # @option options [Symbol] :priority if the alert box is used to show a
34
+ # Rails flash message, the priority of the message. Can be :alert
35
+ # or :notice.
36
+ def alert_box(message_or_options_with_block = nil, options = nil, &block)
37
+ if block_given?
38
+ alert_string capture_alert(&block), message_or_options_with_block || {}
39
+ else
40
+ alert_string message_or_options_with_block, options || {}
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def alert_string(message = nil, options = {})
47
+ dismissible = options[:dismissible] || options[:priority]
48
+ message = add_dismiss_button_to(message) if dismissible
49
+ klass = alert_class options.fetch(:context, options[:priority])
50
+ content_tag :div, message, role: :alert, class: klass
51
+ end
52
+
53
+ def alert_class(context = nil)
54
+ context = case context.to_s
55
+ when 'success', 'notice' then :success
56
+ when 'warning' then :warning
57
+ when 'danger', 'alert' then :danger
58
+ else 'info'
59
+ end
60
+ "alert alert-#{context}"
61
+ end
62
+
63
+ def add_dismiss_button_to(message)
64
+ options = {type: 'button', class: 'close', :'data-dismiss' => 'alert'}
65
+ dismiss_button = content_tag :button, options do
66
+ safe_join [
67
+ content_tag(:span, '&times;'.html_safe, :'aria-hidden' => true),
68
+ content_tag(:span, 'Close', class: 'sr-only'),
69
+ ], ''
70
+ end
71
+ safe_join [dismiss_button, message], "\n"
72
+ end
73
+
74
+ # Captures the message passed as a block and sets a variable so that
75
+ # every +link_to+ helper called inside the block gets the "alert-link"
76
+ # class added, for links to look better.
77
+ # @see http://getbootstrap.com/components/#alerts-links
78
+ def capture_alert(&block)
79
+ @alert_link = true
80
+ capture(&block).tap{ @alert_link = false }
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,24 @@
1
+ require 'action_view'
2
+
3
+ module Bh
4
+ module UrlHelper
5
+ include ActionView::Helpers::UrlHelper # for link_to
6
+ # Overrides ActionView +link_to+ to be able to add the 'alert_link' class
7
+ # to the link in case the link is inside of an alert.
8
+ # @see http://getbootstrap.com/components/#alerts-links
9
+ def link_to(*args, &block)
10
+ args = add_alert_class_to_link!(*args, &block) if @alert_link
11
+ super *args, &block
12
+ end
13
+
14
+ private
15
+
16
+ def add_alert_class_to_link!(*args, &block)
17
+ html_options = (block_given? ? args[1] : args[2]) || {}
18
+ klass = html_options[:class]
19
+ html_options[:class] = [klass, 'alert-link'].compact.join ' '
20
+ block_given? ? args[1] = html_options : args[2] = html_options
21
+ args
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,11 @@
1
+ require 'bh/helpers/alert_helper'
2
+ require 'bh/helpers/url_helper'
3
+
4
+ module Bh
5
+ class Railtie < Rails::Railtie
6
+ initializer 'bh.add_helpers' do
7
+ ActionView::Base.send :include, AlertHelper
8
+ ActionView::Base.send :include, UrlHelper
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module Bh
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+ require 'bh/helpers/alert_helper'
3
+ require 'bh/helpers/url_helper'
4
+ include Bh::AlertHelper
5
+ include Bh::UrlHelper
6
+
7
+ describe 'alert_box' do
8
+ describe 'accepts as parameters:' do
9
+ let(:behave) { be_a String }
10
+
11
+ specify 'a string (message)' do
12
+ expect(alert_box 'message').to behave
13
+ end
14
+
15
+ specify 'a block (message)' do
16
+ expect(alert_box { 'message' }).to behave
17
+ end
18
+
19
+ specify 'a string (message) + a hash (options)' do
20
+ expect(alert_box 'message', context: :danger).to behave
21
+ end
22
+
23
+ specify 'a hash (options) + a block (message)' do
24
+ expect(alert_box(context: :danger) { 'message' }).to behave
25
+ end
26
+ end
27
+
28
+ describe 'with the :dismissible option' do
29
+ specify 'not set, does not show an × to dismiss the alert' do
30
+ expect(alert_box 'message').not_to include '&times;'
31
+ end
32
+
33
+ specify 'set to false, does not show an × to dismiss the alert' do
34
+ expect(alert_box 'message', dismissible: false).not_to include '&times;'
35
+ end
36
+
37
+ specify 'set to true, does not show an × to dismiss the alert' do
38
+ expect(alert_box 'message', dismissible: true).to include '&times;'
39
+ end
40
+ end
41
+
42
+ describe 'with the :context option' do
43
+ specify 'set to :success, shows a "success" alert' do
44
+ expect(alert_box 'message', context: :success).to include 'alert-success'
45
+ end
46
+
47
+ specify 'set to :info, shows a "info" alert' do
48
+ expect(alert_box 'message', context: :info).to include 'alert-info'
49
+ end
50
+
51
+ specify 'set to :warning, shows a "warning" alert' do
52
+ expect(alert_box 'message', context: :warning).to include 'alert-warning'
53
+ end
54
+
55
+ specify 'set to :danger, shows a "danger" alert' do
56
+ expect(alert_box 'message', context: :danger).to include 'alert-danger'
57
+ end
58
+
59
+ specify 'set to any other value, shows an "info" alert' do
60
+ expect(alert_box 'message', context: :unknown).to include 'alert-info'
61
+ end
62
+
63
+ specify 'not set, shows an "info" alert' do
64
+ expect(alert_box 'message').to include 'alert-info'
65
+ end
66
+ end
67
+
68
+ describe 'with the :priority option' do
69
+ specify 'shows a dismissible alert' do
70
+ expect(alert_box 'message', priority: :notice).to include '&times;'
71
+ end
72
+
73
+ specify 'set to :notice, shows a "success" alert' do
74
+ expect(alert_box 'message', priority: :notice).to include 'alert-success'
75
+ end
76
+
77
+ specify 'set to :alert, shows a "danger" alert' do
78
+ expect(alert_box 'message', priority: :alert).to include 'alert-danger'
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ require 'bh/helpers/alert_helper'
3
+ require 'bh/helpers/url_helper'
4
+ include Bh::AlertHelper
5
+ include Bh::UrlHelper
6
+
7
+ describe 'link_to' do
8
+ context 'used without a block' do
9
+ let(:link) { link_to 'Home', '/' }
10
+
11
+ specify 'does not apply the "alert_link" class if used outside of an alert' do
12
+ expect(link).not_to include 'alert-link'
13
+ end
14
+
15
+ specify 'applies the "alert_link" class if used inside an alert' do
16
+ expect(alert_box { link }).to include 'alert-link'
17
+ end
18
+ end
19
+
20
+ context 'used with a block' do
21
+ let(:link) { link_to('/') { 'Home' } }
22
+
23
+ specify 'does not apply the "alert_link" class if used outside of an alert' do
24
+ expect(link).not_to include 'alert-link'
25
+ end
26
+
27
+ specify 'applies the "alert_link" class if used inside an alert' do
28
+ expect(alert_box { link }).to include 'alert-link'
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,15 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ SimpleCov::Formatter::HTMLFormatter,
6
+ Coveralls::SimpleCov::Formatter
7
+ ]
8
+ SimpleCov.start
9
+
10
+ Dir['./spec/support/**/*.rb'].each {|f| require f}
11
+
12
+ RSpec.configure do |config|
13
+ config.order = 'random'
14
+ config.run_all_when_everything_filtered = false
15
+ end
metadata CHANGED
@@ -1,46 +1,138 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
- - claudiob
7
+ - Claudio Baccigalupo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-24 00:00:00.000000000 Z
11
+ date: 2014-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: actionpack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
13
69
  - !ruby/object:Gem::Dependency
14
70
  name: rake
15
71
  requirement: !ruby/object:Gem::Requirement
16
72
  requirements:
17
- - - "~>"
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: yard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
18
88
  - !ruby/object:Gem::Version
19
- version: '10.0'
89
+ version: '0'
20
90
  type: :development
21
91
  prerelease: false
22
92
  version_requirements: !ruby/object:Gem::Requirement
23
93
  requirements:
24
- - - "~>"
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: coveralls
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
25
102
  - !ruby/object:Gem::Version
26
- version: '10.0'
27
- description: Bootstrap Helpers
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Bh - Bootstrap Helpers
28
112
  email:
29
- - claudiob@gmail.com
113
+ - claudio@fullscreen.net
30
114
  executables: []
31
115
  extensions: []
32
116
  extra_rdoc_files: []
33
117
  files:
34
118
  - ".gitignore"
119
+ - ".rspec"
120
+ - CHANGELOG.md
35
121
  - Gemfile
36
- - HISTORY.md
37
- - LICENSE.txt
122
+ - MIT-LICENSE
38
123
  - README.md
39
124
  - Rakefile
125
+ - TODO.md
40
126
  - bh.gemspec
41
127
  - lib/bh.rb
128
+ - lib/bh/helpers/alert_helper.rb
129
+ - lib/bh/helpers/url_helper.rb
130
+ - lib/bh/railtie.rb
42
131
  - lib/bh/version.rb
43
- homepage: http://github.com/claudiob/bh
132
+ - spec/helpers/alert_helper_spec.rb
133
+ - spec/helpers/url_helper_spec.rb
134
+ - spec/spec_helper.rb
135
+ homepage: http://github.com/Fullscreen/bh
44
136
  licenses:
45
137
  - MIT
46
138
  metadata: {}
@@ -52,7 +144,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
52
144
  requirements:
53
145
  - - ">="
54
146
  - !ruby/object:Gem::Version
55
- version: '0'
147
+ version: 1.9.3
56
148
  required_rubygems_version: !ruby/object:Gem::Requirement
57
149
  requirements:
58
150
  - - ">="
@@ -63,5 +155,10 @@ rubyforge_project:
63
155
  rubygems_version: 2.2.2
64
156
  signing_key:
65
157
  specification_version: 4
66
- summary: Bootstrap Helpers for Rails
67
- test_files: []
158
+ summary: Bh provides a set of powerful helpers that streamlines the use of Bootstrap
159
+ components in Rails views.
160
+ test_files:
161
+ - spec/helpers/alert_helper_spec.rb
162
+ - spec/helpers/url_helper_spec.rb
163
+ - spec/spec_helper.rb
164
+ has_rdoc:
data/HISTORY.md DELETED
@@ -1 +0,0 @@
1
- v0.0.1 - 2014/06/23 Scaffold gem