bootstrap_forms 2.0.2 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -5,4 +5,5 @@ pkg/*
5
5
  .DS_Store
6
6
  .rspec
7
7
  .rvmrc
8
+ .project
8
9
  spec/dummy/log
@@ -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 = "2.0.2"
6
+ s.version = "2.0.3"
7
7
  s.author = "Seth Vargo"
8
8
  s.email = "sethvargo@gmail.com"
9
9
  s.homepage = "https://github.com/sethvargo/bootstrap_forms"
@@ -6,4 +6,6 @@ module BootstrapForms
6
6
 
7
7
  autoload :FormBuilder
8
8
  autoload :Helpers
9
+ mattr_accessor(:default_form_builder)
10
+ self.default_form_builder = BootstrapForms::FormBuilder
9
11
  end
@@ -19,7 +19,7 @@ module BootstrapForms
19
19
  end
20
20
  end
21
21
 
22
- %w(collection_select select country_select time_zone_select email_field file_field number_field password_field phone_field range_field search_field telephone_field text_area text_field url_field).each do |method_name|
22
+ %w(collection_select select country_select time_zone_select email_field file_field number_field password_field phone_field range_field search_field telephone_field text_area text_field url_field datetime_select date_select time_select).each do |method_name|
23
23
  define_method(method_name) do |name, *args|
24
24
  @name = name
25
25
  @field_options = args.extract_options!
@@ -27,7 +27,7 @@ module BootstrapForms
27
27
 
28
28
  control_group_div do
29
29
  label_field + input_div do
30
- extras { super(name, *(@args << @field_options)) }
30
+ extras { super(name, *(@args << @field_options.merge(required_attribute))) }
31
31
  end
32
32
  end
33
33
  end
@@ -40,10 +40,11 @@ module BootstrapForms
40
40
 
41
41
  control_group_div do
42
42
  input_div do
43
+ @field_options.merge!(required_attribute)
43
44
  if @field_options[:label] == false || @field_options[:label] == ''
44
45
  extras { super(name, *(@args << @field_options)) }
45
46
  else
46
- label(@name, :class => [ 'checkbox', required_class ].compact.join(' ')) do
47
+ label(@name, :class => 'checkbox') do
47
48
  extras { super(name, *(@args << @field_options)) + (@field_options[:label].blank? ? human_attribute_name : @field_options[:label])}
48
49
  end
49
50
  end
@@ -53,14 +54,14 @@ module BootstrapForms
53
54
 
54
55
  def radio_buttons(name, values = {}, opts = {})
55
56
  @name = name
56
- @field_options = opts
57
+ @field_options = opts.merge(required_attribute)
57
58
  control_group_div do
58
59
  label_field + input_div do
59
- values.map do |text, value|
60
+ values.map do |text, value|
60
61
  if @field_options[:label] == '' || @field_options[:label] == false
61
62
  extras { radio_button(name, value, @field_options) + text }
62
63
  else
63
- label("#{@name}_#{value}", :class => [ 'radio', required_class ].compact.join(' ')) do
64
+ label("#{@name}_#{value}", :class => 'radio') do
64
65
  extras { radio_button(name, value, @field_options) + text }
65
66
  end
66
67
  end
@@ -77,11 +78,12 @@ module BootstrapForms
77
78
  control_group_div do
78
79
  label_field + extras do
79
80
  content_tag(:div, :class => 'controls') do
81
+ options = @field_options.merge(required_attribute)
80
82
  records.collect do |record|
81
- element_id = "#{object_name}_#{attribute}_#{record.send(record_id)}"
82
- checkbox = check_box_tag("#{object_name}[#{attribute}][]", record.send(record_id), [object.send(attribute)].flatten.include?(record.send(record_id)), @field_options.merge({:id => element_id}))
83
+ options[:id] = "#{object_name}_#{attribute}_#{record.send(record_id)}"
84
+ checkbox = check_box_tag("#{object_name}[#{attribute}][]", record.send(record_id), [object.send(attribute)].flatten.include?(record.send(record_id)), options)
83
85
 
84
- content_tag(:label, :class => ['checkbox', ('inline' if @field_options[:inline])].compact.join(' ')) do
86
+ content_tag(:label, :class => ['checkbox', ('inline' if @field_options[:inline])].compact) do
85
87
  checkbox + content_tag(:span, record.send(record_name))
86
88
  end
87
89
  end.join('').html_safe
@@ -98,11 +100,12 @@ module BootstrapForms
98
100
  control_group_div do
99
101
  label_field + extras do
100
102
  content_tag(:div, :class => 'controls') do
103
+ options = @field_options.merge(required_attribute)
101
104
  records.collect do |record|
102
- element_id = "#{object_name}_#{attribute}_#{record.send(record_id)}"
103
- radiobutton = radio_button_tag("#{object_name}[#{attribute}]", record.send(record_id), object.send(attribute) == record.send(record_id), @field_options.merge({:id => element_id}))
105
+ options[:id] = "#{object_name}_#{attribute}_#{record.send(record_id)}"
106
+ radiobutton = radio_button_tag("#{object_name}[#{attribute}]", record.send(record_id), object.send(attribute) == record.send(record_id), options)
104
107
 
105
- content_tag(:label, :class => ['radio', ('inline' if @field_options[:inline])].compact.join(' ')) do
108
+ content_tag(:label, :class => ['radio', ('inline' if @field_options[:inline])].compact) do
106
109
  radiobutton + content_tag(:span, record.send(record_name))
107
110
  end
108
111
  end.join('').html_safe
@@ -119,10 +122,11 @@ module BootstrapForms
119
122
  control_group_div do
120
123
  label_field + input_div do
121
124
  extras do
122
- options = { :class => 'uneditable-input' }
123
- options[:id] = @field_options[:id] if @field_options[:id]
124
- content_tag(:span, options) do
125
- @field_options[:value] || object.send(@name.to_sym) rescue nil
125
+ value = @field_options.delete(:value)
126
+ @field_options[:class] = [@field_options[:class], 'uneditable-input'].compact
127
+
128
+ content_tag(:span, @field_options) do
129
+ value || object.send(@name.to_sym) rescue nil
126
130
  end
127
131
  end
128
132
  end
@@ -134,7 +138,7 @@ module BootstrapForms
134
138
  @field_options = args.extract_options!
135
139
  @args = args
136
140
 
137
- @field_options[:class] = 'btn btn-primary'
141
+ @field_options[:class] ||= 'btn btn-primary'
138
142
  super(name, *(args << @field_options))
139
143
  end
140
144
 
@@ -143,13 +147,14 @@ module BootstrapForms
143
147
  @field_options = args.extract_options!
144
148
  @args = args
145
149
 
146
- @field_options[:class] = 'btn btn-primary'
150
+ @field_options[:class] ||= 'btn btn-primary'
147
151
  super(name, *(args << @field_options))
148
152
  end
149
153
 
150
154
  def cancel(*args)
151
155
  @field_options = args.extract_options!
152
- link_to(I18n.t('bootstrap_forms.buttons.cancel'), (@field_options[:back] || :back), :class => 'btn cancel')
156
+ @field_options[:class] ||= 'btn cancel'
157
+ link_to(I18n.t('bootstrap_forms.buttons.cancel'), (@field_options[:back] || :back), :class => @field_options[:class])
153
158
  end
154
159
 
155
160
  def actions(&block)
@@ -2,7 +2,7 @@ module BootstrapForms
2
2
  module Helpers
3
3
  module FormHelper
4
4
  def bootstrap_form_for(record, options = {}, &block)
5
- options[:builder] = BootstrapForms::FormBuilder
5
+ options[:builder] = options[:builder] || BootstrapForms.default_form_builder
6
6
  form_for(record, options) do |f|
7
7
  if f.object.respond_to?(:errors)
8
8
  f.error_messages.html_safe + capture(f, &block).html_safe
@@ -13,7 +13,7 @@ module BootstrapForms
13
13
  end
14
14
 
15
15
  def bootstrap_fields_for(record, options = {}, &block)
16
- options[:builder] = BootstrapForms::FormBuilder
16
+ options[:builder] = options[:builder] || BootstrapForms.default_form_builder
17
17
  fields_for(record, nil, options, &block)
18
18
  end
19
19
  end
@@ -14,9 +14,8 @@ module BootstrapForms
14
14
  klasses << 'error' if @field_options[:error]
15
15
  klasses << 'success' if @field_options[:success]
16
16
  klasses << 'warning' if @field_options[:warning]
17
- klass = klasses.join(' ')
18
17
 
19
- content_tag(:div, :class => klass, &block)
18
+ content_tag(:div, :class => klasses, &block)
20
19
  end
21
20
 
22
21
  def error_string
@@ -52,19 +51,18 @@ module BootstrapForms
52
51
  return ''.html_safe
53
52
  else
54
53
  if respond_to?(:object)
55
- label(@name, block_given? ? block : @field_options[:label], :class => ['control-label', required_class].compact.join(' '))
54
+ label(@name, block_given? ? block : @field_options[:label], :class => 'control-label')
56
55
  else
57
- label_tag(@name, block_given? ? block : @field_options[:label], :class => ['control-label', required_class].compact.join(' '))
56
+ label_tag(@name, block_given? ? block : @field_options[:label], :class => 'control-label')
58
57
  end
59
58
  end
60
59
  end
61
60
 
62
- def required_class
63
- return 'required' if @field_options[:required]
64
- if respond_to?(:object) and object.respond_to?(:errors)
65
- return 'required' if object.class.validators_on(@name).any? { |v| v.kind_of? ActiveModel::Validations::PresenceValidator }
61
+ def required_attribute
62
+ if respond_to?(:object) and object.respond_to?(:errors) and object.class.respond_to?('validators_on')
63
+ return { :required => true } if object.class.validators_on(@name).any? { |v| v.kind_of? ActiveModel::Validations::PresenceValidator }
66
64
  end
67
- nil
65
+ {}
68
66
  end
69
67
 
70
68
  %w(help_inline error success warning help_block append prepend).each do |method_name|
@@ -0,0 +1,5 @@
1
+ class MyCustomFormBuilder < ActionView::Helpers::FormBuilder
2
+ def error_messages
3
+ '' # return empty string
4
+ end
5
+ end
@@ -1,4 +1,5 @@
1
1
  class Project < ActiveRecord::Base
2
2
  has_many :tasks
3
3
  accepts_nested_attributes_for :tasks
4
+ validates :owner, :presence => true
4
5
  end
@@ -2,6 +2,8 @@ require File.expand_path('../boot', __FILE__)
2
2
 
3
3
  require 'rails/all'
4
4
 
5
+ require 'bundler'
6
+ Bundler.setup
5
7
  Bundler.require
6
8
  require "bootstrap_forms"
7
9
 
Binary file
@@ -0,0 +1,9 @@
1
+ class AddProjectOwner < ActiveRecord::Migration
2
+ def up
3
+ add_column :projects, :owner, :string
4
+ end
5
+
6
+ def down
7
+ remove_column :projects, :owner
8
+ end
9
+ end
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  # This file is auto-generated from the current state of the database. Instead
2
3
  # of editing this file, please use the migrations feature of Active Record to
3
4
  # incrementally modify your database, and then regenerate this schema definition.
@@ -10,7 +11,7 @@
10
11
  #
11
12
  # It's strongly recommended to check this file into your version control system.
12
13
 
13
- ActiveRecord::Schema.define(:version => 20110710143903) do
14
+ ActiveRecord::Schema.define(:version => 20120828002121) do
14
15
 
15
16
  create_table "milestones", :force => true do |t|
16
17
  t.integer "task_id"
@@ -19,6 +20,7 @@ ActiveRecord::Schema.define(:version => 20110710143903) do
19
20
 
20
21
  create_table "projects", :force => true do |t|
21
22
  t.string "name"
23
+ t.string "owner"
22
24
  end
23
25
 
24
26
  create_table "tasks", :force => true do |t|
Binary file
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe "bootstrap_form_for" do
4
+ describe "default_form_builder" do
5
+ it "should be accessible" do
6
+ BootstrapForms.should respond_to(:default_form_builder)
7
+ end
8
+
9
+ it "should be the BootstrapForms form_builder by default" do
10
+ BootstrapForms.default_form_builder.should == BootstrapForms::FormBuilder
11
+ end
12
+
13
+ context "when set to something else" do
14
+ before do
15
+ BootstrapForms.default_form_builder = MyCustomFormBuilder
16
+ end
17
+
18
+ it "should be that other thing" do
19
+ BootstrapForms.default_form_builder.should == MyCustomFormBuilder
20
+ end
21
+
22
+ describe "projects/new.html.erb", :type => :view do
23
+ before do
24
+ assign :project, Project.new
25
+ render :file => "projects/new.html.erb", :layout => "layouts/application.html.erb"
26
+ end
27
+
28
+ it "should render with the other form builder" do
29
+ # in other words, it shouldn't be wrapped with the bootstrap stuff
30
+ rendered.should_not match /<div class=\"control-group\"><label class=\"control-label\" for=\"item_name\">Name<\/label><div class=\"controls\">.*<\/div><\/div>/
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -73,6 +73,12 @@ describe "BootstrapForms::FormBuilder" do
73
73
  end
74
74
  end
75
75
 
76
+ context "an attribute with a PresenceValidator" do
77
+ it "adds the required attribute" do
78
+ @builder.text_field("owner").should match /<input .*required="required"/
79
+ end
80
+ end
81
+
76
82
  context 'submit' do
77
83
  it 'checks persistence of object' do
78
84
  @builder.submit.should match('Create Project')
@@ -96,3 +102,19 @@ describe "BootstrapForms::FormBuilder" do
96
102
  it_behaves_like 'a bootstrap form'
97
103
  end
98
104
  end
105
+
106
+ describe 'BootstrapForms::Helpers::FormTagHelper' do
107
+ describe '#bootstrap_text_field_tag' do
108
+ context 'with no method "validators_on"' do
109
+ before(:each) do
110
+ @non_active_record_object = {:attributes => [:id, :name]}
111
+ @template = ActionView::Base.new
112
+ @template.output_buffer = ""
113
+ @builder = BootstrapForms::FormBuilder.new(:item, @non_active_record_object, @template, {}, proc {})
114
+ end
115
+ it 'returns an empty string with no errors' do
116
+ @template.bootstrap_text_field_tag(@builder.object[:name]).should match /<div class="control-group">.*/
117
+ end
118
+ end
119
+ end
120
+ end
data/spec/spec_helper.rb CHANGED
@@ -19,6 +19,7 @@ RSpec.configure do |config|
19
19
  config.treat_symbols_as_metadata_keys_with_true_values = true
