rails-bootstrap-helpers 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/README.md +336 -14
  2. data/lib/rails-bootstrap-helpers.rb +14 -3
  3. data/lib/rails-bootstrap-helpers/helpers/accordion_helper.rb +8 -0
  4. data/lib/rails-bootstrap-helpers/helpers/alert_helper.rb +24 -14
  5. data/lib/rails-bootstrap-helpers/helpers/base_helper.rb +25 -13
  6. data/lib/rails-bootstrap-helpers/helpers/button_helper.rb +85 -13
  7. data/lib/rails-bootstrap-helpers/helpers/form_tag_helper.rb +42 -10
  8. data/lib/rails-bootstrap-helpers/helpers/navigation_helper.rb +17 -0
  9. data/lib/rails-bootstrap-helpers/helpers/options_helper.rb +24 -3
  10. data/lib/rails-bootstrap-helpers/helpers/tag_helper.rb +26 -0
  11. data/lib/rails-bootstrap-helpers/helpers/url_helper.rb +17 -0
  12. data/lib/rails-bootstrap-helpers/rails/engine.rb +4 -2
  13. data/lib/rails-bootstrap-helpers/renderers/{abstract_button_renderer.rb → abstract_link_renderer.rb} +23 -7
  14. data/lib/rails-bootstrap-helpers/renderers/accordion_renderer.rb +94 -0
  15. data/lib/rails-bootstrap-helpers/renderers/action_link_renderer.rb +19 -0
  16. data/lib/rails-bootstrap-helpers/renderers/button_renderer.rb +6 -4
  17. data/lib/rails-bootstrap-helpers/renderers/content_tag_renderer.rb +67 -0
  18. data/lib/rails-bootstrap-helpers/renderers/dropdown_button_renderer.rb +87 -0
  19. data/lib/rails-bootstrap-helpers/renderers/iconic_icon_renderer.rb +75 -0
  20. data/lib/rails-bootstrap-helpers/renderers/row_link_renderer.rb +12 -0
  21. data/lib/rails-bootstrap-helpers/renderers/tabbable_renderer.rb +186 -0
  22. data/lib/rails-bootstrap-helpers/version.rb +1 -1
  23. data/spec/dummy/log/test.log +296 -0
  24. data/spec/helpers/accordion_helper_spec.rb +35 -0
  25. data/spec/helpers/alert_helper_spec.rb +11 -7
  26. data/spec/helpers/base_helper_spec.rb +44 -0
  27. data/spec/helpers/button_helper_spec.rb +214 -9
  28. data/spec/helpers/form_tag_helper_spec.rb +27 -0
  29. data/spec/helpers/navigation_helper_spec.rb +228 -0
  30. data/spec/helpers/options_helper_spec.rb +50 -0
  31. data/spec/helpers/tag_helper_spec.rb +26 -0
  32. data/spec/helpers/url_helper_spec.rb +33 -0
  33. data/spec/spec_helper.rb +2 -0
  34. data/spec/support/html.rb +9 -0
  35. data/spec/support/matchers/helpers/alert_helper/render_bs_alert.rb +19 -10
  36. data/spec/support/matchers/helpers/base_helper/render_icon.rb +18 -1
  37. data/spec/support/matchers/helpers/base_helper/render_iconic_icon.rb +191 -0
  38. data/spec/support/matchers/helpers/button_helper/render_bs_button_to.rb +44 -3
  39. data/spec/support/matchers/helpers/button_helper/render_inline_button_to.rb +1 -1
  40. data/spec/support/matchers/helpers/form_tag_helper/render_bs_button_tag.rb +39 -1
  41. data/spec/support/matchers/helpers/form_tag_helper/render_bs_submit_tag.rb +96 -0
  42. data/spec/support/matchers/helpers/url_helper/render_action_link_to.rb +123 -0
  43. data/spec/support/matchers/helpers/url_helper/render_row_link_to.rb +97 -0
  44. metadata +59 -8
  45. checksums.yaml +0 -15
