campo 0.3.4 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,41 @@
1
+ # encoding: UTF-8
2
+
3
+ module Campo
4
+ module Plugins
5
+
6
+ module Pluggable
7
+ def before_output( &block )
8
+ befores << block
9
+ end
10
+
11
+ def after_output( &block )
12
+ afters << block
13
+ end
14
+
15
+ def on_plugin( &block )
16
+ @extras = block
17
+ end
18
+
19
+ def extras
20
+ @extras ||= proc {}
21
+ end
22
+
23
+ def plugged_in
24
+ instance_exec &@extras
25
+ end
26
+ end
27
+
28
+ class Plugin
29
+ include Pluggable
30
+ def befores
31
+ @befores ||= []
32
+ end
33
+
34
+ def afters
35
+ @afters ||= []
36
+ end
37
+ end
38
+
39
+ end # Plugins
40
+
41
+ end # Campo
@@ -0,0 +1,92 @@
1
+ # encoding: UTF-8
2
+
3
+ require_relative "../plugins.rb"
4
+
5
+ module Campo
6
+ module Plugins
7
+
8
+ # For more accessible forms
9
+ # http://www.w3.org/WAI/intro/aria
10
+ module Aria
11
+
12
+ # @private
13
+ def self.new( options={} )
14
+ Klass.new options
15
+ end
16
+
17
+ module InstanceMethods
18
+ module Convenience
19
+
20
+ # Adds aria-describedby along with a span.
21
+ # @overload text(message, opts)
22
+ # @param [String] message The text for the aria-describedby attribute.
23
+ # @param [optional,Hash] options Any attributes for the span.
24
+ # @example
25
+ # text("postcode").describe("All in caps", class: "description")
26
+ # @return [String] A span with an id (and any options passed in as attributes) as Haml.
27
+ # @overload text(message-tuples, opts)
28
+ # @param [Array<Array<String,Hash>>] message-tuples An array of tuples, each tuple containing the message string and an options hash for attributes.
29
+ # @param [optional,Hash] options Any attributes for the span.
30
+ # @example
31
+ # text("Address").describe([["postcode",{class: "British"}],["zipcode", {class: "American"}]], class: "description")
32
+ # @return [String] A span with an id (and any options passed in as attributes) as Haml, wrapped around an unordered list with a list-item for each message, each list-item receiving the attributes passed in the tuple for it.
33
+ # @overload text(messages,opts)
34
+ # @param [Array<Array<String>>] messages An array of single valued arrays containing a string, the message.
35
+ # @example
36
+ # text("Address").describe([["A valid address"],["Don't forget the postcode!"]])
37
+ # @return [String] A span with an id (and any options passed in as attributes) as Haml, wrapped around an unordered list with a list-item for each message.
38
+ # @see http://www.w3.org/TR/WCAG20-TECHS/ARIA1.html
39
+ def describe( message, opts={} )
40
+ label, field = if self.kind_of? Campo::Label
41
+ [self,self.fields.first]
42
+ elsif (parent = self.parent).kind_of? Campo::Label
43
+ [parent, self]
44
+ end
45
+
46
+ span_id = "#{label.attributes[:for]}_description"
47
+
48
+ if message.respond_to? :map
49
+ # array
50
+ span = Campo::Span.new( span_id, "%ul", opts )
51
+ message.each do |(x,o)|
52
+ o ||= {}
53
+ li = Campo.literal("%li",o) << Campo.literal(x)
54
+ span.fields.first << li
55
+ end
56
+ else
57
+ span = Campo::Span.new( span_id, message, opts )
58
+ end
59
+
60
+ label.fields.unshift span
61
+
62
+ field.attributes[:"aria-describedby"] = span_id
63
+ self
64
+ end # def
65
+ end # Convenience
66
+ end # InstanceMethods
67
+
68
+ class Klass < Plugin
69
+
70
+ def initialize( opts={} )
71
+
72
+ # adds `describe` to Convenience, it's an easy way to get it where it needs to be
73
+ # @private
74
+ on_plugin do
75
+ Campo::Base.send(:include, Campo::Plugins::Aria::InstanceMethods::Convenience)
76
+ end
77
+
78
+ # Adds the form role attribute to all fields before output.
79
+ # @private
80
+ before_output do |fields,options|
81
+ fields.find_all{|x| x.kind_of? Campo::Form }.each do |form|
82
+ form.attributes[:role] = "form" if form.attributes[:role].nil? || form.attributes[:role].empty?
83
+ end
84
+ end
85
+ end
86
+
87
+ end # Klass
88
+ end # Aria
89
+
90
+
91
+ end # Plugins
92
+ end # Campo
@@ -0,0 +1,155 @@
1
+ # encoding: UTF-8
2
+
3
+ require_relative "../plugins.rb"
4
+
5
+ module Campo
6
+ module Plugins
7
+
8
+ # using the lib from http://docs.jquery.com/Plugins/Validation
9
+ module JQueryValidation
10
+
11
+ def self.new( options={} )
12
+ Klass.new options
13
+ end
14
+
15
+ module Rules
16
+ def self.render
17
+ return "" if Rules.rules.empty?
18
+ output = @jqv_rules.map do |(field,rs)|
19
+ "#{field}: { " <<
20
+ rs.map{|k,v| "#{k}: #{v}" }.join(", ") <<
21
+ " }"
22
+ end.join(",\n" + " " * 4)
23
+ output = <<STR
24
+ rules: {
25
+ #{output}
26
+ }
27
+ STR
28
+
29
+ output.chomp
30
+ end
31
+ def self.rules
32
+ if @jqv_rules.nil?
33
+ @jqv_rules = {}
34
+ @jqv_rules.default_proc = proc {|hash, key| hash[key] = {} }
35
+ end
36
+ @jqv_rules
37
+ end
38
+
39
+ def self.[](key)
40
+ Rules.rules[key]
41
+ end
42
+ def self.[]=( key,value )
43
+ value = {value => true} unless value.kind_of? Hash
44
+ Rules.rules[key].merge! value
45
+ Rules.rules
46
+ end
47
+ def self.reset
48
+ @jqv_rules = nil
49
+ end
50
+ end
51
+
52
+ module InstanceMethods
53
+ # the simplest validation possible
54
+ module Convenience
55
+ def validate( *args )
56
+
57
+ # TODO move this to helper
58
+ label, field = if self.kind_of? Campo::Label
59
+ [self,self.fields.first] # for the key
60
+ elsif self.parent.kind_of? Campo::Label
61
+ [self.parent, self] # for the key
62
+ end
63
+
64
+ key = field.attributes[:id] || field.attributes[:name]
65
+
66
+ # required
67
+ if args.empty? || args.include?( :required )
68
+ [label, field].each do |elem|
69
+ elem.attributes.merge!({ :class => "required" } )
70
+ end
71
+ Rules[key] = :required
72
+ end
73
+
74
+ unless args.empty?
75
+ hashes = {}
76
+ singles = []
77
+ args.each do |x|
78
+ if x.kind_of? Hash
79
+ hashes.merge! x
80
+ else
81
+ singles << x
82
+ end
83
+ end
84
+
85
+ # TODO required letters/symbols/numbers
86
+
87
+ if hashes.include? :minlength
88
+ Rules[key] = {minlength: hashes[:minlength] }
89
+ end
90
+
91
+ if singles.include?( :maxlength )
92
+ if field.attributes.include? :size
93
+ Rules[key] = {maxlength: field.attributes[:size]}
94
+ end
95
+ end
96
+
97
+ if hashes.include? :maxlength
98
+ Rules[key] = {maxlength: hashes[:maxlength] }
99
+ end
100
+ # http://docs.jquery.com/Plugins/Validation/Methods/digits
101
+ Rules[key] = :digits if singles.include? :digits
102
+ end
103
+
104
+ self
105
+ end # def
106
+ end
107
+ module Outputter
108
+
109
+ # holds the names of form(s)
110
+ attr_accessor :jqv_form_names
111
+
112
+ # builds the declaration for the top of the output
113
+ def jquery_script_declaration
114
+ unless jqv_form_names.nil? || jqv_form_names.empty?
115
+ @jqv_form_names.reduce(":javascript\n") do |mem,name|
116
+ mem + <<STR
117
+ $().ready(function(){
118
+ $("##{name}").validate({
119
+ #{JQueryValidation::Rules.render}
120
+ });
121
+ });
122
+ STR
123
+ end
124
+ else
125
+ "" # just in case there are no forms for some reason
126
+ end
127
+ end
128
+ end
129
+ end # instance methods
130
+
131
+ class Klass < Plugin
132
+ def initialize( opts={} )
133
+ before_output do |fields,options|
134
+ #find the form name(s)
135
+ @jqv_form_names = fields.find_all{|x| x.kind_of? Campo::Form }.map{|x| x.attributes[:name]}
136
+ @jqv_form_names << opts[:form] unless opts[:form].nil? || opts[:form].empty?
137
+ end
138
+ after_output do |output,options|
139
+ # concat to the current output
140
+ out = jquery_script_declaration + output
141
+ Rules.reset
142
+ out
143
+ end
144
+ on_plugin do
145
+ # adds `validate` to Convenience, it's an easy way to get it where it needs to be
146
+ Campo::Base.send(:include, Campo::Plugins::JQueryValidation::InstanceMethods::Convenience)
147
+ # only for the outputter
148
+ Campo::Outputter.send(:include, Campo::Plugins::JQueryValidation::InstanceMethods::Outputter)
149
+ end
150
+ end
151
+ end # klass
152
+ end # jqueryvalidation
153
+
154
+ end # Plugins
155
+ end # Campo
@@ -0,0 +1,49 @@
1
+ # encoding: UTF-8
2
+
3
+ require_relative "../plugins.rb"
4
+
5
+ module Campo
6
+ module Plugins
7
+
8
+ module Partial
9
+
10
+ def self.new( options={} )
11
+ Klass.new options
12
+ end
13
+
14
+ module InstanceMethods
15
+ attr_accessor :partial
16
+
17
+ DECLARATIONS = <<STR
18
+ - atts = {} if atts.nil?
19
+ - atts.default_proc = proc {|hash, key| hash[key] = {} } if atts.default_proc.nil?
20
+ - inners = {} if inners.nil?
21
+ - inners.default = "" if inners.default.nil?
22
+ - @campo_tabindex ||= 0 # for tabindex
23
+ STR
24
+ def declarations
25
+ DECLARATIONS
26
+ end
27
+ end
28
+
29
+ class Klass < Plugin
30
+
31
+ def initialize( opts={} )
32
+ after_output do |output,options|
33
+ options[:partial] ?
34
+ output : # partial
35
+ declarations + output # whole form
36
+ end
37
+ on_plugin do
38
+ Campo::Outputter.send(:include, Campo::Plugins::Partial::InstanceMethods)
39
+ Campo::Outputter::DEFAULT_OPTIONS.merge!({partial: false})
40
+ end
41
+ end
42
+
43
+ DEFAULT_OPTIONS={partial: false}
44
+ end # Klass
45
+ end # Partial
46
+
47
+
48
+ end # Plugins
49
+ end # Campo
data/lib/campo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Campo
2
- VERSION = "0.3.4"
2
+ VERSION = "0.9.0"
3
3
  end