20
20
  config.run_all_when_everything_filtered = true
21
21
  config.filter_run :focus
22
+ config.include RSpec::Rails::ViewExampleGroup, :type => :view
22
23
  end
23
24
 
24
- Rails.backtrace_cleaner.remove_silencers!
25
+ Rails.backtrace_cleaner.remove_silencers!
@@ -139,9 +139,10 @@ shared_examples "a bootstrap form" do
139
139
  @builder.text_field(:name, :append => '@', :prepend => '#').should == "<div class=\"control-group\"><label class=\"control-label\" for=\"item_name\">Name</label><div class=\"controls\"><div class=\"input-prepend input-append\"><span class=\"add-on\">\#</span><input id=\"item_name\" name=\"item[name]\" size=\"30\" type=\"text\" /><span class=\"add-on\">@</span></div></div></div>"
140
140
  end
141
141
  end
142
- context "label option" do
143
- %w(select email_field file_field number_field password_field search_field text_area text_field url_field).each do |method_name|
144
142
 
143
+ context "label option" do
144
+ %w(select email_field file_field number_field password_field search_field text_area text_field url_field).each do |method_name|
145
+
145
146
  it "should not add a label when ''" do
146
147
  @builder.send(method_name.to_sym, 'name', :label => '').should_not match /<\/label>/