@@ -0,0 +1,8 @@
1
+ module RailsBootstrapHelpers::Helpers::AccordionHelper
2
+ # Renders a Bootstrap accordion.
3
+ #
4
+ # @param [String] an ID that is unique for the page
5
+ def accordion (id, &block)
6
+ RailsBootstrapHelpers::Renderers::AccordionRenderer.new(self, id, &block).render
7
+ end
8
+ end
@@ -5,31 +5,41 @@ module RailsBootstrapHelpers::Helpers::AlertHelper
5
5
  #
6
6
  # @param text [String] the text to render in the alert
7
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
8
+ # @option options [String] :style the style of alert to render
9
+ #
10
+ # @option options [Boolean] :block (false) indicates if the alert should
11
+ # render with block style
12
+ #
13
+ # @options options [Boolean] :dismiss_button (false) indicates if an dismiss
14
+ # button should be added to the alert
13
15
  def bs_alert (text, options = {})
16
+ options = options.deep_dup
14
17
  cls = "alert"
18
+ type = options.delete(:type)
15
19
 
16
- if type = options[:type]
17
- type = type.to_s
20
+ if type
21
+ ActiveSupport::Deprecation.warn "Usage of the option `:type` is deprecated. Please use the `:style` option instead"
22
+ end
18
23
 
19
- if type == "notice"
20
- type = "success"
24
+ if style = options.delete(:style) || type
25
+ style = style.to_s
26
+
27
+ if style == "notice"
28
+ style = "success"
21
29
  end
22
30
 
23
- unless type == "warning" || type == "default"
24
- cls << " alert-#{type}"
31
+ unless style == "warning" || style == "default"
32
+ cls << " alert-#{style}"
25
33
  end
26
34
  end
27
35
 
28
- if type = options[:block]
36
+ if style = options.delete(:block)
29
37
  cls << " alert-block"
30
38
  end
31
39
 
32
- if dismiss_button = options[:dismiss_button]
40
+ append_class!(options, cls)
41
+
42
+ if options.delete(:dismiss_button)
33
43
  content_tag :div, class: cls do
34
44
  button = content_tag :button, "×",
35
45
  type: "button",
@@ -39,7 +49,7 @@ module RailsBootstrapHelpers::Helpers::AlertHelper
39
49
  button + text
40
50
  end
41
51
  else
42
- content_tag :div, text, class: cls
52
+ content_tag :div, text, options
43
53
  end
44
54
  end
45
55
  end
@@ -1,32 +1,44 @@
1
1
  module RailsBootstrapHelpers::Helpers::BaseHelper
2
+ include RailsBootstrapHelpers::Helpers::OptionsHelper
3
+
2
4
  # Renders the given icon
3
5
  #
4
6
  # Renders an <tt>i</tt> tag with the class "icon-#{icon}"
5
7
  #
6
8
  # @param icon [String, Symbol] the kind of icon to render
7
9
  #
8
- # ==== Options
9
- # @param :invert [Boolean] if the color of the icon should be inverted
10
- def self.icon (icon, options = {})
10
+ # @option options [Boolean] :invert (false) if the color of the icon should be inverted
11
+ def icon (icon, options = {})
12
+ options = options.dup
13
+
11
14
  icon = ERB::Util.html_escape(icon.to_s)
12
- cls = "icon-" + icon
15
+ append_class!(options, "icon-" + icon)
13
16
 
14
- if invert = options.delete(:invert)
15
- cls << " icon-white"
17
+ if options.delete(:invert)
18
+ append_class!(options, "icon-white")
16
19
  end
17
20
 
21
+ cls = options[:class]
22
+
18
23
  "<i class=\"#{cls}\"></i>".html_safe
19
24
  end
20
25
 
21
- # Renders the given icon
22
- #
23
- # Renders an <tt>i</tt> tag with the class "icon-#{icon}"
26
+ # Renders the given Iconic icon.
27
+ #
28
+ # This is the Iconic icons from Jasny Bootstrap.
29
+ # Renders an <tt>i</tt> tag with the class "iconic-#{icon}"
24
30
  #
