Fingertips-form-san 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +12 -0
- data/lib/form_san/form_builder.rb +104 -0
- data/lib/form_san.rb +49 -0
- data/rails/init.rb +1 -0
- metadata +59 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright © 2009 Fingertips, Manfred Stienstra
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
= Form-San
|
2
|
+
|
3
|
+
Form-San is a lean replacement for the Rails form builder.
|
4
|
+
|
5
|
+
<% FormSan.form_for(@book) do |f| %>
|
6
|
+
<% f.fieldset do %>
|
7
|
+
<% f.fields do %>
|
8
|
+
<f.text_field_with_label :email %>
|
9
|
+
<p class="note">We're not going to spam you.</p>
|
10
|
+
<% end %>
|
11
|
+
<% end %>
|
12
|
+
<% end %>
|
@@ -0,0 +1,104 @@
|
|
1
|
+
module FormSan
|
2
|
+
class FormBuilder
|
3
|
+
attr_accessor :output_buffer
|
4
|
+
|
5
|
+
def initialize(output_buffer, record, options={})
|
6
|
+
@output_buffer = output_buffer
|
7
|
+
@record = record
|
8
|
+
@options = options
|
9
|
+
end
|
10
|
+
|
11
|
+
def error_messages(html_options={})
|
12
|
+
unless @record.errors.count.zero?
|
13
|
+
FormSan.content_tag(output_buffer, 'p', html_options.reverse_merge(:class => 'errors')) do
|
14
|
+
attributes_with_errors = @record.errors.map { |attribute, _| attribute } - ['base']
|
15
|
+
if attributes_with_errors.size > 1
|
16
|
+
output_buffer << "Sorry, there were problems with the #{attributes_with_errors.to_sentence}."
|
17
|
+
elsif attributes_with_errors.size == 1
|
18
|
+
output_buffer << "Sorry, there was a problem with the #{attributes_with_errors.first}."
|
19
|
+
else
|
20
|
+
output_buffer << "#{@record.class} #{@record.errors.on(:base)}."
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def error_message(attribute, html_options={})
|
27
|
+
unless @record.errors.on(attribute).blank?
|
28
|
+
FormSan.content_tag(output_buffer, 'p', html_options.reverse_merge(:class => 'notice')) do
|
29
|
+
output_buffer << @record.errors.on(attribute).mb_chars.capitalize
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def fieldset(&block)
|
35
|
+
FormSan.content_tag(output_buffer, 'div', :class => 'fieldset') { block.call(self) }
|
36
|
+
end
|
37
|
+
|
38
|
+
def fields(&block)
|
39
|
+
FormSan.content_tag(output_buffer, 'div', :class => 'fields') { block.call(self) }
|
40
|
+
end
|
41
|
+
|
42
|
+
def field(attribute, options={}, html_options={}, &block)
|
43
|
+
classes = %w(field)
|
44
|
+
classes << 'invalid' if @record.errors.on(attribute)
|
45
|
+
html_options.reverse_merge!(:value => self.class.value_before_type_cast(@record, attribute))
|
46
|
+
FormSan.content_tag(output_buffer, 'div', :class => classes) do
|
47
|
+
label(attribute, options[:humanized])
|
48
|
+
input(attribute, html_options.merge(:type => options[:type]))
|
49
|
+
error_message(attribute)
|
50
|
+
output_buffer << block.call if block_given?
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def label(attribute, humanized_attribute=nil, html_options={}, &block)
|
55
|
+
humanized_attribute ||= @record.class.human_attribute_name(attribute.to_s)
|
56
|
+
if block_given?
|
57
|
+
FormSan.content_tag(output_buffer, 'label', html_options) do
|
58
|
+
block.call(self)
|
59
|
+
output_buffer << " #{humanized_attribute}"
|
60
|
+
end
|
61
|
+
else
|
62
|
+
html_options[:for] ||= "#{@record.class.name.downcase}_#{attribute}"
|
63
|
+
FormSan.content_tag(output_buffer, 'div', :class => 'label') do
|
64
|
+
FormSan.content_tag(output_buffer, 'label', html_options) do
|
65
|
+
output_buffer << humanized_attribute
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def input(attribute, html_options={})
|
72
|
+
html_options.reverse_merge!(
|
73
|
+
:id => "#{@record.class.name.downcase}_#{attribute}",
|
74
|
+
:name => "#{@record.class.name.downcase}[#{attribute}]"
|
75
|
+
)
|
76
|
+
FormSan.tag(output_buffer, 'input', html_options)
|
77
|
+
end
|
78
|
+
|
79
|
+
def text_field(attribute, html_options={})
|
80
|
+
input(attribute, html_options.reverse_merge(:type => 'text'))
|
81
|
+
end
|
82
|
+
|
83
|
+
def password_field(attribute, html_options={})
|
84
|
+
input(attribute, html_options.reverse_merge(:type => 'password'))
|
85
|
+
end
|
86
|
+
|
87
|
+
def submit(label, html_options={})
|
88
|
+
html_options.reverse_merge!(
|
89
|
+
:value => label,
|
90
|
+
:type => 'submit'
|
91
|
+
)
|
92
|
+
FormSan.tag(output_buffer, 'input', html_options)
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.value_before_type_cast(object, attribute)
|
96
|
+
if object.respond_to?("#{attribute}_before_type_cast")
|
97
|
+
object.send("#{attribute}_before_type_cast")
|
98
|
+
else
|
99
|
+
object.send(attribute)
|
100
|
+
end
|
101
|
+
rescue NoMethodError
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
data/lib/form_san.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'form_san/form_builder'
|
2
|
+
|
3
|
+
module FormSan
|
4
|
+
class Helpers
|
5
|
+
include ActionController::UrlWriter
|
6
|
+
include ActionController::RecordIdentifier
|
7
|
+
include ActionController::PolymorphicRoutes
|
8
|
+
|
9
|
+
protected :default_url_options, :default_url_options=
|
10
|
+
|
11
|
+
def form_for(output_buffer, record, options={}, &block)
|
12
|
+
content_tag(output_buffer, 'form', :action => polymorphic_path(record)) do
|
13
|
+
block.call(FormSan::FormBuilder.new(output_buffer, record, options)) if block_given?
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def content_tag(output_buffer, name, html_options={}, &block)
|
18
|
+
output_buffer << "<#{name}#{hash_to_attributes(html_options)}>"
|
19
|
+
block.call.to_s if block_given?
|
20
|
+
output_buffer << "</#{name}>"
|
21
|
+
end
|
22
|
+
|
23
|
+
def tag(output_buffer, name, html_options={})
|
24
|
+
output_buffer << "<#{name}#{hash_to_attributes(html_options)} />"
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def hash_to_attributes(html_options)
|
30
|
+
attributes = html_options.map do |key, value|
|
31
|
+
value = value.join(' ') if value.is_a?(Array)
|
32
|
+
"#{key}=\"#{value}\"" if value
|
33
|
+
end.compact
|
34
|
+
attributes.empty? ? '' : " #{attributes.join(' ')}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.helpers
|
39
|
+
@helpers ||= FormSan::Helpers.new
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.method_missing(method, *arguments, &block)
|
43
|
+
if helpers.public_methods(inherited=false).include?(method.to_s)
|
44
|
+
helpers.send(method, *arguments, &block)
|
45
|
+
else
|
46
|
+
super
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'form_san'
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: Fingertips-form-san
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Manfred Stienstra
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-25 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A lean replacement form builder for Rails.
|
17
|
+
email: manfred@fngtps.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
- LICENSE
|
25
|
+
files:
|
26
|
+
- lib/form_san.rb
|
27
|
+
- lib/form_san/form_builder.rb
|
28
|
+
- rails/init.rb
|
29
|
+
- README.rdoc
|
30
|
+
- LICENSE
|
31
|
+
has_rdoc: true
|
32
|
+
homepage: http://github.com/Fingertips/form-san
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options:
|
35
|
+
- --inline-source
|
36
|
+
- --charset=UTF-8
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
requirements: []
|
52
|
+
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.2.0
|
55
|
+
signing_key:
|
56
|
+
specification_version: 2
|
57
|
+
summary: A lean replacement form builder for Rails.
|
58
|
+
test_files: []
|
59
|
+
|