147
148
  end
@@ -163,22 +164,35 @@ shared_examples "a bootstrap form" do
163
164
  end
164
165
 
165
166
  context "submit" do
166
- it "adds btn primary class" do
167
+ it "adds btn primary class if no class is defined" do
167
168
  @builder.submit.should match /class=\"btn btn-primary\"/
168
169
  end
170
+
171
+ it "allows for custom classes" do
172
+ @builder.submit(:class => 'btn btn-large btn-success').should match /class=\"btn btn-large btn-success\"/
173
+ end
169
174
  end
170
175
 
171
176
  context "button" do
172
- it "adds btn primary class" do
177
+ it "adds btn primary class if no class is defined" do
173
178
  @builder.button.should match /class=\"btn btn-primary\"/
174
179
  end
180
+
181
+ it "allows for custom classes" do
182
+ @builder.button(:class => 'btn btn-large btn-success').should match /class=\"btn btn-large btn-success\"/
183
+ end
175
184
  end
176
185
 
177
186
  context "cancel" do
178
- it "creates link with correct class" do
187
+ it "creates a link with cancel btn class if no class is defined" do
179
188
  @builder.should_receive(:link_to).with(I18n.t('bootstrap_forms.buttons.cancel'), :back, :class => 'btn cancel').and_return("")