25
31
  # @param icon [String, Symbol] the kind of icon to render
26
32
  #
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)
33
+ # @option options [String, Symbol] :color the CSS color of the icon
34
+ # @option options [String, Symbol, Number] :size the CSS font size of the icon
35
+ #
36
+ # @option options [:warning, :error, :info, :success, :muted] :bs_style
37
+ # the Bootstrap style to render the icon in
38
+ #
39
+ # @option options [:primary, :info, :success, :warning, :danger] :action_style
40
+ # renders the icon with this action link style
41
+ def iconic_icon (icon, options = {})
42
+ RailsBootstrapHelpers::Renderers::IconicIconRenderer.new(self, icon, options).render
31
43
  end
32
44
  end
@@ -1,17 +1,20 @@
1
1
  module RailsBootstrapHelpers::Helpers::ButtonHelper
2
+ include RailsBootstrapHelpers::Helpers::OptionsHelper
3
+ include RailsBootstrapHelpers::Helpers::FormTagHelper
4
+
2
5
  # Renders a Bootstrap button. This method behaves just as "link_to" but will
3
6
  # render a Bootstrap button instead of a regular link. Note that this is still
4
7
  # an "a" tag and not an "input" tag. In addition to the options "link_to"
5
8
  # handles this method also handles the following options:
6
9
  #
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
10
+ # @option options [String, Symbol] :style the style of the button
11
+ # @option options ["large", "small", "mini"] :size the size of the button
12
+ # @option options [Boolean] :disabled (false) if the button should be disabled or not
13
+ # @option options [String] :icon the name of an icon to render on the button
14
+ # @option options ["left", "right"] :icon_position (left) the position of the icon, if present
15
+ #
16
+ # @option options [Boolean] :icon_invert (false) if the color of the icon
17
+ # should be inverted or not
15
18
  def bs_button_to (*args, &block)
16
19
  RailsBootstrapHelpers::Renderers::ButtonRenderer.new(self, :link, *args, &block).render
17
20
  end
@@ -21,11 +24,12 @@ module RailsBootstrapHelpers::Helpers::ButtonHelper
21
24
  #
22
25
  # @param url [String] the URL the button should link to
23
26
  # @param icon [String] the icon of the button
24
- # @param options [Hash] a hash of options. See bs_button_to
27
+ # @param options [Hash] a hash of options. See {#bs_button_to}
25
28
  #
26
29
  # @see #bs_button_to
27
30
  def bs_inline_button_to (url, icon, options = {})
28
31
  options = options.reverse_merge icon: icon, size: "mini"
32
+ append_class!(options, "inline")
29
33
  RailsBootstrapHelpers::Renderers::ButtonRenderer.new(self, :link, nil, url, options).render
30
34
  end
31
35
 
@@ -37,19 +41,87 @@ module RailsBootstrapHelpers::Helpers::ButtonHelper
37
41
  #
38
42
  # @param block [block] a block rendering the content of the popover
39
43
  #
40
- # ==== Options
41
- # @param :placement [String, "bottom", "top", "left", "right"]
44
+ # @option options [String, "bottom", "top", "left", "right"] :position the
45
+ # position of the popover
46
+ #
47
+ # @see #bs_button_to
42
48
  def bs_popover_button (name, content_or_options = nil, options = {}, &block)
43
49
  if block_given?
44
50
  bs_popover_button(name, capture(&block).gsub("\n", ""), content_or_options || {})
45
51
  else
46
- placement = options.delete(:placement) || "bottom"
52
+ options = options.deep_dup
53
+ placement = options.delete(:placement)
54
+
55
+ if placement
56
+ ActiveSupport::Deprecation.warn "Usage of the option `:placement` is deprecated. Please use the `:position` option instead"
57
+ end
58
+
59
+ position = options.delete(:position) || placement || "bottom"
47
60
 
48
61
  options = options.reverse_merge :"data-content" => content_or_options,
49
62
  :"data-toggle" => "popover",
50
- :"data-placement" => placement
63
+ :"data-placement" => position
51
64
 
52
65
  bs_button_to(name, '#', options)
53
66
  end
54
67
  end
68
+
69
+ # Renders a collapsible Bootstrap button. That is, a button when clicked opens
70
+ # a collapsible section.
71
+ #
72
+ # @param text [String] the text of the button
73
+ # @param target [String] a selector matching the
74
+ # @param options [Hash] a hash of options. All options are passed straight
75
+ # through to the underlying bs_button_to method.
76
+ #
77
+ # @see #bs_button_to
78
+ def bs_collapsible_button (text, target, options = {})
79
+ options = options.dup.reverse_merge :"data-toggle" => "collapse",
80
+ :"data-target" => target
81
+
82
+ bs_button_tag text, :button, options
83
+ end
84
+
85
+ # Returns a button group. That is, a div tag with the "btn-group" class.
86
+ #
87
+ # @param options [Hash] a hash of options.
88
+ #
89
+ # @option options [Boolean] :vertical (false) if true, appends
90
+ # the "btn-group-vertical" class
91
+ #
92
+ # @option options [Boolean] :toolbar (false) if true, wraps the group in an
93
+ # another group with the "btn-toolbar" class
94
+ #
95
+ # All other options are passed to the button group div.
96
+ def button_group (options = {}, &block)
97
+ if toolbar = options.delete(:toolbar)
98
+ append_class!(options, "btn-toolbar")
99
+ else
100
+ append_class!(options, "btn-group")
101
+ append_class!(options, "btn-group-vertical") if options.delete(:vertical)
102
+ end
103
+
104
+ content_tag(:div, options, &block)
105
+ end
106
+
107
+ # Renders a dropdown button.
108
+ #
109
+ # All options are passed to the underlying button.
110
+ #
111
+ # @param text [String] the text of the button
112
+ #
113
+ # @param url_or_options [String, Hash] if a string, the button will be rendered
114
+ # as split dropdown button. This argument will be interpreted as the
115
+ # URL of the button. If an Hash, it will be interpreted as the options
116
+ # for the button an a normal dropdown button will be rendered.
117
+ #
118
+ # @param options [Hash] if the an URL is passed as the "url_or_options"
119
+ # argument this will will be interpreted a hash of options. Otherwise
120
+ # it will be ignored.
121
+ #
122
+ # @param block [Proc] the block should render a the dropdown menu items in the
123
+ # form of list items with links.
124
+ def bs_dropdown_button_to (text, url_or_options = nil, options = {}, &block)
125
+ RailsBootstrapHelpers::Renderers::DropdownButtonRenderer.new(self, text, url_or_options, options, &block).render
126
+ end
55
127
  end
@@ -2,21 +2,53 @@ module RailsBootstrapHelpers::Helpers::FormTagHelper
2
2
  # Renders a Bootstrap button tag. This method behaves just as
3
3
  # <tt>button_tag</tt> but will render a Bootstrap styled button tag instead.
4
4
  #
5
- # @params text [String] the text of the button
5
+ # @params value [String] the text of the button
6
6
  # @params type [String, Symbol] the type of the button. Adds a "type"
7
7
  # attribute to the tag
8
8
  #
9
9
  # @params options [Hash] a hash of options
10
10
  #
11
- # ==== Options
12
- # @param :style [String, Symbol] the style of the button
13
- # @param :size ["large", "small", "mini"] the size of the button
14
- # @param :disabled [Boolean] if the button should be disabled or not
15
- # @param :icon [String] the name of an icon to render on the button
16
- # @param :icon_position ["left", "right"] the post of the icon, if present
17
- # @param :icon_invert [Boolean] if the color of the icon should be inverted
18
- def bs_button_tag (value, type, options = {})
11
+ # @option options [String, Symbol] :style the style of the button
12
+ # @option options ["large", "small", "mini"] :size the size of the button
13
+ # @option options [Boolean] :disabled (false) if the button should be disabled or not
14
+ # @option options [String] :icon the name of an icon to render on the button
15
+ # @option options ["left", "right"] :icon_position ("left") the post of the icon, if present
16
+ # @option options [Boolean] :icon_invert (left) if the color of the icon should be inverted
17
+ def bs_button_tag (value, type, options = {}, &block)
19
18
  options = options.merge type: type
