formz 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.rdoc ADDED
@@ -0,0 +1,16 @@
1
+
2
+ === 0.0.4 / 2009-10-05
3
+
4
+ * Added .field class to form wrappers
5
+
6
+ === 0.0.3 / 2009-10-05
7
+
8
+ * Errors on models should display regardless of it being a new record
9
+
10
+ === 0.0.2 / 2009-10-05
11
+
12
+ * Fixed errors module
13
+
14
+ === 0.0.1 / YYYY-MM-DD
15
+
16
+ * Initial release
data/Manifest ADDED
@@ -0,0 +1,33 @@
1
+ History.rdoc
2
+ Manifest
3
+ README.rdoc
4
+ Rakefile
5
+ benchmarks/small.rb
6
+ examples/model.rb
7
+ examples/small.rb
8
+ formz.gemspec
9
+ lib/formz.rb
10
+ lib/formz/autoencoding.rb
11
+ lib/formz/descriptions.rb
12
+ lib/formz/errors.rb
13
+ lib/formz/fauxmethod.rb
14
+ lib/formz/helpers.rb
15
+ lib/formz/import.rb
16
+ lib/formz/labels.rb
17
+ lib/formz/models.rb
18
+ lib/formz/version.rb
19
+ lib/formz/wrappers.rb
20
+ spec/autoencoding_spec.rb
21
+ spec/descriptions_spec.rb
22
+ spec/errors_spec.rb
23
+ spec/factories/user.rb
24
+ spec/fauxmethod_spec.rb
25
+ spec/helpers_spec.rb
26
+ spec/labels_spec.rb
27
+ spec/models_spec.rb
28
+ spec/spec.opts
29
+ spec/spec_helper.rb
30
+ spec/wrappers_spec.rb
31
+ tasks/docs.rake
32
+ tasks/gemspec.rake
33
+ tasks/spec.rake
data/README.rdoc ADDED
@@ -0,0 +1,54 @@
1
+
2
+ = Formz
3
+
4
+ Framework independant form helpers.
5
+
6
+ == Features
7
+
8
+ * Fast
9
+ * Framework independant
10
+ * Highly extendable
11
+ * Input labels
12
+ * Input descriptions
13
+ * Fakes out HTTP method to GET / POST
14
+ * Sets enctype when using file inputs
15
+ * ORM Model support
16
+ * Error display
17
+
18
+ == Cherry Pick
19
+
20
+ Use only what you want:
21
+
22
+ include Formz::Labels
23
+ include Formz::Description
24
+ include Formz::Helpers
25
+ ...
26
+
27
+ Or simply:
28
+
29
+ require 'formz/import'
30
+
31
+ == License
32
+
33
+ (The MIT License)
34
+
35
+ Copyright (c) 2009 TJ Holowaychuk <tj@vision-media.ca>
36
+
37
+ Permission is hereby granted, free of charge, to any person obtaining
38
+ a copy of this software and associated documentation files (the
39
+ 'Software'), to deal in the Software without restriction, including
40
+ without limitation the rights to use, copy, modify, merge, publish,
41
+ distribute, sublicense, an d/or sell copies of the Software, and to
42
+ permit persons to whom the Software is furnished to do so, subject to
43
+ the following conditions:
44
+
45
+ The above copyright notice and this permission notice shall be
46
+ included in all copies or substantial portions of the Software.
47
+
48
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
49
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
50
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
51
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
52
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
53
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
54
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+
2
+ $:.unshift 'lib'
3
+ require 'formz'
4
+ require 'rubygems'
5
+ require 'rake'
6
+ require 'echoe'
7
+
8
+ Echoe.new "formz", Formz::VERSION do |p|
9
+ p.author = "TJ Holowaychuk"
10
+ p.email = "tj@vision-media.ca"
11
+ p.summary = "Framework independant tag helpers"
12
+ p.url = "http://github.com/visionmedia/formz"
13
+ p.runtime_dependencies << 'visionmedia-tagz >=1.1.1'
14
+ p.development_dependencies << 'rspec_hpricot_matchers >=1.0.0'
15
+ p.development_dependencies << 'thoughtbot-factory_girl'
16
+ end
17
+
18
+ Dir['tasks/**/*.rake'].sort.each { |f| load f }
@@ -0,0 +1,48 @@
1
+
2
+ $:.unshift File.dirname(__FILE__) + '/../lib'
3
+ require 'rubygems'
4
+ require 'rgauge'
5
+ require 'formz/import'
6
+
7
+ benchmark '', :times => 200 do
8
+ report 'tag' do
9
+ tag :div, 'contents'
10
+ end
11
+ report 'tag with everything' do
12
+ tag :textarea, 'contents', :label => 'Comments', :required => true, :description => 'Leave a comment.'
13
+ end
14
+ report 'input' do
15
+ checkbox :delete
16
+ end
17
+ report 'input with everything' do
18
+ checkbox :delete, :label => 'Delete', :required => true, :description => 'Delete the data.'
19
+ end
20
+ report 'select' do
21
+ select :days, { :mon => 'Monday', :fri => 'Friday' }, :id => 'days'
22
+ end
23
+ report 'select with everything' do
24
+ select :days, { :mon => 'Monday', :fri => 'Friday' }, :id => 'days',
25
+ :label => 'Days', :required => true, :description => 'Select a day.'
26
+ end
27
+ report 'full form' do
28
+ form :register, :action => '/register' do
29
+ fieldset :login, 'Login Information' do
30
+ text :username, :label => 'Username'
31
+ password :password, :label => 'Password'
32
+ password :password_confirm, :required => true, :description => 'Enter password between 6 - 30 characters.'
33
+ end
34
+ fieldset :account, 'Account Details' do
35
+ text :country, :label => 'Country'
36
+ text :city, :label => 'City'
37
+ fieldset :forum, 'Forum Related' do
38
+ file :image, :label => 'Avatar'
39
+ textarea :signature, :label => 'Signature'
40
+ end
41
+ end
42
+ buttons do
43
+ submit :op, 'Join'
44
+ submit :cancel, 'Cancel'
45
+ end
46
+ end
47
+ end
48
+ end
data/examples/model.rb ADDED
@@ -0,0 +1,44 @@
1
+
2
+ $:.unshift File.dirname(__FILE__) + '/../lib'
3
+ require 'rubygems'
4
+ require 'dm-core'
5
+ require 'factory_girl'
6
+ require 'formz/import'
7
+ require 'tagz/import'
8
+
9
+ class User
10
+ include DataMapper::Resource
11
+ property :uid, Serial
12
+ property :name, String, :length => 5..20
13
+ property :email, String, :format => :email_address
14
+ property :role, String
15
+ property :signature, Text, :default => 'Enter your forum signature'
16
+ end
17
+
18
+ Factory.define :user do |user|
19
+ user.uid 1
20
+ user.name 'tjholowaychuk'
21
+ user.email 'tj@vision-media.ca'
22
+ user.role 'admin'
23
+ user.signature 'Foo bar'
24
+ end
25
+
26
+ puts "\nAnonymous"
27
+ markup = form_for :user do
28
+ hidden :uid
29
+ text :name, :label => 'Username', :required => true
30
+ text :email, :default => 'example@site.com'
31
+ textarea :signature
32
+ select :role, :admin => 'Admin', :manager => 'Manager'
33
+ end
34
+ puts markup
35
+
36
+ puts "\nPopulated Model"
37
+ markup = form_for Factory.build(:user), :method => :put do
38
+ hidden :uid
39
+ text :name, :label => 'Username', :required => true
40
+ text :email, :default => 'example@site.com'
41
+ textarea :signature
42
+ select :role, :admin => 'Admin', :manager => 'Manager'
43
+ end
44
+ puts markup
data/examples/small.rb ADDED
@@ -0,0 +1,19 @@
1
+
2
+ $:.unshift File.dirname(__FILE__) + '/../lib'
3
+ require 'rubygems'
4
+ require 'formz'
5
+ require 'tagz/import'
6
+ include Formz::Helpers
7
+
8
+ markup = form :login do
9
+ fieldset :details, 'Details' do
10
+ text :username
11
+ password :password
12
+ end
13
+ fieldset :details, 'Details' do
14
+ text :username
15
+ password :password
16
+ end
17
+ end
18
+ puts markup
19
+
data/formz.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{formz}
5
+ s.version = "0.0.4"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["TJ Holowaychuk"]
9
+ s.date = %q{2009-10-05}
10
+ s.description = %q{Framework independant tag helpers}
11
+ s.email = %q{tj@vision-media.ca}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/formz.rb", "lib/formz/autoencoding.rb", "lib/formz/descriptions.rb", "lib/formz/errors.rb", "lib/formz/fauxmethod.rb", "lib/formz/helpers.rb", "lib/formz/import.rb", "lib/formz/labels.rb", "lib/formz/models.rb", "lib/formz/version.rb", "lib/formz/wrappers.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
13
+ s.files = ["History.rdoc", "Manifest", "README.rdoc", "Rakefile", "benchmarks/small.rb", "examples/model.rb", "examples/small.rb", "formz.gemspec", "lib/formz.rb", "lib/formz/autoencoding.rb", "lib/formz/descriptions.rb", "lib/formz/errors.rb", "lib/formz/fauxmethod.rb", "lib/formz/helpers.rb", "lib/formz/import.rb", "lib/formz/labels.rb", "lib/formz/models.rb", "lib/formz/version.rb", "lib/formz/wrappers.rb", "spec/autoencoding_spec.rb", "spec/descriptions_spec.rb", "spec/errors_spec.rb", "spec/factories/user.rb", "spec/fauxmethod_spec.rb", "spec/helpers_spec.rb", "spec/labels_spec.rb", "spec/models_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "spec/wrappers_spec.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
14
+ s.homepage = %q{http://github.com/visionmedia/formz}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Formz", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{formz}
18
+ s.rubygems_version = %q{1.3.5}
19
+ s.summary = %q{Framework independant tag helpers}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
+ s.add_runtime_dependency(%q<visionmedia-tagz>, [">= 1.1.1"])
27
+ s.add_development_dependency(%q<rspec_hpricot_matchers>, [">= 1.0.0"])
28
+ s.add_development_dependency(%q<thoughtbot-factory_girl>, [">= 0"])
29
+ else
30
+ s.add_dependency(%q<visionmedia-tagz>, [">= 1.1.1"])
31
+ s.add_dependency(%q<rspec_hpricot_matchers>, [">= 1.0.0"])
32
+ s.add_dependency(%q<thoughtbot-factory_girl>, [">= 0"])
33
+ end
34
+ else
35
+ s.add_dependency(%q<visionmedia-tagz>, [">= 1.1.1"])
36
+ s.add_dependency(%q<rspec_hpricot_matchers>, [">= 1.0.0"])
37
+ s.add_dependency(%q<thoughtbot-factory_girl>, [">= 0"])
38
+ end
39
+ end
data/lib/formz.rb ADDED
@@ -0,0 +1,39 @@
1
+ #--
2
+ # Copyright (c) 2009 TJ Holowaychuk <tj@vision-media.ca>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ $:.unshift '/Users/tjholowaychuk/scripts/gems/tagz/lib'
25
+ require 'tagz'
26
+ require 'formz/version'
27
+
28
+ module Formz
29
+ autoload :Labels, 'formz/labels'
30
+ autoload :Errors, 'formz/errors'
31
+ autoload :Models, 'formz/models'
32
+ autoload :Helpers, 'formz/helpers'
33
+ autoload :Wrappers, 'formz/wrappers'
34
+ autoload :FauxMethod, 'formz/fauxmethod'
35
+ autoload :Descriptions, 'formz/descriptions'
36
+ autoload :AutoEncoding, 'formz/autoencoding'
37
+ end
38
+
39
+ Tagz::Tag.send :include, Formz::Helpers::Delegates
@@ -0,0 +1,16 @@
1
+
2
+ module Formz
3
+
4
+ ##
5
+ # = Formz::AutoEncoding
6
+ #
7
+ # Automatically sets form enctype to 'multipart/form-data'
8
+ # when a file field is present.
9
+
10
+ module AutoEncoding
11
+ def create_tag name, contents, attrs, &block
12
+ @__form_encoding = 'multipart/form-data' if name == :file
13
+ super
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,30 @@
1
+
2
+ module Formz
3
+
4
+ ##
5
+ # = Formz::Descriptions
6
+ #
7
+ # The Formz:Descriptions module allows a :description attribute
8
+ # to be passed, which then appends a tag containing the field
9
+ # description.
10
+ #
11
+ # === Examples
12
+ #
13
+ # tag :input, :type => :file, :name => :upload, :description => 'Upload a file.'
14
+ #
15
+ # <div class="form-upload form-file">
16
+ # <input type="file" name="upload" />
17
+ # <span class="description">Upload a file.</span>
18
+ # </div>
19
+ #
20
+
21
+ module Descriptions
22
+ def create_tag name, contents, attrs, &block
23
+ if description = attrs.delete(:description)
24
+ super << Tagz.tag(:span, description, :class => 'description')
25
+ else
26
+ super
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,40 @@
1
+
2
+ module Formz
3
+
4
+ ##
5
+ # = Formz::Errors
6
+ #
7
+ # Adds classes and error messages when :error is
8
+ # present for any field.
9
+ #
10
+ # === Examples
11
+ #
12
+ # password :password, :label => 'Password', :error => 'Invalid Password'
13
+ #
14
+ # <div class="field-password">
15
+ # <label for="password">Password:</label>
16
+ # <input name="password" class="error" type="password" value="" />
17
+ # <span class="error-message">Invalid Password</span>
18
+ # </div>
19
+ #
20
+
21
+ module Errors
22
+
23
+ ##
24
+ # Array of form specific errors.
25
+
26
+ def form_errors
27
+ @__form_errors ||= []
28
+ end
29
+
30
+ def create_tag name, contents, attrs, &block
31
+ if error = attrs.delete(:error)
32
+ form_errors << error
33
+ (attrs[:class] ||= '').add_class 'error'
34
+ super << Tagz.tag(:span, error, :class => 'error-message')
35
+ else
36
+ super
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,32 @@
1
+
2
+ module Formz
3
+
4
+ ##
5
+ # = Formz::FauxMethod
6
+ #
7
+ # Fake out methods using the hidden field '_method'
8
+ # when the HTTP method is not GET or POST. Defaults the
9
+ # method to POST.
10
+
11
+ module FauxMethod
12
+
13
+ def create_tag name, contents, attrs, &block
14
+ if name == :form
15
+ attrs[:method] ||= :post
16
+ unless valid_http_method? attrs[:method]
17
+ method = Tagz.tag :input, :type => :hidden, :name => :_method, :value => attrs[:method]
18
+ attrs[:method] = :post
19
+ contents << method
20
+ end
21
+ end
22
+ super
23
+ end
24
+
25
+ ##
26
+ # Check if _method_ is a valid form HTTP verb. (get, post).
27
+
28
+ def valid_http_method? method
29
+ method.to_s.in? 'post', 'get'
30
+ end
31
+ end
32
+ end