180
189
  @builder.cancel
181
190
  end
191
+
192
+ it "creates a link with custom classes when defined" do
193
+ @builder.should_receive(:link_to).with(I18n.t('bootstrap_forms.buttons.cancel'), :back, :class => 'btn btn-large my-cancel').and_return("")
194
+ @builder.cancel(:class => 'btn btn-large my-cancel')
195
+ end
182
196
  end
183
197
  end # actions
184
198
  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: 2.0.2
4
+ version: 2.0.3
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-07-16 00:00:00.000000000 Z
12
+ date: 2012-09-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec-rails
@@ -139,6 +139,7 @@ files:
139
139
  - spec/dummy/app/controllers/application_controller.rb
140
140
  - spec/dummy/app/controllers/projects_controller.rb
141
141
  - spec/dummy/app/helpers/application_helper.rb
142
+ - spec/dummy/app/helpers/my_custom_form_builder.rb
142
143
  - spec/dummy/app/helpers/projects_helper.rb
143
144
  - spec/dummy/app/mailers/.gitkeep
144
145
  - spec/dummy/app/models/.gitkeep
@@ -165,6 +166,7 @@ files:
165
166
  - spec/dummy/config/routes.rb
166
167
  - spec/dummy/db/development.sqlite3
167
168
  - spec/dummy/db/migrate/20110710143903_initial_tables.rb