20
- RailsBootstrapHelpers::Renderers::ButtonRenderer.new(self, :button, value, options).render
19
+ RailsBootstrapHelpers::Renderers::ButtonRenderer.new(self, :button, value, options, &block).render
20
+ end
21
+
22
+ # Renders a Boolean submit tag. This method behaves just as
23
+ # <tt>submit_tag</tt> but will render a Bootstrap styled submit tag instead.
24
+ #
25
+ # @param value [String] the text of the submit tag
26
+ # @param options [Hash] a hash of options
27
+ #
28
+ # @option options [String, Symbol] :style the style of the button
29
+ # @option options ["large", "small", "mini"] :size the size of the button
30
+ #
31
+ # All the other options are passed straight through to the underlying
32
+ # <tt>submit_tag</tt> method.
33
+ def bs_submit_tag (value, options = {})
34
+ options = options.dup
35
+
36
+ if options[:class].present?
37
+ options[:class] << " "
38
+ else
39
+ options[:class] = ""
40
+ end
41
+
42
+ options[:class] << "btn"
43
+
44
+ if style = options.delete(:style)
45
+ options[:class] << " btn-" + style.to_s
46
+ end
47
+
48
+ if size = options.delete(:size)
49
+ options[:class] << " btn-" + size.to_s
50
+ end
51
+
52
+ submit_tag value, options
21
53
  end
22
54
  end
@@ -0,0 +1,17 @@
1
+ module RailsBootstrapHelpers::Helpers::NavigationHelper
2
+ include RailsBootstrapHelpers::Helpers::OptionsHelper
3
+
4
+ # Renders a Bootstrap tabbable navigation.
5
+ #
6
+ # @param args [Array<String>, Hash] an array of the names of the tab items.
7
+ # If the last item is a Hash it's considered to be the options.
8
+ #
9
+ # @option options [Boolean] :bordered (false) if true, will render the tab
10
+ # container with a border. This option requires the Jasny Bootstrap extensions.
11
+ #
12
+ # @option option [Bootstrap] :fade (false) if true, will add the "fade in"
13
+ # class to all tab panes. This requires the bootstrap-transition.js file.
14
+ def tabbable (*args, &block)
15
+ RailsBootstrapHelpers::Renderers::TabbableRenderer.new(self, *args, &block).render
16
+ end
17
+ end
@@ -6,12 +6,11 @@ module RailsBootstrapHelpers::Helpers::OptionsHelper
6
6
  # @param options [Hash] a hash of options
7
7
  # @param html_options [Hash] a hash of HTML options/attributes
8
8
  #
9
- # ==== Options
10
- # @param :tooltip [String] the text of the tooltip. IF present adds attributes
9
+ # @option option [String] :tooltip the text of the tooltip. If present adds attributes
11
10
  # for a Bootstrap tooltip. This will add the <tt>data-toggle="tooltip"</tt>
12
11
  # and <tt>title="#{tooltip}" </tt> HTML attributes if not already present
13
12
  #
14
- # @param :tooltip_location ["left", "right", "top", "bottom"] the position of
13
+ # @option options ["left", "right", "top", "bottom"] :tooltip_location the position of
15
14
  # the tooltip if <tt>:tooltip</tt> is present. Adds the
16
15
  # <tt>data-placement="#{tooltip_position}"</tt> HTML attribute if not
17
16
  # already present.
@@ -29,4 +28,26 @@ module RailsBootstrapHelpers::Helpers::OptionsHelper
29
28
 
30
29
  options
31
30
  end
