formatted_form 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Jack Neto
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.md ADDED
@@ -0,0 +1,29 @@
1
+ # Formatted Form
2
+ Author: [The Working Group](http://www.theworkinggroup.ca)
3
+
4
+ ---
5
+
6
+ ## What is it?
7
+
8
+ A Rails form builder to simplify your forms and keep your views clean.
9
+
10
+
11
+ ## Install
12
+
13
+ ### 1. Add the gem definition to your Gemfile:
14
+
15
+ config.gem 'form_builder'
16
+
17
+ ### 2. From within your Rails project run:
18
+
19
+ bundle install
20
+
21
+
22
+ ## Usage (with haml)
23
+
24
+ = formatted_form_for @session_user, :url => login_path do |f|
25
+ = f.text_field :email, :label => 'Email address'
26
+ = f.password_field :password
27
+ = f.check_box :remember_me, :label => 'Remember me when I come back'
28
+ = f.submit 'Log In'
29
+
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'jeweler'
4
+
5
+ Jeweler::Tasks.new do |gem|
6
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
7
+ gem.name = "formatted_form"
8
+ gem.homepage = "http://github.com/JackNeto/formatted_form"
9
+ gem.license = "MIT"
10
+ gem.summary = "A Rails form builder to simplify you forms"
11
+ gem.description = ''
12
+ gem.email = "jack@theworkinggroup.ca"
13
+ gem.authors = ["Jack Neto"]
14
+ end
15
+ Jeweler::RubygemsDotOrgTasks.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,44 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{formatted_form}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jack Neto"]
12
+ s.date = %q{2011-01-24}
13
+ s.description = %q{}
14
+ s.email = %q{jack@theworkinggroup.ca}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ "LICENSE.txt",
21
+ "README.md",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "formatted_form.gemspec",
25
+ "lib/formatted_form.rb",
26
+ "lib/formatted_form/form_helper.rb"
27
+ ]
28
+ s.homepage = %q{http://github.com/JackNeto/formatted_form}
29
+ s.licenses = ["MIT"]
30
+ s.require_paths = ["lib"]
31
+ s.rubygems_version = %q{1.3.7}
32
+ s.summary = %q{A Rails form builder to simplify you forms}
33
+
34
+ if s.respond_to? :specification_version then
35
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
36
+ s.specification_version = 3
37
+
38
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
39
+ else
40
+ end
41
+ else
42
+ end
43
+ end
44
+
@@ -0,0 +1,10 @@
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
@@ -0,0 +1,146 @@
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
81
+
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)
140
+ options = args.extract_options!
141
+ options.merge!(:builder => FormattedForm::Builder)
142
+ fields_for(record_or_name_or_array, *(args << options), &block)
143
+ end
144
+ end
145
+
146
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: formatted_form
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Jack Neto
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-24 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: ""
22
+ email: jack@theworkinggroup.ca
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE.txt
29
+ - README.md
30
+ files:
31
+ - LICENSE.txt
32
+ - README.md
33
+ - Rakefile
34
+ - VERSION
35
+ - formatted_form.gemspec
36
+ - lib/formatted_form.rb
37
+ - lib/formatted_form/form_helper.rb
38
+ has_rdoc: true
39
+ homepage: http://github.com/JackNeto/formatted_form
40
+ licenses:
41
+ - MIT
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.7
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: A Rails form builder to simplify you forms
70
+ test_files: []
71
+