data/spec/aria_spec.rb ADDED
@@ -0,0 +1,67 @@
1
+ # encoding: UTF-8
2
+
3
+ require_relative "./spec_helper.rb"
4
+ require_relative "../lib/campo/campo.rb"
5
+ require_relative "../lib/campo/plugins/aria.rb"
6
+ require_relative "../lib/campo/plugins/partial.rb"
7
+
8
+ describe "Aria" do
9
+
10
+ before(:all) {
11
+ Campo.plugins.clear
12
+ Campo.plugin :partial # this is normally done by lib/campo.rb
13
+ Campo.plugin :Aria
14
+ }
15
+ let(:form) {
16
+ Campo.form "example" do
17
+ text("a").describe("mm/yy")
18
+ text("b").describe("All in caps", class: "description")
19
+ text("c").describe([["Must be 8 characters at least.",class: "password validate", id: "password_length"], ["It's better to add some numbers/punctuation.", id: "password_not_email_address", class: "password validate"]], class: "description")
20
+ text("d").describe([["You"], ["Me"], ["Them"]])
21
+ end
22
+ }
23
+
24
+ let(:expected) { <<'STR'
25
+ - atts = {} if atts.nil?
26
+ - atts.default_proc = proc {|hash, key| hash[key] = {} } if atts.default_proc.nil?
27
+ - inners = {} if inners.nil?
28
+ - inners.default = "" if inners.default.nil?
29
+ - @campo_tabindex ||= 0 # for tabindex
30
+ %form{ atts[:example], id: "example", method: "POST", name: "example", role: "form", }
31
+ %label{ for: "a", }
32
+ A
33
+ %span{id: "a_description", }
34
+ mm/yy
35
+ %input{ atts[:a], tabindex: "#{@campo_tabindex += 1}", id: "a", type: "text", name: "a", :"aria-describedby" => "a_description", }
36
+ %label{ for: "b", }
37
+ B
38
+ %span{id: "b_description", class: "description", }
39
+ All in caps
40
+ %input{ atts[:b], tabindex: "#{@campo_tabindex += 1}", id: "b", type: "text", name: "b", :"aria-describedby" => "b_description", }
41
+ %label{ for: "c", }
42
+ C
43
+ %span{id: "c_description", class: "description", }
44
+ %ul
45
+ %li{ id: "password_length", class: "password validate", }
46
+ Must be 8 characters at least.
47
+ %li{ id: "password_not_email_address", class: "password validate", }
48
+ It's better to add some numbers/punctuation.
49
+ %input{ atts[:c], tabindex: "#{@campo_tabindex += 1}", id: "c", type: "text", name: "c", :"aria-describedby" => "c_description", }
50
+ %label{ for: "d", }
51
+ D
52
+ %span{id: "d_description", }
53
+ %ul
54
+ %li
55
+ You
56
+ %li
57
+ Me
58
+ %li
59
+ Them
60
+ %input{ atts[:d], tabindex: "#{@campo_tabindex += 1}", id: "d", type: "text", name: "d", :"aria-describedby" => "d_description", }
61
+ STR
62
+ }
63
+
64
+ subject { Campo.output form }
65
+ it { should == expected }
66
+
67
+ end
data/spec/campo_spec.rb CHANGED
@@ -1,37 +1,35 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  require 'spec_helper'
4
- require_relative "../lib/campo.rb"
4
+ require_relative "../lib/campo/campo.rb"
5
+ require_relative "../lib/campo/plugins/partial.rb"
6
+ require_relative "../lib/campo/plugins/aria.rb"
5
7
  require "rspec"
6
- require "logger"
7
8
 
8
9
 
9
10
  module Campo
10
11
  describe Campo do
11
-
12
- let(:logger){
13
- require 'logger'
14
- logger = Logger.new(STDOUT)
15
- logger.level = Logger::DEBUG
16
- logger
17
- }
12
+
13
+ before(:all) do
14
+ Campo.plugins.clear
15
+ Campo.plugin :partial # this is normally done by lib/campo.rb
16
+ Campo.plugin :Aria
17
+ end
18
18
 
19
19
  let(:top_bit) { s = <<-'STR'
20
20
  - atts = {} if atts.nil?
21
- - atts.default = {} if atts.default.nil?
21
+ - atts.default_proc = proc {|hash, key| hash[key] = {} } if atts.default_proc.nil?
22
22
  - inners = {} if inners.nil?
23
23
  - inners.default = "" if inners.default.nil?
24
- - i = 0 # for tabindex
25
-
24
+ - @campo_tabindex ||= 0 # for tabindex
26
25
  STR
27
- s
28
- }
29
-
26
+ s
27
+ }
30
28
 
31
29
  describe :output do
32
30
  context "Given a form with no fields" do
33
31
  let(:expected) {
34
- expected = top_bit + %q!%form{ atts[:myform], method: "POST", id: "myform", name: "myform", }!.strip + "\n"
32
+ expected = top_bit + %q!%form{ atts[:myform], id: "myform", method: "POST", name: "myform", role: "form", }!.strip + "\n"
35
33
  }
36
34
 
37
35
  subject{ Campo.output Campo::Form.new( "myform" ) }
@@ -44,11 +42,11 @@ STR
44
42
  let(:expected) { %q!
45
43
  %label{ for: "teas", }
46
44
  Favourite tea:
47
- %select{ atts[:teas], tabindex: "#{i += 1}", name: "teas", }
48
- %option{ value: "", disabled: "disabled", name: "teas", }Choose one:
49
- %option{ atts[:teas_ceylon], value: "ceylon", id: "teas_ceylon", name: "teas", }Ceylon
50
- %option{ atts[:teas_breakfast], value: "breakfast", id: "teas_breakfast", name: "teas", }Breakfast
51
- %option{ atts[:teas_earl_grey], value: "earl grey", id: "teas_earl_grey", name: "teas", }Earl grey
45
+ %select{ atts[:teas], tabindex: "#{@campo_tabindex += 1}", id: "teas", name: "teas", }
46
+ %option{ atts[:teas_default], id: "teas_default", value: "", disabled: "disabled", name: "teas", }Choose one:
47
+ %option{ atts[:teas_ceylon], id: "teas_ceylon", value: "ceylon", name: "teas", }Ceylon
48
+ %option{ atts[:teas_breakfast], id: "teas_breakfast", value: "breakfast", name: "teas", }Breakfast
49
+ %option{ atts[:teas_earl_grey], id: "teas_earl_grey", value: "earl grey", name: "teas", }Earl grey
52
50
  !.strip + "\n"
53
51
  }
54
52
  let(:tag) {
@@ -56,7 +54,7 @@ STR
56
54
  tag = select.with_default.option("ceylon").option("breakfast").option("earl grey").labelled("Favourite tea:")
57
55
  tag
58
56
  }
59
- subject { Campo.output :partial, tag }
57
+ subject { Campo.output tag, :partial=>true }
60
58
  it { should_not be_nil }
61
59
  it { should == expected }
62
60
 
@@ -65,54 +63,124 @@ STR
65
63
 
66
64
  describe "A realish form" do
67
65
  context "Given a form" do
