Fingertips-form-san 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,12 +1,27 @@
1
1
  = Form-San
2
2
 
3
- Form-San is a lean replacement for the Rails form builder.
3
+ Form-San is a lean extension for the Rails form builder.
4
4
 
5
- <% FormSan.form_for(@book) do |f| %>
5
+ <% form_for(@member, :builder => FormSan::FormBuilder) do |f| %>
6
6
  <% f.fieldset do %>
7
7
  <% f.fields do %>
8
- <f.text_field_with_label :email %>
9
- <p class="note">We're not going to spam you.</p>
8
+ <%= f.error_messages %>
9
+
10
+ <% f.field(:email, :type => :text, :class => 'medium') do %>
11
+ <p class="hint">You’ll log in using your email address.</p>
12
+ <% end %>
13
+ <%= f.field(:password, :type => :password) %>
14
+
15
+ <% f.fields_for(@member.artist) do |a| %>
16
+ <% a.field(:name, :type => :text, :class => 'medium') do %>
17
+ <p class="hint">Shown on the site.</p>
18
+ <% end %>
19
+ <% end %>
10
20
  <% end %>
11
21
  <% end %>
22
+
23
+ <div class="submit">
24
+ <%= f.submit 'Sign up' %>
25
+ <%= link_to 'Cancel', root_path, :class => 'cancel' %>
26
+ </div>
12
27
  <% end %>
data/lib/form_san.rb CHANGED
@@ -1,49 +1,70 @@
1
- require 'form_san/form_builder'
1
+ require 'action_view/helpers'
2
2
 
3
3
  module FormSan
4
- class Helpers
5
- include ActionController::UrlWriter
6
- include ActionController::RecordIdentifier
7
- include ActionController::PolymorphicRoutes
4
+ class FormBuilder < ActionView::Helpers::FormBuilder
5
+ def fieldset(&block)
6
+ @template.content_tag(:div, :class => 'fieldset', &block)
7
+ end
8
8
 
9
- protected :default_url_options, :default_url_options=
9
+ def fields(&block)
10
+ @template.content_tag(:div, :class => 'fields', &block)
11
+ end
10
12
 
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?
13
+ def error_messages
14
+ unless @object.errors.count.zero?
15
+ attributes_with_errors = @object.errors.map { |attribute, _| attribute } - ['base']
16
+ @template.content_tag(:p, :class => 'errors') do
17
+ if attributes_with_errors.size > 1
18
+ @template.concat "Sorry, there were problems with the #{attributes_with_errors.to_sentence}."
19
+ elsif attributes_with_errors.size == 1
20
+ @template.concat "Sorry, there was a problem with the #{attributes_with_errors.first}."
21
+ else
22
+ @template.concat "#{@object.class} #{@object.errors.on(:base)}."
23
+ end
24
+ end
25
+ else
26
+ ''
14
27
  end
15
28
  end
16
29
 
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}>"
30
+ def error_message(attribute)
31
+ unless @object.errors.on(attribute).blank?
32
+ @template.content_tag(:p, :class => 'notice') do
33
+ @template.concat ERB::Util.html_escape(@object.errors.on(attribute).mb_chars.capitalize)
34
+ end
35
+ else
36
+ ''
37
+ end
21
38
  end
22
39
 
23
- def tag(output_buffer, name, html_options={})
24
- output_buffer << "<#{name}#{hash_to_attributes(html_options)} />"
40
+ def field_without_block(attribute, extra_content=nil, *args)
41
+ options = args.extract_options!
42
+ classes = @object.errors.on(attribute) ? 'invalid field' : 'field'
43
+
44
+ @template.content_tag(:div, :class => classes) do
45
+ @template.concat(@template.content_tag(:div, :class => 'label') do
46
+ @template.concat self.label(attribute, *args)
47
+ end)
48
+ @template.concat ActionView::Helpers::InstanceTag.new(@object_name,
49
+ attribute, self, @object).to_input_field_tag(options.delete(:type), options)
50
+ @template.concat error_message(attribute)
51
+ @template.concat extra_content unless extra_content.blank?
52
+ @template.output_buffer # The block needs to return the current buffer
53
+ end
25
54
  end
26
55
 
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(' ')}"
56
+ def field(attribute, *args, &block)
57
+ if block_given?
58
+ @template.concat field_without_block(attribute, @template.capture(&block), *args)
59
+ else
60
+ field_without_block(attribute, nil, *args)
61
+ end
35
62
  end
36
63
  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
64
+ end
65
+
66
+ class ActionView::Helpers::InstanceTag
67
+ def error_wrapping(html_tag, has_error)
68
+ html_tag
48
69
  end
49
70
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Fingertips-form-san
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manfred Stienstra
@@ -13,7 +13,7 @@ date: 2009-03-25 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
- description: A lean replacement form builder for Rails.
16
+ description: A form builder for ActionView.
17
17
  email: manfred@fngtps.com
18
18
  executables: []
19
19
 
@@ -24,7 +24,6 @@ extra_rdoc_files:
24
24
  - LICENSE
25
25
  files:
26
26
  - lib/form_san.rb
27
- - lib/form_san/form_builder.rb
28
27
  - rails/init.rb
29
28
  - README.rdoc
30
29
  - LICENSE
@@ -54,6 +53,6 @@ rubyforge_project:
54
53
  rubygems_version: 1.2.0
55
54
  signing_key:
56
55
  specification_version: 2
57
- summary: A lean replacement form builder for Rails.
56
+ summary: Form-San is a form builder that makes it slightly easier to create fields with labels and validation messages.
58
57
  test_files: []
59
58
 
@@ -1,104 +0,0 @@
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