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
@@ -1,5 +1,5 @@
1
1
  module RailsBootstrapHelpers::Renderers
2
- class AbstractButtonRenderer < Renderer
2
+ class AbstractLinkRenderer < Renderer
3
3
  def initialize (template, type, *args, &block)
4
4
  super template
5
5
  @args = args
@@ -22,9 +22,10 @@ module RailsBootstrapHelpers::Renderers
22
22
  attr_accessor :options
23
23
  attr_accessor :html_options
24
24
  attr_accessor :text
25
+ attr_accessor :type
25
26
 
26
27
  def method
27
- case @type
28
+ case type
28
29
  when :link then :link_to
29
30
  when :button then :button_tag
30
31
  else :link_to
@@ -32,7 +33,7 @@ module RailsBootstrapHelpers::Renderers
32
33
  end
33
34
 
34
35
  def new_args
35
- case @type
36
+ case type
36
37
  when :link
37
38
  [text, options, html_options, *self.args[3 .. -1]]
38
39
  when :button then
@@ -57,10 +58,15 @@ module RailsBootstrapHelpers::Renderers
57
58
  def append_class (cls)
58
59
  return unless cls
59
60
 
60
- if c = html_options[:class]
61
- html_options[:class] << " " + cls.to_s
61
+ if c = html_options["class"]
62
+ html_options["class"] << " " + cls.to_s
62
63
  else
63
- html_options[:class] = cls.to_s
64
+ if c = has_option?("class")
65
+ c << " " + cls.to_s
66
+ cls = c
67
+ end
68
+
69
+ html_options["class"] = cls.to_s
64
70
  end
65
71
  end
66
72
 
@@ -68,9 +74,19 @@ module RailsBootstrapHelpers::Renderers
68
74
 
69
75
  def extract_options!
70
76
  self.options = args[1]
71
- self.options = options.stringify_keys if options.is_a?(Hash)
77
+
78
+ if options.is_a?(Hash)
79
+ self.options = bs_options(options)
80
+ self.options = options.stringify_keys
81
+ end
82
+
72
83
  self.html_options = args[2] || {}
84
+ self.html_options = bs_options(html_options)
73
85
  self.html_options = html_options.stringify_keys
86
+
87
+ if cls = html_options["class"]
88
+ html_options["class"] = cls.dup
89
+ end
74
90
  end
75
91
 
76
92
  def _has_option? (key, options, html_options = nil)
@@ -0,0 +1,94 @@
1
+ module RailsBootstrapHelpers::Renderers
2
+ class AccordionRenderer < Renderer
3
+ def initialize (template, id, &block)
4
+ super template
5
+ @id = id
6
+ @block = block
7
+ @selector = Selector.new
8
+ end
9
+
10
+ def render
11
+ @context = AccordionContext.new(self)
12
+ block.call(context)
13
+ build_accordion
14
+ end
15
+
16
+ private
17
+ attr_reader :id
18
+ attr_reader :block
19
+ attr_reader :selector
20
+ attr_reader :context
21
+
22
+ def build_accordion
23
+ content_tag :div, id: id, class: "accordion" do
24
+ contents = []
25
+ selector.base "##{id}.accordion" do |base|
26
+ context.groups.each_with_index do |group, count|
27
+ contents << build_group(group, count, base)
28
+ end
29
+ end
30
+
31
+ contents.join("\n").html_safe
32
+ end
33
+ end
34
+
35
+ def build_group (group, count, accordion_base)
36
+ base = "accordion-group"
37
+
38
+ selector.base ".#{base}" do |group_base|
39
+ foobar = self
40
+ content_tag(:div, class: base) do
41
+ body = "accordion-body"
42
+ build_heading(group.heading, body, count, accordion_base, group_base) +
43
+ build_body(body, group.block)
44
+ end
45
+ end
46
+ end
47
+
48
+ def build_heading (heading, body, count, accordion_base, group_base)
49
+ href = "#{group_base}:nth-child(#{count + 1}) .#{body}.collapse"
50
+
51
+ content_tag :div, class: "accordion-heading" do
52
+ content_tag :a, heading,
53
+ href: href,
54
+ class: "accordion-toggle",
55
+ :"data-toggle" => "collapse",
56
+ :"data-parent" => accordion_base
57
+ end
58
+ end
59
+
60
+ def build_body (body, block)
61
+ content_tag :div, class: body + " collapse" do
62
+ content_tag :div, class: "accordion-inner", &block
63
+ end
64
+ end
65
+
66
+ class AccordionContext
67
+ Group = Struct.new(:heading, :block)
68
+
69
+ attr_reader :groups
70
+
71
+ def initialize (renderer)
72
+ @renderer = renderer
73
+ @groups = []
74
+ end
75
+
76
+ def group (heading, &block)
77
+ @groups << Group.new(heading, block)
78
+ end
79
+ end
80
+
81
+ class Selector
82
+ def initialize
83
+ @base = []
84
+ end
85
+
86
+ def base (base, &block)
87
+ @base << base
88
+ block.call @base.join(" ")
89
+ ensure
90
+ @base.pop
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,19 @@
1
+ module RailsBootstrapHelpers::Renderers
2
+ class ActionLinkRenderer < AbstractLinkRenderer
3
+ def initialize (template, *args, &block)
4
+ super template, :link, *args, &block
5
+ end
6
+
7
+ def render
8
+ append_class "act"
9
+
10
+ if style = has_option?("style")
11
+ unless style.to_s == "default"
12
+ append_class "act-" + style.to_s
13
+ end
14
+ end
15
+
16
+ super
17
+ end
18
+ end
19
+ end
@@ -1,9 +1,11 @@
1
1
  module RailsBootstrapHelpers::Renderers
