semantic_form_for 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Gemfile +3 -0
- data/README.markdown +50 -0
- data/lib/semantic_form_for/form_builder.rb +82 -0
- data/lib/semantic_form_for/form_helpers.rb +21 -0
- data/lib/semantic_form_for/railtie.rb +8 -0
- data/lib/semantic_form_for/version.rb +3 -0
- data/lib/semantic_form_for.rb +9 -0
- data/semantic_form_for.gemspec +21 -0
- metadata +88 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.markdown
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
Semantic Forms
|
2
|
+
==============
|
3
|
+
|
4
|
+
semantic_form_for is a Rails FormBuilder DSL that, like Formtastic, makes it
|
5
|
+
easier to create semantically awesome, readily stylable, and wonderfully
|
6
|
+
accessible HTML forms in your Rails applications.
|
7
|
+
|
8
|
+
However, it also tries not to do too much. Rather that trying to guess at
|
9
|
+
input types, and provide its own sizeable set of customization options to
|
10
|
+
each method, it wraps around the existing Rails field helpers.
|
11
|
+
|
12
|
+
Dependencies
|
13
|
+
============
|
14
|
+
|
15
|
+
semantic_form_for depends on Haml. Why? It's what I use, and it was marginally
|
16
|
+
easier than writing it for ERB.
|
17
|
+
|
18
|
+
Syntax
|
19
|
+
======
|
20
|
+
|
21
|
+
= semantic_form_for @user do |user|
|
22
|
+
= user.inputs 'Sign in' do
|
23
|
+
= user.email_field :email, :placeholder => 'me@example.com'
|
24
|
+
= user.password_field :password
|
25
|
+
= user.check_box, :remember, 'Remember me'
|
26
|
+
|
27
|
+
= user.submit 'Sign in'
|
28
|
+
|
29
|
+
This is equivalent to the following Haml.
|
30
|
+
|
31
|
+
= form_for @user do |user|
|
32
|
+
%fieldset.inputs
|
33
|
+
%legend Sign in
|
34
|
+
%ol
|
35
|
+
%li#user_email_input
|
36
|
+
= user.label :email
|
37
|
+
= user.email_field :email, :placeholder => 'me@example.com'
|
38
|
+
%li#user_password_input
|
39
|
+
= user.label :password
|
40
|
+
= user.password_field :password
|
41
|
+
%li#user_remember_input
|
42
|
+
= user.check_box :remember
|
43
|
+
= user.label :remember, 'Remember me'
|
44
|
+
%li
|
45
|
+
= user.submit_tag 'Sign in'
|
46
|
+
|
47
|
+
That's it. All of the Rails field helpers you know and love work just like
|
48
|
+
their normal FormBuilder counterparts, except for the minor addition of an
|
49
|
+
optional second parameter to specify the field's label. Everything else works
|
50
|
+
as expected, but gives you more semantic markup for free.
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'semantic_form_for'
|
2
|
+
|
3
|
+
class SemanticFormFor::FormBuilder < ActionView::Helpers::FormBuilder
|
4
|
+
attr_reader :template
|
5
|
+
attr_reader :object
|
6
|
+
attr_reader :object_name
|
7
|
+
|
8
|
+
FIELDSETS = [ :inputs, :buttons ]
|
9
|
+
INPUTS = [ :check_box, :email_field, :file_field, :hidden_field,
|
10
|
+
:number_field, :password_field, :radio_button, :range_field,
|
11
|
+
:search_field, :select, :text_area, :text_field,
|
12
|
+
:url_field ]
|
13
|
+
BUTTONS = [ :image_submit, :submit ]
|
14
|
+
|
15
|
+
FIELDSETS.each do |set|
|
16
|
+
define_method set do |text, &block|
|
17
|
+
template.capture_haml do
|
18
|
+
template.haml_tag :fieldset, :class => set do
|
19
|
+
template.haml_tag(:legend, text) if text
|
20
|
+
template.haml_tag(:ol, &block)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
INPUTS.each do |input|
|
27
|
+
define_method input do |attribute, *args|
|
28
|
+
options = args.extract_options!
|
29
|
+
text = args.shift
|
30
|
+
|
31
|
+
template.capture_haml do
|
32
|
+
template.haml_tag(:li, :id => _li_id(attribute)) do
|
33
|
+
# check boxes should have their label and input tag in the reverse
|
34
|
+
# order
|
35
|
+
if input != :check_box
|
36
|
+
template.haml_concat self.label(attribute, text)
|
37
|
+
template.haml_concat super(attribute, options)
|
38
|
+
else
|
39
|
+
template.haml_concat super(attribute, options)
|
40
|
+
template.haml_concat self.label(attribute, text)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
BUTTONS.each do |button|
|
48
|
+
define_method button do |text, options = {}|
|
49
|
+
template.capture_haml do
|
50
|
+
template.haml_tag(:li, :id => nil) do
|
51
|
+
template.haml_concat super(text, options)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def _object_name
|
60
|
+
self.object_name.to_s.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
|
61
|
+
end
|
62
|
+
|
63
|
+
def _object_index
|
64
|
+
case
|
65
|
+
when options.has_key?(:index) then options[:index]
|
66
|
+
when defined?(@auto_index) then @auto_index
|
67
|
+
else nil
|
68
|
+
end.to_s
|
69
|
+
end
|
70
|
+
|
71
|
+
def _attribute_name(attribute)
|
72
|
+
attribute.to_s.gsub(/[\?\/\-]$/, '')
|
73
|
+
end
|
74
|
+
|
75
|
+
def _li_id(attribute)
|
76
|
+
[
|
77
|
+
_object_name + _object_index,
|
78
|
+
_attribute_name(attribute),
|
79
|
+
'input'
|
80
|
+
].join('_')
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'semantic_form_for'
|
2
|
+
|
3
|
+
module SemanticFormFor::FormHelpers
|
4
|
+
[:form_for, :field_for, :remote_form_for].each do |method|
|
5
|
+
module_eval do
|
6
|
+
define_method "semantic_#{method}" do |record, *args, &block|
|
7
|
+
options = args.extract_options!
|
8
|
+
options[:builder] = SemanticFormFor::FormBuilder
|
9
|
+
|
10
|
+
class_names = options[:class] ? options[:class].split(' ') : []
|
11
|
+
class_names << case record
|
12
|
+
when String, Symbol then record.to_s
|
13
|
+
when Array then ActiveModel::Naming.singular(record.last.class)
|
14
|
+
else ActiveModel::Naming.singular(record.class)
|
15
|
+
end
|
16
|
+
|
17
|
+
send method, record, *(args << options), &block
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module SemanticFormFor
|
2
|
+
require 'semantic_form_for/version'
|
3
|
+
|
4
|
+
autoload :FormBuilder, 'semantic_form_for/form_builder'
|
5
|
+
autoload :FormHelpers, 'semantic_form_for/form_helpers'
|
6
|
+
autoload :Railtie, 'semantic_form_for/railtie'
|
7
|
+
end
|
8
|
+
|
9
|
+
SemanticFormFor::Railtie # trigger loading the Railtie
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.expand_path('../lib/semantic_form_for/version', __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'semantic_form_for'
|
5
|
+
s.version = SemanticFormFor::VERSION
|
6
|
+
s.platform = Gem::Platform::RUBY
|
7
|
+
s.authors = [ 'Stephen Touset' ]
|
8
|
+
s.email = [ 'stephen@touset.org' ]
|
9
|
+
s.homepage = 'http://github.com/stouset/semantic_form_for'
|
10
|
+
s.summary = 'Simple HTML5-enabled form builder'
|
11
|
+
s.description = 'A custom Rails FormBuilder that assumes HTML5. Because HTML4 is for chumps.'
|
12
|
+
|
13
|
+
s.required_rubygems_version = '>= 1.3.6'
|
14
|
+
s.rubyforge_project = 'semantic_form_for'
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.executables = `git ls-files`.split("\n").map {|f| f =~ /^bin\/(.*)/ ? $1 : nil }.compact
|
18
|
+
s.require_path = 'lib'
|
19
|
+
|
20
|
+
s.add_dependency 'haml', '~> 3.0'
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: semantic_form_for
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Stephen Touset
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-10-29 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: haml
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 3
|
30
|
+
- 0
|
31
|
+
version: "3.0"
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
description: A custom Rails FormBuilder that assumes HTML5. Because HTML4 is for chumps.
|
35
|
+
email:
|
36
|
+
- stephen@touset.org
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- Gemfile
|
46
|
+
- README.markdown
|
47
|
+
- lib/semantic_form_for.rb
|
48
|
+
- lib/semantic_form_for/form_builder.rb
|
49
|
+
- lib/semantic_form_for/form_helpers.rb
|
50
|
+
- lib/semantic_form_for/railtie.rb
|
51
|
+
- lib/semantic_form_for/version.rb
|
52
|
+
- semantic_form_for.gemspec
|
53
|
+
has_rdoc: true
|
54
|
+
homepage: http://github.com/stouset/semantic_form_for
|
55
|
+
licenses: []
|
56
|
+
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
segments:
|
76
|
+
- 1
|
77
|
+
- 3
|
78
|
+
- 6
|
79
|
+
version: 1.3.6
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project: semantic_form_for
|
83
|
+
rubygems_version: 1.3.7
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Simple HTML5-enabled form builder
|
87
|
+
test_files: []
|
88
|
+
|