form_cutter 1.0.1 → 1.0.2
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/README.rdoc +36 -11
- data/lib/form_cutter/action_view_extensions/form_helper.rb +27 -12
- data/lib/form_cutter/version.rb +1 -1
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
== FormCutter
|
2
2
|
|
3
|
-
The default rails form builder is great but it is so tedious to change the
|
4
|
-
This gem wraps *your*
|
3
|
+
The default rails form builder is great but it is so tedious to change the HTML that goes around it.
|
4
|
+
This gem wraps *your* HTML around the form elements. So all you have to do is this:
|
5
5
|
|
6
6
|
<%= f.text_field :name %>
|
7
7
|
|
8
|
-
And add your
|
8
|
+
And add your HTML form helper template in <tt>app/views/forms/text_field.html.erb</tt> like this:
|
9
9
|
|
10
10
|
<div class="forms">
|
11
11
|
<%= label(object_name, method, label_name) %><em><%= required %></em><br/>
|
@@ -35,32 +35,44 @@ Use the form builders normally, except don't add any markup (unless you want to)
|
|
35
35
|
<fieldset>
|
36
36
|
<%= f.text_field :title %>
|
37
37
|
<%= f.text_area :body %>
|
38
|
-
<%= f.select :section, choices %>
|
39
|
-
<%= f.radio_button :am_fm %>
|
38
|
+
<%= f.select :section, ['choices'] %>
|
39
|
+
<%= f.radio_button :am_fm, ['radio'] %>
|
40
40
|
<%= f.check_box :terms_and_conditions %>
|
41
|
+
<%= f.date_select :dob %>
|
42
|
+
<%= f.time_select :check_in_time %>
|
43
|
+
<%= f.datetime_select :updated_at %>
|
41
44
|
</fieldset>
|
42
45
|
<div class="buttons">
|
43
|
-
<%= f.
|
46
|
+
<%= f.submit 'Save' %>
|
44
47
|
</div>
|
45
48
|
<% end %>
|
46
49
|
|
47
|
-
The form builders will now wrap all form helper methods with *your*
|
50
|
+
The form builders will now wrap all form helper methods with *your* HTML.
|
48
51
|
|
49
52
|
You're done.
|
50
53
|
|
51
54
|
=== Customization
|
52
55
|
|
53
|
-
There will always be an exception to the rule. So there are 2 ways to change the
|
56
|
+
There will always be an exception to the rule. So there are 2 ways to change the form helper HTML wrapper.
|
54
57
|
1. Like a resource - by creating a directory inside forms/, <tt>app/views/forms/blog/</tt>.
|
55
58
|
2. Passing the :template option - either false or the '<tt>path/to/template</tt>'.
|
56
59
|
|
57
|
-
|
60
|
+
==== The :template option
|
61
|
+
|
62
|
+
The :template option is useful if you have a field or group of fields that don't work with your normal templates.
|
63
|
+
There's always one. This option tells the renderer to look for a specific HTML erb file or to look in a specific
|
64
|
+
folder, if a file by that name doesn't exist. Below is the order in which files will be found and rendered.
|
65
|
+
|
66
|
+
It will render the first erb template that it finds.
|
67
|
+
For instance in this case of <tt><%= f.text_field :title, :template => 'custom' %></tt> for the @blog model,
|
68
|
+
it looks in this order:
|
69
|
+
|
58
70
|
* forms/blog/custom.html.erb
|
59
71
|
* forms/custom.html.erb
|
60
72
|
* forms/custom/title.html.erb
|
61
73
|
* forms/custom/text_field.html.erb
|
62
74
|
* forms/custom/default.html.erb
|
63
|
-
* forms/title.html.erb
|
75
|
+
* forms/blog/title.html.erb
|
64
76
|
* forms/blog/text_field.html.erb
|
65
77
|
* forms/text_field.html.erb
|
66
78
|
* forms/default.html.erb
|
@@ -73,8 +85,17 @@ This will search in app/views/reports/ instead of app/views/forms/.
|
|
73
85
|
|
74
86
|
<%= f.text_field(:title, :report => true) %>
|
75
87
|
|
88
|
+
In an admin or business data entry setting, this is useful to quickly get a "photocopy" of the completed form.
|
89
|
+
|
90
|
+
<%= form_for(@post, :template => true) do |form| %>
|
91
|
+
<%= render 'form' %>
|
92
|
+
<% end %>
|
93
|
+
|
94
|
+
The :template or :report optons can be passed to <tt>form_for</tt> or <tt>fields_for</tt> and it will be applied to
|
95
|
+
all helper methods within.
|
96
|
+
|
76
97
|
==== Other options
|
77
|
-
You can pass any options to the helper methods. These will become availble in the.
|
98
|
+
You can pass any options to the helper methods. These will become availble in the helper templates.
|
78
99
|
|
79
100
|
<%= f.text_field(:title, :hint => 'This is a field hint', :anything => 'anything you want') %>
|
80
101
|
|
@@ -85,6 +106,10 @@ Will give you:
|
|
85
106
|
|
86
107
|
<%= options[:anything] %>
|
87
108
|
=> 'anything you want'
|
109
|
+
|
110
|
+
note: in the anything case, the option will be added to the HTML output.
|
111
|
+
|
112
|
+
<input type="text" anything="anything you want">
|
88
113
|
|
89
114
|
FormCutter has several configuration values. You can read and change them in the initializer created by FormCutter, so if you haven't executed the command below yet, please do:
|
90
115
|
|
@@ -9,7 +9,7 @@ module FormCutter
|
|
9
9
|
raise template_error if !template?(options) || caller.first.include?("/app/views/"+base_template_path(options))
|
10
10
|
render(:template => template_file(object_name, method, options, '#{helper_method_name}'), :locals => locals(object_name, method, '#{helper_method_name}', options))
|
11
11
|
rescue ActionView::MissingTemplate
|
12
|
-
super
|
12
|
+
super(object_name, method, clean_options(options))
|
13
13
|
end
|
14
14
|
end
|
15
15
|
METHOD
|
@@ -20,7 +20,7 @@ module FormCutter
|
|
20
20
|
raise template_error if !template?(options) || caller.first.include?("/app/views/"+base_template_path(options))
|
21
21
|
render(:template => template_file(object_name, method, options, :check_box), :locals => locals(object_name, method, :check_box, options))
|
22
22
|
rescue ActionView::MissingTemplate
|
23
|
-
super
|
23
|
+
super(object_name, method, clean_options(options), checked_value, unchecked_value)
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
@@ -29,22 +29,33 @@ module FormCutter
|
|
29
29
|
raise template_error if !template?(options) || caller.first.include?("/app/views/"+base_template_path(options))
|
30
30
|
render(:template => template_file(object_name, method, options, :radio_button), :locals => locals(object_name, method, :radio_button, options))
|
31
31
|
rescue ActionView::MissingTemplate
|
32
|
-
super
|
32
|
+
super(object_name, method, tag_value, clean_options(options))
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
+
([:date_select, :time_select, :datetime_select]).each do |helper_method_name|
|
37
|
+
module_eval <<-METHOD, __FILE__, __LINE__ + 1
|
38
|
+
def #{helper_method_name}(object_name, method, options = {}, html_options = {})
|
39
|
+
begin
|
40
|
+
raise template_error if !template?(options) || caller.first.include?("/app/views/"+base_template_path(options))
|
41
|
+
render(:template => template_file(object_name, method, options, '#{helper_method_name}'), :locals => locals(object_name, method, '#{helper_method_name}', options, html_options))
|
42
|
+
rescue ActionView::MissingTemplate
|
43
|
+
super(object_name, method, clean_options(options), html_options = {})
|
44
|
+
end
|
45
|
+
end
|
46
|
+
METHOD
|
47
|
+
end
|
48
|
+
|
36
49
|
private
|
37
50
|
|
38
51
|
def locals(object_name, method, helper_method_name, options, content = nil)
|
52
|
+
options[:hint] = (options[:hint] || '').html_safe
|
53
|
+
options[:label_name] = (options[:label] || '').html_safe
|
39
54
|
options[:required] = options[:object].class.validators_on(method).map(&:class).include? ActiveModel::Validations::PresenceValidator if options[:required].nil?
|
40
|
-
options[:required] =
|
55
|
+
options[:required] = options[:required] ? I18n.t(:"form_cutter.required.mark", :default => '*') : ''
|
41
56
|
|
42
|
-
locals = { :object => options[:object], :object_name => object_name, :method => method, :helper_method_name => helper_method_name, :options => options }
|
43
|
-
|
44
|
-
locals.merge!({:label_name => options.delete(:label)})
|
45
|
-
locals.merge!({:report => options.delete(:report)})
|
46
|
-
locals.merge!({:required => options.delete(:required)})
|
47
|
-
locals.merge!({:template => options.delete(:template)})
|
57
|
+
locals = { :object => options[:object], :object_name => object_name, :method => method, :helper_method_name => helper_method_name, :options => options }.merge(options)
|
58
|
+
options = clean_options(options)
|
48
59
|
locals
|
49
60
|
end
|
50
61
|
|
@@ -61,7 +72,7 @@ module FormCutter
|
|
61
72
|
[path, template, method.to_s].join('/'), # forms/custom/title.html.erb
|
62
73
|
[path, template, helper_method_name].join('/'), # forms/custom/text_field.html.erb
|
63
74
|
[path, template, 'default'].join('/'), # forms/custom/default.html.erb
|
64
|
-
[path, resource, method.to_s].join('/'), # forms/title.html.erb
|
75
|
+
[path, resource, method.to_s].join('/'), # forms/blog/title.html.erb
|
65
76
|
[path, resource, helper_method_name].join('/'), # forms/blog/text_field.html.erb
|
66
77
|
[path, helper_method_name].join('/'), # forms/text_field.html.erb
|
67
78
|
[path, 'default'].join('/') # forms/default.html.erb
|
@@ -72,12 +83,16 @@ module FormCutter
|
|
72
83
|
report?(options) ? FormCutter.reports_path : FormCutter.forms_path
|
73
84
|
end
|
74
85
|
|
86
|
+
def clean_options(options)
|
87
|
+
options.delete_if { |k, v| [:hint, :label, :label_name, :report, :required, :template].include?(k) }
|
88
|
+
end
|
89
|
+
|
75
90
|
def report?(options)
|
76
91
|
options[:report]
|
77
92
|
end
|
78
93
|
|
79
94
|
def template?(options)
|
80
|
-
!(options[:template]
|
95
|
+
!(options[:template] === false)
|
81
96
|
end
|
82
97
|
|
83
98
|
def template_error
|
data/lib/form_cutter/version.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 1
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 1.0.
|
8
|
+
- 2
|
9
|
+
version: 1.0.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Benjamin Lewis
|
@@ -14,11 +14,11 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-10-
|
17
|
+
date: 2010-10-26 00:00:00 +10:30
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
21
|
-
description: Lets you wrap your form helper methods
|
21
|
+
description: Lets you wrap your form helper methods in custom html
|
22
22
|
email: 23inhouse@gmail.com
|
23
23
|
executables: []
|
24
24
|
|