formatted_form 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'jeweler'
5
5
  Jeweler::Tasks.new do |gem|
6
6
  # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
7
7
  gem.name = "formatted_form"
8
- gem.homepage = "http://github.com/JackNeto/formatted_form"
8
+ gem.homepage = "http://github.com/twg/formatted_form"
9
9
  gem.license = "MIT"
10
10
  gem.summary = "A Rails form builder to simplify you forms"
11
11
  gem.description = ''
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.0.1
@@ -0,0 +1,13 @@
1
+ .form_element{:class => "#{field_name}"}
2
+
3
+ .value
4
+ = field
5
+ = label_text
6
+ - if required
7
+ %span.required *
8
+
9
+ - if error_messages.present?
10
+ != error_messages
11
+
12
+ - if description.present?
13
+ .description= description
@@ -0,0 +1,15 @@
1
+ .form_element{:class => "#{field_name}"}
2
+ .label
3
+ = label_text
4
+ - if required
5
+ %span.required *
6
+
7
+ .value
8
+ = before_text
9
+ = field
10
+ = after_text
11
+ - if error_messages.present?
12
+ = error_messages
13
+
14
+ - if description.present?
15
+ .description= description
@@ -0,0 +1,17 @@
1
+ .form_element{:class => "#{field_name}"}
2
+ .label
3
+ = label_text
4
+ - if required
5
+ %span.required *
6
+
7
+ .value
8
+ - choices.each do |choice|
9
+ .option
10
+ = choice[:field]
11
+ = choice[:label]
12
+
13
+ - if error_messages.present?
14
+ != error_messages
15
+
16
+ - if description.present?
17
+ .description= description
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{formatted_form}
8
- s.version = "1.0.0"
8
+ s.version = "1.0.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jack Neto"]
12
- s.date = %q{2011-01-24}
12
+ s.date = %q{2011-02-15}
13
13
  s.description = %q{}
14
14
  s.email = %q{jack@theworkinggroup.ca}
15
15
  s.extra_rdoc_files = [
@@ -21,11 +21,15 @@ Gem::Specification.new do |s|
21
21
  "README.md",
22
22
  "Rakefile",
23
23
  "VERSION",
24
+ "app/views/formatted_form/_check_box.html.haml",
25
+ "app/views/formatted_form/_default_field.html.haml",
26
+ "app/views/formatted_form/_radio_button.html.haml",
24
27
  "formatted_form.gemspec",
25
28
  "lib/formatted_form.rb",
26
- "lib/formatted_form/form_helper.rb"
29
+ "lib/formatted_form/builder.rb",
30
+ "lib/formatted_form/engine.rb"
27
31
  ]
