foundation_form_builder 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 641a1f19acac3199b1398734ccd81f852057dfdf
4
+ data.tar.gz: 29acdd3bf4eb8b85999dab0e2a0efae033dabbf0
5
+ SHA512:
6
+ metadata.gz: bfe7df075ac97f33f9b4e16b7c67d52add64ac42661638c908258646b82651ab61c3ba4403dda301a9b7685d824fc75e1104ac3cc2b5d49184616ce1ad122466
7
+ data.tar.gz: 83d310b59371eda1a206dc8f74fd011a4237a28b0c4be4f99e64e240c3d9ef6d4ea96f9198f7eea961e4c6a835f2539d0265539f5e177561ea76c5ff7c1db4ae
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # FoundationFormBuilder changelog
2
+
3
+ # 0.1.0
4
+
5
+ 25 Feb 2015
6
+
7
+ Initial release.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in foundation_form_builder.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,51 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features)
6
+ directories %w(lib spec)
7
+
8
+ ## Uncomment to clear the screen before every task
9
+ # clearing :on
10
+
11
+ ## Guard internally checks for changes in the Guardfile and exits.
12
+ ## If you want Guard to automatically start up again, run guard in a
13
+ ## shell loop, e.g.:
14
+ ##
15
+ ## $ while bundle exec guard; do echo "Restarting Guard..."; done
16
+ ##
17
+ ## Note: if you are using the `directories` clause above and you are not
18
+ ## watching the project directory ('.'), then you will want to move
19
+ ## the Guardfile to a watched dir and symlink it back, e.g.
20
+ #
21
+ # $ mkdir config
22
+ # $ mv Guardfile config/
23
+ # $ ln -s config/Guardfile .
24
+ #
25
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
26
+
27
+ # Note: The cmd option is now required due to the increasing number of ways
28
+ # rspec may be run, below are examples of the most common uses.
29
+ # * bundler: 'bundle exec rspec'
30
+ # * bundler binstubs: 'bin/rspec'
31
+ # * spring: 'bin/rspec' (This will use spring if running and you have
32
+ # installed the spring binstubs per the docs)
33
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
34
+ # * 'just' rspec: 'rspec'
35
+
36
+ guard :rspec, cmd: "bundle exec rspec" do
37
+ require "guard/rspec/dsl"
38
+ dsl = Guard::RSpec::Dsl.new(self)
39
+
40
+ # Feel free to open issues for suggestions and improvements
41
+
42
+ # RSpec files
43
+ rspec = dsl.rspec
44
+ watch(rspec.spec_helper) { rspec.spec_dir }
45
+ watch(rspec.spec_support) { rspec.spec_dir }
46
+ watch(rspec.spec_files)
47
+
48
+ # Ruby files
49
+ ruby = dsl.ruby
50
+ dsl.watch_spec_files_for(ruby.lib_files)
51
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Marnen Laibow-Koser
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ [![Code Climate](https://codeclimate.com/github/marnen/foundation_form_builder/badges/gpa.svg)](https://codeclimate.com/github/marnen/foundation_form_builder)
2
+ [![Test Coverage](https://codeclimate.com/github/marnen/foundation_form_builder/badges/coverage.svg)](https://codeclimate.com/github/marnen/foundation_form_builder)
3
+
4
+ # FoundationFormBuilder
5
+
6
+ ## What is this gem for?
7
+
8
+ [Foundation](http://foundation.zurb.com) is an excellent CSS framework, and provides some [lovely components](http://foundation.zurb.com/docs/components/forms.html) for working with forms. Unfortunately, using them with Rails can be a bit of a hassle, as Foundation wants each form input to be wrapped in a `<div>`, with another `<div>` for errors.
9
+
10
+ In addition, I wanted to use something like [Formtastic](http://github.com/justinfrench/formtastic) to DRY up my form field markup, but writing new renderers for Formtastic is non-trivial, and I wanted something a bit simpler anyway, so I rolled my own.
11
+
12
+ This gem is a work in progress. I welcome bug reports and pull requests! It's only tested on Rails 4.2 and Ruby 2.2 so far, but I'd love to support older versions of both Rails and Ruby if it's not too difficult to do so.
13
+
14
+ ## Sounds great! How do I use it?
15
+
16
+ Install the gem, then put the following in your `application.rb` file:
17
+ ```ruby
18
+ config.action_view.default_form_builder = FoundationFormBuilder
19
+ ```
20
+ This tells Rails to use the gem 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.
21
+
22
+ ### Simple usage
23
+
24
+ ```ruby
25
+ form_for @user do |f|
26
+ f.input_div :email
27
+ end
28
+ ```
29
+ will create something like
30
+ ```html
31
+ <form action='users/1' and='all the other standard Rails attributes :)'>
32
+ <div class='email'>
33
+ <label for='user_email'>Email</label>
34
+ <input type='email' id='user_email' name='user[email]' />
35
+ <!-- if there are validation errors, also the following: -->
36
+ <div class='error'>can't be blank</div>
37
+ </div>
38
+ </form>
39
+ ```
40
+
41
+ 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.
42
+
43
+
44
+ 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.
45
+
46
+ ### Advanced usage
47
+
48
+ `input_div` takes several options if you want to override its defaults. `:label` overrides the label text, while `:type` overrides the inferred type (known values are `:date`, `:email`, `:password`, `:select`, `:textarea`, and `:time_zone`; anything else is understood as a text field). `:field` takes a hash of options which are passed through to the underlying Rails form helper.
49
+
50
+ If `type: :select` is specified, then the values for the `select`options must be specified in `:values`, like this:
51
+
52
+ ```ruby
53
+ f.input_div :size, label: 'My size is:', type: :select, values: [['Small', 1], ['Large', 2]], field: {prompt: 'Choose a size'}
54
+ ```
55
+
56
+ which produces:
57
+ ```html
58
+ <div class='size'>
59
+ <label for='product_size'>My size is:</label>
60
+ <select name='product[size]' id='product_size'>
61
+ <option>Choose a size</option>
62
+ <option value='1'>Small</option>
63
+ <option value='2'>Large</option>
64
+ </select>
65
+ <!-- error div if necessary, as above -->
66
+ </div>
67
+ ```
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "foundation_form_builder"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'foundation_form_builder/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "foundation_form_builder"
8
+ spec.version = FoundationFormBuilder::VERSION
9
+ spec.authors = ["Marnen Laibow-Koser"]
10
+ spec.email = ["marnen@marnen.org"]
11
+
12
+ if spec.respond_to?(:metadata)
13
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com' to prevent pushes to rubygems.org, or delete to allow pushes to any server."
14
+ end
15
+
16
+ spec.summary = %q{Rails FormBuilder for use with ZURB Foundation.}
17
+ # spec.description = %q{TODO: Write a longer description or delete this line.}
18
+ spec.homepage = "https://github.com/marnen/foundation_form_builder"
19
+ spec.license = "MIT"
20
+
21
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ [
27
+ ['actionview', '>= 4.2.0'],
28
+ ['activerecord', '>= 4.2.0'],
29
+ ['activesupport', '>= 4.2.0']
30
+ ].each {|gem| spec.add_dependency *gem }
31
+
32
+ [
33
+ ['bundler', '~> 1.8'],
34
+ ['rake', '~> 10.0'],
35
+ 'ffaker',
36
+ 'guard-rspec',
37
+ 'rspec-html-matchers'
38
+ ].each {|gem| spec.add_development_dependency *gem }
39
+ end
@@ -0,0 +1,5 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'foundation_form_builder')
2
+
3
+ FoundationFormBuilder.class_eval do
4
+ self::VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,70 @@
1
+ require 'action_view'
2
+ require 'active_record'
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
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: foundation_form_builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marnen Laibow-Koser
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-02-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionview
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 4.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 4.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 4.2.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 4.2.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: ffaker
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec-html-matchers
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ - marnen@marnen.org
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".travis.yml"
135
+ - CHANGELOG.md
136
+ - Gemfile
137
+ - Guardfile
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - bin/console
142
+ - bin/setup
143
+ - foundation_form_builder.gemspec
144
+ - lib/foundation_form_builder.rb
145
+ - lib/foundation_form_builder/version.rb
146
+ homepage: https://github.com/marnen/foundation_form_builder
147
+ licenses:
148
+ - MIT
149
+ metadata: {}
150
+ post_install_message:
151
+ rdoc_options: []
152
+ require_paths:
153
+ - lib
154
+ required_ruby_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ requirements: []
165
+ rubyforge_project:
166
+ rubygems_version: 2.4.5
167
+ signing_key:
168
+ specification_version: 4
169
+ summary: Rails FormBuilder for use with ZURB Foundation.
170
+ test_files: []