ms_form 0.2.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 +5 -0
- data/.gitignore +32 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/lib/ms_form.rb +11 -0
- data/lib/ms_form/engine.rb +7 -0
- data/lib/ms_form/form_builder.rb +136 -0
- data/lib/ms_form/form_helper.rb +87 -0
- data/lib/tbl_form/engine.rb +7 -0
- data/lib/tbl_form/form_builder.rb +130 -0
- data/lib/tbl_form/form_helper.rb +121 -0
- data/ms_form.gemspec +60 -0
- data/test/helper.rb +10 -0
- data/test/test_ms_form.rb +7 -0
- metadata +97 -0
data/.document
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
## MAC OS
|
2
|
+
.DS_Store
|
3
|
+
|
4
|
+
## TEXTMATE
|
5
|
+
*.tmproj
|
6
|
+
tmtags
|
7
|
+
|
8
|
+
## EMACS
|
9
|
+
*~
|
10
|
+
\#*
|
11
|
+
.\#*
|
12
|
+
|
13
|
+
## VIM
|
14
|
+
*.swp
|
15
|
+
|
16
|
+
## KOMODO
|
17
|
+
.komodotools
|
18
|
+
*.komodoproject
|
19
|
+
*.kpf
|
20
|
+
|
21
|
+
## RAILS
|
22
|
+
.bundle
|
23
|
+
db/*.sqlite3
|
24
|
+
log/*.log
|
25
|
+
tmp/**/*
|
26
|
+
|
27
|
+
## PROJECT::GENERAL
|
28
|
+
coverage
|
29
|
+
rdoc
|
30
|
+
pkg
|
31
|
+
|
32
|
+
## PROJECT::SPECIFIC
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 wmerrell
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= ms_form
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 wmerrell. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "ms_form"
|
8
|
+
gem.summary = %Q{Fancy Form Handling.}
|
9
|
+
gem.description = %Q{Wraps the standard form handling functions with additional formatting and features.}
|
10
|
+
gem.email = "will@morelandsolutions.com"
|
11
|
+
gem.homepage = "http://github.com/wmerrell/ms_form"
|
12
|
+
gem.authors = ["wmerrell"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "ms_form #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.0
|
data/lib/ms_form.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'action_view'
|
2
|
+
require 'ms_form/engine' if defined?(Rails) && Rails::VERSION::MAJOR == 3
|
3
|
+
require 'ms_form/form_helper'
|
4
|
+
module MsForm
|
5
|
+
autoload :FormBuilder, 'ms_form/form_builder'
|
6
|
+
end
|
7
|
+
require 'tbl_form/engine' if defined?(Rails) && Rails::VERSION::MAJOR == 3
|
8
|
+
require 'tbl_form/form_helper'
|
9
|
+
module TblForm
|
10
|
+
autoload :FormBuilder, 'tbl_form/form_builder'
|
11
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
module MsForm
|
2
|
+
class FormBuilder < ActionView::Helpers::FormBuilder
|
3
|
+
|
4
|
+
include ActionView::Helpers::TagHelper
|
5
|
+
|
6
|
+
# Creates a button:
|
7
|
+
#
|
8
|
+
# form_for @user do |f|
|
9
|
+
# f.button :submit
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# It just acts as a proxy to method name given.
|
13
|
+
#
|
14
|
+
def button(type, *args, &block)
|
15
|
+
if respond_to?(:"#{type}_button")
|
16
|
+
send(:"#{type}_button", *args, &block)
|
17
|
+
else
|
18
|
+
send(type, *args, &block)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Generates a label
|
23
|
+
#
|
24
|
+
# If +options+ includes :for,
|
25
|
+
# that is used as the :for of the label. Otherwise,
|
26
|
+
# "#{this form's object name}_#{method}" is used.
|
27
|
+
#
|
28
|
+
# If +options+ includes :label,
|
29
|
+
# that value is used for the text of the label. Otherwise,
|
30
|
+
# "#{method titleized}: " is used.
|
31
|
+
def label method, options = {}
|
32
|
+
text = options.delete(:label) || "#{method.to_s.titleize}: "
|
33
|
+
hint = options.delete(:hint) || ""
|
34
|
+
text = text + " <span class=\"ms_hint\">#{hint}</span>".html_safe unless hint.blank?
|
35
|
+
if options[:for]
|
36
|
+
"<label class=\"ms_label\" for='#{options.delete(:for)}'>#{text}</label>".html_safe
|
37
|
+
else
|
38
|
+
#need to use InstanceTag to build the correct ID for :for
|
39
|
+
ActionView::Helpers::InstanceTag.new(@object_name, method, self, @object).to_label_tag(text.html_safe, {:class=>"ms_label"}).html_safe
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def submit(value = "Submit", options = {})
|
44
|
+
text = options.delete(:label) || value
|
45
|
+
tag_str = tag( :input, { "type" => "submit", "name" => "commit", "value" => text }.update(options.stringify_keys) )
|
46
|
+
output = "<br /><div class=\"button_bar\">#{ tag_str }</div>".html_safe
|
47
|
+
end
|
48
|
+
|
49
|
+
def value(method, options = {})
|
50
|
+
@object[method.to_s]
|
51
|
+
end
|
52
|
+
|
53
|
+
def display(method, options = {})
|
54
|
+
options.to_options!
|
55
|
+
front(method, options) + ("<span class=\"ms_field_content\">#{method.is_a?(String) ? method.to_s : @object[method.to_s]}</span>").html_safe + back(method, options)
|
56
|
+
end
|
57
|
+
|
58
|
+
def select method, choices, options = {}, html_options = {}
|
59
|
+
options.to_options!
|
60
|
+
front(method, options) + super + back(method, options)
|
61
|
+
end
|
62
|
+
|
63
|
+
def text_field method, options = {}
|
64
|
+
options.to_options!
|
65
|
+
naked = options.delete(:naked) || false
|
66
|
+
if naked
|
67
|
+
super
|
68
|
+
else
|
69
|
+
front(method, options) + super + back(method, options)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def password_field method, options = {}
|
74
|
+
options.to_options!
|
75
|
+
front(method, options) + super + back(method, options)
|
76
|
+
end
|
77
|
+
|
78
|
+
def file_field method, options = {}
|
79
|
+
options.to_options!
|
80
|
+
front(method, options) + super + back(method, options)
|
81
|
+
end
|
82
|
+
|
83
|
+
def text_area method, options = {}
|
84
|
+
options.to_options!
|
85
|
+
front(method, options) + super + back(method, options)
|
86
|
+
end
|
87
|
+
|
88
|
+
def check_box method, options = {}
|
89
|
+
options.to_options!
|
90
|
+
front(method, options) + super + back(method, options)
|
91
|
+
end
|
92
|
+
|
93
|
+
def radio_button method, tag_value, options = {}
|
94
|
+
options.to_options!
|
95
|
+
fieldstyle = options.delete(:fieldstyle) || ""
|
96
|
+
fieldstyle += " width: #{options.delete(:fieldwidth) || "100%"};"
|
97
|
+
fieldextra = options.delete(:fieldextra) || ""
|
98
|
+
"<div class=\"ms_field\" style=\"#{fieldstyle}\">".html_safe + super + " <span class=\"ms_field_label\">#{fieldextra}#{label(method.to_s+"_"+tag_value.to_s, options)}</span></div>".html_safe
|
99
|
+
end
|
100
|
+
|
101
|
+
def calendar_field method, options = {}
|
102
|
+
options.to_options!
|
103
|
+
expired = options.delete(:expired) || false
|
104
|
+
if not expired; options.merge!(:class => 'calendar'); else; options.merge!(:disabled => true); end
|
105
|
+
text_field method, options
|
106
|
+
end
|
107
|
+
|
108
|
+
def date_select method, options = {}
|
109
|
+
options.to_options!
|
110
|
+
front(method, options) + super + back(method, options)
|
111
|
+
end
|
112
|
+
|
113
|
+
def select method, choices, options = {}, html_options = {}
|
114
|
+
options.to_options!
|
115
|
+
html_options.to_options!
|
116
|
+
front(method, options) + super + back(method, options)
|
117
|
+
end
|
118
|
+
|
119
|
+
#def field_for method, options = {}
|
120
|
+
# options.to_options!
|
121
|
+
# label = options.delete(:label) || "#{text.titleize}: "
|
122
|
+
# "<div class=\"ms_field\"><span class=\"ms_label\">#{label}</span><br />#{text}</div>\n"
|
123
|
+
#end
|
124
|
+
|
125
|
+
def front method = '', options = {}
|
126
|
+
fieldstyle = options.delete(:fieldstyle) || ""
|
127
|
+
fieldstyle += " width: #{options.delete(:fieldwidth) || "100%"};"
|
128
|
+
fieldextra = options.delete(:fieldextra) || ""
|
129
|
+
"<div class=\"ms_field\", style=\"#{fieldstyle}\"><div class=\"ms_field_label\">#{fieldextra}#{label(method, options)}</div>".html_safe
|
130
|
+
end
|
131
|
+
|
132
|
+
def back method = '', options = {}
|
133
|
+
"</div>".html_safe
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module MsForm
|
2
|
+
module ActionViewExtensions
|
3
|
+
# This modules create simple form wrappers around default form_for,
|
4
|
+
# fields_for and remote_form_for.
|
5
|
+
#
|
6
|
+
# Example:
|
7
|
+
#
|
8
|
+
# ms_form_for @user do |f|
|
9
|
+
# f.input :name, :hint => 'My hint'
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
module FormHelper
|
13
|
+
# based on what is done in formtastic
|
14
|
+
# http://github.com/justinfrench/formtastic/blob/master/lib/formtastic.rb#L1706
|
15
|
+
@@default_field_error_proc = nil
|
16
|
+
|
17
|
+
# Override the default ActiveRecordHelper behaviour of wrapping the input.
|
18
|
+
# This gets taken care of semantically by adding an error class to the wrapper tag
|
19
|
+
# containing the input.
|
20
|
+
#
|
21
|
+
FIELD_ERROR_PROC = proc do |html_tag, instance_tag|
|
22
|
+
html_tag
|
23
|
+
end
|
24
|
+
|
25
|
+
def with_custom_field_error_proc(&block)
|
26
|
+
@@default_field_error_proc = ::ActionView::Base.field_error_proc
|
27
|
+
::ActionView::Base.field_error_proc = FIELD_ERROR_PROC
|
28
|
+
result = yield
|
29
|
+
::ActionView::Base.field_error_proc = @@default_field_error_proc
|
30
|
+
result
|
31
|
+
end
|
32
|
+
|
33
|
+
[:form_tag, :form_for, :fields_for, :remote_form_for].each do |helper|
|
34
|
+
class_eval <<-METHOD, __FILE__, __LINE__
|
35
|
+
def ms_#{helper}(record_or_name_or_array, *args, &block)
|
36
|
+
options = args.extract_options!
|
37
|
+
options[:builder] = MsForm::FormBuilder
|
38
|
+
css_class = case record_or_name_or_array
|
39
|
+
when String, Symbol then record_or_name_or_array.to_s
|
40
|
+
when Array then dom_class(record_or_name_or_array.last)
|
41
|
+
else dom_class(record_or_name_or_array)
|
42
|
+
end
|
43
|
+
options[:html] ||= {}
|
44
|
+
options[:html][:class] = "ms_form \#{css_class} \#{options[:html][:class]}".strip
|
45
|
+
|
46
|
+
with_custom_field_error_proc do
|
47
|
+
#{helper}(record_or_name_or_array, *(args << options), &block)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
METHOD
|
51
|
+
end
|
52
|
+
|
53
|
+
def ms_display_form(legend = nil, *args, &block)
|
54
|
+
options = args.extract_options!
|
55
|
+
content = capture(&block)
|
56
|
+
|
57
|
+
"<div class=\"ms_display_form\">".html_safe + content + "</div>".html_safe
|
58
|
+
end
|
59
|
+
|
60
|
+
def ms_display(label = '', value = '', options = {})
|
61
|
+
fieldstyle = options.delete(:fieldstyle) || ""
|
62
|
+
fieldstyle += " width: #{options.delete(:fieldwidth) || "100%"};"
|
63
|
+
fieldextra = options.delete(:fieldextra) || ""
|
64
|
+
"<div class=\"ms_field\", style=\"#{fieldstyle}\"><div class=\"ms_field_label\"><span class=\"ms_label\">#{fieldextra}#{label}</span></div><span class=\"ms_field_content\">#{value}</span></div>".html_safe
|
65
|
+
end
|
66
|
+
|
67
|
+
def ms_wrap(label = '', *args, &block)
|
68
|
+
options = args.extract_options!
|
69
|
+
content = capture(&block)
|
70
|
+
|
71
|
+
fieldstyle = options.delete(:fieldstyle) || ""
|
72
|
+
fieldstyle += " width: #{options.delete(:fieldwidth) || "100%"};"
|
73
|
+
fieldextra = options.delete(:fieldextra) || ""
|
74
|
+
|
75
|
+
forvalue = options.delete(:for) || ""
|
76
|
+
text = options.delete(:label) || "#{label.to_s.titleize}"
|
77
|
+
hint = options.delete(:hint) || ""
|
78
|
+
text = text + "<span class=\"ms_hint\">#{hint}</span>" unless hint.blank?
|
79
|
+
|
80
|
+
"<div class=\"ms_field\", style=\"#{fieldstyle}\"><div class=\"ms_field_label\">#{fieldextra}<label class=\"ms_label\" for=\"#{forvalue}\">#{text}</label></div>".html_safe + content + "</div>".html_safe
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
ActionView::Base.send :include, MsForm::ActionViewExtensions::FormHelper
|
@@ -0,0 +1,130 @@
|
|
1
|
+
module TblForm
|
2
|
+
class FormBuilder < ActionView::Helpers::FormBuilder
|
3
|
+
|
4
|
+
include ActionView::Helpers::TagHelper
|
5
|
+
|
6
|
+
# Creates a button:
|
7
|
+
#
|
8
|
+
# form_for @user do |f|
|
9
|
+
# f.button :submit
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# It just acts as a proxy to method name given.
|
13
|
+
#
|
14
|
+
def button(type, *args, &block)
|
15
|
+
if respond_to?(:"#{type}_button")
|
16
|
+
send(:"#{type}_button", *args, &block)
|
17
|
+
else
|
18
|
+
send(type, *args, &block)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Generates a label
|
23
|
+
#
|
24
|
+
# If +options+ includes :for,
|
25
|
+
# that is used as the :for of the label. Otherwise,
|
26
|
+
# "#{this form's object name}_#{method}" is used.
|
27
|
+
#
|
28
|
+
# If +options+ includes :label,
|
29
|
+
# that value is used for the text of the label. Otherwise,
|
30
|
+
# "#{method titleized}: " is used.
|
31
|
+
def label method, options = {}
|
32
|
+
text = options.delete(:label) || "#{method.to_s.titleize}: "
|
33
|
+
hint = options.delete(:hint) || ""
|
34
|
+
text = text + " <span class=\"ms_hint\">#{hint}</span>" unless hint.blank?
|
35
|
+
if options[:for]
|
36
|
+
"<label class=\"ms_label\" for='#{options.delete(:for)}'>#{text}</label>"
|
37
|
+
else
|
38
|
+
#need to use InstanceTag to build the correct ID for :for
|
39
|
+
ActionView::Helpers::InstanceTag.new(@object_name, method, self, @object).to_label_tag(text, {:class=>"ms_label"})
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def submit(value = "Submit", options = {})
|
44
|
+
text = options.delete(:label) || value
|
45
|
+
tag_str = tag( :input, { "type" => "submit", "name" => "commit", "value" => text }.update(options.stringify_keys) )
|
46
|
+
output = "<br /><div class=\"button_bar\">#{ tag_str }</div>".html_safe
|
47
|
+
end
|
48
|
+
|
49
|
+
def value(method, options = {})
|
50
|
+
@object[method.to_s]
|
51
|
+
end
|
52
|
+
|
53
|
+
def display(method, options = {})
|
54
|
+
options.to_options!
|
55
|
+
front(method, options) + ("<span class=\"ms_field_content\">#{method.is_a?(String) ? method.to_s : @object[method.to_s]}</span>").html_safe + back(method, options)
|
56
|
+
end
|
57
|
+
|
58
|
+
def select method, choices, options = {}, html_options = {}
|
59
|
+
options.to_options!
|
60
|
+
front(method, options) + super + back(method, options)
|
61
|
+
end
|
62
|
+
|
63
|
+
def text_field method, options = {}
|
64
|
+
options.to_options!
|
65
|
+
naked = options.delete(:naked) || false
|
66
|
+
if naked
|
67
|
+
super
|
68
|
+
else
|
69
|
+
front(method, options) + super + back(method, options)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def password_field method, options = {}
|
74
|
+
options.to_options!
|
75
|
+
front(method, options) + super + back(method, options)
|
76
|
+
end
|
77
|
+
|
78
|
+
def file_field method, options = {}
|
79
|
+
options.to_options!
|
80
|
+
front(method, options) + super + back(method, options)
|
81
|
+
end
|
82
|
+
|
83
|
+
def text_area method, options = {}
|
84
|
+
options.to_options!
|
85
|
+
front(method, options) + super + back(method, options)
|
86
|
+
end
|
87
|
+
|
88
|
+
def check_box method, options = {}
|
89
|
+
options.to_options!
|
90
|
+
front(method, options) + super + back(method, options)
|
91
|
+
end
|
92
|
+
|
93
|
+
def radio_button method, tag_value, options = {}
|
94
|
+
options.to_options!
|
95
|
+
fieldstyle = options.delete(:fieldstyle) || ""
|
96
|
+
fieldstyle += " width: #{options.delete(:fieldwidth) || "100%"};"
|
97
|
+
fieldextra = options.delete(:fieldextra) || ""
|
98
|
+
front(method, options) + super + back(method, options)
|
99
|
+
end
|
100
|
+
|
101
|
+
def calendar_field method, options = {}
|
102
|
+
options.to_options!
|
103
|
+
expired = options.delete(:expired) || false
|
104
|
+
if not expired; options.merge!(:class => 'calendar'); else; options.merge!(:disabled => true); end
|
105
|
+
text_field method, options
|
106
|
+
end
|
107
|
+
|
108
|
+
def date_select method, options = {}
|
109
|
+
options.to_options!
|
110
|
+
front(method, options) + super + back(method, options)
|
111
|
+
end
|
112
|
+
|
113
|
+
def select method, choices, options = {}, html_options = {}
|
114
|
+
options.to_options!
|
115
|
+
html_options.to_options!
|
116
|
+
front(method, options) + super + back(method, options)
|
117
|
+
end
|
118
|
+
|
119
|
+
def front method = '', options = {}
|
120
|
+
fieldstyle = options.delete(:fieldstyle) || ""
|
121
|
+
fieldstyle += " width: #{options.delete(:fieldwidth) || "100%"};"
|
122
|
+
fieldextra = options.delete(:fieldextra) || ""
|
123
|
+
"<tr class=\"tbl_field\", style=\"#{fieldstyle}\"><th class=\"tbl_field_label\">#{fieldextra}#{label(method, options)}</th><td>".html_safe
|
124
|
+
end
|
125
|
+
|
126
|
+
def back method = '', options = {}
|
127
|
+
"</td></tr>".html_safe
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
module TblForm
|
2
|
+
module ActionViewExtensions
|
3
|
+
# This modules create simple form wrappers around default form_for,
|
4
|
+
# fields_for and remote_form_for.
|
5
|
+
#
|
6
|
+
# Example:
|
7
|
+
#
|
8
|
+
# ms_form_for @user do |f|
|
9
|
+
# f.input :name, :hint => 'My hint'
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
module FormHelper
|
13
|
+
# based on what is done in formtastic
|
14
|
+
# http://github.com/justinfrench/formtastic/blob/master/lib/formtastic.rb#L1706
|
15
|
+
@@default_field_error_proc = nil
|
16
|
+
|
17
|
+
# Override the default ActiveRecordHelper behaviour of wrapping the input.
|
18
|
+
# This gets taken care of semantically by adding an error class to the wrapper tag
|
19
|
+
# containing the input.
|
20
|
+
#
|
21
|
+
FIELD_ERROR_PROC = proc do |html_tag, instance_tag|
|
22
|
+
html_tag
|
23
|
+
end
|
24
|
+
|
25
|
+
def with_custom_field_error_proc(&block)
|
26
|
+
@@default_field_error_proc = ::ActionView::Base.field_error_proc
|
27
|
+
::ActionView::Base.field_error_proc = FIELD_ERROR_PROC
|
28
|
+
result = yield
|
29
|
+
::ActionView::Base.field_error_proc = @@default_field_error_proc
|
30
|
+
result
|
31
|
+
end
|
32
|
+
|
33
|
+
[:form_tag, :form_for, :fields_for, :remote_form_for].each do |helper|
|
34
|
+
class_eval <<-METHOD, __FILE__, __LINE__
|
35
|
+
def tbl_#{helper}(record_or_name_or_array, *args, &block)
|
36
|
+
options = args.extract_options!
|
37
|
+
options[:builder] = TblForm::FormBuilder
|
38
|
+
css_class = case record_or_name_or_array
|
39
|
+
when String, Symbol then record_or_name_or_array.to_s
|
40
|
+
when Array then dom_class(record_or_name_or_array.last)
|
41
|
+
else dom_class(record_or_name_or_array)
|
42
|
+
end
|
43
|
+
options[:html] ||= {}
|
44
|
+
options[:html][:class] = "tbl_form \#{css_class} \#{options[:html][:class]}".strip
|
45
|
+
|
46
|
+
with_custom_field_error_proc do
|
47
|
+
#{helper}(record_or_name_or_array, *(args << options), &block)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
METHOD
|
51
|
+
end
|
52
|
+
|
53
|
+
def table_set(summary = nil, *args, &block)
|
54
|
+
options = args.extract_options!
|
55
|
+
content = capture(&block)
|
56
|
+
"<table class=\"table_set\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" summary=\"#{summary}\">\n".html_safe + content + "</table>\n".html_safe
|
57
|
+
end
|
58
|
+
|
59
|
+
def tbl_display_form(summary = nil, *args, &block)
|
60
|
+
options = args.extract_options!
|
61
|
+
content = capture(&block)
|
62
|
+
"<table class=\"tbl_display_form\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" summary=\"#{summary}\">\n".html_safe + content + "</table>\n".html_safe
|
63
|
+
end
|
64
|
+
|
65
|
+
def tbl_display(label = '', text = '', options = {})
|
66
|
+
options = options.to_options!
|
67
|
+
fieldoptions = options.delete(:fieldoptions)
|
68
|
+
labeloptions = options.delete(:labeloptions)
|
69
|
+
content_tag(:tr, content_tag(:th, label, labeloptions) + content_tag(:td, text, options), fieldoptions)
|
70
|
+
end
|
71
|
+
|
72
|
+
def tbl_wrap(label = '', *args, &block)
|
73
|
+
options = args.extract_options!
|
74
|
+
content = capture(&block)
|
75
|
+
|
76
|
+
#fieldstyle = options.delete(:fieldstyle) || ""
|
77
|
+
#fieldstyle += " width: #{options.delete(:fieldwidth) || "100%"};".html_safe
|
78
|
+
#fieldextra = options.delete(:fieldextra) || ""
|
79
|
+
|
80
|
+
forvalue = options.delete(:for) || ""
|
81
|
+
label = options.delete(:label) || "#{label.to_s.titleize}".html_safe
|
82
|
+
hint = options.delete(:hint) || ""
|
83
|
+
label = label + "<span class=\"ms_hint\">#{hint}</span>".html_safe unless hint.blank?
|
84
|
+
|
85
|
+
content_tag(:tr, content_tag(:th, label, options) + content_tag(:td, content, options), options)
|
86
|
+
end
|
87
|
+
#def ms_display_form(legend = nil, *args, &block)
|
88
|
+
# options = args.extract_options!
|
89
|
+
# content = capture(&block)
|
90
|
+
#
|
91
|
+
# "<div>".html_safe + content + "</div>".html_safe
|
92
|
+
#end
|
93
|
+
#
|
94
|
+
#def ms_display(label = '', value = '', options = {})
|
95
|
+
# fieldstyle = options.delete(:fieldstyle) || ""
|
96
|
+
# fieldstyle += " width: #{options.delete(:fieldwidth) || "100%"};"
|
97
|
+
# fieldextra = options.delete(:fieldextra) || ""
|
98
|
+
# "<div class=\"ms_field\", style=\"#{fieldstyle}\"><div class=\"ms_field_label\"><span class=\"ms_label\">#{fieldextra}#{label}</span></div><span class=\"ms_field_content\">#{value}</span></div>".html_safe
|
99
|
+
#end
|
100
|
+
#
|
101
|
+
#def ms_wrap(label = '', *args, &block)
|
102
|
+
# options = args.extract_options!
|
103
|
+
# content = capture(&block)
|
104
|
+
#
|
105
|
+
# fieldstyle = options.delete(:fieldstyle) || ""
|
106
|
+
# fieldstyle += " width: #{options.delete(:fieldwidth) || "100%"};"
|
107
|
+
# fieldextra = options.delete(:fieldextra) || ""
|
108
|
+
#
|
109
|
+
# forvalue = options.delete(:for) || ""
|
110
|
+
# text = options.delete(:label) || "#{label.to_s.titleize}"
|
111
|
+
# hint = options.delete(:hint) || ""
|
112
|
+
# text = text + "<span class=\"ms_hint\">#{hint}</span>" unless hint.blank?
|
113
|
+
#
|
114
|
+
# "<div class=\"ms_field\", style=\"#{fieldstyle}\"><div class=\"ms_field_label\">#{fieldextra}<label class=\"ms_label\" for=\"#{forvalue}\">#{text}</label></div>".html_safe + content + "</div>".html_safe
|
115
|
+
#end
|
116
|
+
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
ActionView::Base.send :include, TblForm::ActionViewExtensions::FormHelper
|
data/ms_form.gemspec
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{ms_form}
|
8
|
+
s.version = "0.2.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["wmerrell"]
|
12
|
+
s.date = %q{2010-11-13}
|
13
|
+
s.description = %q{Wraps the standard form handling functions with additional formatting and features.}
|
14
|
+
s.email = %q{will@morelandsolutions.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/ms_form.rb",
|
27
|
+
"lib/ms_form/engine.rb",
|
28
|
+
"lib/ms_form/form_builder.rb",
|
29
|
+
"lib/ms_form/form_helper.rb",
|
30
|
+
"lib/tbl_form/engine.rb",
|
31
|
+
"lib/tbl_form/form_builder.rb",
|
32
|
+
"lib/tbl_form/form_helper.rb",
|
33
|
+
"ms_form.gemspec",
|
34
|
+
"test/helper.rb",
|
35
|
+
"test/test_ms_form.rb"
|
36
|
+
]
|
37
|
+
s.homepage = %q{http://github.com/wmerrell/ms_form}
|
38
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
39
|
+
s.require_paths = ["lib"]
|
40
|
+
s.rubygems_version = %q{1.3.7}
|
41
|
+
s.summary = %q{Fancy Form Handling.}
|
42
|
+
s.test_files = [
|
43
|
+
"test/test_ms_form.rb",
|
44
|
+
"test/helper.rb"
|
45
|
+
]
|
46
|
+
|
47
|
+
if s.respond_to? :specification_version then
|
48
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
49
|
+
s.specification_version = 3
|
50
|
+
|
51
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
52
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ms_form
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- wmerrell
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-11-13 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: thoughtbot-shoulda
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
description: Wraps the standard form handling functions with additional formatting and features.
|
36
|
+
email: will@morelandsolutions.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- lib/ms_form.rb
|
52
|
+
- lib/ms_form/engine.rb
|
53
|
+
- lib/ms_form/form_builder.rb
|
54
|
+
- lib/ms_form/form_helper.rb
|
55
|
+
- lib/tbl_form/engine.rb
|
56
|
+
- lib/tbl_form/form_builder.rb
|
57
|
+
- lib/tbl_form/form_helper.rb
|
58
|
+
- ms_form.gemspec
|
59
|
+
- test/helper.rb
|
60
|
+
- test/test_ms_form.rb
|
61
|
+
has_rdoc: true
|
62
|
+
homepage: http://github.com/wmerrell/ms_form
|
63
|
+
licenses: []
|
64
|
+
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options:
|
67
|
+
- --charset=UTF-8
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 3
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
hash: 3
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
version: "0"
|
88
|
+
requirements: []
|
89
|
+
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.3.7
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: Fancy Form Handling.
|
95
|
+
test_files:
|
96
|
+
- test/test_ms_form.rb
|
97
|
+
- test/helper.rb
|