2
- class ButtonRenderer < AbstractButtonRenderer
2
+ class ButtonRenderer < AbstractLinkRenderer
3
+ include RailsBootstrapHelpers::Helpers::BaseHelper
4
+
3
5
  def render
4
6
  append_class "btn"
5
7
 
6
- if style = has_option?("style")
8
+ if (style = has_option?("style")) && style != "default"
7
9
  append_class "btn-" + style.to_s
8
10
  end
9
11
 
@@ -20,9 +22,9 @@ module RailsBootstrapHelpers::Renderers
20
22
  icon_args = [icon, invert: has_option?("icon_invert")]
21
23
 
22
24
  if pos.to_s == "right"
23
- self.text = self.text.to_s + " " + RailsBootstrapHelpers::Helpers::BaseHelper.icon(*icon_args)
25
+ self.text = self.text.to_s + " " + icon(*icon_args)
24
26
  else
25
- self.text = RailsBootstrapHelpers::Helpers::BaseHelper.icon(*icon_args) + " " + self.text.to_s
27
+ self.text = icon(*icon_args) + " " + self.text.to_s
26
28
  end
27
29
 
28
30
  self.text = self.text.html_safe
@@ -0,0 +1,67 @@
1
+ module RailsBootstrapHelpers::Renderers
2
+ class ContentTagRenderer < Renderer
3
+ def initialize (template, name, options, &block)
4
+ super template
5
+
6
+ @name = name
7
+ @options = options
8
+ @block = block
9
+
10
+ @indentation_level = 0
11
+ @default_indentation = 2
12
+ @buffer = ""
13
+ @context = Context.new(self)
14
+ end
15
+
16
+ def render
17
+ content_tag_impl(name, options, &block)
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :name
23
+ attr_reader :content
24
+ attr_reader :options
25
+ attr_reader :block
26
+ attr_reader :context
27
+ attr_reader :buffer
28
+ attr_reader :default_indentation
29
+
30
+ def content_tag_impl (name, options = {}, &block)
31
+ append tag(name, options, true)
32
+ indent { context.instance_eval(&block) }
33
+ append "</#{name}>"
34
+ buffer.html_safe
35
+ end
36
+
37
+ def indent (&block)
38
+ last_indentation_level = @indentation_level
39
+ @indentation_level += default_indentation
40
+ block.call
41
+ ensure
42
+ @indentation_level = last_indentation_level
43
+ end
44
+
45
+ def append (string)
46
+ unless string.nil?
47
+ buffer << " " * @indentation_level
48
+ buffer << string
49
+ buffer << "\n"
50
+ end
51
+ end
52
+
53
+ class Context
54
+ def initialize (renderer)
55
+ @renderer = renderer
56
+ end
57
+
58
+ def bs_content_tag (name, options = {}, &block)
59
+ @renderer.send :content_tag_impl, name, options, &block
60
+ end
61
+
62
+ def append (string)
63
+ @renderer.send :append, string
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,87 @@
1
+ module RailsBootstrapHelpers::Renderers
2
+ class DropdownButtonRenderer < Renderer
3
+ def initialize (template, text, url_or_options = nil, options = {}, &block)
4
+ super template
5
+ @text = text
6
+ @block = block
7
+
8
+ extract_args!(url_or_options, options)
9
+ end
10
+
11
+ def render
12
+ button_group do
13
+ render_toggle_button + "\n" +
14
+ render_menu
15
+ end
16
+ end
17
+
18
+ private
19
+ attr_reader :options
20
+ attr_reader :url
21
+ attr_reader :split
22
+ attr_reader :block
23
+ attr_reader :text
24
+
25
+ def extract_args! (url_or_options, options)
26
+ if url_or_options.is_a?(Hash)
27
+ @options = url_or_options.dup
28
+ @split = false
29
+ elsif url_or_options.nil?
30
+ @options = options.dup
31
+ @split = false
32
+ else
33
+ @url = url_or_options
34
+ @options = options.dup
35
+ @split = true
36
+ end
37
+ end
38
+
39
+ def render_toggle_button
40
+ if split
41
+ bs_button_to(text, url, options) << "\n" <<
42
+ render_toggle_button_impl(false)
43
+ else
44
+ render_toggle_button_impl(true)
45
+ end
46
+ end
47
+
48
+ def toggle_options
49
+ @toggle_options ||= begin
50
+ opts = split ? {} : options.deep_dup
51
+ data = opts[:data] || {}
52
+ data[:toggle] = "dropdown"
53
+ opts[:data] = data
54
+
55
+ append_class!(opts, "dropdown-toggle")
56
+
57
+ if (style = options[:style]) && style != "default"
58
+ append_class!(opts, "btn-#{style}")
59
+ end
60
+
61
+ opts
62
+ end
63
+ end
64
+
65
+ def render_caret
66
+ content_tag :span, nil, class: "caret"
67
+ end
68
+
69
+ def render_toggle_button_impl (with_text)
70
+ text = with_text ? text_with_caret : render_caret
71
+ bs_button_tag text, nil, toggle_options
72
+ end
73
+
74
+ def text_with_caret
75
+ @text_with_caret ||= begin
76
+ escaped_text = ERB::Util.html_escape(text)
77
+ (escaped_text + " " + render_caret).html_safe
78
+ end
79
+ end
80
+
81
+ def render_menu
82
+ content_tag :ul, class: "dropdown-menu" do
83
+ template.capture(&block)
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,75 @@
1
+ module RailsBootstrapHelpers::Renderers
2
+ class IconicIconRenderer < Renderer
3
+ def initialize (template, icon, options)
4
+ super template
5
+ @icon = icon
6
+ @options = options.dup
7
+ end
8
+
9
+ def render
10
+ icon = ERB::Util.html_escape(@icon.to_s)
11
+ append_class!(options, "iconic-#{icon}")
12
+ append_style(:color, options.delete(:color))
13
+
14
+ handle_size
15
+ handle_bs_style
16
+ handle_action_style
17
+
18
+ content_tag :i, "", bs_options(options)
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :icon
24
+ attr_reader :options
25
+
26
+ def handle_bs_style
27
+ if bs_style = options.delete(:bs_style)
28
+ if bs_style.to_s == "muted"
29
+ append_class!(options, :muted)
30
+ else
31
+ append_class!(options, "text-#{bs_style}")
32
+ end
33
+ end
34
+ end
35
+
36
+ def handle_action_style
37
+ if action_style = options.delete(:action_style)
38
+ append_class!(options, "act")
39
+
40
+ unless action_style.to_s == "default"
41
+ append_class!(options, "act-#{action_style}")
42
+ end
43
+ end
44
+ end
45
+
46
+ def handle_size
47
+ if size = options.delete(:size)
48
+ size = size.to_s
49
+
50
+ if size.to_i > 0
51
+ size << "px"
52
+ end
53
+
54
+ append_style("font-size", size)
55
+ end
56
+ end
57
+
58
+ def append_style (key, value)
59
+ if value
60
+ style = options[:style].to_s
61
+
62
+ if style.present?
63
+ unless style.end_with?(";")
64
+ style << ";"
65
+ end
66
+
67
+ style << " "
68
+ end
69
+
70
+ style << "#{key}: #{value};"
71
+ options[:style] = style
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,12 @@
1
+ module RailsBootstrapHelpers::Renderers
2
+ class RowLinkRenderer < AbstractLinkRenderer
3
+ def initialize (template, *args, &block)
4
+ super template, :link, *args, &block
5
+ end
6
+
7
+ def render
8
+ append_class "rowlink"
9
+ super
10
+ end
11
+ end
12
+ end