twilson63-sinatra-formhelpers 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ sinatra-formhelpers - Suite of form helpers for sinatra
2
+ Copyright 2009 Tom Wilson
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
File without changes
data/README.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ = sinatra-formhelpers
2
+
3
+ Description goes here.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 twilson63. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,68 @@
1
+ require 'rake/clean'
2
+ require 'rake/testtask'
3
+ require 'fileutils'
4
+
5
+ require 'rubygems'
6
+ require 'rake'
7
+
8
+ begin
9
+ require 'jeweler'
10
+ Jeweler::Tasks.new do |gem|
11
+ gem.name = "sinatra-formhelpers"
12
+ gem.summary = %Q{use basic form helpers for generic form management}
13
+ gem.email = "tom@jackrussellsoftware.com"
14
+ gem.homepage = "http://github.com/twilson63/sinatra-formhelpers"
15
+ gem.authors = ["twilson63"]
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ gem.add_runtime_dependency 'sinatra', ['>= 0.9.2.0']
18
+ gem.add_runtime_dependency 'activesupport', ['>= 2.3.2.0']
19
+
20
+
21
+ end
22
+
23
+ rescue LoadError
24
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
25
+ end
26
+
27
+ task :spec => :test
28
+
29
+ # SPECS ===============================================================
30
+
31
+ Rake::TestTask.new(:test) do |t|
32
+ t.test_files = FileList['test/*_test.rb']
33
+ t.ruby_opts = ['-rubygems'] if defined? Gem
34
+ end
35
+
36
+ begin
37
+ require 'rcov/rcovtask'
38
+ Rcov::RcovTask.new do |test|
39
+ test.libs << 'test'
40
+ test.pattern = 'test/**/*_test.rb'
41
+ test.verbose = true
42
+ end
43
+ rescue LoadError
44
+ task :rcov do
45
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
46
+ end
47
+ end
48
+
49
+
50
+ task :default => :test
51
+
52
+ require 'rake/rdoctask'
53
+ Rake::RDocTask.new do |rdoc|
54
+ if File.exist?('VERSION.yml')
55
+ config = YAML.load(File.read('VERSION.yml'))
56
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
57
+ else
58
+ version = ""
59
+ end
60
+
61
+ rdoc.rdoc_dir = 'rdoc'
62
+ rdoc.title = "test-gem #{version}"
63
+ rdoc.rdoc_files.include('README*')
64
+ rdoc.rdoc_files.include('lib/**/*.rb')
65
+ end
66
+
67
+
68
+
data/Rakefile.backup ADDED
@@ -0,0 +1,13 @@
1
+ require 'rake/clean'
2
+ require 'rake/testtask'
3
+ require 'fileutils'
4
+
5
+ task :default => :test
6
+ task :spec => :test
7
+
8
+ # SPECS ===============================================================
9
+
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.test_files = FileList['test/*_test.rb']
12
+ t.ruby_opts = ['-rubygems'] if defined? Gem
13
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :patch: 0
@@ -0,0 +1,98 @@
1
+ require 'sinatra/base'
2
+ require 'activesupport'
3
+
4
+ module Sinatra
5
+ module FormHelpers
6
+ # FormHelpers are a suite of helper methods
7
+ # built to make building forms in Sinatra
8
+ # a breeze.
9
+ #
10
+ # link "jackhq", "http://www.jackhq.com"
11
+ #
12
+ # label :person, :first_name
13
+ # text :person, :first_name
14
+ #
15
+ # area :person, :notes
16
+ #
17
+ # etc.
18
+
19
+ def link(content, href, options={})
20
+ tag :a, content, options.merge(:href => href)
21
+ end
22
+
23
+ def label(obj,field, options={})
24
+ tag :label, field.to_s.titleize, options.merge(:for => "#{obj}_#{field}")
25
+ end
26
+
27
+ def text(obj, field, options={})
28
+ content = @params[obj] ? @params[obj][field.to_s] : ""
29
+ single_tag :input, options.merge(:type => "text", :id => "#{obj}_#{field}", :name => "#{obj}[#{field}]", :value => content)
30
+ end
31
+
32
+ def area(obj, field, options={})
33
+ content = @params[obj] ? @params[obj][field.to_s] : ""
34
+ tag :textarea, content, options.merge(:id => "#{obj}_#{field}", :name => "#{obj}[#{field}]")
35
+ end
36
+
37
+ def image(src, options={})
38
+ single_tag :img, options.merge(:src => src)
39
+ end
40
+
41
+ def submit(obj, value, options={})
42
+ single_tag :input, options.merge(:type => "submit", :value => value)
43
+ end
44
+
45
+ def checkbox(obj, field, options={})
46
+ single_tag :input, options.merge(:type => "checkbox", :id => "#{obj}_#{field}", :name => "#{obj}[#{field}]")
47
+
48
+ end
49
+
50
+ def radio(obj, field, value, options={})
51
+ content = @params[obj] && @params[obj][field.to_s] == value ? "true" : ""
52
+ tag :input, value, options.merge(:type => "radio", :id => "#{obj}_#{field}", :name => "#{obj}[#{field}]", :value => value, :checked => content)
53
+
54
+ end
55
+
56
+ def select(obj, field, items, options={})
57
+ content = ""
58
+ items.each do |i|
59
+ content = content + tag(:option, i, { :value => i })
60
+ end
61
+ tag :select, content, options
62
+ end
63
+
64
+
65
+ # standard open and close tags
66
+ # EX : tag :h1, "shizam", :title => "shizam"
67
+ # => <h1 title="shizam">shizam</h1>
68
+ def tag(name,content,options={})
69
+ with_opts = "<#{name.to_s} #{options.to_html_attrs}>#{content}</#{name}>"
70
+ no_opts = "<#{name.to_s}>#{content}</#{name}>"
71
+ options.blank? ? no_opts : with_opts
72
+ end
73
+
74
+ # standard single closing tags
75
+ # single_tag :img, :src => "images/google.jpg"
76
+ # => <img src="images/google.jpg" />
77
+ def single_tag(name,options={})
78
+ options ||= options.stringify_keys
79
+ "<#{name.to_s} #{options.to_html_attrs} />"
80
+ end
81
+
82
+ end
83
+
84
+ helpers FormHelpers
85
+ end
86
+
87
+
88
+ class Hash
89
+
90
+ def to_html_attrs
91
+ html_attrs = ""
92
+ self.stringify_keys.each do |key, val|
93
+ html_attrs << "#{key}='#{val}' "
94
+ end
95
+ html_attrs.chop
96
+ end
97
+
98
+ end
@@ -0,0 +1,57 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{sinatra-formhelpers}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["twilson63"]
9
+ s.date = %q{2009-06-14}
10
+ s.email = %q{tom@jackrussellsoftware.com}
11
+ s.extra_rdoc_files = [
12
+ "LICENSE",
13
+ "README",
14
+ "README.rdoc"
15
+ ]
16
+ s.files = [
17
+ ".document",
18
+ ".gitignore",
19
+ "LICENSE",
20
+ "README",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "Rakefile.backup",
24
+ "VERSION.yml",
25
+ "lib/sinatra/formhelpers.rb",
26
+ "sinatra-formhelpers.gemspec",
27
+ "test/contest.rb",
28
+ "test/formhelpers_test.rb",
29
+ "test/helper.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/twilson63/sinatra-formhelpers}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.4}
35
+ s.summary = %q{use basic form helpers for generic form management}
36
+ s.test_files = [
37
+ "test/helper.rb",
38
+ "test/contest.rb",
39
+ "test/formhelpers_test.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
+ s.add_runtime_dependency(%q<sinatra>, [">= 0.9.2.0"])
48
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3.2.0"])
49
+ else
50
+ s.add_dependency(%q<sinatra>, [">= 0.9.2.0"])
51
+ s.add_dependency(%q<activesupport>, [">= 2.3.2.0"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<sinatra>, [">= 0.9.2.0"])
55
+ s.add_dependency(%q<activesupport>, [">= 2.3.2.0"])
56
+ end
57
+ end
data/test/contest.rb ADDED
@@ -0,0 +1,64 @@
1
+ require "test/unit"
2
+
3
+ # Test::Unit loads a default test if the suite is empty, and the only
4
+ # purpose of that test is to fail. As having empty contexts is a common
5
+ # practice, we decided to overwrite TestSuite#empty? in order to
6
+ # allow them. Having a failure when no tests have been defined seems
7
+ # counter-intuitive.
8
+ class Test::Unit::TestSuite
9
+ unless method_defined?(:empty?)
10
+ def empty?
11
+ false
12
+ end
13
+ end
14
+ end
15
+
16
+ # We added setup, test and context as class methods, and the instance
17
+ # method setup now iterates on the setup blocks. Note that all setup
18
+ # blocks must be defined with the block syntax. Adding a setup instance
19
+ # method defeats the purpose of this library.
20
+ class Test::Unit::TestCase
21
+ def self.setup(&block)
22
+ setup_blocks << block
23
+ end
24
+
25
+ def setup
26
+ self.class.setup_blocks.each do |block|
27
+ instance_eval(&block)
28
+ end
29
+ end
30
+
31
+ def self.context(name, &block)
32
+ subclass = Class.new(self.superclass)
33
+ subclass.setup_blocks.unshift(*setup_blocks)
34
+ subclass.class_eval(&block)
35
+ const_set(context_name(name), subclass)
36
+ end
37
+
38
+ def self.test(name, &block)
39
+ define_method(test_name(name), &block)
40
+ end
41
+
42
+ class << self
43
+ alias_method :should, :test
44
+ alias_method :describe, :context
45
+ end
46
+
47
+ private
48
+
49
+ def self.setup_blocks
50
+ @setup_blocks ||= []
51
+ end
52
+
53
+ def self.context_name(name)
54
+ "Test#{sanitize_name(name).gsub(/(^| )(\w)/) { $2.upcase }}".to_sym
55
+ end
56
+
57
+ def self.test_name(name)
58
+ "test_#{sanitize_name(name).gsub(/\s+/,'_')}".to_sym
59
+ end
60
+
61
+ def self.sanitize_name(name)
62
+ name.gsub(/\W+/, ' ').strip
63
+ end
64
+ end
@@ -0,0 +1,195 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+ require 'haml'
3
+ require 'sinatra/formhelpers'
4
+
5
+ class FormHelpersTest < Test::Unit::TestCase
6
+
7
+ def test_default
8
+ assert true
9
+ end
10
+
11
+ describe 'formhelpers link' do
12
+ setup do
13
+ mock_app {
14
+ helpers Sinatra::FormHelpers
15
+ get '/' do
16
+ haml "= link 'google', 'http://www.google.com'"
17
+ end
18
+ }
19
+ end
20
+
21
+ it 'renders an achor tag' do
22
+ get '/'
23
+ assert_equal "<a href='http://www.google.com'>google</a>\n", body
24
+ end
25
+ end
26
+
27
+
28
+ describe 'formhelpers label' do
29
+ setup do
30
+ mock_app {
31
+ helpers Sinatra::FormHelpers
32
+ get '/' do
33
+ haml "= label :person, :first_name"
34
+ end
35
+ }
36
+ end
37
+
38
+ it 'renders an label tag' do
39
+ get '/'
40
+ assert_equal "<label for='person_first_name'>First Name</label>\n", body
41
+ end
42
+ end
43
+
44
+ describe 'formhelpers text without @params' do
45
+ setup do
46
+ mock_app {
47
+ helpers Sinatra::FormHelpers
48
+ get '/' do
49
+ haml "= text :person, :first_name"
50
+ end
51
+ }
52
+ end
53
+
54
+ it 'renders an input tag type text without @params' do
55
+ get '/'
56
+ assert_equal "<input name='person[first_name]' id='person_first_name' value='' type='text' />\n", body
57
+ end
58
+ end
59
+
60
+ describe 'formhelpers text with @params' do
61
+ setup do
62
+ mock_app {
63
+ helpers Sinatra::FormHelpers
64
+ get '/' do
65
+ @params = { :person => {"first_name" => "Tom"}}
66
+ haml "= text :person, :first_name"
67
+ end
68
+ }
69
+ end
70
+
71
+ it 'renders an input tag type text with @params' do
72
+ get '/'
73
+ assert_equal "<input name='person[first_name]' id='person_first_name' value='Tom' type='text' />\n", body
74
+ end
75
+ end
76
+
77
+ describe 'formhelpers area without @params' do
78
+ setup do
79
+ mock_app {
80
+ helpers Sinatra::FormHelpers
81
+ get '/' do
82
+ haml "= area :person, :notes"
83
+ end
84
+ }
85
+ end
86
+
87
+ it 'renders an textarea tag type text without @params' do
88
+ get '/'
89
+ assert_equal "<textarea name='person[notes]' id='person_notes'></textarea>\n", body
90
+ end
91
+ end
92
+
93
+ describe 'formhelpers textarea with @params' do
94
+ setup do
95
+ mock_app {
96
+ helpers Sinatra::FormHelpers
97
+ get '/' do
98
+ @params = { :person => {"notes" => "This is a note"}}
99
+ haml "= area :person, :notes"
100
+ end
101
+ }
102
+ end
103
+
104
+ it 'renders a textarea tag with @params' do
105
+ get '/'
106
+ assert_equal "<textarea name='person[notes]' id='person_notes'>This is a note</textarea>\n", body
107
+ end
108
+ end
109
+
110
+
111
+ describe 'formhelpers image' do
112
+ setup do
113
+ mock_app {
114
+ helpers Sinatra::FormHelpers
115
+ get '/' do
116
+ haml "= image '/images/hello.png'"
117
+ end
118
+ }
119
+ end
120
+
121
+ it 'renders a textarea tag with @params' do
122
+ get '/'
123
+ assert_equal "<img src='/images/hello.png' />\n", body
124
+ end
125
+ end
126
+
127
+ describe 'formhelpers submit' do
128
+ setup do
129
+ mock_app {
130
+ helpers Sinatra::FormHelpers
131
+ get '/' do
132
+ haml "= submit :person, 'Create'"
133
+ end
134
+ }
135
+ end
136
+
137
+ it 'renders an input tag with a submit type' do
138
+ get '/'
139
+ assert_equal "<input value='Create' type='submit' />\n", body
140
+ end
141
+ end
142
+
143
+
144
+ describe 'formhelpers checkbox' do
145
+ setup do
146
+ mock_app {
147
+ helpers Sinatra::FormHelpers
148
+ get '/' do
149
+ haml "= checkbox :person, :active"
150
+ end
151
+ }
152
+ end
153
+
154
+ it 'renders an input tag with a checkbox type' do
155
+ get '/'
156
+ assert_equal "<input name='person[active]' id='person_active' type='checkbox' />\n", body
157
+ end
158
+ end
159
+
160
+
161
+ describe 'formhelpers radio' do
162
+ setup do
163
+ mock_app {
164
+ helpers Sinatra::FormHelpers
165
+ get '/' do
166
+ haml "= radio :person, :gender, 'Male'"
167
+ end
168
+ }
169
+ end
170
+
171
+ it 'renders an input tag with a radio type' do
172
+ get '/'
173
+ assert_equal "<input checked='' name='person[gender]' id='person_gender' value='Male' type='radio'>Male</input>\n", body
174
+ end
175
+ end
176
+
177
+ describe 'formhelpers select' do
178
+ setup do
179
+ mock_app {
180
+ helpers Sinatra::FormHelpers
181
+ get '/' do
182
+ haml "= select :person, :relationship, ['Friend','CoWorker','Lead']"
183
+ end
184
+ }
185
+ end
186
+
187
+ it 'renders an select tag' do
188
+ get '/'
189
+ assert_equal "<select><option value='Friend'>Friend</option><option value='CoWorker'>CoWorker</option><option value='Lead'>Lead</option></select>\n", body
190
+ end
191
+ end
192
+
193
+
194
+ end
195
+
data/test/helper.rb ADDED
@@ -0,0 +1,76 @@
1
+ ENV['RACK_ENV'] = 'test'
2
+
3
+ begin
4
+ require 'rack'
5
+ rescue LoadError
6
+ require 'rubygems'
7
+ require 'rack'
8
+ end
9
+
10
+ testdir = File.dirname(__FILE__)
11
+ $LOAD_PATH.unshift testdir unless $LOAD_PATH.include?(testdir)
12
+
13
+ libdir = File.dirname(File.dirname(__FILE__)) + '/lib'
14
+ $LOAD_PATH.unshift libdir unless $LOAD_PATH.include?(libdir)
15
+
16
+ require 'contest'
17
+ require 'rack/test'
18
+ require 'sinatra/base'
19
+
20
+ class Sinatra::Base
21
+ # Allow assertions in request context
22
+ include Test::Unit::Assertions
23
+ end
24
+
25
+ Sinatra::Base.set :environment, :test
26
+
27
+ class Test::Unit::TestCase
28
+ include Rack::Test::Methods
29
+
30
+ class << self
31
+ alias_method :it, :test
32
+ end
33
+
34
+ alias_method :response, :last_response
35
+
36
+ setup do
37
+ Sinatra::Base.set :environment, :test
38
+ end
39
+
40
+ # Sets up a Sinatra::Base subclass defined with the block
41
+ # given. Used in setup or individual spec methods to establish
42
+ # the application.
43
+ def mock_app(base=Sinatra::Base, &block)
44
+ @app = Sinatra.new(base, &block)
45
+ end
46
+
47
+ def app
48
+ Rack::Lint.new(@app)
49
+ end
50
+
51
+ def body
52
+ response.body.to_s
53
+ end
54
+
55
+ # Delegate other missing methods to response.
56
+ def method_missing(name, *args, &block)
57
+ if response && response.respond_to?(name)
58
+ response.send(name, *args, &block)
59
+ else
60
+ super
61
+ end
62
+ end
63
+
64
+ # Also check response since we delegate there.
65
+ def respond_to?(symbol, include_private=false)
66
+ super || (response && response.respond_to?(symbol, include_private))
67
+ end
68
+
69
+ # Do not output warnings for the duration of the block.
70
+ def silence_warnings
71
+ $VERBOSE, v = nil, $VERBOSE
72
+ yield
73
+ ensure
74
+ $VERBOSE = v
75
+ end
76
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twilson63-sinatra-formhelpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - twilson63
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-14 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sinatra
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.9.2.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.3.2.0
34
+ version:
35
+ description:
36
+ email: tom@jackrussellsoftware.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README
44
+ - README.rdoc
45
+ files:
46
+ - .document
47
+ - .gitignore
48
+ - LICENSE
49
+ - README
50
+ - README.rdoc
51
+ - Rakefile
52
+ - Rakefile.backup
53
+ - VERSION.yml
54
+ - lib/sinatra/formhelpers.rb
55
+ - sinatra-formhelpers.gemspec
56
+ - test/contest.rb
57
+ - test/formhelpers_test.rb
58
+ - test/helper.rb
59
+ has_rdoc: false
60
+ homepage: http://github.com/twilson63/sinatra-formhelpers
61
+ post_install_message:
62
+ rdoc_options:
63
+ - --charset=UTF-8
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ version:
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.2.0
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: use basic form helpers for generic form management
85
+ test_files:
86
+ - test/helper.rb
87
+ - test/contest.rb
88
+ - test/formhelpers_test.rb