simple_bootstrap_form 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +1 -0
  3. data/Gemfile +9 -0
  4. data/Guardfile +9 -0
  5. data/README.md +84 -1
  6. data/Rakefile +9 -0
  7. data/circle.yml +5 -0
  8. data/lib/simple_bootstrap_form/action_view_extensions.rb +5 -5
  9. data/lib/simple_bootstrap_form/css_class_list.rb +1 -1
  10. data/lib/simple_bootstrap_form/field_factory.rb +58 -0
  11. data/lib/simple_bootstrap_form/horizontal_form/fields/base_field.rb +118 -0
  12. data/lib/simple_bootstrap_form/horizontal_form/fields/boolean_field.rb +16 -0
  13. data/lib/simple_bootstrap_form/horizontal_form/fields/datetime_field.rb +55 -0
  14. data/lib/simple_bootstrap_form/horizontal_form/fields/email_field.rb +10 -0
  15. data/lib/simple_bootstrap_form/horizontal_form/fields/password_field.rb +10 -0
  16. data/lib/simple_bootstrap_form/horizontal_form/fields/text_field.rb +10 -0
  17. data/lib/simple_bootstrap_form/horizontal_form/fields/textarea_field.rb +14 -0
  18. data/lib/simple_bootstrap_form/horizontal_form/form_builder.rb +59 -0
  19. data/lib/simple_bootstrap_form/version.rb +1 -1
  20. data/lib/simple_bootstrap_form.rb +9 -8
  21. data/simple_bootstrap_form.gemspec +1 -1
  22. data/spec/field_factory_spec.rb +65 -0
  23. data/spec/simple_bootstrap_form_spec.rb +360 -83
  24. data/spec/spec_helper.rb +3 -0
  25. data/spec/support/have_element.rb +109 -0
  26. metadata +19 -13
  27. data/lib/simple_bootstrap_form/fields/base_field.rb +0 -103
  28. data/lib/simple_bootstrap_form/fields/boolean_field.rb +0 -14
  29. data/lib/simple_bootstrap_form/fields/datetime_field.rb +0 -53
  30. data/lib/simple_bootstrap_form/fields/email_field.rb +0 -8
  31. data/lib/simple_bootstrap_form/fields/password_field.rb +0 -8
  32. data/lib/simple_bootstrap_form/fields/text_field.rb +0 -8
  33. data/lib/simple_bootstrap_form/fields/textarea_field.rb +0 -12
  34. data/lib/simple_bootstrap_form/form_builder.rb +0 -51
  35. data/spec/support/input_matchers.rb +0 -72
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_bootstrap_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Pierson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-14 00:00:00.000000000 Z
11
+ date: 2014-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: capybara
70
+ name: nokogiri
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -103,23 +103,27 @@ extensions: []
103
103
  extra_rdoc_files: []
104
104
  files:
105
105
  - ".gitignore"
106
+ - ".rspec"
106
107
  - ".ruby-gemset"
107
108
  - ".ruby-version"
108
109
  - Gemfile
110
+ - Guardfile
109
111
  - LICENSE.txt
110
112
  - README.md
111
113
  - Rakefile
114
+ - circle.yml
112
115
  - lib/simple_bootstrap_form.rb
113
116
  - lib/simple_bootstrap_form/action_view_extensions.rb
114
117
  - lib/simple_bootstrap_form/css_class_list.rb
115
- - lib/simple_bootstrap_form/fields/base_field.rb
116
- - lib/simple_bootstrap_form/fields/boolean_field.rb
117
- - lib/simple_bootstrap_form/fields/datetime_field.rb
118
- - lib/simple_bootstrap_form/fields/email_field.rb
119
- - lib/simple_bootstrap_form/fields/password_field.rb
120
- - lib/simple_bootstrap_form/fields/text_field.rb
121
- - lib/simple_bootstrap_form/fields/textarea_field.rb
122
- - lib/simple_bootstrap_form/form_builder.rb
118
+ - lib/simple_bootstrap_form/field_factory.rb
119
+ - lib/simple_bootstrap_form/horizontal_form/fields/base_field.rb
120
+ - lib/simple_bootstrap_form/horizontal_form/fields/boolean_field.rb
121
+ - lib/simple_bootstrap_form/horizontal_form/fields/datetime_field.rb
122
+ - lib/simple_bootstrap_form/horizontal_form/fields/email_field.rb
123
+ - lib/simple_bootstrap_form/horizontal_form/fields/password_field.rb
124
+ - lib/simple_bootstrap_form/horizontal_form/fields/text_field.rb
125
+ - lib/simple_bootstrap_form/horizontal_form/fields/textarea_field.rb
126
+ - lib/simple_bootstrap_form/horizontal_form/form_builder.rb
123
127
  - lib/simple_bootstrap_form/version.rb
124
128
  - simple_bootstrap_form.gemspec
125
129
  - spec/dummy/.rspec
@@ -165,9 +169,10 @@ files:
165
169
  - spec/dummy/public/422.html
166
170
  - spec/dummy/public/500.html
167
171
  - spec/dummy/public/favicon.ico
