formulate 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .rvmrc
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in formulate.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Tyler Hunt
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # Formulate
2
+
3
+ Rails form builder with flexible markup and styles.
4
+
5
+ Formulate consists of a custom form builder, which replaces the default form
6
+ builder in Rails, and a Sass style sheet that can be customized with variables.
7
+
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's `Gemfile`:
12
+
13
+ ``` ruby
14
+ gem 'formulate'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install formulate
24
+
25
+
26
+ ## Usage
27
+
28
+ The form builder will be used automatically, so you don't need to do anything.
29
+
30
+ If you'd like to use Formulate's style sheet, you'll need to include it in
31
+ a Sass file, like `app/assets/stylesheets/form.css.sass`, and add the following:
32
+
33
+ ``` sass
34
+ @import formulate
35
+ ```
36
+
37
+ Formulate's styles are all scoped under the selector
38
+ `form.formulate`, so it shouldn't clobber anything in your own applications.
39
+
40
+
41
+ ### Customizing Styles
42
+
43
+ To customize the styles, you may set any of the variables used by the style
44
+ sheet before importing it:
45
+
46
+ ``` sass
47
+ $input_focus_border_color: $blue
48
+ $submit-button-color: $blue
49
+
50
+ @import formulate
51
+ ```
52
+
53
+ Take a look at the variables at the top of
54
+ `/vendor/assets/stylesheets/formulate.css.sass` to see what's available for
55
+ customization.
56
+
57
+ You can always override any of the styles after importing the Formulate style
58
+ sheet, too.
59
+
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create new Pull Request
68
+
69
+
70
+ ## Copyright
71
+
72
+ Copyright © 2012 Tyler Hunt. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/formulate.gemspec ADDED
@@ -0,0 +1,18 @@
1
+ require './lib/formulate/version'
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'formulate'
5
+ gem.version = Formulate::VERSION
6
+ gem.summary = 'Rails form builder with flexible markup and styles.'
7
+ gem.homepage = 'http://github.com/tylerhunt/formulate'
8
+ gem.author = 'Tyler Hunt'
9
+
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
12
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
+ gem.require_paths = ['lib']
14
+
15
+ gem.add_dependency 'actionpack', '>= 3.0'
16
+ gem.add_dependency 'activesupport', '>= 3.0'
17
+ gem.add_dependency 'sass-rails', '>= 3.0'
18
+ end
@@ -0,0 +1,11 @@
1
+ module Formulate
2
+ class Engine < ::Rails::Engine
3
+ initializer 'formulate.initialize' do |app|
4
+ ActiveSupport.on_load(:action_view) do
5
+ include Formulate::FormHelper
6
+
7
+ app.config.default_form_builder = Formulate::FormBuilder
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,190 @@
1
+ module Formulate
2
+ class FormBuilder < ActionView::Helpers::FormBuilder
3
+ def errors
4
+ if (errors = errors_on(:base)).any?
5
+ header = I18n.t('errors.template.header', count: errors.length)
6
+
7
+ @template.capture_haml do
8
+ @template.haml_tag(:fieldset, class: 'errors') do
9
+ @template.haml_tag(:legend, header)
10
+
11
+ @template.haml_tag(:ul, class: 'errors') do
12
+ errors.each { |message| @template.haml_tag(:li, message) }
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ def fieldset(options={}, &block)
20
+ legend = options.delete(:legend)
21
+
22
+ @template.capture_haml do
23
+ @template.haml_tag(:fieldset, options) do
24
+ @template.haml_tag(:legend, legend) if legend
25
+ yield(self)
26
+ end
27
+ end
28
+ end
29
+
30
+ def section(options={}, &block)
31
+ options[:class] = "#{options[:class]} section".strip
32
+
33
+ @template.capture_haml do
34
+ @template.haml_tag(:div, options) { yield(self) }
35
+ end
36
+ end
37
+
38
+ def input(method, options={}, &block)
39
+ type = options.delete(:type)
40
+ instructions = options.delete(:instructions)
41
+ errors = errors_on(method)
42
+
43
+ style = ['field']
44
+ style << (options.delete(:required) ? 'required' : 'optional')
45
+ style << 'checkbox' if type == :check_box
46
+ style << 'radio' if type == :radio_button
47
+ style << options.delete(:style) if options[:style]
48
+ style.uniq!
49
+
50
+ input = case type
51
+ when :check_box
52
+ checked_value = options.delete(:checked_value)
53
+ unchecked_value = options.delete(:unchecked_value)
54
+ checked_value = '1' if unchecked_value && checked_value.blank?
55
+ arguments = [options, checked_value, unchecked_value].compact
56
+ send(type, method, *arguments)
57
+ when :radio_button
58
+ value = options.delete(:value)
59
+ object_method = method
60
+ method = "#{method.to_s.gsub(/\?$/, '')}_#{value.gsub(/\s/, '_')}".downcase
61
+ send(type, object_method, value, options)
62
+ when :date_select
63
+ @template.capture_haml do
64
+ @template.haml_tag(:div, input, class: 'group')
65
+ end
66
+ when :collection_select
67
+ collection = options.delete(:collection)
68
+ value_method = options.delete(:value_method)
69
+ text_method = options.delete(:text_method)
70
+ html_options = options.delete(:html) || {}
71
+ send(type, method, collection, value_method, text_method, options, html_options)
72
+ when :time_zone_select
73
+ priority_zones = options.delete(:priority_zones)
74
+ send(type, method, priority_zones, options)
75
+ else
76
+ send(type, method, options)
77
+ end
78
+
79
+ label = options[:label] != false ? label(method, options[:label]) : nil
80
+ markup = [label, input].compact
81
+ markup.reverse! if type.in?(:check_box, :radio_button)
82
+
83
+ markup << @template.capture_haml do
84
+ yield(self) if block_given?
85
+ errors_list(errors)
86
+ instructions(instructions)
87
+ end
88
+
89
+ @template.capture_haml do
90
+ @template.haml_tag(:div, class: style.join(' ')) do
91
+ @template.haml_concat(markup.join)
92
+ end
93
+ end
94
+ end
95
+
96
+ alias_method :hidden, :hidden_field
97
+
98
+ def text(method, options={}, &block)
99
+ input(method, options.merge(type: :text_field), &block)
100
+ end
101
+
102
+ def email(method, options={}, &block)
103
+ input(method, options.merge(type: :email_field), &block)
104
+ end
105
+
106
+ def number(method, options={}, &block)
107
+ input(method, options.merge(type: :number_field), &block)
108
+ end
109
+
110
+ def password(method, options={}, &block)
111
+ input(method, options.merge(type: :password_field), &block)
112
+ end
113
+
114
+ def area(method, options={}, &block)
115
+ input(method, options.merge(type: :text_area), &block)
116
+ end
117
+
118
+ def checkbox(method, options={}, &block)
119
+ input(method, options.merge(type: :check_box), &block)
120
+ end
121
+
122
+ def radio(method, options={}, &block)
123
+ input(method, options.merge(type: :radio_button), &block)
124
+ end
125
+
126
+ def file(method, options={}, &block)
127
+ input(method, options.merge(type: :file_field), &block)
128
+ end
129
+
130
+ def select(*args, &block)
131
+ method = args.shift
132
+ options = args.extract_options!
133
+
134
+ options[:collection] = args[0]
135
+ options[:value_method] = args[1]
136
+ options[:text_method] = args[2]
137
+ input(method, options.merge(type: :collection_select), &block)
138
+ end
139
+
140
+ def date(method, options={}, &block)
141
+ input(method, options.merge(type: :date_select), &block)
142
+ end
143
+
144
+ def time(method, options={}, &block)
145
+ input(method, options.merge(type: :time_select), &block)
146
+ end
147
+
148
+ def date_time(method, options={}, &block)
149
+ input(method, options.merge(type: :datetime_select), &block)
150
+ end
151
+
152
+ def expiration(method, options={}, &block)
153
+ options.reverse_merge!(add_month_numbers: true, discard_day: true, order: [:month, :year], start_year: Date.today.year, prompt: '')
154
+ input(method, options.merge(type: :date_select), &block)
155
+ end
156
+
157
+ def time_zone(method, options, &block)
158
+ input(method, options.merge(type: :time_zone_select), &block)
159
+ end
160
+
161
+ def instructions(text_or_nil_with_block=nil, &block)
162
+ if text_or_nil_with_block
163
+ content = text_or_nil_with_block || @template.capture_haml(&block)
164
+ @template.haml_tag(:p, content, class: 'instructions')
165
+ end
166
+ end
167
+
168
+ def submit(value, options={}, &block)
169
+ @template.capture_haml do
170
+ @template.haml_tag(:div, class: 'submit') do
171
+ @template.haml_concat(super(value, options))
172
+ yield(self) if block_given?
173
+ end
174
+ end
175
+ end
176
+
177
+ def errors_on(method)
178
+ object.respond_to?(:errors) ? object.errors[method] : []
179
+ end
180
+ private :errors_on
181
+
182
+ def errors_list(errors)
183
+ if errors.any?
184
+ error_messages = "#{errors.to_sentence.capitalize}."
185
+ @template.haml_tag(:p, error_messages, class: 'errors')
186
+ end
187
+ end
188
+ private :errors_list
189
+ end
190
+ end
@@ -0,0 +1,9 @@
1
+ module Formulate
2
+ module FormHelper
3
+ def form_for(record, options={}, &proc)
4
+ options[:html] ||= {}
5
+ options[:html][:class] ||= 'formulate'
6
+ super
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Formulate
2
+ VERSION = '0.0.1'
3
+ end
data/lib/formulate.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'formulate/version'
2
+ require 'formulate/engine'
3
+
4
+ module Formulate
5
+ extend ActiveSupport::Autoload
6
+
7
+ autoload :FormBuilder
8
+ autoload :FormHelper
9
+ end
@@ -0,0 +1,187 @@
1
+ $line-height: 1.5 !default
2
+ $spacer-size: 1.5em !default
3
+ $border-width: 0.125em !default
4
+
5
+ $fieldset-border-color: #EEE !default
6
+ $fieldset-legend-color: #999 !default
7
+
8
+ $input-border-color: #CCC !default
9
+ $input-readonly-border-color: #EEE !default
10
+ $input-focus-border-color: #666 !default
11
+
12
+ $button-color: #999 !default
13
+ $button-text-color: #FFF !default
14
+
15
+ $submit-border-color: #EEE !default
16
+ $submit-button-color: #999 !default
17
+
18
+ $label-color: #000
19
+ $label-optional-color: #666
20
+
21
+ $error-color: #F33 !default
22
+ $instructions-color: #666 !default
23
+
24
+ @mixin button($color: $button-color)
25
+ -webkit-appearance: none
26
+ background: $color
27
+ border: $border-width solid $color
28
+ color: $button-text-color
29
+ display: inline-block
30
+ font-weight: bold
31
+ line-height: 1.125em
32
+ margin-right: 0.25em
33
+ padding: 0.25em 0.5em
34
+ text-decoration: none
35
+
36
+ form.formulate
37
+ margin: $spacer-size 0
38
+
39
+ a,
40
+ div,
41
+ fieldset,
42
+ form,
43
+ label,
44
+ legend,
45
+ ol,
46
+ p,
47
+ span,
48
+ ul
49
+ border: 0
50
+ font-family: inherit
51
+ font-size: 100%
52
+ font-style: inherit
53
+ font-weight: inherit
54
+ line-height: 1
55
+ margin: 0
56
+ padding: 0
57
+ vertical-align: baseline
58
+
59
+ ol,
60
+ ul
61
+ list-style: none
62
+
63
+ input,
64
+ select,
65
+ textarea,
66
+ p.readonly
67
+ border: $border-width solid $input-border-color
68
+ font-family: 'Helvetica', sans-serif
69
+ font-size: 1em
70
+ margin: 0
71
+ padding: 0.25em
72
+
73
+ &:focus
74
+ border-color: $input-focus-border-color
75
+ outline: none
76
+
77
+ p.readonly
78
+ border-color: $input-readonly-border-color
79
+ line-height: 1.125em
80
+
81
+ a.button
82
+ @include button
83
+
84
+ fieldset
85
+ border-top: $border-width solid $fieldset-border-color
86
+ margin: ($spacer-size / 2) 0
87
+ padding-top: $spacer-size
88
+
89
+ legend
90
+ color: $fieldset-legend-color
91
+ font-size: 0.75em
92
+ font-weight: bold
93
+ padding-right: (0.5em / 0.75)
94
+ text-transform: uppercase
95
+
96
+ fieldset
97
+ margin-left: $spacer-size * 2
98
+
99
+ & > p
100
+ margin-bottom: $spacer-size
101
+
102
+ .section
103
+ clear: left
104
+
105
+ .field
106
+ float: left
107
+ margin-right: $spacer-size * 2
108
+
109
+ .field
110
+ margin-bottom: $spacer-size
111
+
112
+ label
113
+ color: $label-color
114
+ display: block
115
+ line-height: $line-height
116
+
117
+ &.required label
118
+ font-weight: bold
119
+
120
+ &.optional label
121
+ color: $label-optional-color
122
+ font-style: italic
123
+
124
+ &.checkbox,
125
+ &.radio
126
+ white-space: nowrap
127
+
128
+ input
129
+ display: inline
130
+ margin-right: 0.5em
131
+ width: inherit
132
+
133
+ p
134
+ font-size: 0.875em
135
+ font-style: italic
136
+ line-height: $line-height / 0.875
137
+
138
+ &.instructions
139
+ color: $instructions-color
140
+
141
+ .submit
142
+ border-top: $border-width solid $submit-border-color
143
+ overflow: auto
144
+ padding-top: $spacer-size
145
+
146
+ input
147
+ @include button($submit-button-color)
148
+
149
+ a
150
+ font-weight: bold
151
+ margin: 0 0.25em
152
+
153
+ nav
154
+ margin: $spacer-size 0
155
+
156
+ ul
157
+ float: left
158
+ margin-right: $spacer-size
159
+
160
+ li a
161
+ font-weight: normal
162
+ margin: 0
163
+ line-height: $line-height
164
+
165
+ .errors
166
+ color: $error-color
167
+ border-color: $error-color
168
+
169
+ legend
170
+ color: $error-color
171
+ border-color: $error-color
172
+
173
+ ul
174
+ margin-bottom: $spacer-size
175
+
176
+ li
177
+ list-style: disc
178
+ line-height: $line-height
179
+
180
+ .field_with_errors
181
+ label
182
+ color: $error-color
183
+
184
+ input,
185
+ select,
186
+ textarea
187
+ border-color: $error-color
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: formulate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tyler Hunt
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: actionpack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '3.0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '3.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sass-rails
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ description:
63
+ email:
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - .gitignore
69
+ - Gemfile
70
+ - LICENSE
71
+ - README.md
72
+ - Rakefile
73
+ - formulate.gemspec
74
+ - lib/formulate.rb
75
+ - lib/formulate/engine.rb
76
+ - lib/formulate/form_builder.rb
77
+ - lib/formulate/form_helper.rb
78
+ - lib/formulate/version.rb
79
+ - vendor/assets/stylesheets/formulate.css.sass
80
+ homepage: http://github.com/tylerhunt/formulate
81
+ licenses: []
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.23
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Rails form builder with flexible markup and styles.
104
+ test_files: []