68
- let(:form) do
69
- form = Campo::Form.new( "personal_details", action: %Q!"uri("/my/personal_details/update/")! )
70
-
71
- form.fieldset("Your details") do |f|
72
-
73
- f.text( "full_name", "Full name: ", size: 60 )
74
- f.text( "dob", "Date of birth: ", size: 10 ) #TODO change this
75
- f.fieldset( "Gender: " ) do |genders|
76
- genders.radio( "gender", "Male", value: 1 )
77
- genders.radio( "gender", "Female", value: 2 )
78
- end
79
- f.select( "ethnicorigin_id", {opts: [[1, "White"],[2,"Asian"],[3,"Black"],[4,"Chinese and Other"], [5,"Mixed"] ] }).with_default.labelled( "Ethnic-origin: " )
80
- f.text( "occupation", "Occupation: ", size: 60 )
81
- f.text( "phone_landline", "Phone (landline): ", size: 20 )
82
- f.text( "phone_mobile", "Phone (mobile): ", size: 20 )
83
- f.fieldset( "May we contact you..." ) do |c|
84
- c.checkbox( "contactable", "In the day?", value: "day" )
85
- c.checkbox( "contactable", "In the evening?", value: "evening" )
86
- end
87
- f.submit("Save")
88
-
89
- end # form
90
- form
91
- end # let
92
-
93
-
94
66
  let(:expected) {
95
- %Q!- atts = {} if atts.nil?\n- atts.default = {} if atts.default.nil?\n- inners = {} if inners.nil?\n- inners.default = "" if inners.default.nil?\n- i = 0 # for tabindex\n\n%form{ atts[:personal_details], method: "POST", action: uri("/my/personal_details/update/"), id: "personal_details", name: "personal_details", }\n %fieldset{ }\n %legend{ }Your details\n %label{ for: "full_name", }\n Full name: \n %input{ atts[:full_name], tabindex: "\#{i += 1}", type: "text", id: "full_name", size: "60", name: "full_name", }\n %label{ for: "dob", }\n Date of birth: \n %input{ atts[:dob], tabindex: "\#{i += 1}", type: "text", id: "dob", size: "10", name: "dob", }\n %fieldset{ }\n %legend{ }Gender: \n %label{ for: "gender_1", }\n Male\n %input{ atts[:gender_1], tabindex: "\#{i += 1}", type: "radio", id: "gender_1", value: "1", name: "gender", }\n %label{ for: "gender_2", }\n Female\n %input{ atts[:gender_2], tabindex: "\#{i += 1}", type: "radio", id: "gender_2", value: "2", name: "gender", }\n %label{ for: "ethnicorigin_id", }\n Ethnic-origin: \n %select{ atts[:ethnicorigin_id], tabindex: "\#{i += 1}", name: "ethnicorigin_id", }\n %option{ value: "", disabled: "disabled", name: "ethnicorigin_id", }Choose one:\n %option{ atts[:ethnicorigin_id_1], value: "1", id: "ethnicorigin_id_1", name: "ethnicorigin_id", }White\n %option{ atts[:ethnicorigin_id_2], value: "2", id: "ethnicorigin_id_2", name: "ethnicorigin_id", }Asian\n %option{ atts[:ethnicorigin_id_3], value: "3", id: "ethnicorigin_id_3", name: "ethnicorigin_id", }Black\n %option{ atts[:ethnicorigin_id_4], value: "4", id: "ethnicorigin_id_4", name: "ethnicorigin_id", }Chinese and Other\n %option{ atts[:ethnicorigin_id_5], value: "5", id: "ethnicorigin_id_5", name: "ethnicorigin_id", }Mixed\n %label{ for: "occupation", }\n Occupation: \n %input{ atts[:occupation], tabindex: "\#{i += 1}", type: "text", id: "occupation", size: "60", name: "occupation", }\n %label{ for: "phone_landline", }\n Phone (landline): \n %input{ atts[:phone_landline], tabindex: "\#{i += 1}", type: "text", id: "phone_landline", size: "20", name: "phone_landline", }\n %label{ for: "phone_mobile", }\n Phone (mobile): \n %input{ atts[:phone_mobile], tabindex: "\#{i += 1}", type: "text", id: "phone_mobile", size: "20", name: "phone_mobile", }\n %fieldset{ }\n %legend{ }May we contact you...\n %label{ for: "contactable_day", }\n In the day?\n %input{ atts[:contactable_day], tabindex: "\#{i += 1}", type: "checkbox", id: "contactable_day", value: "day", name: "contactable", }\n %label{ for: "contactable_evening", }\n In the evening?\n %input{ atts[:contactable_evening], tabindex: "\#{i += 1}", type: "checkbox", id: "contactable_evening", value: "evening", name: "contactable", }\n %input{ atts[:Save_Save], tabindex: "\#{i += 1}", type: "submit", id: "Save_Save", value: "Save", }\n!
67
+ top_bit + %Q!%form{ atts[:personal_details], id: "personal_details", method: "POST", action: uri("/my/personal_details/update/"), name: "personal_details", role: "form", }\n %fieldset{ }\n %legend{ }Your details\n %label{ for: "full_name", }\n Full name: \n %input{ atts[:full_name], tabindex: "\#{@campo_tabindex += 1}", id: "full_name", type: "text", size: "60", name: "full_name", }\n %label{ for: "dob", }\n Date of birth: \n %input{ atts[:dob], tabindex: "\#{@campo_tabindex += 1}", id: "dob", type: "text", size: "10", name: "dob", }\n %fieldset{ }\n %legend{ }Gender: \n %label{ for: "gender_1", }\n Male\n %input{ atts[:gender_1], tabindex: "\#{@campo_tabindex += 1}", id: "gender_1", type: "radio", value: "1", name: "gender", }\n %label{ for: "gender_2", }\n Female\n %input{ atts[:gender_2], tabindex: "\#{@campo_tabindex += 1}", id: "gender_2", type: "radio", value: "2", name: "gender", }\n %label{ for: "ethnicorigin_id", }\n Ethnic-origin: \n %select{ atts[:ethnicorigin_id], tabindex: "\#{@campo_tabindex += 1}", id: "ethnicorigin_id", name: "ethnicorigin_id", }\n %option{ atts[:ethnicorigin_id_default], id: "ethnicorigin_id_default", value: "", disabled: "disabled", name: "ethnicorigin_id", }Choose one:\n %option{ atts[:ethnicorigin_id_1], id: "ethnicorigin_id_1", value: "1", name: "ethnicorigin_id", }White\n %option{ atts[:ethnicorigin_id_2], id: "ethnicorigin_id_2", value: "2", name: "ethnicorigin_id", }Asian\n %option{ atts[:ethnicorigin_id_3], id: "ethnicorigin_id_3", value: "3", name: "ethnicorigin_id", }Black\n %option{ atts[:ethnicorigin_id_4], id: "ethnicorigin_id_4", value: "4", name: "ethnicorigin_id", }Chinese and Other\n %option{ atts[:ethnicorigin_id_5], id: "ethnicorigin_id_5", value: "5", name: "ethnicorigin_id", }Mixed\n %label{ for: "occupation", }\n Occupation: \n %input{ atts[:occupation], tabindex: "\#{@campo_tabindex += 1}", id: "occupation", type: "text", size: "60", name: "occupation", }\n %label{ for: "phone_landline", }\n Phone (landline): \n %input{ atts[:phone_landline], tabindex: "\#{@campo_tabindex += 1}", id: "phone_landline", type: "text", size: "20", name: "phone_landline", }\n %label{ for: "phone_mobile", }\n Phone (mobile): \n %input{ atts[:phone_mobile], tabindex: "\#{@campo_tabindex += 1}", id: "phone_mobile", type: "text", size: "20", name: "phone_mobile", }\n %fieldset{ }\n %legend{ }May we contact you...\n %label{ for: "contactable_day", }\n In the day?\n %input{ atts[:contactable_day], tabindex: "\#{@campo_tabindex += 1}", id: "contactable_day", type: "checkbox", value: "day", name: "contactable", }\n %label{ for: "contactable_evening", }\n In the evening?\n %input{ atts[:contactable_evening], tabindex: "\#{@campo_tabindex += 1}", id: "contactable_evening", type: "checkbox", value: "evening", name: "contactable", }\n %input{ atts[:method], id: "method", type: "hidden", value: "put", name: "method", }\n %input{ atts[:Save], tabindex: "\#{@campo_tabindex += 1}", id: "Save", type: "submit", value: "Save", }\n!
96
68
  } # let expected
97
-
98
- subject{ Campo.output form }
99
- it { should == expected }
69
+ context "block with var style" do
70
+ let(:form) do
71
+ form = Campo::Form.new( "personal_details", action: %Q!"uri("/my/personal_details/update/")! )
72
+
73
+ form.fieldset("Your details") do |f|
74
+
75
+ f.text( "full_name", "Full name: ", size: 60 )
76
+ f.text( "dob", "Date of birth: ", size: 10 ) #TODO change this
77
+ f.fieldset( "Gender: " ) do |genders|
78
+ genders.radio( "gender", "Male", value: 1 )
79
+ genders.radio( "gender", "Female", value: 2 )
80
+ end
81
+ f.select( "ethnicorigin_id", {opts: [[1, "White"],[2,"Asian"],[3,"Black"],[4,"Chinese and Other"], [5,"Mixed"] ] }).with_default.labelled( "Ethnic-origin: " )
82
+ f.text( "occupation", "Occupation: ", size: 60 )
83
+ f.text( "phone_landline", "Phone (landline): ", size: 20 )
84
+ f.text( "phone_mobile", "Phone (mobile): ", size: 20 )
85
+ f.fieldset( "May we contact you..." ) do |c|
86
+ c.checkbox( "contactable", "In the day?", value: "day" )
87
+ c.checkbox( "contactable", "In the evening?", value: "evening" )
88
+ end
89
+ f.hidden "method", value: "put"
90
+ f.submit("Save")
100
91
 
92
+ end # form
93
+ form
94
+ end # let
101
95
 
102
- context "With a div to wrap it in" do
103
- let(:doc) {
104
- doc = Campo.literal ".centred.form" do |wrapper|
105
- wrapper << form
106
- end
107
- }
108
- let(:expected) {
109
- %Q!- atts = {} if atts.nil?\n- atts.default = {} if atts.default.nil?\n- inners = {} if inners.nil?\n- inners.default = "" if inners.default.nil?\n- i = 0 # for tabindex\n\n.centred.form\n %form{ atts[:personal_details], method: "POST", action: uri("/my/personal_details/update/"), id: "personal_details", name: "personal_details", }\n %fieldset{ }\n %legend{ }Your details\n %label{ for: "full_name", }\n Full name: \n %input{ atts[:full_name], tabindex: "\#{i += 1}", type: "text", id: "full_name", size: "60", name: "full_name", }\n %label{ for: "dob", }\n Date of birth: \n %input{ atts[:dob], tabindex: "\#{i += 1}", type: "text", id: "dob", size: "10", name: "dob", }\n %fieldset{ }\n %legend{ }Gender: \n %label{ for: "gender_1", }\n Male\n %input{ atts[:gender_1], tabindex: "\#{i += 1}", type: "radio", id: "gender_1", value: "1", name: "gender", }\n %label{ for: "gender_2", }\n Female\n %input{ atts[:gender_2], tabindex: "\#{i += 1}", type: "radio", id: "gender_2", value: "2", name: "gender", }\n %label{ for: "ethnicorigin_id", }\n Ethnic-origin: \n %select{ atts[:ethnicorigin_id], tabindex: "\#{i += 1}", name: "ethnicorigin_id", }\n %option{ value: "", disabled: "disabled", name: "ethnicorigin_id", }Choose one:\n %option{ atts[:ethnicorigin_id_1], value: "1", id: "ethnicorigin_id_1", name: "ethnicorigin_id", }White\n %option{ atts[:ethnicorigin_id_2], value: "2", id: "ethnicorigin_id_2", name: "ethnicorigin_id", }Asian\n %option{ atts[:ethnicorigin_id_3], value: "3", id: "ethnicorigin_id_3", name: "ethnicorigin_id", }Black\n %option{ atts[:ethnicorigin_id_4], value: "4", id: "ethnicorigin_id_4", name: "ethnicorigin_id", }Chinese and Other\n %option{ atts[:ethnicorigin_id_5], value: "5", id: "ethnicorigin_id_5", name: "ethnicorigin_id", }Mixed\n %label{ for: "occupation", }\n Occupation: \n %input{ atts[:occupation], tabindex: "\#{i += 1}", type: "text", id: "occupation", size: "60", name: "occupation", }\n %label{ for: "phone_landline", }\n Phone (landline): \n %input{ atts[:phone_landline], tabindex: "\#{i += 1}", type: "text", id: "phone_landline", size: "20", name: "phone_landline", }\n %label{ for: "phone_mobile", }\n Phone (mobile): \n %input{ atts[:phone_mobile], tabindex: "\#{i += 1}", type: "text", id: "phone_mobile", size: "20", name: "phone_mobile", }\n %fieldset{ }\n %legend{ }May we contact you...\n %label{ for: "contactable_day", }\n In the day?\n %input{ atts[:contactable_day], tabindex: "\#{i += 1}", type: "checkbox", id: "contactable_day", value: "day", name: "contactable", }\n %label{ for: "contactable_evening", }\n In the evening?\n %input{ atts[:contactable_evening], tabindex: "\#{i += 1}", type: "checkbox", id: "contactable_evening", value: "evening", name: "contactable", }\n %input{ atts[:Save_Save], tabindex: "\#{i += 1}", type: "submit", id: "Save_Save", value: "Save", }\n!
110
- } # let expected
111
-
112
- subject{ Campo.output doc }
96
+ subject{ Campo.output form }
113
97
  it { should == expected }
98
+
99
+ context "With a div to wrap it in" do
100
+ let(:doc) {
101
+ doc = Campo.literal( ".centred.form" ) << form
102
+ }
103
+ let(:expected) {
104
+ top_bit + %Q!.centred.form
105
+ %form{ atts[:personal_details], id: "personal_details", method: "POST", action: uri("/my/personal_details/update/"), name: "personal_details", role: "form", }
106
+ %fieldset{ }
107
+ %legend{ }Your details
108
+ %label{ for: "full_name", }
109
+ Full name:
110
+ %input{ atts[:full_name], tabindex: "\#{@campo_tabindex += 1}", id: "full_name", type: "text", size: "60", name: "full_name", }
111
+ %label{ for: "dob", }
112
+ Date of birth:
113
+ %input{ atts[:dob], tabindex: "\#{@campo_tabindex += 1}", id: "dob", type: "text", size: "10", name: "dob", }
114
+ %fieldset{ }
115
+ %legend{ }Gender:
116
+ %label{ for: "gender_1", }
117
+ Male
118
+ %input{ atts[:gender_1], tabindex: "\#{@campo_tabindex += 1}", id: "gender_1", type: "radio", value: "1", name: "gender", }
119
+ %label{ for: "gender_2", }
120
+ Female
121
+ %input{ atts[:gender_2], tabindex: "\#{@campo_tabindex += 1}", id: "gender_2", type: "radio", value: "2", name: "gender", }
122
+ %label{ for: "ethnicorigin_id", }
123
+ Ethnic-origin:
124
+ %select{ atts[:ethnicorigin_id], tabindex: "\#{@campo_tabindex += 1}", id: "ethnicorigin_id", name: "ethnicorigin_id", }
125
+ %option{ atts[:ethnicorigin_id_default], id: "ethnicorigin_id_default", value: "", disabled: "disabled", name: "ethnicorigin_id", }Choose one:
126
+ %option{ atts[:ethnicorigin_id_1], id: "ethnicorigin_id_1", value: "1", name: "ethnicorigin_id", }White
127
+ %option{ atts[:ethnicorigin_id_2], id: "ethnicorigin_id_2", value: "2", name: "ethnicorigin_id", }Asian
128
+ %option{ atts[:ethnicorigin_id_3], id: "ethnicorigin_id_3", value: "3", name: "ethnicorigin_id", }Black
129
+ %option{ atts[:ethnicorigin_id_4], id: "ethnicorigin_id_4", value: "4", name: "ethnicorigin_id", }Chinese and Other
130
+ %option{ atts[:ethnicorigin_id_5], id: "ethnicorigin_id_5", value: "5", name: "ethnicorigin_id", }Mixed
131
+ %label{ for: "occupation", }
132
+ Occupation:
133
+ %input{ atts[:occupation], tabindex: "\#{@campo_tabindex += 1}", id: "occupation", type: "text", size: "60", name: "occupation", }
134
+ %label{ for: "phone_landline", }
135
+ Phone (landline):
136
+ %input{ atts[:phone_landline], tabindex: "\#{@campo_tabindex += 1}", id: "phone_landline", type: "text", size: "20", name: "phone_landline", }
137
+ %label{ for: "phone_mobile", }
138
+ Phone (mobile):
139
+ %input{ atts[:phone_mobile], tabindex: "\#{@campo_tabindex += 1}", id: "phone_mobile", type: "text", size: "20", name: "phone_mobile", }
140
+ %fieldset{ }
141
+ %legend{ }May we contact you...
142
+ %label{ for: "contactable_day", }
143
+ In the day?
144
+ %input{ atts[:contactable_day], tabindex: "\#{@campo_tabindex += 1}", id: "contactable_day", type: "checkbox", value: "day", name: "contactable", }
145
+ %label{ for: "contactable_evening", }
146
+ In the evening?
147
+ %input{ atts[:contactable_evening], tabindex: "\#{@campo_tabindex += 1}", id: "contactable_evening", type: "checkbox", value: "evening", name: "contactable", }
148
+ %input{ atts[:method], id: "method", type: "hidden", value: "put", name: "method", }
149
+ %input{ atts[:Save], tabindex: "\#{@campo_tabindex += 1}", id: "Save", type: "submit", value: "Save", }
150
+ !
151
+ } # let expected
152
+
153
+ subject{ Campo.output doc }
154
+ it { should == expected }
155
+ end # context
114
156
  end # context
115
- end # context
157
+ context "with no var for block" do
158
+ let(:form) do
159
+ form = Campo.form "personal_details", action: %Q!"uri("/my/personal_details/update/")! do
160
+ fieldset("Your details") do
161
+ text( "full_name", "Full name: ", size: 60 )
162
+ text( "dob", "Date of birth: ", size: 10 ) #TODO change this
163
+ fieldset( "Gender: " ) do
164
+ radio( "gender", "Male", value: 1 )
165
+ radio( "gender", "Female", value: 2 )
166
+ end
167
+ select( "ethnicorigin_id", {opts: [[1, "White"],[2,"Asian"],[3,"Black"],[4,"Chinese and Other"], [5,"Mixed"] ] }).with_default.labelled( "Ethnic-origin: " )
168
+ text( "occupation", "Occupation: ", size: 60 )
169
+ text( "phone_landline", "Phone (landline): ", size: 20 )
170
+ text( "phone_mobile", "Phone (mobile): ", size: 20 )
171
+ fieldset( "May we contact you..." ) do
172
+ checkbox( "contactable", "In the day?", value: "day" )
173
+ checkbox( "contactable", "In the evening?", value: "evening" )
174
+ end
175
+ hidden "method", value: "put"
176
+ submit("Save")
177
+ end
178
+ end
179
+ end # let
180
+ subject{ Campo.output form }
181
+ it { should == expected }
182
+ end
183
+ end
116
184
  end # describe a form
117
185
  end
118
186
 
@@ -131,10 +199,10 @@ let(:expected) {
131
199
  context "of text" do
132
200
  context "with a label" do
133
201
  let(:expected) { top_bit + %q!
134
- %form{ atts[:myform], method: "POST", id: "myform", name: "myform", }
135
- %label{ for: "blah_blahdeblah", }
202
+ %form{ atts[:myform], id: "myform", method: "POST", name: "myform", role: "form", }
203
+ %label{ for: "blah", }
136
204
  Blahd
137
- %input{ atts[:blah_blahdeblah], tabindex: "#{i += 1}", type: "text", id: "blah_blahdeblah", value: "blahdeblah", name: "blah", }!.strip + "\n" }
205
+ %input{ atts[:blah], tabindex: "#{@campo_tabindex += 1}", id: "blah", type: "text", value: "blahdeblah", name: "blah", }!.strip + "\n" }
138
206
  subject {
139
207
  form.input( "blah", :text, "Blahd", value: "blahdeblah" )
140
208
  Campo.output form
@@ -152,10 +220,10 @@ let(:expected) {
152
220
  end
153
221
  context "without a label" do
154
222
  let(:expected) { top_bit + %q!
155
- %form{ atts[:myform], method: "POST", id: "myform", name: "myform", }
156
- %label{ for: "blah_blahdeblah", }
223
+ %form{ atts[:myform], id: "myform", method: "POST", name: "myform", role: "form", }
224
+ %label{ for: "blah", }
157
225
  Blah
158
- %input{ atts[:blah_blahdeblah], tabindex: "#{i += 1}", type: "text", id: "blah_blahdeblah", value: "blahdeblah", name: "blah", }!.strip + "\n" }
226
+ %input{ atts[:blah], tabindex: "#{@campo_tabindex += 1}", id: "blah", type: "text", value: "blahdeblah", name: "blah", }!.strip + "\n" }
159
227
 
160
228
  subject {
161
229
  form.input( "blah", :text, value: "blahdeblah" )
@@ -176,10 +244,10 @@ let(:expected) {
176
244
  end # text
177
245
  context "of checkbox" do
178
246
  let(:expected) { top_bit + %q!
179
- %form{ atts[:myform], method: "POST", id: "myform", name: "myform", }
247
+ %form{ atts[:myform], id: "myform", method: "POST", name: "myform", role: "form", }
180
248
  %label{ for: "blah_blahdeblah", }
181
249
  Blahd
182
- %input{ atts[:blah_blahdeblah], tabindex: "#{i += 1}", type: "checkbox", id: "blah_blahdeblah", value: "blahdeblah", name: "blah", }!.strip + "\n" }
250
+ %input{ atts[:blah_blahdeblah], tabindex: "#{@campo_tabindex += 1}", id: "blah_blahdeblah", type: "checkbox", value: "blahdeblah", name: "blah", }!.strip + "\n" }
183
251
 
184
252
  subject {
185
253
  form.input( "blah", :checkbox, "Blahd", value: "blahdeblah" )
@@ -413,7 +481,7 @@ s.chomp
413
481
 
414
482
 
415
483
  context "simple output" do
416
- let(:expected) { %q!%form{ atts[:myform], method: "POST", id: "myform", name: "myform", }! }
484
+ let(:expected) { %q!%form{ atts[:myform], id: "myform", method: "POST", name: "myform", }! }
417
485
  subject { form.output }
418
486
  it { should == expected }
419
487
  end
@@ -425,7 +493,7 @@ s.chomp
425
493
  it { should be_a_kind_of(Form) }
426
494
 
427
495
  context "simple output" do
428
- let(:expected) { %q!%form{ atts[:myform], method: "POST", action: "/", id: "myform", name: "myform", }! }
496
+ let(:expected) { %q!%form{ atts[:myform], id: "myform", method: "POST", action: "/", name: "myform", }! }
429
497
  subject { form.output }
430
498
  it { should == expected }
431
499
  end
@@ -436,14 +504,14 @@ s.chomp
436
504
 
437
505
  describe :fieldset do
438
506
  context "When given a form with only a name" do
439
- let(:form) { Campo::Form.new( "myform" ) }
507
+ let(:form) { Campo::Form.new( "myform" ).fieldset("Do you like these colours? Tick for yes:") }
440
508
  let(:expected) { top_bit + %q!
441
- %form{ atts[:myform], method: "POST", id: "myform", name: "myform", }
509
+ %form{ atts[:myform], id: "myform", method: "POST", name: "myform", role: "form", }
442
510
  %fieldset{ }
443
511
  %legend{ }Do you like these colours? Tick for yes:
444
512
 
445
513
  !.strip + "\n" }
446
- subject { form.fieldset("Do you like these colours? Tick for yes:")
514
+ subject {
447
515
  Campo.output form
448
516
  }
449
517
  it { should_not be_nil }
@@ -458,14 +526,14 @@ s.chomp
458
526
  let(:form) { Campo::Form.new( "myform" ) }
459
527
  context "Given one select tag" do
460
528
  let(:expected) { top_bit + %q!
461
- %form{ atts[:myform], method: "POST", id: "myform", name: "myform", }
529
+ %form{ atts[:myform], id: "myform", method: "POST", name: "myform", role: "form", }
462
530
  %label{ for: "teas", }
463
531
  Favourite tea:
464
- %select{ atts[:teas], tabindex: "#{i += 1}", name: "teas", }
465
- %option{ value: "", disabled: "disabled", name: "teas", }Choose one:
466
- %option{ atts[:teas_ceylon], value: "ceylon", id: "teas_ceylon", name: "teas", }Ceylon
467
- %option{ atts[:teas_breakfast], value: "breakfast", id: "teas_breakfast", name: "teas", }Breakfast
468
- %option{ atts[:teas_earl_grey], value: "earl grey", id: "teas_earl_grey", name: "teas", }Earl grey
532
+ %select{ atts[:teas], tabindex: "#{@campo_tabindex += 1}", id: "teas", name: "teas", }
533
+ %option{ atts[:teas_default], id: "teas_default", value: "", disabled: "disabled", name: "teas", }Choose one:
534
+ %option{ atts[:teas_ceylon], id: "teas_ceylon", value: "ceylon", name: "teas", }Ceylon
535
+ %option{ atts[:teas_breakfast], id: "teas_breakfast", value: "breakfast", name: "teas", }Breakfast
536
+ %option{ atts[:teas_earl_grey], id: "teas_earl_grey", value: "earl grey", name: "teas", }Earl grey
469
537
 
470
538
  !.strip + "\n"
471
539
  }
@@ -479,22 +547,22 @@ s.chomp
479
547
  end
480
548
  context "Given several select tags" do
481
549
  let(:expected) { top_bit + %q!
482
- %form{ atts[:myform], method: "POST", id: "myform", name: "myform", }
550
+ %form{ atts[:myform], id: "myform", method: "POST", name: "myform", role: "form", }
483
551
  %label{ for: "teas", }
484
552
  Favourite tea:
485
- %select{ atts[:teas], tabindex: "#{i += 1}", name: "teas", }
486
- %option{ value: "", disabled: "disabled", name: "teas", }Choose one:
487
- %option{ atts[:teas_ceylon], value: "ceylon", id: "teas_ceylon", name: "teas", }Ceylon
488
- %option{ atts[:teas_breakfast], value: "breakfast", id: "teas_breakfast", name: "teas", }Breakfast
489
- %option{ atts[:teas_earl_grey], value: "earl grey", id: "teas_earl_grey", name: "teas", }Earl grey
553
+ %select{ atts[:teas], tabindex: "#{@campo_tabindex += 1}", id: "teas", name: "teas", }
554
+ %option{ atts[:teas_default], id: "teas_default", value: "", disabled: "disabled", name: "teas", }Choose one:
555
+ %option{ atts[:teas_ceylon], id: "teas_ceylon", value: "ceylon", name: "teas", }Ceylon
556
+ %option{ atts[:teas_breakfast], id: "teas_breakfast", value: "breakfast", name: "teas", }Breakfast
557
+ %option{ atts[:teas_earl_grey], id: "teas_earl_grey", value: "earl grey", name: "teas", }Earl grey
490
558
  %label{ for: "coffees", }
491
559
  Favourite coffee:
492
- %select{ atts[:coffees], tabindex: "#{i += 1}", name: "coffees", }
493
- %option{ value: "", disabled: "disabled", name: "coffees", }Choose one:
494
- %option{ atts[:coffees_blue_mountain], value: "blue mountain", id: "coffees_blue_mountain", name: "coffees", }Blue mountain
495
- %option{ atts[:coffees_kenyan_peaberry], value: "kenyan peaberry", id: "coffees_kenyan_peaberry", name: "coffees", }Kenyan peaberry
496
- %option{ atts[:coffees_colombian], value: "colombian", id: "coffees_colombian", name: "coffees", }Colombian
497
- %option{ atts[:coffees_java], value: "java", id: "coffees_java", name: "coffees", }Java
560
+ %select{ atts[:coffees], tabindex: "#{@campo_tabindex += 1}", id: "coffees", name: "coffees", }
561
+ %option{ atts[:coffees_default], id: "coffees_default", value: "", disabled: "disabled", name: "coffees", }Choose one:
562
+ %option{ atts[:coffees_blue_mountain], id: "coffees_blue_mountain", value: "blue mountain", name: "coffees", }Blue mountain
563
+ %option{ atts[:coffees_kenyan_peaberry], id: "coffees_kenyan_peaberry", value: "kenyan peaberry", name: "coffees", }Kenyan peaberry
564
+ %option{ atts[:coffees_colombian], id: "coffees_colombian", value: "colombian", name: "coffees", }Colombian
565
+ %option{ atts[:coffees_java], id: "coffees_java", value: "java", name: "coffees", }Java
498
566
 
499
567
  !.strip + "\n" }
500
568
  before {
@@ -513,8 +581,8 @@ s.chomp
513
581
  context "Given a submit button" do
514
582
  context "With no arguments" do
515
583
  let(:expected) { top_bit + %q!
516
- %form{ atts[:myform], method: "POST", id: "myform", name: "myform", }
517
- %input{ atts[:Submit_Submit], tabindex: "#{i += 1}", type: "submit", id: "Submit_Submit", value: "Submit", }
584
+ %form{ atts[:myform], id: "myform", method: "POST", name: "myform", role: "form", }
585
+ %input{ atts[:Submit], tabindex: "#{@campo_tabindex += 1}", id: "Submit", type: "submit", value: "Submit", }
518
586
 
519
587
  !.strip + "\n" }
520
588
 
@@ -528,13 +596,13 @@ s.chomp
528
596
  end
529
597
  context "With a name" do
530
598
  let(:expected) { top_bit + %q!
531
- %form{ atts[:myform], method: "POST", id: "myform", name: "myform", }
532
- %input{ atts[:Save_Save], tabindex: "#{i += 1}", type: "submit", id: "Save_Save", value: "Save", }
599
+ %form{ atts[:myform], id: "myform", method: "POST", name: "myform", role: "form", }
600
+ %input{ atts[:save], tabindex: "#{@campo_tabindex += 1}", id: "save", type: "submit", value: "Save", }
533
601
 
534
602
  !.strip + "\n" }
535
603
 
536
604
  subject {
537
- form.submit( "Save" )
605
+ form.submit( "save", value: "Save" )
538
606
  Campo.output form
539
607
  }
540
608
 
@@ -545,6 +613,25 @@ s.chomp
545
613
  end
546
614
  end
547
615
 
616
+ describe Span do
617
+ let(:tag) { Span.new "my_id", "anything at all at all" }
618
+ subject { tag }
619
+ it { should_not be_nil }
620
+ it { should be_a_kind_of( Span ) }
621
+
622
+ describe :output do
623
+ let(:expected) { %Q!%span{id: "my_id", }! }
624
+ subject { tag.output }
625
+ it { should == expected }
626
+ end
627
+
628
+ describe "Campo.output" do
629
+ let(:expected) { %Q!%span{id: "my_id", }\n anything at all at all\n! }
630
+ subject { Campo.output tag, :partial=>true }
631
+ it { should == expected }
632
+ end
633
+
634
+ end
548
635
 
549
636
  describe Literal do
550
637
  let(:tag) { Literal.new "anything at all at all" }
@@ -560,7 +647,7 @@ s.chomp
560
647
 
561
648
  describe "Campo.output" do
562
649
  let(:expected) { "anything at all at all\n" }
563
- subject { Campo.output :partial, tag }
650
+ subject { Campo.output tag, :partial=>true }
564
651
  it { should == expected }
565
652
  end
566
653
 
@@ -572,7 +659,7 @@ s.chomp
572
659
 
573
660
  describe "the full output" do
574
661
  let(:expected) { top_bit + %q$
575
- %form{ atts[:myform], method: "POST", id: "myform", name: "myform", }
662
+ %form{ atts[:myform], id: "myform", method: "POST", name: "myform", role: "form", }
576
663
  Hello, World!$.strip + "\n"}
577
664
  let(:form){
578
665
  form = Campo::Form.new( "myform" )
@@ -598,7 +685,7 @@ s.chomp
598
685
  end
599
686
  Campo.output form
600
687
  }
601
- let(:expected) { top_bit + %q$%form{ atts[:myform], method: "POST", id: "myform", name: "myform", }
688
+ let(:expected) { top_bit + %q$%form{ atts[:myform], id: "myform", method: "POST", name: "myform", role: "form", }
602
689
  %p
603
690
  Whatever
604
691
  %br
@@ -631,7 +718,7 @@ $.strip + "\n" }
631
718
 
632
719
  describe "Campo.output" do
633
720
  let(:expected) { %Q!= sel_opts\n! }
634
- subject { Campo.output :partial, tag }
721
+ subject { Campo.output tag,:partial=>true }
635
722
  it { should == expected }
636
723
  end
637
724
 
@@ -643,7 +730,7 @@ $.strip + "\n" }
643
730
 
644
731
  describe "the full output" do
645
732
  let(:expected) { top_bit + %q!
646
- %form{ atts[:myform], method: "POST", id: "myform", name: "myform", }
733
+ %form{ atts[:myform], id: "myform", method: "POST", name: "myform", role: "form", }
647
734
  = 5 + 1!.strip + "\n"}
648
735
  let(:form){
649
736
  form = Campo::Form.new( "myform" )
@@ -668,7 +755,7 @@ $.strip + "\n" }
668
755
  end
669
756
  Campo.output form
670
757
  }
671
- let(:expected) { top_bit + %q$%form{ atts[:myform], method: "POST", id: "myform", name: "myform", }
758
+ let(:expected) { top_bit + %q$%form{ atts[:myform], id: "myform", method: "POST", name: "myform", role: "form", }
672
759
  = 5 + 1
673
760
  ="%p"
674
761
  Whatever
@@ -707,27 +794,71 @@ $.strip + "\n" }
707
794
  subject { tag }
708
795
  it { should_not be_nil }
709
796
  it { should be_a_kind_of(Select) }
710
- specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
797
+ specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }! }
711
798
 
712
799
  context "Campo.output" do
713
- let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }!.strip + "\n" }
714
- subject { Campo.output :partial, tag }
800
+ let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }!.strip + "\n" }
801
+ subject { Campo.output tag,:partial=>true }
715
802
  it { should_not be_nil }
716
803
  it { should == expected }
717
804
  end
718
805
 
719
806
  context "and a default" do
807
+ context "With no arguments" do
720
808
 
721
- subject { tag.with_default }
722
- it { should_not be_nil }
723
- it { should be_a_kind_of(Select) }
724
- specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
725
-
726
- context "Campo.output" do
727
- let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }
728
- %option{ value: "", disabled: "disabled", name: "pqr", }Choose one:!.strip + "\n" }
729
- subject { Campo.output :partial, tag.with_default }
730
- it { should == expected }
809
+ subject { tag.with_default }
810
+ it { should_not be_nil }
811
+ it { should be_a_kind_of(Select) }
812
+ specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }! }
813
+
814
+ context "Campo.output" do
815
+ let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }
816
+ %option{ atts[:pqr_default], id: "pqr_default", value: "", disabled: "disabled", name: "pqr", }Choose one:!.strip + "\n" }
817
+ subject { Campo.output tag.with_default, :partial=>true }
818
+ it { should == expected }
819
+ end
820
+ end
821
+ context "Given an inner text" do
822
+ context "And no other args" do
823
+ subject { tag.with_default("Choose wisely!") }
824
+ it { should_not be_nil }
825
+ it { should be_a_kind_of(Select) }
826
+ specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }! }
827
+
828
+ context "Campo.output" do
829
+ let(:expected) { %q<%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }
830
+ %option{ atts[:pqr_default], id: "pqr_default", value: "", disabled: "disabled", name: "pqr", }Choose wisely!>.strip + "\n" }
831
+ subject { Campo.output tag.with_default("Choose wisely!"), :partial=>true }
832
+ it { should == expected }
833
+ end
834
+ end
835
+ context "And an empty hash" do
836
+ subject { tag.with_default("Choose wisely!",{}) }
837
+ it { should_not be_nil }
838
+ it { should be_a_kind_of(Select) }
839
+ specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }! }
840
+
841
+ context "Campo.output" do
842
+ let(:expected) { %q<%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }
843
+ %option{ atts[:pqr_default], id: "pqr_default", value: "", name: "pqr", }Choose wisely!>.strip + "\n" }
844
+ subject { Campo.output tag.with_default("Choose wisely!",{}), :partial=>true }
845
+ it { should == expected }
846
+ end
847
+ end
848
+ context "And an empty hash" do
849
+ subject { tag.with_default("Choose wisely!",{class: "default"}) }
850
+ it { should_not be_nil }
851
+ it { should be_a_kind_of(Select) }
852
+ specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }! }
853
+
854
+ context "Campo.output" do
855
+ let(:expected) { %q<%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }
856
+ %option{ atts[:pqr_default], id: "pqr_default", value: "", class: "default", name: "pqr", }Choose wisely!>.strip + "\n" }
857
+ subject { Campo.output tag.with_default("Choose wisely!",{class: "default"}), :partial=>true }
858
+ it { should == expected }
859
+ end
860
+ end
861
+
731
862
  end
732
863
  end
733
864
  end
@@ -745,15 +876,15 @@ $.strip + "\n" }
745
876
 
746
877
  it { should_not be_nil }
747
878
  it { should be_a_kind_of(Select) }
748
- specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
879
+ specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }! }
749
880
 
750
881
  context "Campo.output" do
751
- let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }
752
- %option{ atts[:pqr_volvo], value: "volvo", id: "pqr_volvo", name: "pqr", }Volvo
753
- %option{ atts[:pqr_saab], value: "saab", id: "pqr_saab", name: "pqr", }Saab
754
- %option{ atts[:pqr_audi], value: "audi", id: "pqr_audi", name: "pqr", }Audi
882
+ let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }
883
+ %option{ atts[:pqr_volvo], id: "pqr_volvo", value: "volvo", name: "pqr", }Volvo
884
+ %option{ atts[:pqr_saab], id: "pqr_saab", value: "saab", name: "pqr", }Saab
885
+ %option{ atts[:pqr_audi], id: "pqr_audi", value: "audi", name: "pqr", }Audi
755
886
  !.strip + "\n" }
756
- subject { Campo.output :partial, tag }
887
+ subject { Campo.output tag, :partial=>true }
757
888
  it { should_not be_nil }
758
889
  it { should == expected }
759
890
  end
@@ -764,16 +895,16 @@ $.strip + "\n" }
764
895
  subject { tag.with_default }
765
896
  it { should_not be_nil }
766
897
  it { should be_a_kind_of(Select) }
767
- specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
898
+ specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }! }
768
899
 
769
900
  context "Campo.output" do
770
- let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }
771
- %option{ value: "", disabled: "disabled", name: "pqr", }Choose one:
772
- %option{ atts[:pqr_volvo], value: "volvo", id: "pqr_volvo", name: "pqr", }Volvo
773
- %option{ atts[:pqr_saab], value: "saab", id: "pqr_saab", name: "pqr", }Saab
774
- %option{ atts[:pqr_audi], value: "audi", id: "pqr_audi", name: "pqr", }Audi
901
+ let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }
902
+ %option{ atts[:pqr_default], id: "pqr_default", value: "", disabled: "disabled", name: "pqr", }Choose one:
903
+ %option{ atts[:pqr_volvo], id: "pqr_volvo", value: "volvo", name: "pqr", }Volvo
904
+ %option{ atts[:pqr_saab], id: "pqr_saab", value: "saab", name: "pqr", }Saab
905
+ %option{ atts[:pqr_audi], id: "pqr_audi", value: "audi", name: "pqr", }Audi
775
906
  !.strip + "\n" }
776
- subject { Campo.output :partial, tag.with_default }
907
+ subject { Campo.output tag.with_default, :partial=>true }
777
908
  it { should == expected }
778
909
  end
779
910
  end
@@ -788,16 +919,16 @@ $.strip + "\n" }
788
919
  end
789
920
  }
790
921
  subject { tag }
791
- specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
922
+ specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }! }
792
923
 
793
924
  context "Campo.output" do
794
- let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }
795
- %option{ atts[:pqr_volvo], value: "volvo", id: "pqr_volvo", name: "pqr", }Volvo
796
- %option{ atts[:pqr_saab], value: "saab", id: "pqr_saab", name: "pqr", }Saab
797
- %option{ atts[:pqr_audi], value: "audi", id: "pqr_audi", name: "pqr", }Audi
925
+ let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }
926
+ %option{ atts[:pqr_volvo], id: "pqr_volvo", value: "volvo", name: "pqr", }Volvo
927
+ %option{ atts[:pqr_saab], id: "pqr_saab", value: "saab", name: "pqr", }Saab
928
+ %option{ atts[:pqr_audi], id: "pqr_audi", value: "audi", name: "pqr", }Audi
798
929
  = opts!.strip + "\n" }
799
930
  subject {
800
- Campo.output :partial, tag
931
+ Campo.output tag, :partial=>true
801
932
  }
802
933
  it { should_not be_nil }
803
934
  it { should == expected }
@@ -806,35 +937,35 @@ $.strip + "\n" }
806
937
  end
807
938
 
808
939
  context "and an array" do
809
- # context "of type [String]" do
810
- # let(:opts) { [["ford"], ["bmw"], ["ferrari", "checked"]] }
811
- # subject { Campo::Select.new( "pqr", {opts: opts} ) }
812
- #
813
- # it { should_not be_nil }
814
- # it { should be_a_kind_of(Select) }
815
- # specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
816
- # context "Campo.output" do
817
- # let(:tag){
818
- # Campo::Select.new( "pqr", {opts: opts} )
819
- # }
820
- # let(:expected) {
821
- # %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }
822
- # %option{ atts[:pqr_ford], value: "ford", id: "pqr_ford", name: "pqr", }Ford
823
- # %option{ atts[:pqr_bmw], value: "bmw", id: "pqr_bmw", name: "pqr", }BMW
824
- # %option{ atts[:pqr_ferrari], value: "ferrari", selected: "selected", id: "pqr_ferrari", name: "pqr", }Ferrari!.strip + "\n" }
825
- # subject { Campo.output :partial, tag }
826
- # it { should_not be_nil }
827
- # it { should == expected }
828
- # end
829
- #
830
- # end
940
+ context "of type [String]" do
941
+ let(:opts) { [["ford"], ["bmw"], ["ferrari", :selected]] }
942
+ subject { Campo::Select.new( "pqr", {opts: opts} ) }
943
+
944
+ it { should_not be_nil }
945
+ it { should be_a_kind_of(Select) }
946
+ specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }! }
947
+ context "Campo.output" do
948
+ let(:tag){
949
+ Campo::Select.new( "pqr", {opts: opts} )
950
+ }
951
+ let(:expected) {
952
+ %q!%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }
953
+ %option{ atts[:pqr_ford], id: "pqr_ford", value: "ford", name: "pqr", }Ford
954
+ %option{ atts[:pqr_bmw], id: "pqr_bmw", value: "bmw", name: "pqr", }Bmw
955
+ %option{ atts[:pqr_ferrari], id: "pqr_ferrari", value: "ferrari", selected: "selected", name: "pqr", }Ferrari!.strip + "\n" }
956
+ subject { Campo.output tag, :partial=>true }
957
+ it { should_not be_nil }
958
+ it { should == expected }
959
+ end
960
+
961
+ end
831
962
  context "of type [String, String]" do
832
963
  let(:opts) { [["ford", "ford"], ["bmw", "BMW"], ["ferrari", "Ferrari", "checked"]] }
833
964
  subject { Campo::Select.new( "pqr", {opts: opts} ) }
834
965
 
835
966
  it { should_not be_nil }
836
967
  it { should be_a_kind_of(Select) }
837
- specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
968
+ specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }! }
838
969
 
839
970
  context "with a block with options" do
840
971
  let(:opts) { [["ford", "Ford"], ["bmw", "BMW"], ["ferrari", "Ferrari", "checked"]] }
@@ -849,17 +980,17 @@ $.strip + "\n" }
849
980
 
850
981
  it { should_not be_nil }
851
982
  it { should be_a_kind_of(Select) }
852
- specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
983
+ specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }! }
853
984
 
854
985
  context "Campo.output" do
855
- let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }
856
- %option{ atts[:pqr_volvo], value: "volvo", id: "pqr_volvo", name: "pqr", }Volvo
857
- %option{ atts[:pqr_saab], value: "saab", id: "pqr_saab", name: "pqr", }Saab
858
- %option{ atts[:pqr_audi], value: "audi", id: "pqr_audi", name: "pqr", }Audi
859
- %option{ atts[:pqr_ford], value: "ford", id: "pqr_ford", name: "pqr", }Ford
860
- %option{ atts[:pqr_bmw], value: "bmw", id: "pqr_bmw", name: "pqr", }BMW
861
- %option{ atts[:pqr_ferrari], value: "ferrari", selected: "selected", id: "pqr_ferrari", name: "pqr", }Ferrari!.strip + "\n" }
862
- subject { Campo.output :partial, tag }
986
+ let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }
987
+ %option{ atts[:pqr_volvo], id: "pqr_volvo", value: "volvo", name: "pqr", }Volvo
988
+ %option{ atts[:pqr_saab], id: "pqr_saab", value: "saab", name: "pqr", }Saab
989
+ %option{ atts[:pqr_audi], id: "pqr_audi", value: "audi", name: "pqr", }Audi
990
+ %option{ atts[:pqr_ford], id: "pqr_ford", value: "ford", name: "pqr", }Ford
991
+ %option{ atts[:pqr_bmw], id: "pqr_bmw", value: "bmw", name: "pqr", }BMW
992
+ %option{ atts[:pqr_ferrari], id: "pqr_ferrari", value: "ferrari", selected: "selected", name: "pqr", }Ferrari!.strip + "\n" }
993
+ subject { Campo.output tag, :partial=>true }
863
994
  it { should_not be_nil }
864
995
  it { should == expected }
865
996
 
@@ -872,14 +1003,14 @@ $.strip + "\n" }
872
1003
  s.option "audi", "Audi"
873
1004
  end
874
1005
  }
875
- let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }
876
- %option{ atts[:pqr_volvo], value: "volvo", id: "pqr_volvo", name: "pqr", }Volvo
877
- %option{ atts[:pqr_saab], value: "saab", id: "pqr_saab", name: "pqr", }Saab
878
- %option{ atts[:pqr_audi], value: "audi", id: "pqr_audi", name: "pqr", }Audi
879
- %option{ atts[:pqr_ford], value: "ford", id: "pqr_ford", class: "blue", name: "pqr", }Ford
880
- %option{ atts[:pqr_bmw], value: "bmw", id: "pqr_bmw", name: "pqr", }BMW
881
- %option{ atts[:pqr_ferrari], value: "ferrari", selected: "selected", id: "pqr_ferrari", class: "green", name: "pqr", }Ferrari!.strip + "\n" }
882
- subject { Campo.output :partial, tag }
1006
+ let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }
1007
+ %option{ atts[:pqr_volvo], id: "pqr_volvo", value: "volvo", name: "pqr", }Volvo
1008
+ %option{ atts[:pqr_saab], id: "pqr_saab", value: "saab", name: "pqr", }Saab
1009
+ %option{ atts[:pqr_audi], id: "pqr_audi", value: "audi", name: "pqr", }Audi
1010
+ %option{ atts[:pqr_ford], id: "pqr_ford", value: "ford", class: "blue", name: "pqr", }Ford
1011
+ %option{ atts[:pqr_bmw], id: "pqr_bmw", value: "bmw", name: "pqr", }BMW
1012
+ %option{ atts[:pqr_ferrari], id: "pqr_ferrari", value: "ferrari", selected: "selected", class: "green", name: "pqr", }Ferrari!.strip + "\n" }
1013
+ subject { Campo.output tag, :partial=>true }
883
1014
  it { should_not be_nil }
884
1015
  it { should == expected }
885
1016
  end
@@ -890,18 +1021,18 @@ $.strip + "\n" }
890
1021
  subject { tag.with_default }
891
1022
  it { should_not be_nil }
892
1023
  it { should be_a_kind_of(Select) }
893
- specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }! }
1024
+ specify { subject.output.should == %q!%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }! }
894
1025
 
895
1026
  context "Campo.output" do
896
- let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{i += 1}", name: "pqr", }
897
- %option{ value: "", disabled: "disabled", name: "pqr", }Choose one:
898
- %option{ atts[:pqr_volvo], value: "volvo", id: "pqr_volvo", name: "pqr", }Volvo
899
- %option{ atts[:pqr_saab], value: "saab", id: "pqr_saab", name: "pqr", }Saab
900
- %option{ atts[:pqr_audi], value: "audi", id: "pqr_audi", name: "pqr", }Audi
901
- %option{ atts[:pqr_ford], value: "ford", id: "pqr_ford", name: "pqr", }Ford
902
- %option{ atts[:pqr_bmw], value: "bmw", id: "pqr_bmw", name: "pqr", }BMW
903
- %option{ atts[:pqr_ferrari], value: "ferrari", selected: "selected", id: "pqr_ferrari", name: "pqr", }Ferrari!.strip + "\n" }
904
- subject { Campo.output :partial, tag.with_default }
1027
+ let(:expected) { %q!%select{ atts[:pqr], tabindex: "#{@campo_tabindex += 1}", id: "pqr", name: "pqr", }
1028
+ %option{ atts[:pqr_default], id: "pqr_default", value: "", disabled: "disabled", name: "pqr", }Choose one:
1029
+ %option{ atts[:pqr_volvo], id: "pqr_volvo", value: "volvo", name: "pqr", }Volvo
1030
+ %option{ atts[:pqr_saab], id: "pqr_saab", value: "saab", name: "pqr", }Saab
1031
+ %option{ atts[:pqr_audi], id: "pqr_audi", value: "audi", name: "pqr", }Audi
1032
+ %option{ atts[:pqr_ford], id: "pqr_ford", value: "ford", name: "pqr", }Ford
1033
+ %option{ atts[:pqr_bmw], id: "pqr_bmw", value: "bmw", name: "pqr", }BMW
1034
+ %option{ atts[:pqr_ferrari], id: "pqr_ferrari", value: "ferrari", selected: "selected", name: "pqr", }Ferrari!.strip + "\n" }
1035
+ subject { Campo.output tag.with_default, :partial=>true }
905
1036
  it { should == expected }
906
1037
  end
907
1038
  end
@@ -923,7 +1054,7 @@ $.strip + "\n" }
923
1054
 
924
1055
  it { should_not be_nil }
925
1056
  it { should be_a_kind_of(Select) }
926
- specify { Campo.output( :partial, subject ).should == %Q!%select{ atts[:tea], tabindex: "\#{i += 1}", name: "tea", }\n %option{ atts[:tea_ceylon], value: "ceylon", id: "tea_ceylon", name: "tea", }Ceylon\n %option{ atts[:tea_english_breakfast], value: "english_breakfast", id: "tea_english_breakfast", name: "tea", }English breakfast\n %option{ atts[:tea_earl_grey], value: "earl_grey", id: "tea_earl_grey", name: "tea", }Earl grey\n! }
1057
+ specify { Campo.output( subject, :partial=>true ).should == %Q!%select{ atts[:tea], tabindex: "\#{@campo_tabindex += 1}", id: "tea", name: "tea", }\n %option{ atts[:tea_ceylon], id: "tea_ceylon", value: "ceylon", name: "tea", }Ceylon\n %option{ atts[:tea_english_breakfast], id: "tea_english_breakfast", value: "english_breakfast", name: "tea", }English breakfast\n %option{ atts[:tea_earl_grey], id: "tea_earl_grey", value: "earl_grey", name: "tea", }Earl grey\n! }
927
1058
  end
928
1059
  context "and a single string value" do
929
1060
  let(:opts) {
@@ -940,7 +1071,7 @@ $.strip + "\n" }
940
1071
 
941
1072
  it { should_not be_nil }
942
1073
  it { should be_a_kind_of(Select) }
943
- specify { Campo.output( :partial, subject ).should == %Q!%select{ atts[:tea], tabindex: "\#{i += 1}", name: "tea", }\n %option{ atts[:tea_ceylon], value: "ceylon", id: "tea_ceylon", name: "tea", }Ceylon\n %option{ atts[:tea_english_breakfast], value: "english_breakfast", id: "tea_english_breakfast", name: "tea", }English Breakfast\n %option{ atts[:tea_earl_grey], value: "earl_grey", id: "tea_earl_grey", name: "tea", }Earl Grey\n! }
1074
+ specify { Campo.output( subject, :partial=>true ).should == %Q!%select{ atts[:tea], tabindex: "\#{@campo_tabindex += 1}", id: "tea", name: "tea", }\n %option{ atts[:tea_ceylon], id: "tea_ceylon", value: "ceylon", name: "tea", }Ceylon\n %option{ atts[:tea_english_breakfast], id: "tea_english_breakfast", value: "english_breakfast", name: "tea", }English Breakfast\n %option{ atts[:tea_earl_grey], id: "tea_earl_grey", value: "earl_grey", name: "tea", }Earl Grey\n! }
944
1075
  end
945
1076
  context "and an array value" do
946
1077
  let(:opts) {
@@ -957,7 +1088,7 @@ $.strip + "\n" }
957
1088
 
958
1089
  it { should_not be_nil }
959
1090
  it { should be_a_kind_of(Select) }
960
- specify { Campo.output( :partial, subject ).should == %Q!%select{ atts[:tea], tabindex: "\#{i += 1}", name: "tea", }\n %option{ atts[:tea_ceylon], value: "ceylon", id: "tea_ceylon", name: "tea", }Ceylon\n %option{ atts[:tea_english_breakfast], value: "english_breakfast", selected: "selected", id: "tea_english_breakfast", name: "tea", }English Breakfast\n %option{ atts[:tea_earl_grey], value: "earl_grey", id: "tea_earl_grey", name: "tea", }Earl Grey\n! }
1091
+ specify { Campo.output( subject, :partial=>true ).should == %Q!%select{ atts[:tea], tabindex: "\#{@campo_tabindex += 1}", id: "tea", name: "tea", }\n %option{ atts[:tea_ceylon], id: "tea_ceylon", value: "ceylon", name: "tea", }Ceylon\n %option{ atts[:tea_english_breakfast], id: "tea_english_breakfast", value: "english_breakfast", selected: "selected", name: "tea", }English Breakfast\n %option{ atts[:tea_earl_grey], id: "tea_earl_grey", value: "earl_grey", name: "tea", }Earl Grey\n! }
961
1092
  end
962
1093
  end
963
1094
  end
@@ -976,7 +1107,7 @@ $.strip + "\n" }
976
1107
  context "Given a name" do
977
1108
  context "and nothing else" do
978
1109
  let(:tag) { Campo::Input.new( "abc" ) }
979
- let(:output) { %q!%input{ atts[:abc], tabindex: "#{i += 1}", type: "text", id: "abc", name: "abc", }! }
1110
+ let(:output) { %q!%input{ atts[:abc], tabindex: "#{@campo_tabindex += 1}", id: "abc", type: "text", name: "abc", }! }
980
1111
  subject { tag }
981
1112
  it { should_not be_nil }
982
1113
  it { should be_a_kind_of(Input) }
@@ -984,16 +1115,16 @@ $.strip + "\n" }
984
1115
  specify { subject.output.should == output }
985
1116
  context "Campo.output" do
986
1117
  let(:expected) { output + "\n" }
987
- subject { Campo.output :partial, tag }
1118
+ subject { Campo.output tag, :partial=>true }
988
1119
  it { should_not be_nil }
989
1120
  it { should == expected }
990
1121
  end
991
1122
  end
992
-
1123
+
993
1124
  context "and a type" do
994
1125
  context "of text" do
995
1126
  let(:tag) { Campo::Input.new( "abc", :text ) }
996
- let(:output) { %q!%input{ atts[:abc], tabindex: "#{i += 1}", type: "text", id: "abc", name: "abc", }! }
1127
+ let(:output) { %q!%input{ atts[:abc], tabindex: "#{@campo_tabindex += 1}", id: "abc", type: "text", name: "abc", }! }
997
1128
  subject { tag }
998
1129
  it { should_not be_nil }
999
1130
  it { should be_a_kind_of(Input) }
@@ -1001,14 +1132,14 @@ $.strip + "\n" }
1001
1132
  specify { subject.output.should == output }
1002
1133
  context "Campo.output" do
1003
1134
  let(:expected) { output + "\n" }
1004
- subject { Campo.output :partial, tag }
1135
+ subject { Campo.output tag, :partial=>true }
1005
1136
  it { should_not be_nil }
1006
1137
  it { should == expected }
1007
1138
  end
1008
1139
  end
1009
1140
  context "of password" do
1010
1141
  let(:tag) { Campo::Input.new( "abc", :password ) }
1011
- let(:output) { %q!%input{ atts[:abc], tabindex: "#{i += 1}", type: "password", id: "abc", name: "abc", }! }
1142
+ let(:output) { %q!%input{ atts[:abc], tabindex: "#{@campo_tabindex += 1}", id: "abc", type: "password", name: "abc", }! }
1012
1143
  subject { tag }
1013
1144
  it { should_not be_nil }
1014
1145
  it { should be_a_kind_of(Input) }
@@ -1017,7 +1148,7 @@ $.strip + "\n" }
1017
1148
 
1018
1149
  context "Campo.output" do
1019
1150
  let(:expected) { output + "\n" }
1020
- subject { Campo.output :partial, tag }
1151
+ subject { Campo.output tag, :partial=>true }
1021
1152
  it { should_not be_nil }
1022
1153
  it { should == expected }
1023
1154
  end
@@ -1025,7 +1156,7 @@ $.strip + "\n" }
1025
1156
  end
1026
1157
  context "of checkbox" do
1027
1158
  let(:tag) { Campo::Input.new( "abc", :checkbox ) }
1028
- let(:output) { %q!%input{ atts[:abc], tabindex: "#{i += 1}", type: "checkbox", id: "abc", name: "abc", }! }
1159
+ let(:output) { %q!%input{ atts[:abc], tabindex: "#{@campo_tabindex += 1}", id: "abc", type: "checkbox", name: "abc", }! }
1029
1160
  subject { tag }
1030
1161
  it { should_not be_nil }
1031
1162
  it { should be_a_kind_of(Input) }
@@ -1034,15 +1165,86 @@ $.strip + "\n" }
1034
1165
 
1035
1166
  context "Campo.output" do
1036
1167
  let(:expected) { output + "\n" }
1037
- subject { Campo.output :partial, tag }
1168
+ subject { Campo.output tag, :partial=>true }
1038
1169
  it { should_not be_nil }
1039
1170
  it { should == expected }
1040
1171
  end
1041
1172
 
1042
1173
  end
1174
+
1175
+ context "Given an input with a [] to notify it's an array" do
1176
+ let(:tag) { Campo::Input.new( "abc[]", :checkbox, value: "many" ) }
1177
+ let(:output) { %q!%input{ atts[:abc_many], tabindex: "#{@campo_tabindex += 1}", id: "abc_many", type: "checkbox", value: "many", name: "abc[]", }! }
1178
+ subject { tag }
1179
+ it { should_not be_nil }
1180
+ it { should be_a_kind_of(Input) }
1181
+ specify { subject.attributes[:type].should == "checkbox" }
1182
+ specify { subject.output.should == output }
1183
+
1184
+ context "Campo.output" do
1185
+ let(:expected) { output + "\n" }
1186
+ subject { Campo.output tag, :partial=>true }
1187
+ it { should_not be_nil }
1188
+ it { should == expected }
1189
+ end
1190
+ context "Given several inputs, some grouped, some not" do
1191
+ let(:expected) { %q!- atts = {} if atts.nil?
1192
+ - atts.default_proc = proc {|hash, key| hash[key] = {} } if atts.default_proc.nil?
1193
+ - inners = {} if inners.nil?
1194
+ - inners.default = "" if inners.default.nil?
1195
+ - @campo_tabindex ||= 0 # for tabindex
1196
+ %form{ atts[:example], id: "example", method: "POST", name: "example", role: "form", }
1197
+ %label{ for: "a", }
1198
+ A
1199
+ %input{ atts[:a], tabindex: "#{@campo_tabindex += 1}", id: "a", type: "text", name: "a", }
1200
+ %label{ for: "b", }
1201
+ B
1202
+ %input{ atts[:b], tabindex: "#{@campo_tabindex += 1}", id: "b", type: "text", name: "b", }
1203
+ %fieldset{ }
1204
+ %legend{ }Which C?
1205
+ %label{ for: "c_grouped", }
1206
+ Grouped
1207
+ %input{ atts[:c_grouped], tabindex: "#{@campo_tabindex += 1}", id: "c_grouped", type: "checkbox", value: "grouped", name: "c", }
1208
+ %label{ for: "c", }
1209
+ Also grouped
1210
+ %input{ atts[:c], tabindex: "#{@campo_tabindex += 1}", id: "c", type: "checkbox", valued: "also grouped", name: "c", }
1211
+ %fieldset{ }
1212
+ %legend{ }Which D?
1213
+ %label{ for: "d_grouped", }
1214
+ Grouped
1215
+ %input{ atts[:d_grouped], tabindex: "#{@campo_tabindex += 1}", id: "d_grouped", type: "checkbox", value: "grouped", name: "d[]", }
1216
+ %label{ for: "d_also_grouped", }
1217
+ Also grouped
1218
+ %input{ atts[:d_also_grouped], tabindex: "#{@campo_tabindex += 1}", id: "d_also_grouped", type: "checkbox", value: "also grouped", name: "d[]", }
1219
+ %input{ atts[:Submit], tabindex: "#{@campo_tabindex += 1}", id: "Submit", type: "submit", value: "Submit", }
1220
+ !
1221
+ }
1222
+ let(:form) {
1223
+ form = Campo.form "example" do
1224
+ text "a"
1225
+ text "b"
1226
+ fieldset "Which C?" do
1227
+ checkbox "c", "Grouped", value: "grouped"
1228
+ checkbox "c", "Also grouped", valued: "also grouped"
1229
+ end
1230
+ fieldset "Which D?" do
1231
+ checkbox "d[]", "Grouped", value: "grouped"
1232
+ checkbox "d[]", "Also grouped", value: "also grouped"
1233
+ end
1234
+ submit
1235
+ end
1236
+ form
1237
+ }
1238
+
1239
+ subject { Campo.output form }
1240
+ it { should_not be_nil }
1241
+ it { should == expected }
1242
+ end
1243
+ end
1244
+
1043
1245
  context "of radio" do
