scaffold_form_generator 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,10 @@
1
+ == 1.1.0 / 2007-10-30
2
+ * Changed form fields to use the form_for syntax (f.text_field and such)
3
+ * Removed fields ending in _id from the generated forms.
4
+
5
+ == 1.0.0 / 2007-10-25
6
+ * Added the RESTful routing to the forms
7
+ * Skipped datestamp and timestamp fields
8
+ * changed booleans to checkboxes instead of selects
9
+ * Birthday!
10
+
data/README.txt ADDED
@@ -0,0 +1,51 @@
1
+ ScaffoldForm
2
+ by Brian Hogan
3
+ http://www.napcs.com
4
+
5
+ == DESCRIPTION:
6
+
7
+ Scaffolding, out of the box, is terrible. However, there's one part that saves you some serious time, and that's the form creation. This plugin takes code from Rails' legacy scaffold generator and makes it available again, but this time it only builds the new and edit forms.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Using form_for with the RESTful syntax with form_for and friends
12
+ * LOCK_VERSION, CREATED_AT, CREATED_ON, UPDATED_AT, UPDATED_ON fields, and anything with an _id at the end are skipped
13
+ * Boolean fields are done as checkboxes instead of selects.
14
+
15
+ == SYNOPSIS:
16
+
17
+ ruby script/generate Project projects
18
+
19
+ == REQUIREMENTS:
20
+
21
+ * Rails, a model, and a table in your database that is already migrated.
22
+
23
+ == INSTALL:
24
+
25
+ * Mac and Linux: sudo gem install scaffold_form_generator
26
+ * Windows: gem install scaffold_form
27
+
28
+ == LICENSE:
29
+
30
+ (The MIT License)
31
+
32
+ Copyright (c) 2007 Brian Hogan with code from http://dev.rubyonrails.org/
33
+
34
+ Permission is hereby granted, free of charge, to any person obtaining
35
+ a copy of this software and associated documentation files (the
36
+ 'Software'), to deal in the Software without restriction, including
37
+ without limitation the rights to use, copy, modify, merge, publish,
38
+ distribute, sublicense, and/or sell copies of the Software, and to
39
+ permit persons to whom the Software is furnished to do so, subject to
40
+ the following conditions:
41
+
42
+ The above copyright notice and this permission notice shall be
43
+ included in all copies or substantial portions of the Software.
44
+
45
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
46
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
47
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
48
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
49
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
50
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
51
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,228 @@
1
+
2
+
3
+ class ScaffoldingSandbox
4
+ include ActionView::Helpers::ActiveRecordHelper
5
+
6
+ attr_accessor :form_action, :singular_name, :suffix, :model_instance
7
+
8
+ def sandbox_binding
9
+ binding
10
+ end
11
+
12
+ def default_input_block
13
+ Proc.new { |record, column| "<p><label for=\"#{record}_#{column.name}\">#{column.human_name}</label><br/>\n#{input(record, column.name)}</p>\n" }
14
+ end
15
+
16
+ end
17
+
18
+
19
+ module ActionView::Helpers::ActiveRecordHelper
20
+
21
+ def all_input_tags(record, record_name, options)
22
+ input_block = options[:input_block] || default_input_block
23
+ skip_columns = ["created_at", "lock_version", "created_on", "updated_at", "updated_on"]
24
+ @results = record.class.content_columns.collect do |column|
25
+ unless skip_columns.detect{|c| c == column.name} || column.name.last(3) == "id"
26
+ input_block.call(record_name, column)
27
+ end
28
+ end
29
+
30
+ @results.join("")
31
+
32
+
33
+ end
34
+
35
+
36
+
37
+ def all_associations(record)
38
+
39
+ @results =""
40
+ puts record.class.reflect_on_all_associations.inspect
41
+ record.class.reflect_on_all_associations.each do |a|
42
+ puts a.inspect
43
+ if a.macro == :belongs_to
44
+ @results += "<p>#{a.klass}<br/><%=f.select #{a.klass}.find(:all).collect {|i| [ p.inspect, p.id ] }, { :include_blank => true }) %>"
45
+ end
46
+
47
+ end
48
+
49
+ @results
50
+ end
51
+
52
+
53
+
54
+
55
+ end
56
+
57
+
58
+ class ActionView::Helpers::InstanceTag
59
+
60
+
61
+ def to_tag(options = {})
62
+ case column_type
63
+ when :string
64
+ field_type = @method_name.include?("password") ? "password" : "text"
65
+ to_input_field_tag(field_type, options)
66
+ when :text
67
+ to_text_area_tag(options)
68
+ when :integer, :float, :decimal
69
+ to_input_field_tag("text", options)
70
+ when :date
71
+ to_date_select_tag(options)
72
+ when :datetime, :timestamp
73
+ to_datetime_select_tag(options)
74
+ when :time
75
+ to_time_select_tag(options)
76
+ when :boolean
77
+ to_boolean_checkbox_tag(options)
78
+ end
79
+ end
80
+
81
+
82
+ def to_input_field_tag(field_type, options={})
83
+ field_meth = "#{field_type}_field"
84
+ "<%= f.#{field_meth} '#{@method_name}' #{options.empty? ? '' : ', '+options.inspect} %>"
85
+ end
86
+
87
+ def to_boolean_checkbox_tag(options = {})
88
+ "<%= f.check_box '#{@method_name}' #{options.empty? ? '' : ', '+ options.inspect} %>"
89
+ end
90
+
91
+ def to_text_area_tag(options = {})
92
+ "<%= f.text_area '#{@method_name}' #{options.empty? ? '' : ', '+ options.inspect} %>"
93
+ end
94
+
95
+ def to_date_select_tag(options = {})
96
+ "<%= f.date_select '#{@method_name}' #{options.empty? ? '' : ', '+ options.inspect} %>"
97
+ end
98
+
99
+ def to_datetime_select_tag(options = {})
100
+ "<%= f.datetime_select '#{@method_name}' #{options.empty? ? '' : ', '+ options.inspect} %>"
101
+ end
102
+
103
+ def to_time_select_tag(options = {})
104
+ "<%= f.time_select '#{@method_name}' #{options.empty? ? '' : ', '+ options.inspect} %>"
105
+ end
106
+ end
107
+
108
+ class ScaffoldFormGenerator < Rails::Generator::NamedBase
109
+ attr_reader :controller_name,
110
+ :controller_class_path,
111
+ :controller_file_path,
112
+ :controller_class_nesting,
113
+ :controller_class_nesting_depth,
114
+ :controller_class_name,
115
+ :controller_singular_name,
116
+ :controller_plural_name
117
+ alias_method :controller_file_name, :controller_singular_name
118
+ alias_method :controller_table_name, :controller_plural_name
119
+
120
+ def initialize(runtime_args, runtime_options = {})
121
+ super
122
+
123
+ # Take controller name from the next argument. Default to the pluralized model name.
124
+ @controller_name = args.shift
125
+ @controller_name ||= ActiveRecord::Base.pluralize_table_names ? @name.pluralize : @name
126
+
127
+ base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
128
+ @controller_class_name_without_nesting, @controller_singular_name, @controller_plural_name = inflect_names(base_name)
129
+
130
+ if @controller_class_nesting.empty?
131
+ @controller_class_name = @controller_class_name_without_nesting
132
+ else
133
+ @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
134
+ end
135
+ end
136
+
137
+ def manifest
138
+ record do |m|
139
+ # Check for class naming collisions.
140
+ m.class_collisions controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}ControllerTest", "#{controller_class_name}Helper"
141
+
142
+ # Controller, helper, views, and test directories.
143
+
144
+ m.directory File.join('app/views', controller_class_path, controller_file_name)
145
+
146
+
147
+
148
+ # Scaffolded forms.
149
+ m.complex_template "form.rhtml",
150
+ File.join('app/views',
151
+ controller_class_path,
152
+ controller_file_name,
153
+ "_form.html.erb"),
154
+ :insert => 'form_scaffolding.rhtml',
155
+ :sandbox => lambda { create_sandbox },
156
+ :begin_mark => 'form',
157
+ :end_mark => 'eoform',
158
+ :mark_id => singular_name
159
+
160
+
161
+ # Scaffolded views.
162
+ scaffold_views.each do |action|
163
+ m.template "view_#{action}.rhtml",
164
+ File.join('app/views',
165
+ controller_class_path,
166
+ controller_file_name,
167
+ "#{action}.html.erb"),
168
+ :assigns => { :action => action }
169
+ end
170
+
171
+
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+
180
+
181
+ end
182
+ end
183
+
184
+ protected
185
+ # Override with your own usage banner.
186
+ def banner
187
+ "Usage: #{$0} scaffold ModelName [ControllerName]"
188
+ end
189
+
190
+ def scaffold_views
191
+ %w(new edit)
192
+ end
193
+
194
+
195
+
196
+ def model_name
197
+ class_name.demodulize
198
+ end
199
+
200
+ def suffix
201
+ "_#{singular_name}" if options[:suffix]
202
+ end
203
+
204
+ def create_sandbox
205
+ sandbox = ScaffoldingSandbox.new
206
+ sandbox.singular_name = singular_name
207
+ begin
208
+ sandbox.model_instance = model_instance
209
+ sandbox.instance_variable_set("@#{singular_name}", sandbox.model_instance)
210
+ rescue ActiveRecord::StatementInvalid => e
211
+ logger.error "Before updating scaffolding from new DB schema, try creating a table for your model (#{class_name})"
212
+ raise SystemExit
213
+ end
214
+ sandbox.suffix = suffix
215
+ sandbox
216
+ end
217
+
218
+ def model_instance
219
+ base = class_nesting.split('::').inject(Object) do |base, nested|
220
+ break base.const_get(nested) if base.const_defined?(nested)
221
+ base.const_set(nested, Module.new)
222
+ end
223
+ unless base.const_defined?(@class_name_without_nesting)
224
+ base.const_set(@class_name_without_nesting, Class.new(ActiveRecord::Base))
225
+ end
226
+ class_name.constantize.new
227
+ end
228
+ end
@@ -0,0 +1,4 @@
1
+ <%%= error_messages_for '<%= singular_name %>' %>
2
+
3
+ <%= template_for_inclusion %>
4
+
@@ -0,0 +1,2 @@
1
+ <%= all_input_tags(@model_instance, @singular_name, {}) %>
2
+
@@ -0,0 +1,9 @@
1
+ <h1>Editing <%= singular_name %></h1>
2
+
3
+ <%% form_for(@<%= singular_name %>) do |f| %>
4
+ <%%= render :partial => 'form' , :locals=>{:f => f}%>
5
+ <%%= submit_tag 'Save' %>
6
+ <%% end %>
7
+
8
+ <%%= link_to 'Show', @<%= singular_name %> %> |
9
+ <%%= link_to 'Back', <%= plural_name %>_path %>
@@ -0,0 +1,8 @@
1
+ <h1>New <%= singular_name %></h1>
2
+
3
+ <%% form_for(@<%= singular_name %>) do |f| %>
4
+ <%%= render :partial => 'form' , :locals=>{:f => f}%>
5
+ <%%= submit_tag "Add" %>
6
+ <%% end %>
7
+
8
+ <%%= link_to 'Back', <%= plural_name %>_path %>
File without changes
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: scaffold_form_generator
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.1.0
7
+ date: 2007-10-31 00:00:00 -05:00
8
+ summary: Form generation for Rails models based on original scaffolding
9
+ require_paths:
10
+ - lib
11
+ email: info@napcs.com
12
+ homepage: http://www.zenspider.com/ZSS/Products/scaffold_form_generator/
13
+ rubyforge_project: scaffoldform
14
+ description: Form generation for Rails models based on original scaffolding with some minor tweaks.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Brian Hogan
31
+ files:
32
+ - README.txt
33
+ - History.txt
34
+ - scaffold_form_generator.rb
35
+ - templates/form.rhtml
36
+ - templates/form_scaffolding.rhtml
37
+ - templates/view_edit.rhtml
38
+ - templates/view_new.rhtml
39
+ test_files:
40
+ - test/test_scaffold_form.rb
41
+ rdoc_options:
42
+ - --main
43
+ - README.txt
44
+ extra_rdoc_files:
45
+ - README.txt
46
+ - History.txt
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ requirements: []
52
+
53
+ dependencies:
54
+ - !ruby/object:Gem::Dependency
55
+ name: hoe
56
+ version_requirement:
57
+ version_requirements: !ruby/object:Gem::Version::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.0
62
+ version: