foundation_form_builder 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +4 -3
- data/lib/foundation_form_builder/rails.rb +72 -0
- data/lib/foundation_form_builder/version.rb +2 -4
- data/lib/foundation_form_builder.rb +2 -70
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9ccd7ad463eab8551fc51c89a681dd0d0a1628ab
|
4
|
+
data.tar.gz: a6e6044e7f35b82953b980a370d714621729de54
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d5daccccdb4a1a6c2d90ff3c0000c44395281f533211c49ae826cf9f9eb5b9e645d6f7c529764febf8d16700889f21bea60810301d5f22372b98fa45a7b510e
|
7
|
+
data.tar.gz: 8fcfbccf0e256f5181da5ecaa0755d755c57ed57ff718dda1342fd2b3ee03f9d3b2c7e26ca97940ff1592dda6fc98d4ea1f0516b44fa11fa2bf5907893e4ca33
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/marnen/foundation_form_builder.svg)](https://travis-ci.org/marnen/foundation_form_builder)
|
1
2
|
[![Code Climate](https://codeclimate.com/github/marnen/foundation_form_builder/badges/gpa.svg)](https://codeclimate.com/github/marnen/foundation_form_builder)
|
2
3
|
[![Test Coverage](https://codeclimate.com/github/marnen/foundation_form_builder/badges/coverage.svg)](https://codeclimate.com/github/marnen/foundation_form_builder)
|
3
4
|
|
@@ -19,9 +20,9 @@ gem install foundation_form_builder
|
|
19
20
|
```
|
20
21
|
Then put the following in your `application.rb` file:
|
21
22
|
```ruby
|
22
|
-
config.action_view.default_form_builder = FoundationFormBuilder
|
23
|
+
config.action_view.default_form_builder = FoundationFormBuilder::Rails
|
23
24
|
```
|
24
|
-
This tells Rails to use
|
25
|
+
This tells Rails to use `FoundationFormBuilder::Rails` for all forms. If you don't want to do that, then specify it for the particular form you'd like to use it on.
|
25
26
|
|
26
27
|
### Simple usage
|
27
28
|
|
@@ -42,7 +43,7 @@ will create something like
|
|
42
43
|
</form>
|
43
44
|
```
|
44
45
|
|
45
|
-
Note that `FoundationFormBuilder` uses the Rails form helper functions to render the fields, and that it is somewhat smart in its choice of helpers. It uses `email_field` automatically if the name of the field is `email`; similarly, it uses `password_field` for any field whose name *contains* `password`. If the name of the field is `time_zone`, it renders a Rails `time_zone_select`. If the field corresponds to a `text` column in the database, it calls `text_area`; for a `date` column, it uses `date_field`. Otherwise, a plain `text_field` is used.
|
46
|
+
Note that `FoundationFormBuilder::Rails` uses the Rails form helper functions to render the fields, and that it is somewhat smart in its choice of helpers. It uses `email_field` automatically if the name of the field is `email`; similarly, it uses `password_field` for any field whose name *contains* `password`. If the name of the field is `time_zone`, it renders a Rails `time_zone_select`. If the field corresponds to a `text` column in the database, it calls `text_area`; for a `date` column, it uses `date_field`. Otherwise, a plain `text_field` is used.
|
46
47
|
|
47
48
|
|
48
49
|
Since `FoundationFormBuilder` is a subclass of the standard Rails `ActionView::Helpers::FormBuilder`, all the usual Rails `FormBuilder` helpers are also available. You'll probably need to use `f.submit` at least; we're not doing anything special with submit buttons at the moment.
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'action_view'
|
2
|
+
require 'active_record'
|
3
|
+
require 'active_support/time_with_zone'
|
4
|
+
|
5
|
+
module FoundationFormBuilder
|
6
|
+
class Rails < ActionView::Helpers::FormBuilder
|
7
|
+
def input_div(field_name, label: nil, type: nil, values: nil, field: {})
|
8
|
+
raise ArgumentError, ':values is only meaningful with type: :select' if values && type != :select
|
9
|
+
@template.content_tag :div, class: field_name do
|
10
|
+
[
|
11
|
+
label(field_name, label),
|
12
|
+
input_for(field_name, type, field, values: values),
|
13
|
+
error_div(field_name)
|
14
|
+
].compact.join("\n").html_safe
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def error_div(field_name)
|
21
|
+
error_messages = errors[field_name]
|
22
|
+
if error_messages.present?
|
23
|
+
@template.content_tag :div, class: :error do
|
24
|
+
error_messages.join(@template.tag :br).html_safe
|
25
|
+
end
|
26
|
+
else
|
27
|
+
nil
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def errors
|
32
|
+
@errors ||= @object.errors
|
33
|
+
end
|
34
|
+
|
35
|
+
def input_for(field_name, type, field_options, values: nil)
|
36
|
+
type ||= infer_type field_name
|
37
|
+
|
38
|
+
case type
|
39
|
+
when :select
|
40
|
+
select field_name, values, field_options
|
41
|
+
when :time_zone
|
42
|
+
priority_zones = field_options.delete(:priority_zones)
|
43
|
+
time_zone_select field_name, priority_zones, field_options
|
44
|
+
else
|
45
|
+
method_mappings = {
|
46
|
+
date: :date_field,
|
47
|
+
email: :email_field,
|
48
|
+
password: :password_field,
|
49
|
+
textarea: :text_area,
|
50
|
+
}
|
51
|
+
|
52
|
+
field_method = method_mappings[type] || :text_field
|
53
|
+
|
54
|
+
self.send field_method, field_name, field_options
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def infer_type(field_name)
|
59
|
+
case field_name
|
60
|
+
when :email, :time_zone
|
61
|
+
field_name
|
62
|
+
when %r{(\b|_)password(\b|_)}
|
63
|
+
:password
|
64
|
+
else
|
65
|
+
type_mappings = {text: :textarea}
|
66
|
+
|
67
|
+
db_type = @object.column_for_attribute(field_name).type
|
68
|
+
type_mappings[db_type] || db_type
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -1,70 +1,2 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
3
|
-
require 'active_support/time_with_zone'
|
4
|
-
|
5
|
-
class FoundationFormBuilder < ActionView::Helpers::FormBuilder
|
6
|
-
def input_div(field_name, label: nil, type: nil, values: nil, field: {})
|
7
|
-
raise ArgumentError, ':values is only meaningful with type: :select' if values && type != :select
|
8
|
-
@template.content_tag :div, class: field_name do
|
9
|
-
[
|
10
|
-
label(field_name, label),
|
11
|
-
input_for(field_name, type, field, values: values),
|
12
|
-
error_div(field_name)
|
13
|
-
].compact.join("\n").html_safe
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
private
|
18
|
-
|
19
|
-
def error_div(field_name)
|
20
|
-
error_messages = errors[field_name]
|
21
|
-
if error_messages.present?
|
22
|
-
@template.content_tag :div, class: :error do
|
23
|
-
error_messages.join(@template.tag :br).html_safe
|
24
|
-
end
|
25
|
-
else
|
26
|
-
nil
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def errors
|
31
|
-
@errors ||= @object.errors
|
32
|
-
end
|
33
|
-
|
34
|
-
def input_for(field_name, type, field_options, values: nil)
|
35
|
-
type ||= infer_type field_name
|
36
|
-
|
37
|
-
case type
|
38
|
-
when :select
|
39
|
-
select field_name, values, field_options
|
40
|
-
when :time_zone
|
41
|
-
priority_zones = field_options.delete(:priority_zones)
|
42
|
-
time_zone_select field_name, priority_zones, field_options
|
43
|
-
else
|
44
|
-
method_mappings = {
|
45
|
-
date: :date_field,
|
46
|
-
email: :email_field,
|
47
|
-
password: :password_field,
|
48
|
-
textarea: :text_area,
|
49
|
-
}
|
50
|
-
|
51
|
-
field_method = method_mappings[type] || :text_field
|
52
|
-
|
53
|
-
self.send field_method, field_name, field_options
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
def infer_type(field_name)
|
58
|
-
case field_name
|
59
|
-
when :email, :time_zone
|
60
|
-
field_name
|
61
|
-
when %r{(\b|_)password(\b|_)}
|
62
|
-
:password
|
63
|
-
else
|
64
|
-
type_mappings = {text: :textarea}
|
65
|
-
|
66
|
-
db_type = @object.column_for_attribute(field_name).type
|
67
|
-
type_mappings[db_type] || db_type
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
1
|
+
require 'foundation_form_builder/version'
|
2
|
+
require 'foundation_form_builder/rails'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: foundation_form_builder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marnen Laibow-Koser
|
@@ -142,6 +142,7 @@ files:
|
|
142
142
|
- bin/setup
|
143
143
|
- foundation_form_builder.gemspec
|
144
144
|
- lib/foundation_form_builder.rb
|
145
|
+
- lib/foundation_form_builder/rails.rb
|
145
146
|
- lib/foundation_form_builder/version.rb
|
146
147
|
homepage: https://github.com/marnen/foundation_form_builder
|
147
148
|
licenses:
|