172
+ - spec/field_factory_spec.rb
168
173
  - spec/simple_bootstrap_form_spec.rb
169
174
  - spec/spec_helper.rb
170
- - spec/support/input_matchers.rb
175
+ - spec/support/have_element.rb
171
176
  homepage: https://github.com/Piersonally/simple_bootstrap_form
172
177
  licenses:
173
178
  - MIT
@@ -236,6 +241,7 @@ test_files:
236
241
  - spec/dummy/public/422.html
237
242
  - spec/dummy/public/500.html
238
243
  - spec/dummy/public/favicon.ico
244
+ - spec/field_factory_spec.rb
239
245
  - spec/simple_bootstrap_form_spec.rb
240
246
  - spec/spec_helper.rb
241
- - spec/support/input_matchers.rb
247
+ - spec/support/have_element.rb
@@ -1,103 +0,0 @@
1
- module SimpleBootstrapForm
2
- module Fields
3
- class BaseField
4
-
5
- class << self
6
- attr_accessor :type
7
- end
8
-
9
- def initialize(form_builder, template, name, options)
10
- @form_builder = form_builder
11
- @template = template
12
- @name = name
13
- @options = options.dup
14
- end
15
-
16
- def to_s
17
- @template.content_tag :div, group_options do
18
- field_label +
19
- input_tag +
20
- errors_block
21
- end
22
- end
23
-
24
- private
25
-
26
- def group_options
27
- css_classes = CssClassList.new 'form-group', group_name
28
- css_classes << 'has-error' if has_error?
29
- { class: css_classes }
30
- end
31
-
32
- def group_name # added as a class on form group to make it more testable
33
- "#{@form_builder.object_name.to_s.underscore}_#{@name}_group"
34
- end
35
-
36
- def field_label
37
- @form_builder.label @name, label_text, label_options
38
- end
39
-
40
- def label_text
41
- text = @options[:label] || @name.to_s.humanize.capitalize
42
- required_asterisk + text
43
- end
44
-
45
- def required_asterisk
46
- if required?
47
- @template.content_tag(:abbr, '*', title: 'required') + ' '
48
- else
49
- ''
50
- end.html_safe
51
- end
52
-
53
- def label_options
54
- css_classes = CssClassList.new 'control-label col-sm-3'
55
- { class: css_classes }
56
- end
57
-
58
- def input_tag
59
- @template.content_tag(:div, class: 'col-sm-6') do
60
- @form_builder.text_field @name, input_options
61
- end
62
- end
63
-
64
- def input_options
65
- @options.merge! class: 'form-control',
66
- placeholder: placeholder,
67
- type: self.class.type
68
- @options.merge! required: 'required' if required?
69
- @options
70
- end
71
-
72
- def placeholder
73
- @options[:placeholder] || @name.to_s.humanize
74
- end
75
-
76
- def errors_block
77
- return '' unless errors.any?
78
- @template.content_tag :span, errors.join(', '),
79
- class: 'help-block col-sm-3'
80
- end
81
-
82
- def model
83
- @form_builder.object
84
- end
85
-
86
- def errors
87
- return [] unless model
88
- @cached_errors ||= model.errors[@name.to_sym]
89
- end
90
-
91
- def required?
92
- return false unless model
93
- model.class.validators_on(@name).any? do |validator|
94
- validator.kind_of? ActiveModel::Validations::PresenceValidator
95
- end
96
- end
97
-
98
- def has_error?
99
- errors.any?
100
- end
101
- end
102
- end
103
- end
@@ -1,14 +0,0 @@
1
- module SimpleBootstrapForm
2
- module Fields
3
- class BooleanField < BaseField
4
-
5
- self.type = 'checkbox'
6
-
7
- def input_tag
8
- @template.content_tag(:div, class: 'col-sm-6') do
9
- @form_builder.check_box @name
10
- end
11
- end
12
- end
13
- end
14
- end
@@ -1,53 +0,0 @@
1
- module SimpleBootstrapForm
2
- module Fields
3
- class DatetimeField < BaseField
4
-
5
- self.type = 'datetime'
6
-
7
- def input_tag
8
- @template.content_tag(:div, class: 'col-sm-6') do
9
- @template.content_tag :div, class: 'input-group' do
10
- @form_builder.text_field(@name, input_options) +
11
- calendar_icon
12
- end
13
- end
14
- end
15
-
16
- private
17
-
18
- def calendar_icon
19
- @template.content_tag(:div, class: 'input-group-addon') do
20
- @template.content_tag(:span, '',
21
- class: 'glyphicon glyphicon-calendar',
22
- data: { 'activate-datepicker' => "##{tag_id}" }
23
- )
24
- end
25
- end
26
-
27
- def input_options
28
- super
29
- @options.merge! value: value_suitable_for_use_by_jquery_datetimepicker
30
- @options
31
- end
32
-
33
- def value_suitable_for_use_by_jquery_datetimepicker
34
- @form_builder.object.send(@name).try(:strftime, '%Y/%m/%d %H:%M')
35
- end
36
-
37
- # Adapted from module ActionView::Helpers::Tags::Base
38
- def tag_id
39
- "#{sanitized_object_name}_#{sanitized_method_name}"
40
- end
41
-
42
- # Adapted from module ActionView::Helpers::Tags::Base
43
- def sanitized_object_name
44
- @form_builder.object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
45
- end
46
-
47
- # Adapted from module ActionView::Helpers::Tags::Base
48
- def sanitized_method_name
49
- @name.to_s.sub(/\?$/,"")
50
- end
51
- end
52
- end
53
- end
@@ -1,8 +0,0 @@
1
- module SimpleBootstrapForm
2
- module Fields
3
- class EmailField < BaseField
4
-
5
- self.type = 'email'
6
- end
7
- end
8
- end
@@ -1,8 +0,0 @@
1
- module SimpleBootstrapForm
2
- module Fields
3
- class PasswordField < BaseField
4
-
5
- self.type = 'password'
6
- end
7
- end
8
- end
@@ -1,8 +0,0 @@
1
- module SimpleBootstrapForm
2
- module Fields
3
- class TextField < BaseField
4
-
5
- self.type = 'text'
6
- end
7
- end
8
- end
@@ -1,12 +0,0 @@
1
- module SimpleBootstrapForm
2
- module Fields
3
- class TextareaField < BaseField
4
-
5
- def input_tag
6
- @template.content_tag(:div, class: 'col-sm-6') do
7
- @form_builder.text_area @name, input_options
8
- end
9
- end
10
- end
11
- end
12
- end
@@ -1,51 +0,0 @@
1
- module SimpleBootstrapForm
2
- class FormBuilder < ActionView::Helpers::FormBuilder
3
-
4
- # Context inherited from ActionView::Helpers::FormBuilder:
5
- #
6
- # @template
7
- # object
8
-
9
- def input(name, options = {})
10
- klass = map_object_attribute_to_field_class name, options
11
- klass.new(self, @template, name, options).to_s
12
- end
13
-
14
- private
15
-
16
- def map_object_attribute_to_field_class(attr, options)
17
- prefix = field_class_prefix attr, options
18
- "SimpleBootstrapForm::Fields::#{prefix}Field".constantize
19
- end
20
-
21
- def field_class_prefix(attr, options)
22
- if options[:as]
23
- options[:as].to_s.capitalize
24
- else
25
- derive_field_class_prefix attr
26
- end
27
- end
28
-
29
- def derive_field_class_prefix(attr)
30
- case attr_column_type(attr)
31
- when :boolean; 'Boolean'
32
- when :datetime; 'Datetime'
33
- when :string; string_field_class_prefix_based_on_column_name(attr)
34
- when :text ; 'Textarea'
35
- else 'Text'
36
- end
37
- end
38
-
39
- def attr_column_type(attr)
40
- object.try(:column_for_attribute, attr).try(:type) || :string
41
- end
42
-
43
- def string_field_class_prefix_based_on_column_name(attr)
44
- x = case
45
- when attr.to_s =~ /email/i ; 'Email'
46
- when attr.to_s =~ /password/i ; 'Password'
47
- else 'Text'
48
- end
49
- end
50
- end
51
- end
@@ -1,72 +0,0 @@
1
- RSpec::Matchers.define :have_input do |input_id|
2
- match_for_should do |markup|
3
- expect(markup).to have_selector selector(input_id)
4
- end
5
-
6
- match_for_should_not do |markup|
7
- !(expect(markup).not_to have_selector selector(input_id))
8
- end
9
-
10
- chain :with_attr_value do |attr, value|
11
- attrs[attr] = value
12
- end
13
-
14
- chain :with_type do |type|
15
- attrs['type'] = type
16
- end
17
-
18
- chain :with_placeholder do |placeholder|
19
- attrs['placeholder'] = placeholder
20
- end
21
-
22
- def attrs
23
- @attrs ||= {}
24
- end
25
-
26
- def selector(input_id)
27
- s = "input#{input_id}"
28
- attrs.each do |attr, value|
29
- s << %([#{attr}="#{value}"])
30
- end
31
- s
32
- end
33
- end
34
-
35
- RSpec::Matchers.define :have_tag do |tag|
36
- match_for_should do |markup|
37
- expect(markup).to have_selector selector(tag)
38
- end
39
-
40
- match_for_should_not do |markup|
41
- !(expect(markup).not_to have_selector selector(tag))
42
- end
43
-
44
- chain :with_id do |id|
45
- @id = id
46
- end
47
-
48
- chain :with_attr_value do |attr, value|
49
- attrs[attr] = value
50
- end
51
-
52
- chain :with_type do |type|
53
- attrs['type'] = type
54
- end
55
-
56
- chain :with_placeholder do |placeholder|
57
- attrs['placeholder'] = placeholder
58
- end
59
-
60
- def attrs
61
- @attrs ||= {}
62
- end
63
-
64
- def selector(tag)
65
- s = tag.to_s
66
- s << "##{@id}" if @id
67
- attrs.each do |attr, value|
68
- s << "[#{attr}=#{value}]"
69
- end
70
- s
71
- end
72
- end