rails-bootstrap-helpers 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +15 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +158 -0
  4. data/Rakefile +38 -0
  5. data/lib/rails-bootstrap-helpers.rb +20 -0
  6. data/lib/rails-bootstrap-helpers/core_ext/abstract.rb +72 -0
  7. data/lib/rails-bootstrap-helpers/helpers/alert_helper.rb +45 -0
  8. data/lib/rails-bootstrap-helpers/helpers/base_helper.rb +32 -0
  9. data/lib/rails-bootstrap-helpers/helpers/button_helper.rb +55 -0
  10. data/lib/rails-bootstrap-helpers/helpers/form_tag_helper.rb +22 -0
  11. data/lib/rails-bootstrap-helpers/helpers/label_helper.rb +35 -0
  12. data/lib/rails-bootstrap-helpers/helpers/options_helper.rb +32 -0
  13. data/lib/rails-bootstrap-helpers/rails/engine.rb +15 -0
  14. data/lib/rails-bootstrap-helpers/renderers/abstract_button_renderer.rb +86 -0
  15. data/lib/rails-bootstrap-helpers/renderers/button_renderer.rb +43 -0
  16. data/lib/rails-bootstrap-helpers/renderers/renderer.rb +14 -0
  17. data/lib/rails-bootstrap-helpers/version.rb +3 -0
  18. data/lib/tasks/bootstrap-rails-helpers_tasks.rake +4 -0
  19. data/spec/dummy/README.rdoc +261 -0
  20. data/spec/dummy/Rakefile +7 -0
  21. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  22. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  23. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  24. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  25. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  26. data/spec/dummy/config.ru +4 -0
  27. data/spec/dummy/config/application.rb +59 -0
  28. data/spec/dummy/config/boot.rb +10 -0
  29. data/spec/dummy/config/database.yml +25 -0
  30. data/spec/dummy/config/environment.rb +5 -0
  31. data/spec/dummy/config/environments/development.rb +37 -0
  32. data/spec/dummy/config/environments/production.rb +67 -0
  33. data/spec/dummy/config/environments/test.rb +37 -0
  34. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  35. data/spec/dummy/config/initializers/inflections.rb +15 -0
  36. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  37. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  38. data/spec/dummy/config/initializers/session_store.rb +8 -0
  39. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  40. data/spec/dummy/config/locales/en.yml +5 -0
  41. data/spec/dummy/config/routes.rb +58 -0
  42. data/spec/dummy/db/test.sqlite3 +0 -0
  43. data/spec/dummy/log/test.log +47 -0
  44. data/spec/dummy/public/404.html +26 -0
  45. data/spec/dummy/public/422.html +26 -0
  46. data/spec/dummy/public/500.html +25 -0
  47. data/spec/dummy/public/favicon.ico +0 -0
  48. data/spec/dummy/script/rails +6 -0
  49. data/spec/helpers/alert_helper_spec.rb +24 -0
  50. data/spec/helpers/base_helper_spec.rb +18 -0
  51. data/spec/helpers/button_helper_spec.rb +94 -0
  52. data/spec/helpers/form_tag_helper_spec.rb +43 -0
  53. data/spec/helpers/label_helper_spec.rb +23 -0
  54. data/spec/helpers/options_helper_spec.rb +47 -0
  55. data/spec/spec_helper.rb +20 -0
  56. data/spec/support/matchers/helpers/alert_helper/render_bs_alert.rb +116 -0
  57. data/spec/support/matchers/helpers/base_helper/render_icon.rb +55 -0
  58. data/spec/support/matchers/helpers/button_helper/render_bs_button_to.rb +135 -0
  59. data/spec/support/matchers/helpers/button_helper/render_inline_button_to.rb +79 -0
  60. data/spec/support/matchers/helpers/form_tag_helper/render_bs_button_tag.rb +136 -0
  61. data/spec/support/matchers/helpers/label_helper/render_bs_label.rb +114 -0
  62. metadata +287 -0
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MTExOWU1ODk5MGFiM2Y1Mjk4NGZlNDVlNGYzOTMzYjkzZGEyMWJlNQ==
5
+ data.tar.gz: !binary |-
6
+ MGM1ZThmNjhkZGJkNTA4OGQzMDQ5MjAyNTAyYjgyMDMzN2RlYjM0ZA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ YzA0YTY2NTIwZjE1NjUwMTM2Y2M4MWE0ZjUyN2FjN2Q1NTdmYTQ4MzdmZDI3
10
+ MjA0MzQzNjc0MTM4ZGUxMGJmNTJhM2VkMjhjNTQxNzJkY2IxMTljZWU5Nzhj
11
+ NThlMmRlYTdjNDIzZTU2YTlmNDlmMmM2ZTcyYWRiZGFkYjM4ZTI=
12
+ data.tar.gz: !binary |-
13
+ YWM1ZGU1NjZkMGZlYzczNGE1NmE4N2JhYmFjNjY0ZDU4YjJjZDQxNjI2Mzc0
14
+ MDc2MDk4ZGI0YmNhMzU1NmVlNGJjYWM4NmZiNTA2YzgzM2IzYzFkMDE5MjIw
15
+ N2Q4NzY2ZDZiNjMzYjc0YzE2NDg5ZmRlYmJiNmNhNDhhODc1NDk=
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,158 @@
1
+ # Rails Bootstrap Helpers
2
+
3
+ Rails Bootstrap Helpers is a plugin for Ruby on Rails that adds view helpers for
4
+ [Bootstrap](http://twitter.github.io/bootstrap/).
5
+
6
+ ## Installation
7
+
8
+ Add it to your Gemfile:
9
+
10
+ ```ruby
11
+ gem "rails-bootstrap-helpers"
12
+ ```
13
+
14
+ Manually include the necessary stylesheets and JavaScript files from Bootstrap.
15
+
16
+ Although it has no direct dependencies on other gems than Rails, it is necessary
17
+ to include Bootstrap in some way or another to make this gem useful.
18
+ [bootstrap-sass](https://github.com/thomas-mcdonald/bootstrap-sass) is
19
+ recommended.
20
+
21
+ ### JavaScript
22
+
23
+ Some of the helpers uses features of Bootstrap that requires JavaScript to be
24
+ initialized. You need to manually do this initialization. The following helpers
25
+ uses JavaScript that needs manually initialization:
26
+
27
+ * Any helper with the `:tooltip` option
28
+ * [bs\_popover\_button](#bs_popover_button)
29
+
30
+ For which JavaScript file to include, follow the
31
+ [Bootstrap documentation](http://twitter.github.io/bootstrap/javascript.html).
32
+
33
+ ## Usage
34
+
35
+ ### <a id="common"></a>Common
36
+
37
+ #### <a id="icon"></a>icon
38
+
39
+ ```erb
40
+ <%= icon :edit %> # renders an icon with the icon-edit icon
41
+ <%= icon :edit, invert: true %> # inverts the color of the icon, making it white
42
+ ```
43
+
44
+ [Bootstrap documentation](http://twitter.github.io/bootstrap/base-css.html#icons)
45
+
46
+ ### <a id="alerts"></a>Alerts
47
+
48
+ #### <a id="bs_alert"></a>bs\_alert
49
+ ```erb
50
+ <%= bs_alert "foo" %> # standard alert box
51
+ <%= bs_alert "foo", block: true %> # alert box with block style
52
+ <%= bs_alert "foo", dismiss_button: true %> # alert box with a dismiss button
53
+ ```
54
+
55
+ [Bootstrap documentation](http://twitter.github.io/bootstrap/components.html#alerts)
56
+
57
+ ### <a id="buttons"></a>Buttons
58
+
59
+ #### <a id="bs_button_to"></a>bs\_button\_to
60
+
61
+ ```erb
62
+ <%= bs_button_to "google", "http://www.google.se" %>
63
+ <%= bs_button_to "google", "http://www.google.se", style: "success" %>
64
+ <%= bs_button_to "google", "http://www.google.se", disabled: true %>
65
+ <%= bs_button_to "google", "http://www.google.se", icon: "edit" %>
66
+ <%= bs_button_to "google", "http://www.google.se", icon_position: "left" %>
67
+ <%= bs_button_to "google", "http://www.google.se", icon_invert: "left" %>
68
+ ```
69
+
70
+ The `bs_button_to` helper renders an `a` tag, styled as a Bootstrap button. It's
71
+ basically a wrapper around the `link_to` helper. In addition all the standard
72
+ arguments and options that `link_to` accepts it also accepts the above options.
73
+
74
+ #### <a id="bs_inline_button_to"></a>bs\_inline\_button\_to
75
+
76
+ ```erb
77
+ <%= bs_inline_button_to "http://www.google.se", :edit %>
78
+ ```
79
+
80
+ The `bs_inline_button_to` helper renders an `a` tag, styled as a inline
81
+ Bootstrap button. That is, a button with the an icon (no text) and the size
82
+ "mini". Except from that it accepts all options as the [bs\_button\_to](#bs_button_to) does.
83
+
84
+ #### <a id="bs_popover_button"></a>bs\_popover\_button
85
+
86
+ ```erb
87
+ <%= bs_popover_button "foo", "bar"
88
+ <%= bs_popover_button "foo", "bar", placement: "right" %>
89
+ <%= bs_popover_button "foo" do %>
90
+ <%= link_to "Google", "http://www.google.se" %>
91
+ <% end %>
92
+ ```
93
+
94
+ Renders a Bootstrap button that when clicked opens a popover. The content of the
95
+ popover can either be supplied as the second argument or as a block.
96
+
97
+ **Note:** this helper requires JavaScript to be manually initialized. Add the
98
+ following code to your JavaScript file:
99
+
100
+ ````javascript
101
+ $("[data-toggle=popover]").popover(html: true)
102
+ // The "html" option tells the plugin to not escape HTML. Useful when rendering
103
+ // the popover content using a block.
104
+ ```
105
+
106
+ [Bootstrap documentation](http://twitter.github.io/bootstrap/base-css.html#buttons)
107
+
108
+ ### <a id="forms"></a>Forms
109
+
110
+ #### <a id="bs_button_tag"></a> bs\_button\_tag
111
+
112
+ ```erb
113
+ <%= bs_button_to "google", :submit %>
114
+ ```
115
+
116
+ Renders an `button` tag styled as a Bootstrap button. First argument is the text
117
+ to be rendered on the button, the other is what type of button (that is, the HTML
118
+ attribute `type`). Accepts all the options as [bs\_button\_to](#bs_button_to) does.
119
+
120
+ [Bootstrap documentation](http://twitter.github.io/bootstrap/base-css.html#buttons)
121
+
122
+ ### <a id="labels"></a>Labels
123
+
124
+ #### <a id="bs_label"></a>bs\_label
125
+
126
+ ```erb
127
+ <%= bs_label "foo" # standard label%>
128
+ <%= bs_label "foo", style: "success" # styled label %>
129
+ ```
130
+
131
+ ### <a id="tooltips"></a>Tooltips
132
+
133
+ ```erb
134
+ <%= bs_label "foo", tooltip: "bar" %>
135
+ ```
136
+
137
+ Basically any helper accepts the `:tooltip` option. This will add a Bootstrap
138
+ tooltip to the rendered component.
139
+
140
+ **Note:** this option requires JavaScript to be manually initialized. Add the
141
+ following code to your JavaScript file:
142
+
143
+ ````javascript
144
+ $("[data-toggle=tooltip]").tooltip()
145
+ ```
146
+
147
+ [Bootstrap documentation](http://twitter.github.io/bootstrap/components.html#labels-badges)
148
+
149
+ ## Tests
150
+
151
+ Run the tests using RSpec
152
+
153
+ $ bundle install
154
+ $ bundle exec rspec
155
+
156
+ ## License
157
+
158
+ Rails Bootstrap Helpers is licensed under [The MIT license](http://opensource.org/licenses/MIT)
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'RailsBootstrapHelpers'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ require 'rake/testtask'
29
+
30
+ Rake::TestTask.new(:test) do |t|
31
+ t.libs << 'lib'
32
+ t.libs << 'test'
33
+ t.pattern = 'test/**/*_test.rb'
34
+ t.verbose = false
35
+ end
36
+
37
+
38
+ task :default => :test
@@ -0,0 +1,20 @@
1
+ require "rails-bootstrap-helpers/core_ext/abstract"
2
+
3
+ module RailsBootstrapHelpers
4
+ module Renderers
5
+ autoload :AbstractButtonRenderer, "rails-bootstrap-helpers/renderers/abstract_button_renderer"
6
+ autoload :ButtonRenderer, "rails-bootstrap-helpers/renderers/button_renderer"
7
+ autoload :Renderer, "rails-bootstrap-helpers/renderers/renderer"
8
+ end
9
+
10
+ module Helpers
11
+ autoload :AlertHelper, "rails-bootstrap-helpers/helpers/alert_helper"
12
+ autoload :BaseHelper, "rails-bootstrap-helpers/helpers/base_helper"
13
+ autoload :ButtonHelper, "rails-bootstrap-helpers/helpers/button_helper"
14
+ autoload :FormTagHelper, "rails-bootstrap-helpers/helpers/form_tag_helper"
15
+ autoload :LabelHelper, "rails-bootstrap-helpers/helpers/label_helper"
16
+ autoload :OptionsHelper, "rails-bootstrap-helpers/helpers/options_helper"
17
+ end
18
+ end
19
+
20
+ require "rails-bootstrap-helpers/rails/engine"
@@ -0,0 +1,72 @@
1
+ #
2
+ class AbstractError < Exception; end
3
+
4
+ # Class method that marks one or several methods as abstract, or a whole
5
+ # class. If an abstract method is called an AbstractError will be thrown.
6
+ #
7
+ # Note: when making a whole class abstract the call to "abstract" must come
8
+ # after any constructors.
9
+ #
10
+ # ==== Parameters
11
+ # @param args [Array<String, Symbol>] A list of methods that should be abstract.
12
+ # If the list is empty the class that called the method will be made
13
+ # abstract instead.
14
+ #
15
+ # ==== Examples
16
+ #
17
+ # class Foo
18
+ # abstract
19
+ # end
20
+ #
21
+ # class Bar < Foo
22
+ # end
23
+ #
24
+ # Foo.new
25
+ # # => AbstractError: Cannot instantiate abstract class Foo.
26
+ #
27
+ # Bar.new
28
+ # # => #<Bar:0x100123a30>
29
+ #
30
+ # class Base
31
+ # abstract :foo
32
+ # end
33
+ #
34
+ # class Sub < Base
35
+ # end
36
+ #
37
+ # class Foo < Base
38
+ # def foo
39
+ # 3
40
+ # end
41
+ # end
42
+ #
43
+ # Sub.new.foo
44
+ # # => AbstractError: Unimplemented abstract method foo.
45
+ #
46
+ # Foo.new.foo
47
+ # # => 3
48
+ def abstract(*args)
49
+ if args.length == 0
50
+ class_eval do
51
+ alias __abstract_initialize__ initialize
52
+
53
+ def initialize (*params, &block)
54
+ raise AbstractError.new("Cannot instantiate abstract class #{self.class.name}.")
55
+ end
56
+
57
+ def self.inherited (subclass)
58
+ subclass.send(:define_method, :initialize) do |*args|
59
+ __abstract_initialize__ *args
60
+ end
61
+ end
62
+ end
63
+ else
64
+ class_eval do
65
+ args.each do |name|
66
+ define_method name do |*params, &block|
67
+ raise AbstractError.new("Unimplemented abstract method #{name}.")
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,45 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ module RailsBootstrapHelpers::Helpers::AlertHelper
4
+ # Renders a Bootstrap alert with the given text.
5
+ #
6
+ # @param text [String] the text to render in the alert
7
+ #
8
+ # ==== Options
9
+ # @param :type [String] the type of alert to render
10
+ # @param :block [Boolean] indicates if the alert should render with block style
11
+ # @param :dismiss_button [Boolean] indicates if an dismiss button should be
12
+ # added to the alert
13
+ def bs_alert (text, options = {})
14
+ cls = "alert"
15
+
16
+ if type = options[:type]
17
+ type = type.to_s
18
+
19
+ if type == "notice"
20
+ type = "success"
21
+ end
22
+
23
+ unless type == "warning" || type == "default"
24
+ cls << " alert-#{type}"
25
+ end
26
+ end
27
+
28
+ if type = options[:block]
29
+ cls << " alert-block"
30
+ end
31
+
32
+ if dismiss_button = options[:dismiss_button]
33
+ content_tag :div, class: cls do
34
+ button = content_tag :button, "×",
35
+ type: "button",
36
+ class: "close",
37
+ :"data-dismiss" => "alert"
38
+
39
+ button + text
40
+ end
41
+ else
42
+ content_tag :div, text, class: cls
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,32 @@
1
+ module RailsBootstrapHelpers::Helpers::BaseHelper
2
+ # Renders the given icon
3
+ #
4
+ # Renders an <tt>i</tt> tag with the class "icon-#{icon}"
5
+ #
6
+ # @param icon [String, Symbol] the kind of icon to render
7
+ #
8
+ # ==== Options
9
+ # @param :invert [Boolean] if the color of the icon should be inverted
10
+ def self.icon (icon, options = {})
11
+ icon = ERB::Util.html_escape(icon.to_s)
12
+ cls = "icon-" + icon
13
+
14
+ if invert = options.delete(:invert)
15
+ cls << " icon-white"
16
+ end
17
+
18
+ "<i class=\"#{cls}\"></i>".html_safe
19
+ end
20
+
21
+ # Renders the given icon
22
+ #
23
+ # Renders an <tt>i</tt> tag with the class "icon-#{icon}"
24
+ #
25
+ # @param icon [String, Symbol] the kind of icon to render
26
+ #
27
+ # ==== Options
28
+ # @param :invert [Boolean] if the color of the icon should be inverted
29
+ def icon (icon, options = {})
30
+ RailsBootstrapHelpers::Helpers::BaseHelper.icon(icon, options)
31
+ end
32
+ end
@@ -0,0 +1,55 @@
1
+ module RailsBootstrapHelpers::Helpers::ButtonHelper
2
+ # Renders a Bootstrap button. This method behaves just as "link_to" but will
3
+ # render a Bootstrap button instead of a regular link. Note that this is still
4
+ # an "a" tag and not an "input" tag. In addition to the options "link_to"
5
+ # handles this method also handles the following options:
6
+ #
7
+ # ==== Options
8
+ # @param :style [String, Symbol] the style of the button
9
+ # @param :size ["large", "small", "mini"] the size of the button
10
+ # @param :disabled [Boolean] if the button should be disabled or not
11
+ # @param :icon [String] the name of an icon to render on the button
12
+ # @param :icon_position ["left", "right"] the position of the icon, if present
13
+ # @present :icon_invert [Boolean] if the color of the icon should be inverted
14
+ # or not
15
+ def bs_button_to (*args, &block)
16
+ RailsBootstrapHelpers::Renderers::ButtonRenderer.new(self, :link, *args, &block).render
17
+ end
18
+
19
+ # Renders an inline Bootstrap button. That is, a small button having only an
20
+ # icon and no text.
21
+ #
22
+ # @param url [String] the URL the button should link to
23
+ # @param icon [String] the icon of the button
24
+ # @param options [Hash] a hash of options. See bs_button_to
25
+ #
26
+ # @see #bs_button_to
27
+ def bs_inline_button_to (url, icon, options = {})
28
+ options = options.reverse_merge icon: icon, size: "mini"
29
+ RailsBootstrapHelpers::Renderers::ButtonRenderer.new(self, :link, nil, url, options).render
30
+ end
31
+
32
+ # Renders a Bootstrap button with a popover.
33
+ #
34
+ # @param name [String] the name/title of the button
35
+ # @param content_or_options [String, Hash] a hash of options if a block is
36
+ # passed, otherwise the content of the popover
37
+ #
38
+ # @param block [block] a block rendering the content of the popover
39
+ #
40
+ # ==== Options
41
+ # @param :placement [String, "bottom", "top", "left", "right"]
42
+ def bs_popover_button (name, content_or_options = nil, options = {}, &block)
43
+ if block_given?
44
+ bs_popover_button(name, capture(&block).gsub("\n", ""), content_or_options || {})
45
+ else
46
+ placement = options.delete(:placement) || "bottom"
47
+
48
+ options = options.reverse_merge :"data-content" => content_or_options,
49
+ :"data-toggle" => "popover",
50
+ :"data-placement" => placement
51
+
52
+ bs_button_to(name, '#', options)
53
+ end
54
+ end
55
+ end