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.
@@ -0,0 +1,149 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ describe Formz do
5
+ describe "helpers" do
6
+ describe "#select" do
7
+ it "should create a select element with options" do
8
+ markup = select :province, :ab => 'Alberta', :bc => 'British Columbia'
9
+ markup.should have_tag('select') do |select|
10
+ select.should have_tag('option[@value=ab]', 'Alberta')
11
+ select.should have_tag('option[@value=bc]', 'British Columbia')
12
+ end
13
+ end
14
+
15
+ it "should create a select with optgroups" do
16
+ options = {}
17
+ options['Canada'] = { :ab => 'Alberta', :bc => 'British Columbia' }
18
+ options['United States'] = { :oh => 'Ohio' }
19
+ options[:other] = 'Other'
20
+ markup = select :province, options, :selected => :ab
21
+ markup.should have_tag('select') do |select|
22
+ select.should have_tag('optgroup[@label=Canada]') do |group|
23
+ group.should have_tag('option[@value=ab]', 'Alberta') do |option|
24
+ option['selected'].should == 'selected'
25
+ end
26
+ group.should have_tag('option[@value=bc]', 'British Columbia')
27
+ end
28
+ select.should have_tag('optgroup[@label="United States"]') do |group|
29
+ group.should have_tag('option[@value=oh]', 'Ohio')
30
+ end
31
+ select.should have_tag('option[@value=other]', 'Other')
32
+ end
33
+ end
34
+
35
+ it "should allow a null prompt with a default" do
36
+ markup = select :vehicle, { :nissan => 'Nissan' }, :prompt => true
37
+ markup.should have_tag('option[@value=""]', '- Select -')
38
+ end
39
+
40
+ it "should allow an arbitrary prompt" do
41
+ markup = select :vehicle, { :nissan => 'Nissan' }, :prompt => 'Please Select'
42
+ markup.should have_tag('option[@value=""]', 'Please Select')
43
+ end
44
+
45
+ it "should allow selections by value" do
46
+ markup = select :province, { :ab => 'Alberta', :bc => 'British Columbia' }, :selected => :bc, :prompt => true
47
+ markup.should have_tag('option[@value=bc]') do |option|
48
+ option['selected'].should == 'selected'
49
+ end
50
+ end
51
+
52
+ it "should allow selections of symbols / strings" do
53
+ markup = select :province, { 'ab' => 'Alberta', :bc => 'British Columbia' }, :selected => [:ab, 'bc'], :prompt => true
54
+ markup.should have_tag('option[@value=bc]') do |option|
55
+ option['selected'].should == 'selected'
56
+ end
57
+ markup.should have_tag('option[@value=ab]') do |option|
58
+ option['selected'].should == 'selected'
59
+ end
60
+ end
61
+
62
+ it "should allow multiple selections" do
63
+ options = { :nissan => 'Nissan', :acura => 'Acura', :ford => 'Sucks' }
64
+ markup = select :vehicle, options, :selected => [:nissan, :acura]
65
+ markup.should have_tag('option[@value=nissan]') do |option|
66
+ option['selected'].should == 'selected'
67
+ end
68
+ markup.should have_tag('option[@value=acura]') do |option|
69
+ option['selected'].should == 'selected'
70
+ end
71
+ end
72
+ end
73
+
74
+ describe "#form" do
75
+ it "should add a form-ID" do
76
+ form(:login).should have_tag('form[@id=form-login]')
77
+ end
78
+ end
79
+
80
+ describe "#fieldset" do
81
+ it "should add a legend when string passed as second arg" do
82
+ markup = form :register do
83
+ fieldset :details, 'Account Details'
84
+ end
85
+ markup.should_not have_tag('form > legend')
86
+ markup.should have_tag('fieldset[@id=fieldset-details]') do |fieldset|
87
+ fieldset.should have_tag('legend', 'Account Details')
88
+ end
89
+ end
90
+
91
+ it "should allow hash of attributes, without legend" do
92
+ markup = fieldset :details, :id => :foo
93
+ markup.should have_tag('fieldset[@id=foo]') do |fieldset|
94
+ fieldset.should_not have_tag('legend')
95
+ end
96
+ end
97
+ end
98
+
99
+ describe "#legend" do
100
+ it "should create a legend tag" do
101
+ legend('Details', :id => 'user-details').should have_tag('legend[@id=user-details]', 'Details')
102
+ end
103
+ end
104
+
105
+ describe "#buttons" do
106
+ it "should wrap in a buttons div" do
107
+ buttons(:id => 'login-buttons').should have_tag('div[@id=login-buttons]') do |div|
108
+ div['class'].should == 'form-buttons'
109
+ end
110
+ end
111
+ end
112
+
113
+ describe "#group" do
114
+ it "should wrap elements in a div" do
115
+ markup = form :register do
116
+ group :general do
117
+ text :name
118
+ text :email
119
+ end
120
+ group :details do
121
+ text :city
122
+ text :postal_code
123
+ end
124
+ end
125
+ markup.should have_tag('div[@class=group-general]') do |div|
126
+ div.should have_tag('input[@name=name]')
127
+ div.should have_tag('input[@name=email]')
128
+ end
129
+ markup.should have_tag('div[@class=group-details]') do |div|
130
+ div.should have_tag('input[@name=city]')
131
+ end
132
+ end
133
+ end
134
+
135
+ describe "#radio_group" do
136
+ it "should create a radio button group" do
137
+ markup = radio_group :choice, { :yes => 'Yes', :no => 'No' }, :selected => :no
138
+ markup.should have_tag('input[@name=choice]', :times => 2)
139
+ markup.should have_tag('input[@value=no]') { |radio| radio['selected'].should == 'selected' }
140
+ end
141
+
142
+ it "should allow multiple selections (though not legal)" do
143
+ markup = radio_group :choice, { :yes => 'Yes', :no => 'No' }, :selected => [:no, :yes]
144
+ markup.should have_tag('input[@value=yes]') { |radio| radio['selected'].should == 'selected' }
145
+ markup.should have_tag('input[@value=no]') { |radio| radio['selected'].should == 'selected' }
146
+ end
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,18 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ describe Formz do
5
+ describe "labels" do
6
+ it "should add a label element" do
7
+ tag(:textarea, :name => 'comments', :label => 'Comments').
8
+ should have_tag('label[@for=comments]', 'Comments:')
9
+ end
10
+
11
+ it "should allow an element to be required" do
12
+ tag(:textarea, :name => 'comments', :label => 'Comments', :required => true).
13
+ should have_tag('label[@for=comments]', 'Comments*:') do |label|
14
+ label.should have_tag('em', '*')
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,115 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ describe Formz do
5
+ describe "models" do
6
+ before :each do
7
+ @user = Factory.build :user
8
+ @invalid_user = Factory.build :invalid_user
9
+ end
10
+
11
+ describe "#form_for" do
12
+ it "should create a form in context to a model, populating fields" do
13
+ markup = form_for @user do
14
+ hidden :uid
15
+ text :name
16
+ text :email
17
+ textarea :signature
18
+ end
19
+ markup.should have_tag('input[@name=user[uid]]') do |id|
20
+ id['value'].should == '1'
21
+ end
22
+ markup.should have_tag('input[@name=user[name]]') do |name|
23
+ name['value'].should == 'tjholowaychuk'
24
+ end
25
+ markup.should have_tag('input[@name=user[email]]') do |email|
26
+ email['value'].should == 'tj@vision-media.ca'
27
+ end
28
+ markup.should have_tag('textarea[@name=user[signature]]', 'Foo bar')
29
+ markup.should_not have_tag('input[@name=user[_method]]')
30
+ end
31
+
32
+ it "should default selected options to the model's property" do
33
+ markup = form_for @user do
34
+ select :role, :admin => 'Admin', :manager => 'Manager'
35
+ end
36
+ markup.should have_tag('select[@name=user[role]]') do |role|
37
+ role.should have_tag('option[@value=admin]') do |admin|
38
+ admin['selected'].should == 'selected'
39
+ end
40
+ role.should have_tag('option[@value=manager]') do |manager|
41
+ manager['selected'].should_not == 'selected'
42
+ end
43
+ end
44
+ end
45
+
46
+ it "should default selected options to the form defaults" do
47
+ markup = form_for @user do
48
+ select :role, { :admin => 'Admin', :manager => 'Manager' }, :selected => :manager
49
+ end
50
+ markup.should have_tag('select[@name=user[role]]') do |role|
51
+ role.should have_tag('option[@value=admin]') do |admin|
52
+ admin['selected'].should_not == 'selected'
53
+ end
54
+ role.should have_tag('option[@value=manager]') do |manager|
55
+ manager['selected'].should == 'selected'
56
+ end
57
+ end
58
+ end
59
+
60
+ it "should override defaults when a value is present" do
61
+ markup = form_for @user do
62
+ text :name, :default => 'foo'
63
+ end
64
+ markup.should have_tag('input[@value=tjholowaychuk]')
65
+ end
66
+
67
+ it "should not override :value" do
68
+ markup = form_for @user do
69
+ text :name, :value => 'foo'
70
+ end
71
+ markup.should have_tag('input[@value=foo]')
72
+ end
73
+
74
+ it "should create an instance when using a symbol" do
75
+ markup = form_for :user do
76
+ text :name, :default => 'foo'
77
+ end
78
+ markup.should have_tag('input[@value=foo]')
79
+ end
80
+
81
+ it "should allow model defaults" do
82
+ markup = form_for :user do
83
+ textarea :signature
84
+ end
85
+ markup.should have_tag('textarea', 'Enter your forum signature')
86
+ end
87
+
88
+ it "should create an instance when using a class" do
89
+ markup = form_for User do
90
+ text :name, :default => 'foo'
91
+ end
92
+ markup.should have_tag('input[@value=foo]')
93
+ end
94
+
95
+ it "should not display errors for new records" do
96
+ markup = form_for :user do
97
+ text :name
98
+ end
99
+ markup.should_not have_tag('.error_message')
100
+ end
101
+
102
+ it "should display errors invalid records" do
103
+ @invalid_user.stub(:new?).and_return false
104
+ @invalid_user.valid?
105
+ markup = form_for @invalid_user do
106
+ text :name
107
+ text :email
108
+ end
109
+ markup.should have_tag('.error-message')
110
+ markup.should have_tag('input[@type=text][@class=" error"]')
111
+ end
112
+ end
113
+
114
+ end
115
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format specdoc
@@ -0,0 +1,14 @@
1
+
2
+ $:.unshift File.dirname(__FILE__) + '/../lib'
3
+ $:.unshift '/Users/tjholowaychuk/scripts/gems/tags/lib'
4
+ require 'rubygems'
5
+ require 'dm-core'
6
+ require 'dm-validations'
7
+ require 'factory_girl'
8
+ require 'tagz/import'
9
+ require 'formz/import'
10
+ require 'rspec_hpricot_matchers'
11
+
12
+ Spec::Runner.configure do |config|
13
+ config.include RspecHpricotMatchers
14
+ end
@@ -0,0 +1,20 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ describe Formz do
5
+ describe "wrappers" do
6
+ before :each do
7
+ @markup = tag :input, :type => :file, :name => :upload
8
+ end
9
+
10
+ it "should wrap a field in a div for styling" do
11
+ @markup.should have_tag('div') do |div|
12
+ div.should have_tag('input')
13
+ end
14
+ end
15
+
16
+ it "should add classes for the type of field and its name" do
17
+ @markup.should have_tag('div.field-upload.field-file')
18
+ end
19
+ end
20
+ end
data/tasks/docs.rake ADDED
@@ -0,0 +1,13 @@
1
+
2
+ namespace :docs do
3
+
4
+ desc 'Remove rdoc products'
5
+ task :remove => [:clobber_docs]
6
+
7
+ desc 'Build docs, and open in browser for viewing (specify BROWSER)'
8
+ task :open do
9
+ browser = ENV["BROWSER"] || "safari"
10
+ sh "open -a #{browser} doc/index.html"
11
+ end
12
+
13
+ end
@@ -0,0 +1,3 @@
1
+
2
+ desc 'Build gemspec file'
3
+ task :gemspec => [:build_gemspec]
data/tasks/spec.rake ADDED
@@ -0,0 +1,25 @@
1
+
2
+ require 'spec/rake/spectask'
3
+
4
+ desc "Run all specifications"
5
+ Spec::Rake::SpecTask.new(:spec) do |t|
6
+ t.libs << "lib"
7
+ t.spec_opts = ["--color", "--require", "spec/spec_helper.rb"]
8
+ end
9
+
10
+ namespace :spec do
11
+
12
+ desc "Run all specifications verbosely"
13
+ Spec::Rake::SpecTask.new(:verbose) do |t|
14
+ t.libs << "lib"
15
+ t.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"]
16
+ end
17
+
18
+ desc "Run specific specification verbosely (specify SPEC)"
19
+ Spec::Rake::SpecTask.new(:select) do |t|
20
+ t.libs << "lib"
21
+ t.spec_files = [ENV["SPEC"]]
22
+ t.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"]
23
+ end
24
+
25
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: formz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - TJ Holowaychuk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-05 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: visionmedia-tagz
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.1.1
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec_hpricot_matchers
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: thoughtbot-factory_girl
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ description: Framework independant tag helpers
46
+ email: tj@vision-media.ca
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - README.rdoc
53
+ - lib/formz.rb
54
+ - lib/formz/autoencoding.rb
55
+ - lib/formz/descriptions.rb
56
+ - lib/formz/errors.rb
57
+ - lib/formz/fauxmethod.rb
58
+ - lib/formz/helpers.rb
59
+ - lib/formz/import.rb
60
+ - lib/formz/labels.rb
61
+ - lib/formz/models.rb
62
+ - lib/formz/version.rb
63
+ - lib/formz/wrappers.rb
64
+ - tasks/docs.rake
65
+ - tasks/gemspec.rake
66
+ - tasks/spec.rake
67
+ files:
68
+ - History.rdoc
69
+ - Manifest
70
+ - README.rdoc
71
+ - Rakefile
72
+ - benchmarks/small.rb
73
+ - examples/model.rb
74
+ - examples/small.rb
75
+ - formz.gemspec
76
+ - lib/formz.rb
77
+ - lib/formz/autoencoding.rb
78
+ - lib/formz/descriptions.rb
79
+ - lib/formz/errors.rb
80
+ - lib/formz/fauxmethod.rb
81
+ - lib/formz/helpers.rb
82
+ - lib/formz/import.rb
83
+ - lib/formz/labels.rb
84
+ - lib/formz/models.rb
85
+ - lib/formz/version.rb
86
+ - lib/formz/wrappers.rb
87
+ - spec/autoencoding_spec.rb
88
+ - spec/descriptions_spec.rb
89
+ - spec/errors_spec.rb
90
+ - spec/factories/user.rb
91
+ - spec/fauxmethod_spec.rb
92
+ - spec/helpers_spec.rb
93
+ - spec/labels_spec.rb
94
+ - spec/models_spec.rb
95
+ - spec/spec.opts
96
+ - spec/spec_helper.rb
97
+ - spec/wrappers_spec.rb
98
+ - tasks/docs.rake
99
+ - tasks/gemspec.rake
100
+ - tasks/spec.rake
101
+ has_rdoc: true
102
+ homepage: http://github.com/visionmedia/formz
103
+ licenses: []
104
+
105
+ post_install_message:
106
+ rdoc_options:
107
+ - --line-numbers
108
+ - --inline-source
109
+ - --title
110
+ - Formz
111
+ - --main
112
+ - README.rdoc
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: "0"
120
+ version:
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: "1.2"
126
+ version:
127
+ requirements: []
128
+
129
+ rubyforge_project: formz
130
+ rubygems_version: 1.3.5
131
+ signing_key:
132
+ specification_version: 3
133
+ summary: Framework independant tag helpers
134
+ test_files: []
135
+