169
+ - spec/dummy/db/migrate/20120828002121_add_project_owner.rb
168
170
  - spec/dummy/db/schema.rb
169
171
  - spec/dummy/db/test.sqlite3
170
172
  - spec/dummy/public/404.html
@@ -175,6 +177,7 @@ files:
175
177
  - spec/dummy/test/functional/projects_controller_test.rb
176
178
  - spec/dummy/test/unit/helpers/projects_helper_test.rb
177
179
  - spec/dummy/tmp/cache/.gitkeep
180
+ - spec/lib/bootstrap_forms/bootstrap_form_for_spec.rb
178
181
  - spec/lib/bootstrap_forms/form_builder_spec.rb
179
182
  - spec/spec_helper.rb
180
183
  - spec/support/shared_context.rb
@@ -192,7 +195,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
192
195
  version: '0'
193
196
  segments:
194
197
  - 0
195
- hash: 3940498627574462359
198
+ hash: -2577927973588577500
196
199
  required_rubygems_version: !ruby/object:Gem::Requirement
197
200
  none: false
198
201
  requirements:
@@ -201,7 +204,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
201
204
  version: '0'
202
205
  segments:
203
206
  - 0
204
- hash: 3940498627574462359
207
+ hash: -2577927973588577500
205
208
  requirements: []
206
209
  rubyforge_project:
207
210
  rubygems_version: 1.8.24
@@ -215,6 +218,7 @@ test_files:
215
218
  - spec/dummy/app/controllers/application_controller.rb
216
219
  - spec/dummy/app/controllers/projects_controller.rb
217
220
  - spec/dummy/app/helpers/application_helper.rb
221
+ - spec/dummy/app/helpers/my_custom_form_builder.rb
218
222
  - spec/dummy/app/helpers/projects_helper.rb
219
223
  - spec/dummy/app/mailers/.gitkeep
220
224
  - spec/dummy/app/models/.gitkeep
@@ -241,6 +245,7 @@ test_files:
241
245
  - spec/dummy/config/routes.rb
242
246
  - spec/dummy/db/development.sqlite3
243
247
  - spec/dummy/db/migrate/20110710143903_initial_tables.rb
248
+ - spec/dummy/db/migrate/20120828002121_add_project_owner.rb
244
249
  - spec/dummy/db/schema.rb
245
250
  - spec/dummy/db/test.sqlite3
246
251
  - spec/dummy/public/404.html
@@ -251,6 +256,7 @@ test_files:
251
256
  - spec/dummy/test/functional/projects_controller_test.rb
252
257
  - spec/dummy/test/unit/helpers/projects_helper_test.rb
253
258
  - spec/dummy/tmp/cache/.gitkeep
259
+ - spec/lib/bootstrap_forms/bootstrap_form_for_spec.rb
254
260
  - spec/lib/bootstrap_forms/form_builder_spec.rb
255
261
  - spec/spec_helper.rb
256
262
  - spec/support/shared_context.rb