twitter-bootstrap-form-builder 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -99,17 +99,24 @@ the regular `FormBuilder#text_field`, use `:label => false`:
99
99
  <%= f.text_field :email, :label => false %> # <input type="text" id="post_email" />
100
100
  ```
101
101
 
102
+ To render the field within the Bootstrap control-group markup but without a label tag,
103
+ use `:label => nil`:
104
+
105
+ ```erb
106
+ <%= f.text_field :email, :label => nil %>
107
+ ```
108
+
102
109
  Checkboxes are a special case. They can contain two labels in a horizontal form, on to the left and
103
110
  one to the right.
104
111
 
105
112
  The left label uses the `:label` option and works as expected.
106
113
 
107
- The right label is controled by the `:about` option.
114
+ The right label is controled by the `:text` option.
108
115
 
109
116
  Example:
110
117
 
111
118
  ```erb
112
- <%= f.check_box :hide_email, :about => "Do not display my email in my post" %>
119
+ <%= f.check_box :hide_email, :text=> "Do not display my email in my post" %>
113
120
  ```
114
121
 
115
122
  Produces the following HTML:
@@ -25,24 +25,22 @@ module MNE
25
25
  define_method method_name.to_sym do |field, *args|
26
26
 
27
27
  # find the options hash, and extract the options for the label tag
28
- opts = extract_options(args)
29
- label_opts = extract_sub_options(opts, :label)
28
+ opts = extract_options(args)
30
29
 
31
- # If label is false, we're rendering the field without modification
32
- return super(field, *args) if label_opts === false
30
+ label_tag = build_label_tag(field, opts)
33
31
 
34
- # Add the TB class to the label
35
- label_opts << { :class => "control-label" }
32
+ # If label is false, we're rendering the field without modification
33
+ return super(field, *args) if label_tag === false
36
34
 
37
35
  # create a help-block if present
38
36
  help_block = opts[:help_block] ? @template.content_tag(:p, opts[:help_block], :class => "help-block") : ""
39
37
 
40
38
  # propogate properties of control group up
41
39
  control_group_opts = opts[:control_group] || {}
42
- control_group_opts[:class] = control_group_opts[:class] ? control_group_opts[:class] + " #{method_name}" : "#{method_name}"
40
+ control_group_opts[:class] = "#{control_group_opts[:class]} #{method_name}"
43
41
 
44
42
  control_group(field, control_group_opts) do
45
- label(field, *label_opts) + @template.content_tag(:div, :class => "controls") do
43
+ label_tag + @template.content_tag(:div, :class => "controls") do
46
44
  super(field, *args) + help_block + errors_for(field)
47
45
  end
48
46
  end.html_safe
@@ -52,48 +50,85 @@ module MNE
52
50
 
53
51
  # Special handling for check boxes, which can have two labels
54
52
  def check_box(field, options = {}, checked_value = "1", unchecked_value = "0")
55
- label_opts = extract_sub_options(options, :label)
56
- about_opts = extract_sub_options(options, :about)
57
-
58
- return super if label_opts == false
53
+ label_tag = build_label_tag(field, options)
59
54
 
60
- #raise [object.errors.messages, errors_for(field)].inspect
61
- # Add the TB class to the thelabel
62
- label_opts << { :class => "control-label" } unless label_opts.nil?
63
- about_opts << { :class => "checkbox" } if about_opts.any?
55
+ return super if label_tag === false
64
56
 
65
57
  control_group_opts = options[:control_group] || {}
66
58
 
67
- label_tag = (label_opts ? label(field, *label_opts) : "").html_safe
68
- check_box = super(field, options, checked_value, unchecked_value).html_safe
59
+ options[:text] ||= false
69
60
 
70
61
  control_group(field, control_group_opts) do
71
62
  label_tag + @template.content_tag(:div, :class => "controls") do
72
- check_box = label(field, *about_opts) { check_box + about_opts[0] } if about_opts
73
- check_box + errors_for(field)
74
- end
63
+
64
+ check_box_tag = super(field, options, checked_value, unchecked_value).html_safe
65
+
66
+ about_tag = build_label_tag(field, options, :text, {:class => "checkbox"}) do |opts|
67
+ check_box_tag + opts[0].html_safe
68
+ end
69
+
70
+ about_tag ||= @template.content_tag(:label, check_box_tag, :class => "checkbox")
71
+
72
+ about_tag.html_safe + errors_for(field)
73
+ end.html_safe
75
74
  end.html_safe
76
75
  end
77
76
 
78
77
  protected
79
78
 
80
79
  def extract_options(args)
81
- args.find { |a| a.is_a?(Hash) && a.has_key?(:label) } || {}
80
+ args.find { |a| a.is_a?(Hash) } || {}
82
81
  end
83
82
 
84
- def extract_sub_options(opts, key = :label)
85
- sub_opts = opts.keys.include?(key) ? opts[key] : []
86
- opts.delete(key)
87
- return false if sub_opts === false
88
- return nil if sub_opts.nil?
89
- Array(sub_opts)
83
+ # Build a label tag (or return false, or "") based on the rules for passing
84
+ # nil/false/a string/an array/a hash into the options hash
85
+ def build_label_tag(field, opts, key = :label, defaults = { :class => "control-label" }, &block)
86
+ label_opts = extract_sub_options(opts, key, defaults)
87
+
88
+ # False indicates complete fall-back to FormBuilder's implementation
89
+ return false if label_opts === false
90
+
91
+ if label_opts
92
+ if block_given?
93
+ label(field, *label_opts) { yield(label_opts) }
94
+ else
95
+ label(field, *label_opts)
96
+ end
97
+ else
98
+ ""
99
+ end.html_safe
90
100
  end
91
101
 
92
- def label_opts(opts, key = :label)
93
- label_opts = extract_sub_options(opts, key)
94
- label_opts << { :class => "control-label" }
102
+ # Pull nested (label) options out of an options hash
103
+ # Used primarily by checkbox's :label/:text tags
104
+ def extract_sub_options(opts, key = :label, defaults = {})
105
+ if opts.keys.include?(key)
106
+ sub_opts = opts.delete(key)
107
+
108
+ return [sub_opts.merge(defaults)] if sub_opts.is_a? Hash
109
+ return [sub_opts, defaults] if sub_opts.is_a? String
110
+
111
+ # If we were explicitly given an array, we're done.
112
+ # Freeze it, and pass it directly to label_tag
113
+ if sub_opts.is_a? Array
114
+ sub_opts.freeze
115
+ return sub_opts
116
+ end
117
+
118
+ # If the user explicitly passed in nil, return it
119
+ return nil if sub_opts.nil?
120
+ end
121
+
122
+ return false if sub_opts === false
123
+
124
+ sub_opts || [defaults]
95
125
  end
96
126
 
127
+ #def label_opts(opts, key = :label)
128
+ # label_opts = Array(extract_sub_options(opts, key))
129
+ # label_opts << { :class => "control-label" }
130
+ #end
131
+
97
132
  def errors_for(field)
98
133
  @template.content_tag(:span, "#{object.class.human_attribute_name(field)} #{object.errors.messages[field].to_sentence}",
99
134
  :class => "help-inline") if object.errors.messages.has_key?(field) && object.errors.messages[field].any?
@@ -1,5 +1,5 @@
1
1
  module MNE
2
2
  module TwitterBootstrapFormBuilder
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Matthew Eagar
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2012-08-03 00:00:00 -04:00
17
+ date: 2012-08-04 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency