bootstrap_forms 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,12 @@ Bootstrap Forms is a nice Rails generator that makes working with [Bootstrap (by
4
4
 
5
5
  Forms with Bootstrap are crowded with additional layout markup. While it's necessary, you shouldn't have to type it every time you create a form! That's why I created Bootstrap Forms.
6
6
 
7
+ Bootstrap 2.0 Compliant!
8
+ ------------------------
9
+ A super special thanks to [vincenzor](https://github.com/vincenzor) for updating `bootstrap_forms` to comply with the new methods and features in Twitter Bootstrap 2.0.
10
+
11
+ To get these new features, ensure you are using `bootstrap_forms ~> 1.0.0`.
12
+
7
13
  Note/Caution/Warning
8
14
  --------------------
9
15
  There were **major** changes in the release of version `0.1.0`:
@@ -35,18 +41,6 @@ With Bootstrap, you would need the following code for a form:
35
41
  = f.text_area :field, :opts => {...}
36
42
  ```
37
43
 
38
- ```erb
39
- <!-- using ERB -->
40
- <%= form_for @model do |f| %>
41
- <div class="clearfix">
42
- <label>MyLabel</label>
43
- <div class="input">
44
- <%= f.text_area :field, :opts => {...} %>
45
- </div>
46
- </div>
47
- <% end %>
48
- ```
49
-
50
44
  Using Bootstrap Forms, this is **much** simpler:
51
45
 
52
46
  ```haml
@@ -55,13 +49,6 @@ Using Bootstrap Forms, this is **much** simpler:
55
49
  = f.text_area :field, :opts => {...}
56
50
  ```
57
51
 
58
- ```erb
59
- <!-- using ERB -->
60
- <%= bootstrap_form_for @model do |f| %>
61
- <%= f.text_area :field, :opts => {...} %>
62
- <% end %>
63
- ```
64
-
65
52
  The custom form builder will automatically wrap everything for you. This helps clean up your view layer significantly!
66
53
 
67
54
  Additional Form Methods
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "bootstrap_forms"
6
- s.version = "0.1.1"
6
+ s.version = "1.0.0"
7
7
  s.author = "Seth Vargo"
8
8
  s.email = "sethvargo@gmail.com"
9
9
  s.homepage = "https://github.com/sethvargo/bootstrap_forms"
@@ -1,166 +1,166 @@
1
1
  module BootstrapForms
2
- class FormBuilder < ActionView::Helpers::FormBuilder
3
- delegate :content_tag, :hidden_field_tag, :check_box_tag, :radio_button_tag, :link_to, :to => :@template
4
-
5
- def error_messages
6
- if object.errors.full_messages.any?
7
- content_tag(:div, :class => 'alert-message block-message error') do
8
- link_to('&times;'.html_safe, '#', :class => 'close') +
9
- content_tag(:p, "<strong>Oh snap! You got an error!</strong> Fix the errors below and try again.".html_safe) +
10
- content_tag(:ul) do
11
- object.errors.full_messages.map do |message|
12
- content_tag(:li, message)
13
- end.join('').html_safe
14
- end
15
- end
16
- else
17
- '' # return empty string
18
- end
19
- end
20
-
21
- def bootstrap_fields_for(record_name, record_object = nil, options = {}, &block)
22
- options[:builder] = BootstrapForms::FormBuilder
23
- fields_for(record_name, record_object, options, &block)
24
- end
25
-
26
- %w(collection_select select check_box email_field file_field number_field password_field phone_field radio_button range_field search_field telephone_field text_area text_field url_field).each do |method_name|
27
- define_method(method_name) do |name, *args|
28
- @name = name
29
- @options = args.extract_options!
30
- @args = args
31
-
32
- clearfix_div do
33
- label_field + input_div do
34
- extras { super(name, *(@args << @options)) }
35
- end
36
- end
37
- end
38
- end
39
-
40
- def collection_check_boxes(attribute, records, record_id, record_name, *args)
41
- @name = attribute
42
- @options = args.extract_options!
43
- @args = args
44
-
45
- clearfix_div do
46
- label_field + input_div do
47
- extras do
48
- content_tag(:ul, :class => 'inputs-list') do
49
- records.collect do |record|
50
- element_id = "#{object_name}_#{attribute}_#{record.send(record_id)}"
51
- checkbox = check_box_tag("#{object_name}[#{attribute}][]", record.send(record_id), object.send(attribute).include?(record.send(record_id)), :id => element_id)
52
-
53
- content_tag(:li) do
54
- content_tag(:label) do
55
- checkbox + content_tag(:span, record.send(record_name))
56
- end
57
- end
58
- end.join('').html_safe
59
- end
60
- end
61
- end + hidden_field_tag("#{object_name}[#{attribute}][]")
62
- end
63
- end
64
-
65
- def collection_radio_buttons(attribute, records, record_id, record_name, *args)
66
- @name = attribute
67
- @options = args.extract_options!
68
- @args = args
69
-
70
- clearfix_div do
71
- label_field + input_div do
72
- extras do
73
- content_tag(:ul, :class => 'inputs-list') do
74
- records.collect do |record|
75
- element_id = "#{object_name}_#{attribute}_#{record.send(record_id)}"
76
- radiobutton = radio_button_tag("#{object_name}[#{attribute}][]", record.send(record_id), object.send(attribute) == record.send(record_id), :id => element_id)
77
-
78
- content_tag(:li) do
79
- content_tag(:label) do
80
- radiobutton + content_tag(:span, record.send(record_name))
81
- end
82
- end
83
- end.join('').html_safe
84
- end
85
- end
86
- end
87
- end
88
- end
89
-
90
- def uneditable_field(name, *args)
91
- @name = name
92
- @options = args.extract_options!
93
- @args = args
94
-
95
- clearfix_div do
96
- label_field + input_div do
97
- extras do
98
- content_tag(:span, :class => 'uneditable-input') do
99
- @options[:value] || object.send(@name.to_sym)
100
- end
101
- end
102
- end
103
- end
104
- end
105
-
106
- def submit(name = nil, *args)
107
- @name = name
108
- @options = args.extract_options!
109
- @args = args
110
-
111
- @options[:class] = 'btn primary'
112
-
113
- content_tag(:div, :class => 'actions') do
114
- super(name, *(args << @options)) + ' ' + link_to('Cancel', :back, :class => 'btn')
115
- end
116
- end
117
-
118
- private
119
- def clearfix_div(&block)
120
- @options[:error] = object.errors[@name].collect{|e| "#{@options[:label] || @name} #{e}".humanize}.join(', ') unless object.errors[@name].empty?
121
-
122
- klasses = ['clearfix']
123
- klasses << 'error' if @options[:error]
124
- klasses << 'success' if @options[:success]
125
- klasses << 'warning' if @options[:warning]
126
- klass = klasses.join(' ')
127
-
128
- content_tag(:div, :class => klass, &block)
129
- end
130
-
131
- def input_div(&block)
132
- content_tag(:div, :class => 'input') do
133
- if @options[:append] || @options[:prepend]
134
- klass = 'input-prepend' if @options[:prepend]
135
- klass = 'input-append' if @options[:append]
136
- content_tag(:div, :class => klass, &block)
137
- else
138
- yield if block_given?
139
- end
140
- end
141
- end
142
-
143
- def label_field(&block)
144
- required = object.class.validators_on(@name).any? { |v| v.kind_of? ActiveModel::Validations::PresenceValidator }
145
- label(@name, block_given? ? block : @options[:label], :class => ('required' if required))
146
- end
147
-
148
- %w(help_inline error success warning help_block append prepend).each do |method_name|
149
- define_method(method_name) do |*args|
150
- return '' unless value = @options[method_name.to_sym]
151
- klass = 'help-inline'
152
- klass = 'help-block' if method_name == 'help_block'
153
- klass = 'add-on' if method_name == 'append' || method_name == 'prepend'
154
- content_tag(:span, value, :class => klass)
155
- end
156
- end
157
-
158
- def extras(&block)
159
- [prepend, (yield if block_given?), append, help_inline, error, success, warning, help_block].join('').html_safe
160
- end
161
-
162
- def objectify_options(options)
163
- super.except(:label, :help_inline, :error, :success, :warning, :help_block, :prepend, :append)
164
- end
165
- end
2
+ class FormBuilder < ActionView::Helpers::FormBuilder
3
+ delegate :content_tag, :hidden_field_tag, :check_box_tag, :radio_button_tag, :link_to, :to => :@template
4
+
5
+ def error_messages
6
+ if object.errors.full_messages.any?
7
+ content_tag(:div, :class => 'alert alert-error') do
8
+ link_to('&times;'.html_safe, '#', {:class => 'close', :data => { :dismiss => "alert" }}) +
9
+ content_tag(:p, "<strong>Oh snap! You got an error!</strong> Fix the errors below and try again.".html_safe) +
10
+ content_tag(:ul) do
11
+ object.errors.full_messages.map do |message|
12
+ content_tag(:li, message)
13
+ end.join('').html_safe
14
+ end
15
+ end
16
+ else
17
+ '' # return empty string
18
+ end
19
+ end
20
+
21
+ def bootstrap_fields_for(record_name, record_object = nil, options = {}, &block)
22
+ options[:builder] = BootstrapForms::FormBuilder
23
+ fields_for(record_name, record_object, options, &block)
24
+ end
25
+
26
+ %w(collection_select select check_box email_field file_field number_field password_field phone_field radio_button range_field search_field telephone_field text_area text_field url_field).each do |method_name|
27
+ define_method(method_name) do |name, *args|
28
+ @name = name
29
+ @options = args.extract_options!
30
+ @args = args
31
+
32
+ clearfix_div do
33
+ label_field + input_div do
34
+ extras { super(name, *(@args << @options)) }
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ def collection_check_boxes(attribute, records, record_id, record_name, *args)
41
+ @name = attribute
42
+ @options = args.extract_options!
43
+ @args = args
44
+
45
+ clearfix_div do
46
+ label_field + input_div do
47
+ extras do
48
+ content_tag(:ul, :class => 'inputs-list') do
49
+ records.collect do |record|
50
+ element_id = "#{object_name}_#{attribute}_#{record.send(record_id)}"
51
+ checkbox = check_box_tag("#{object_name}[#{attribute}][]", record.send(record_id), object.send(attribute).include?(record.send(record_id)), :id => element_id)
52
+
53
+ content_tag(:li) do
54
+ content_tag(:label) do
55
+ checkbox + content_tag(:span, record.send(record_name))
56
+ end
57
+ end
58
+ end.join('').html_safe
59
+ end
60
+ end
61
+ end + hidden_field_tag("#{object_name}[#{attribute}][]")
62
+ end
63
+ end
64
+
65
+ def collection_radio_buttons(attribute, records, record_id, record_name, *args)
66
+ @name = attribute
67
+ @options = args.extract_options!
68
+ @args = args
69
+
70
+ clearfix_div do
71
+ label_field + input_div do
72
+ extras do
73
+ content_tag(:ul, :class => 'inputs-list') do
74
+ records.collect do |record|
75
+ element_id = "#{object_name}_#{attribute}_#{record.send(record_id)}"
76
+ radiobutton = radio_button_tag("#{object_name}[#{attribute}][]", record.send(record_id), object.send(attribute) == record.send(record_id), :id => element_id)
77
+
78
+ content_tag(:li) do
79
+ content_tag(:label) do
80
+ radiobutton + content_tag(:span, record.send(record_name))
81
+ end
82
+ end
83
+ end.join('').html_safe
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ def uneditable_field(name, *args)
91
+ @name = name
92
+ @options = args.extract_options!
93
+ @args = args
94
+
95
+ clearfix_div do
96
+ label_field + input_div do
97
+ extras do
98
+ content_tag(:span, :class => 'uneditable-input') do
99
+ @options[:value] || object.send(@name.to_sym)
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
105
+
106
+ def submit(name = nil, *args)
107
+ @name = name
108
+ @options = args.extract_options!
109
+ @args = args
110
+
111
+ @options[:class] = 'btn btn-primary'
112
+
113
+ content_tag(:div, :class => 'form-actions') do
114
+ super(name, *(args << @options)) + ' ' + link_to('Cancel', @options[:back_path] || :back, :class => 'btn cancel')
115
+ end
116
+ end
117
+
118
+ private
119
+ def clearfix_div(&block)
120
+ @options[:error] = object.errors[@name].collect{|e| "#{@options[:label] || @name} #{e}".humanize}.join(', ') unless object.errors[@name].empty?
121
+
122
+ klasses = ['control-group']
123
+ klasses << 'error' if @options[:error]
124
+ klasses << 'success' if @options[:success]
125
+ klasses << 'warning' if @options[:warning]
126
+ klass = klasses.join(' ')
127
+
128
+ content_tag(:div, :class => klass, &block)
129
+ end
130
+
131
+ def input_div(&block)
132
+ content_tag(:div, :class => 'controls') do
133
+ if @options[:append] || @options[:prepend]
134
+ klass = 'input-prepend' if @options[:prepend]
135
+ klass = 'input-append' if @options[:append]
136
+ content_tag(:div, :class => klass, &block)
137
+ else
138
+ yield if block_given?
139
+ end
140
+ end
141
+ end
142
+
143
+ def label_field(&block)
144
+ required = object.class.validators_on(@name).any? { |v| v.kind_of? ActiveModel::Validations::PresenceValidator }
145
+ label(@name, block_given? ? block : @options[:label], :class => 'control-label' + (' required' if required))
146
+ end
147
+
148
+ %w(help_inline error success warning help_block append prepend).each do |method_name|
149
+ define_method(method_name) do |*args|
150
+ return '' unless value = @options[method_name.to_sym]
151
+ klass = 'help-inline'
152
+ klass = 'help-block' if method_name == 'help_block'
153
+ klass = 'add-on' if method_name == 'append' || method_name == 'prepend'
154
+ content_tag(:span, value, :class => klass)
155
+ end
156
+ end
157
+
158
+ def extras(&block)
159
+ [prepend, (yield if block_given?), append, help_inline, error, success, warning, help_block].join('').html_safe
160
+ end
161
+
162
+ def objectify_options(options)
163
+ super.except(:label, :help_inline, :error, :success, :warning, :help_block, :prepend, :append)
164
+ end
165
+ end
166
166
  end
@@ -1,14 +1,14 @@
1
1
  require 'bootstrap_forms/form_builder'
2
2
 
3
3
  module ActionView
4
- module Helpers
5
- module FormHelper
6
- def bootstrap_form_for(record, options = {}, &block)
7
- options[:builder] = BootstrapForms::FormBuilder
8
- form_for(record, options) do |f|
9
- f.error_messages.html_safe + capture(f, &block).html_safe
10
- end
11
- end
4
+ module Helpers
5
+ module FormHelper
6
+ def bootstrap_form_for(record, options = {}, &block)
7
+ options[:builder] = BootstrapForms::FormBuilder
8
+ form_for(record, options) do |f|
9
+ f.error_messages.html_safe + capture(f, &block).html_safe
10
+ end
11
+ end
12
12
  end
13
13
  end
14
14
  end
@@ -1,11 +1,11 @@
1
1
  module BootstrapForms
2
- class Railtie < ::Rails::Railtie
3
- config.after_initialize do |app|
4
- require 'bootstrap_forms/initializer'
5
- end
6
-
7
- initializer 'bootstrap_forms.initialize' do |app|
8
-
9
- end
10
- end
2
+ class Railtie < ::Rails::Railtie
3
+ config.after_initialize do |app|
4
+ require 'bootstrap_forms/initializer'
5
+ end
6
+
7
+ initializer 'bootstrap_forms.initialize' do |app|
8
+
9
+ end
10
+ end
11
11
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bootstrap_forms
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-21 00:00:00.000000000 Z
12
+ date: 2012-02-02 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Bootstrap Forms makes Twitter's Bootstrap on Rails easy to use by creating
15
15
  helpful form builders that minimize markup in your views.