28
- s.homepage = %q{http://github.com/JackNeto/formatted_form}
32
+ s.homepage = %q{http://github.com/twg/formatted_form}
29
33
  s.licenses = ["MIT"]
30
34
  s.require_paths = ["lib"]
31
35
  s.rubygems_version = %q{1.3.7}
@@ -0,0 +1,149 @@
1
+ module FormattedForm
2
+ class Builder < ActionView::Helpers::FormBuilder
3
+
4
+ %w(text_field password_field text_area file_field datetime_select date_select).each do |field_name|
5
+ define_method field_name do |method, *args|
6
+ options = args.detect { |a| a.is_a?(Hash) } || {}
7
+ render_field(field_name, method, options) { super(method, options) }
8
+ end
9
+ end
10
+
11
+ def select(method, choices, options = {}, html_options = {})
12
+ render_field('select', method, options) { super(method, choices, options, html_options) }
13
+ end
14
+
15
+ def hidden_field(method, options = {}, html_options = {})
16
+ super(method, options)
17
+ end
18
+
19
+ def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
20
+ render_field('check_box', method, options) do
21
+ super(method, options, checked_value, unchecked_value)
22
+ end
23
+ end
24
+
25
+ def radio_button(method, tag_value, options = {})
26
+ case tag_value
27
+ when Array
28
+ choices = tag_value.collect do |choice|
29
+ if !choice.is_a?(Array)
30
+ choice = [choice, choice]
31
+ elsif choice.length == 1
32
+ choice << choice[0]
33
+ end
34
+ {
35
+ :field => super(method, choice[1], options),
36
+ :label => label_for("#{method}_#{choice[1].to_s.gsub(' ', '_').underscore}", :label => choice[0])
37
+ }
38
+ end
39
+ else
40
+ choices = [{
41
+ :field => super(method, tag_value),
42
+ :label => label_for("#{method}_#{tag_value.to_s.gsub(' ', '_').underscore}", :label => tag_value)
43
+ }]
44
+ end
45
+
46
+ @template.render(:partial => "formatted_form/radio_button", :locals => {
47
+ :field_name => 'radio_button',
48
+ :label_text => label_for(method, options),
49
+ :choices => choices,
50
+ :required => options.delete(:required),
51
+ :before_text => @template.raw(options.delete(:before_text)),
52
+ :after_text => @template.raw(options.delete(:after_text)),
53
+ :description => @template.raw(options.delete(:desc)),
54
+ :error_messages => error_messages_for(method)
55
+ })
56
+
57
+ end
58
+
59
+ def submit(value, options={}, &block)
60
+ cancel_link = @template.capture(&block) if block_given?
61
+ cancel_link ||= options[:cancel_url] ? ' or ' + options.delete(:cancel_url) : ''
62
+ if options[:show_ajax_loader]
63
+ options[:onclick] = "$(this).parent().next().css('display', 'block');$(this).parent().hide();"
64
+ end
65
+ if options[:image_button] == true
66
+ submit_id = Time.now.usec
67
+ out = @template.content_tag(:div,
68
+ %{
69
+ #{super(value, options.merge(:style=>'visibility:hidden;position: absolute', :id => submit_id)).html_safe}
70
+ <a class="red_button" href="" onclick="$('##{submit_id}').closest('form').submit();return false"><span>#{value}</span></a>
71
+ #{cancel_link.html_safe}
72
+ }.html_safe, :class => 'form_element submit_element').html_safe
73
+
74
+ else
75
+ out = @template.content_tag(:div, super(value, options) + cancel_link.html_safe, :class => 'form_element submit_element').html_safe
76
+ end
77
+
78
+ if options[:show_ajax_loader]
79
+ out << %{
80
+ <div class="form_element submit" style="display:none">
81
+ <div class="submit_ajax_loader">#{options[:show_ajax_loader]}</div>
82
+ </div>
83
+ }.html_safe
84
+ end
85
+ out.html_safe
86
+ end
87
+
88
+ # generic container for all things form
89
+ def element(label = '&nbsp;', value = '', type = 'text_field', &block)
90
+ value += @template.capture(&block) if block_given?
91
+ %{
92
+ <div class='form_element #{type}'>
93
+ <div class='label'>
94
+ #{label}
95
+ </div>
96
+ <div class='value'>
97
+ #{value}
98
+ </div>
99
+ </div>
100
+ }.html_safe
101
+ end
102
+
103
+ def error_messages
104
+ if object && !object.errors.empty?
105
+ message = object.errors[:base].present? ? object.errors[:base]: 'There were some problems submitting this form. Please correct all the highlighted fields and try again'
106
+ @template.content_tag(:div, message, :class => 'form_error')
107
+ end
108
+ end
109
+
110
+ def error_messages_for(method)
111
+ if (object and object.respond_to?(:errors) and errors = object.errors[method] and !errors.empty?)
112
+ @template.content_tag(:div, errors.is_a?(Array) ? errors.first : errors, :class => 'errors')
113
+ end
114
+ end
115
+
116
+ def formatted_fields_for(record_or_name_or_array, *args, &block)
117
+ options = args.extract_options!
118
+ options.merge!(:builder => FormattedForm::Builder)
119
+ fields_for(record_or_name_or_array, *(args << options), &block)
120
+ end
121
+
122
+
123
+ protected
124
+
125
+ def render_field(field_name, method, options={}, &block)
126
+ case field_name
127
+ when 'check_box'
128
+ template = field_name
129
+ else
130
+ template = 'default_field'
131
+ end
132
+ @template.render(:partial => "formatted_form/#{template}", :locals => {
133
+ :field_name => field_name,
134
+ :label_text => label_for(method, options),
135
+ :required => options.delete(:required),
136
+ :before_text => @template.raw(options.delete(:before_text)),
137
+ :after_text => @template.raw(options.delete(:after_text)),
138
+ :field => @template.capture(&block),
139
+ :description => @template.raw(options.delete(:desc)),
140
+ :error_messages => error_messages_for(method)
141
+ })
142
+ end
143
+
144
+ def label_for(method, options)
145
+ @template.label(object_name, method, @template.raw(options.delete(:label)) || method.to_s.titleize.capitalize)
146
+ end
147
+
148
+ end
149
+ end
@@ -0,0 +1,10 @@
1
+ require "formatted_form"
2
+ require "rails"
3
+
4
+ module FormattedForm
5
+ class Engine < Rails::Engine
6
+ initializer 'formatted_form.helper' do |app|
7
+ ActionView::Base.send(:include, FormattedForm::FormattedFormHelper)
8
+ end
9
+ end
10
+ end
@@ -1,146 +1,15 @@
1
1
  module FormattedForm
2
- require 'formatted_form/form_helper'
3
- ActionView::Base.send(:include, FormattedForm::FormHelper)
4
-
5
- class Builder < ActionView::Helpers::FormBuilder
6
- %w[ date_select text_field password_field text_area file_field datetime_select ].each do |selector|
7
- src = <<-end_src
8
- def #{selector}(method, options = {})
9
- if (options[:simple] == true)
10
- super(method, options)
11
- else
12
- options.merge!(:size=> '') if #{%w{text_field password_field}.include?(selector)}
13
- standard_field('#{selector}', method, options) { super(method, options) }
14
- end
15
- end
16
- end_src
17
- class_eval src, __FILE__, __LINE__
18
- end
19
-
20
- def standard_field(type, method, options={}, &block)
21
- description = options.delete(:desc)
22
- content = options.delete(:content)
23
- prev_content = options.delete(:prev_content)
24
- label = label_for(method, options)
25
- required = options.delete(:required)
26
- check_box_details = options.delete(:check_box_details)
27
- text_snippet = options.delete(:text_snippet)
28
- %{
29
- <div class='form_element #{type}_element'>
30
- #{"<div class='text_snippet'>"+text_snippet+"</div>" if text_snippet}
31
- <div class='label'>
32
- #{description(description) || '&nbsp;' if type == 'check_box' }
33
- #{label if type != 'check_box' }
34
- #{@template.content_tag(:span, '*', :class => 'required_ind') if required }
35
- </div>
36
- <div class='value'>
37
- #{prev_content}#{yield}#{content}
38
- #{error_messages_for(method)}
39
- #{description(description) if type != 'check_box'}
40
- #{description(check_box_details) if type == 'check_box'}
41
- </div>
42
- </div>
43
- }.html_safe
44
- end
45
-
46
- # generic container for all things form
47
- def element(label = '&nbsp;', value = '', type = 'text_field', &block)
48
- value += @template.capture(&block) if block_given?
49
- %{
50
- <div class='form_element #{type}_element'>
51
- <div class='label'>
52
- #{label}
53
- </div>
54
- <div class='value'>
55
- #{value}
56
- </div>
57
- </div>
58
- }.html_safe
59
- end
60
-
61
- def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
62
- options[:content] = label_for(method, options)
63
- options[:label] = ''
64
- standard_field('check_box', method, options) { super(method, options, checked_value, unchecked_value) }
65
- end
66
-
67
- def radio_button(method, tag_value, options = {})
68
- if options && options[:choices]
69
- radios = options.delete(:choices).collect{|choice| %{<div class="radio_button">}+super(method, choice[0], options) + %{<label for="#{object_name.to_s.gsub(']', '').gsub('[', '_')}_#{method}_#{choice[0]}">#{choice[1]}</label></div>}}.join.html_safe
70
- standard_field('radio_button', method, options) { radios }
71
- elsif options && options[:value_name]
72
- standard_field('radio_button', method, options) { super(method, tag_value) + %{<label for="#{object_name}_#{method}_#{tag_value}">#{options[:value_name]}</label><div class="clearfloat"></div>}.html_safe}
73
- else
74
- standard_field('radio_button', method, options) { super(method, tag_value, options = {}) }
75
- end
76
- end
77
-
78
- def select(method, choices, options = {}, html_options = {})
79
- standard_field('select', method, options) { super(method, choices, options, html_options) }
80
- end
2
+ require 'formatted_form/engine' if defined?(Rails)
3
+ require 'formatted_form/builder'
81
4
 
82
- def hidden_field(method, options = {}, html_options = {})
83
- super(method, options)
84
- end
85
-
86
- def submit(value, options={}, &block)
87
- cancel_link = @template.capture(&block) if block_given?
88
- cancel_link ||= options[:cancel_url] ? ' or ' + options.delete(:cancel_url) : ''
89
- if options[:show_ajax_loader]
90
- options[:onclick] = "$(this).parent().next().css('display', 'block');$(this).parent().hide();"
91
- end
92
- if options[:image_button] == true
93
- submit_id = Time.now.usec
94
- out = @template.content_tag(:div,
95
- %{
96
- #{super(value, options.merge(:style=>'visibility:hidden;position: absolute', :id => submit_id)).html_safe}
97
- <a class="red_button" href="" onclick="$('##{submit_id}').closest('form').submit();return false"><span>#{value}</span></a>
98
- #{cancel_link.html_safe}
99
- }.html_safe, :class => 'form_element submit_element').html_safe
100
-
101
- else
102
- out = @template.content_tag(:div, super(value, options) + cancel_link.html_safe, :class => 'form_element submit_element').html_safe
103
- end
104
-
105
- if options[:show_ajax_loader]
106
- out << %{
107
- <div class="form_element submit_element" style="display:none">
108
- <div class="submit_ajax_loader">#{options[:show_ajax_loader]}</div>
109
- </div>
110
- }.html_safe
111
- end
112
- out.html_safe
113
- end
114
-
115
- def label_for(method, options)
116
- label = options.delete(:label) || method.to_s.titleize.capitalize
117
- "<label for=\"#{object_name}_#{method}\">#{label}</label>"
118
- end
119
-
120
- def description(description)
121
- "<div class='description'>#{description}</div>" unless description.nil?
122
- end
123
-
124
- def error_messages
125
- if object && !object.errors.empty?
126
- message = object.errors[:base].present? ? object.errors[:base]: 'There were some problems submitting this form. Please correct all the highlighted fields and try again'
127
- @template.content_tag(:div, message, :class => 'form_error')
128
- end
129
- end
130
-
131
- def error_messages_for(method)
132
- if (object and object.respond_to?(:errors) and errors = object.errors[method])
133
- "<div class='errors'>#{errors.is_a?(Array) ? errors.first : errors}</div>"
134
- else
135
- ''
136
- end
137
- end
138
-
139
- def formatted_fields_for(record_or_name_or_array, *args, &block)
5
+ module FormattedFormHelper
6
+ def formatted_form_for(record_or_name_or_array, *args, &proc)
140
7
  options = args.extract_options!
141
8
  options.merge!(:builder => FormattedForm::Builder)
142
- fields_for(record_or_name_or_array, *(args << options), &block)
9
+ (options[:html] ||= { }).merge!(:class => "#{options[:html][:class]} formatted")
10
+ form_for(record_or_name_or_array, *(args << options), &proc)
143
11
  end
144
12
  end
145
13
 
146
- end
14
+ end
15
+
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 0
9
- version: 1.0.0
8
+ - 1
9
+ version: 1.0.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jack Neto
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-24 00:00:00 -05:00
17
+ date: 2011-02-15 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -32,11 +32,15 @@ files:
32
32
  - README.md
33
33
  - Rakefile
34
34
  - VERSION
35
+ - app/views/formatted_form/_check_box.html.haml
36
+ - app/views/formatted_form/_default_field.html.haml
37
+ - app/views/formatted_form/_radio_button.html.haml
35
38
  - formatted_form.gemspec
36
39
  - lib/formatted_form.rb
37
- - lib/formatted_form/form_helper.rb
40
+ - lib/formatted_form/builder.rb
41
+ - lib/formatted_form/engine.rb
38
42
  has_rdoc: true
39
- homepage: http://github.com/JackNeto/formatted_form
43
+ homepage: http://github.com/twg/formatted_form
40
44
  licenses:
41
45
  - MIT
42
46
  post_install_message:
@@ -1,10 +0,0 @@
1
- module FormattedForm
2
- module FormHelper
3
- def formatted_form_for(record_or_name_or_array, *args, &proc)
4
- options = args.extract_options!
5
- options.merge!(:builder => FormattedForm::Builder)
6
- (options[:html] ||= { }).merge!(:class => "#{options[:html][:class]} formatted")
7
- form_for(record_or_name_or_array, *(args << options), &proc)
8
- end
9
- end
10
- end