1044
1246
  let(:tag) { Campo::Input.new( "abc", :radio ) }
1045
- let(:output) { %q!%input{ atts[:abc], tabindex: "#{i += 1}", type: "radio", id: "abc", name: "abc", }! }
1247
+ let(:output) { %q!%input{ atts[:abc], tabindex: "#{@campo_tabindex += 1}", id: "abc", type: "radio", name: "abc", }! }
1046
1248
  subject { tag }
1047
1249
  it { should_not be_nil }
1048
1250
  it { should be_a_kind_of(Input) }
@@ -1051,7 +1253,7 @@ $.strip + "\n" }
1051
1253
 
1052
1254
  context "Campo.output" do
1053
1255
  let(:expected) { output + "\n" }
1054
- subject { Campo.output :partial, tag }
1256
+ subject { Campo.output tag, :partial=>true }
1055
1257
  it { should_not be_nil }
1056
1258
  it { should == expected }
1057
1259
  end
@@ -1063,16 +1265,16 @@ $.strip + "\n" }
1063
1265
  context "Labelling" do
1064
1266
  let(:expected) {
1065
1267
  top_bit + %q!
1066
- %form{ atts[:myform], method: "POST", id: "myform", name: "myform", }
1268
+ %form{ atts[:myform], id: "myform", method: "POST", name: "myform", role: "form", }
1067
1269
  %label{ for: "abc", }
1068
1270
  abc
1069
- %input{ atts[:abc], tabindex: "#{i += 1}", type: "text", id: "abc", name: "abc", }
1271
+ %input{ atts[:abc], tabindex: "#{@campo_tabindex += 1}", id: "abc", type: "text", name: "abc", }
1070
1272
  %label{ for: "deff", }
1071
1273
  deff
1072
- %input{ atts[:deff], tabindex: "#{i += 1}", type: "text", id: "deff", name: "deff", }
1274
+ %input{ atts[:deff], tabindex: "#{@campo_tabindex += 1}", id: "deff", type: "text", name: "deff", }
1073
1275
  %label{ for: "ghi", }
1074
1276
  ghi
1075
- %input{ atts[:ghi], tabindex: "#{i += 1}", type: "text", id: "ghi", name: "ghi", }
1277
+ %input{ atts[:ghi], tabindex: "#{@campo_tabindex += 1}", id: "ghi", type: "text", name: "ghi", }
1076
1278
 
1077
1279
  !.strip + "\n"
1078
1280
  }
@@ -1090,27 +1292,28 @@ $.strip + "\n" }
1090
1292
  context "Within a fieldset" do
1091
1293
  let(:expected) {
1092
1294
  top_bit + %q!
1093
- %form{ atts[:myform], method: "POST", id: "myform", name: "myform", }
1295
+ %form{ atts[:myform], id: "myform", method: "POST", name: "myform", role: "form", }
1094
1296
  %fieldset{ }
1095
1297
  %legend{ }Alphabetty spaghetti
1096
1298
  %label{ for: "abc", }
1097
- abc
1098
- %input{ atts[:abc], tabindex: "#{i += 1}", type: "text", id: "abc", name: "abc", }
1299
+ Abc
1300
+ %input{ atts[:abc], tabindex: "#{@campo_tabindex += 1}", id: "abc", type: "text", name: "abc", }
1099
1301
  %label{ for: "def", }
1100
- def
1101
- %input{ atts[:def], tabindex: "#{i += 1}", type: "text", id: "def", name: "def", }
1302
+ Def
1303
+ %input{ atts[:def], tabindex: "#{@campo_tabindex += 1}", id: "def", type: "text", name: "def", }
1102
1304
  %label{ for: "ghi", }
1103
- ghi
1104
- %input{ atts[:ghi], tabindex: "#{i += 1}", type: "text", id: "ghi", name: "ghi", }
1305
+ Ghi
1306
+ %input{ atts[:ghi], tabindex: "#{@campo_tabindex += 1}", id: "ghi", type: "text", name: "ghi", }
1105
1307
 
1106
1308
  !.strip + "\n"
1107
1309
  }
1108
1310
  let(:form) {
1109
- form = Campo::Form.new( "myform" )
1110
- myfieldset = form.fieldset( "Alphabetty spaghetti" ) do |f|
1111
- f << Campo::Input.new( "abc", :text ).labelled("abc")
1112
- f << Campo::Input.new( "def", :text ).labelled("def")
1113
- f << Campo::Input.new( "ghi", :text ).labelled("ghi")
1311
+ form = Campo::Form.new( "myform" ) do
1312
+ fieldset( "Alphabetty spaghetti" ) do
1313
+ text( "abc" )
1314
+ text( "def" )
1315
+ text( "ghi" )
1316
+ end
1114
1317
  end
1115
1318
  form
1116
1319
  }
@@ -1124,24 +1327,24 @@ $.strip + "\n" }
1124
1327
  describe "A form with a group of radio buttons" do
1125
1328
  let(:expected) {
1126
1329
  top_bit + %q!
1127
- %form{ atts[:myform], method: "POST", id: "myform", name: "myform", }
1330
+ %form{ atts[:myform], id: "myform", method: "POST", name: "myform", role: "form", }
1128
1331
  %fieldset{ }
1129
1332
  %legend{ }Select the colour you like most:
1130
1333
  %label{ for: "radio1_green", }
1131
1334
  green
1132
- %input{ atts[:radio1_green], tabindex: "#{i += 1}", type: "radio", id: "radio1_green", value: "green", name: "radio1", }
1335
+ %input{ atts[:radio1_green], tabindex: "#{@campo_tabindex += 1}", id: "radio1_green", type: "radio", value: "green", name: "radio1", }
1133
1336
  %label{ for: "radio1_yellow", }
1134
1337
  yellow
1135
- %input{ atts[:radio1_yellow], tabindex: "#{i += 1}", type: "radio", id: "radio1_yellow", value: "yellow", name: "radio1", }
1338
+ %input{ atts[:radio1_yellow], tabindex: "#{@campo_tabindex += 1}", id: "radio1_yellow", type: "radio", value: "yellow", name: "radio1", }
1136
1339
  %label{ for: "radio1_red", }
1137
1340
  red
1138
- %input{ atts[:radio1_red], tabindex: "#{i += 1}", type: "radio", id: "radio1_red", value: "red", name: "radio1", }
1341
+ %input{ atts[:radio1_red], tabindex: "#{@campo_tabindex += 1}", id: "radio1_red", type: "radio", value: "red", name: "radio1", }
1139
1342
  %label{ for: "radio1_blue", }
1140
1343
  blue
1141
- %input{ atts[:radio1_blue], tabindex: "#{i += 1}", type: "radio", id: "radio1_blue", value: "blue", name: "radio1", }
1344
+ %input{ atts[:radio1_blue], tabindex: "#{@campo_tabindex += 1}", id: "radio1_blue", type: "radio", value: "blue", name: "radio1", }
1142
1345
  %label{ for: "radio1_purple", }
1143
1346
  purple
1144
- %input{ atts[:radio1_purple], tabindex: "#{i += 1}", type: "radio", id: "radio1_purple", value: "purple", name: "radio1", }
1347
+ %input{ atts[:radio1_purple], tabindex: "#{@campo_tabindex += 1}", id: "radio1_purple", type: "radio", value: "purple", name: "radio1", }
1145
1348
 
1146
1349
  !.strip + "\n"
1147
1350
  }
@@ -1175,22 +1378,48 @@ $.strip + "\n" }
1175
1378
 
1176
1379
  context "and using convenience method" do
1177
1380
  let(:form) { Campo::Form.new( "myform" ) }
1178
- subject { form.textarea( "textie" ) }
1179
- it { should_not be_nil }
1180
- it { should be_a_kind_of(Textarea) }
1181
-
1182
- describe "the full output" do
1183
- let(:expected) { top_bit + %q!
1184
- %form{ atts[:myform], method: "POST", id: "myform", name: "myform", }
1185
- %textarea{ atts[:textie], tabindex: "#{i += 1}", cols: "40", rows: "10", name: "textie", }= inners[:textie] !.strip + " \n"}
1186
- let(:form){
1187
- form = Campo::Form.new( "myform" )
1188
- form.textarea( "textie" )
1189
- form
1190
- }
1191
- subject { Campo.output form }
1192
- it { should == expected }
1193
- end
1381
+ context "Without an explicit label parameter" do
1382
+ subject { form.textarea( "textie" ) }
1383
+ it { should_not be_nil }
1384
+ it { should be_a_kind_of(Label) }
1385
+ it { should contain_a_kind_of(Textarea) }
1386
+
1387
+ describe "the full output" do
1388
+ let(:expected) { top_bit + %q!
1389
+ %form{ atts[:myform], id: "myform", method: "POST", name: "myform", role: "form", }
1390
+ %label{ for: "textie", }
1391
+ Textie
1392
+ %textarea{ atts[:textie], tabindex: "#{@campo_tabindex += 1}", id: "textie", cols: "40", rows: "10", name: "textie", }= inners[:textie] !.strip + " \n"}
1393
+ let(:form){
1394
+ form = Campo::Form.new( "myform" )
1395
+ form.textarea( "textie" )
1396
+ form
1397
+ }
1398
+ subject { Campo.output form }
1399
+ it { should == expected }
1400
+ end
1401
+ end
1402
+ context "With an explicit label parameter" do
1403
+ subject { form.textarea( "textie", label: "Textie labelled" ) }
1404
+ it { should_not be_nil }
1405
+ it { should be_a_kind_of(Label) }
1406
+ it { should contain_a_kind_of(Textarea) }
1407
+
1408
+ describe "the full output" do
1409
+ let(:expected) { top_bit + %q!
1410
+ %form{ atts[:myform], id: "myform", method: "POST", name: "myform", role: "form", }
1411
+ %label{ for: "textie", }
1412
+ Textie labelled
1413
+ %textarea{ atts[:textie], tabindex: "#{@campo_tabindex += 1}", id: "textie", cols: "40", rows: "10", name: "textie", }= inners[:textie] !.strip + " \n"}
1414
+ let(:form){
1415
+ form = Campo::Form.new( "myform" )
1416
+ form.textarea( "textie", labelled: "Textie labelled")
1417
+ form
1418
+ }
1419
+ subject { Campo.output form }
1420
+ it { should == expected }
1421
+ end
1422
+ end
1194
1423
  end
1195
1424
 
1196
1425
  context "and an attribute" do
@@ -1202,12 +1431,15 @@ $.strip + "\n" }
1202
1431
  let(:form) { Campo::Form.new( "myform" ) }
1203
1432
  subject { form.textarea( "textie" ) }
1204
1433
  it { should_not be_nil }
1205
- it { should be_a_kind_of(Textarea) }
1434
+ it { should be_a_kind_of(Label) }
1435
+ it { should contain_a_kind_of(Textarea) }
1206
1436
 
1207
1437
  describe "the full output" do
1208
1438
  let(:expected) { top_bit + %q!
1209
- %form{ atts[:myform], method: "POST", id: "myform", name: "myform", }
1210
- %textarea{ atts[:textie], tabindex: "#{i += 1}", cols: "60", rows: "10", name: "textie", }= inners[:textie]
1439
+ %form{ atts[:myform], id: "myform", method: "POST", name: "myform", role: "form", }
1440
+ %label{ for: "textie", }
1441
+ Textie
1442
+ %textarea{ atts[:textie], tabindex: "#{@campo_tabindex += 1}", id: "textie", cols: "60", rows: "10", name: "textie", }= inners[:textie]
1211
1443
  !.strip + " \n"}
1212
1444
  let(:form){
1213
1445
  form = Campo::Form.new( "myform" )