31
+
32
+ # Appends the given classes on the given options hash.
33
+ #
34
+ # It will look for both the "class" and :class key. This will create a new
35
+ # :class key in the given hash if neither exist.
36
+ #
37
+ # @param options [Hash] hash of options to append the classes to
38
+ # @param new_classes [Array<String>] the classes to append
39
+ # @return options
40
+ def append_class! (options, *new_classes)
41
+ return options if new_classes.empty?
42
+ key = options.key?("class") ? "class" : :class
43
+ cls = options[key].to_s || ""
44
+
45
+ if cls.present? && new_classes.first.present?
46
+ cls << " "
47
+ end
48
+
49
+ cls << new_classes.join(" ")
50
+ options[key] = cls
51
+ options
52
+ end
32
53
  end
@@ -0,0 +1,26 @@
1
+ module RailsBootstrapHelpers::Helpers::TagHelper
2
+ # Returns an HTML block tag. Properly adds indentation and newlines for the
3
+ # returned HTML.
4
+ #
5
+ # ==== Example
6
+ # bs_content_tag "div", id: "foo" do
7
+ # bs_content_tag "div"
8
+ # append "foo"
9
+ # end
10
+ #
11
+ # append "bar"
12
+ # end
13
+ #
14
+ # <div id="foo">
15
+ # <div>
16
+ # foo
17
+ # </div>
18
+ # bar
19
+ # </div>
20
+ #
21
+ # @param [String] the name of the tag to render
22
+ # @param [Hash] a hash of HTML attributes
23
+ def bs_content_tag (name, options = {}, &block)
24
+ RailsBootstrapHelpers::Renderers::ContentTagRenderer.new(self, name, options, &block).render
25
+ end
26
+ end
@@ -0,0 +1,17 @@
1
+ module RailsBootstrapHelpers::Helpers::UrlHelper
2
+ # Renders a Jasny Bootstrap action link. This method behaves just as "link_to"
3
+ # but will render a Jasny Bootstrap action link instead of a regular link.
4
+ # In addition to the options "link_to" handles, this method also handles the
5
+ # following options:
6
+ #
7
+ # @option options [String, Symbol] :style the style of the link
8
+ def action_link_to (*args, &block)
9
+ RailsBootstrapHelpers::Renderers::ActionLinkRenderer.new(self, *args, &block).render
10
+ end
11
+
12
+ # Renders a Jasny Bootstrap row link. This method behaves just as "link_to"
13
+ # but will render a Jasny Bootstrap row link instead of a regular link.
14
+ def row_link_to (*args, &block)
15
+ RailsBootstrapHelpers::Renderers::RowLinkRenderer.new(self, *args, &block).render
16
+ end
17
+ end
@@ -1,14 +1,16 @@
1
- #require "action"
2
-
3
1
  module RailsBootstrapHelpers
4
2
  module Rails
5
3
  class Engine < ::Rails::Engine
6
4
  initializer "rails-bootstrap-helpers.helpers" do
5
+ ActionView::Base.send :include, RailsBootstrapHelpers::Helpers::AccordionHelper
6
+ ActionView::Base.send :include, RailsBootstrapHelpers::Helpers::UrlHelper
7
7
  ActionView::Base.send :include, RailsBootstrapHelpers::Helpers::AlertHelper
8
8
  ActionView::Base.send :include, RailsBootstrapHelpers::Helpers::BaseHelper
9
9
  ActionView::Base.send :include, RailsBootstrapHelpers::Helpers::ButtonHelper
10
10
  ActionView::Base.send :include, RailsBootstrapHelpers::Helpers::FormTagHelper
11
11
  ActionView::Base.send :include, RailsBootstrapHelpers::Helpers::LabelHelper
12
+ ActionView::Base.send :include, RailsBootstrapHelpers::Helpers::TagHelper
13
+ ActionView::Base.send :include, RailsBootstrapHelpers::Helpers::NavigationHelper
12
14
  end
13
15
  end
14
16
  end