ajax_scaffold_generator 1.0.0 → 2.0.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/MIT-LICENSE CHANGED
@@ -1,20 +1,20 @@
1
- Copyright (c) 2005 Maurycy Pawlowski-Wieronski
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.
1
+ Copyright (c) 2006 Richard White
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/{USAGE → README} RENAMED
@@ -1,31 +1,28 @@
1
- Description:
2
- The ajax scaffold generator creates a controller to interact with a model.
3
- If the model does not exist, it creates the model as well. Unlike the
4
- standard scaffold, the ajax scaffold generator uses AJAX.
5
-
6
- The generator takes a model name, an optional controller name, and a
7
- list of views as arguments. Scaffolded actions and views are created
8
- automatically. Any views left over generate empty stubs.
9
-
10
- The scaffolded actions are:
11
- index, list, new, edit, destroy
12
-
13
- If a controller name is not given, the plural form of the model name
14
- will be used. The model and controller names may be given in CamelCase
15
- or under_score and should not be suffixed with 'Model' or 'Controller'.
16
- Both model and controller names may be prefixed with a module like a
17
- file path; see the Modules Example for usage.
18
-
19
- Example:
20
- ./script/generate ajax_scaffold Account Bank debit credit
21
-
22
- This will generate an Account model and BankController with a full test
23
- suite and a basic user interface. Now create the accounts table in your
24
- database and browse to http://localhost/bank/ -- voila, you're on Rails!
25
-
26
- Modules Example:
27
- ./script/generate ajax_scaffold CreditCard 'admin/credit_card' suspend late_fee
28
-
29
- This will generate a CreditCard model and CreditCardController controller
30
- in the admin module.
31
-
1
+ Description:
2
+ The ajax scaffold generator creates a controller to interact with a model.
3
+ If the model does not exist, it creates the model as well. Unlike the
4
+ standard scaffold, the ajax scaffold generator uses AJAX.
5
+
6
+ The generator takes a model name, an optional controller name, and a
7
+ list of views as arguments. Scaffolded actions and views are created
8
+ automatically. Any views left over generate empty stubs.
9
+
10
+ If a controller name is not given, the plural form of the model name
11
+ will be used. The model and controller names may be given in CamelCase
12
+ or under_score and should not be suffixed with 'Model' or 'Controller'.
13
+ Both model and controller names may be prefixed with a module like a
14
+ file path; see the Modules Example for usage.
15
+
16
+ Example:
17
+ ./script/generate ajax_scaffold Account Bank debit credit
18
+
19
+ This will generate an Account model and BankController with a full test
20
+ suite and a basic user interface. Now create the accounts table in your
21
+ database and browse to http://localhost/bank/ -- voila, you're on Rails!
22
+
23
+ Modules Example:
24
+ ./script/generate ajax_scaffold CreditCard 'admin/credit_card' suspend late_fee
25
+
26
+ This will generate a CreditCard model and CreditCardController controller
27
+ in the admin module.
28
+
@@ -1,201 +1,123 @@
1
- class ScaffoldingSandbox
2
- include ActionView::Helpers::ActiveRecordHelper
3
-
4
- attr_accessor :form_action, :singular_name, :suffix, :model_instance
5
-
6
- def sandbox_binding
7
- binding
8
- end
9
-
10
- def default_input_block
11
- Proc.new { |record, column| "<p><label for=\"#{record}_#{column.name}\">#{column.human_name}</label><br/>\n#{input(record, column.name)}</p>\n" }
12
- end
13
- end
14
-
15
- class ActionView::Helpers::InstanceTag
16
- def to_input_field_tag(field_type, options={})
17
- field_meth = "#{field_type}_field"
18
- "<%= #{field_meth} '#{@object_name}', '#{@method_name}' #{options.empty? ? '' : ', '+options.inspect} %>"
19
- end
20
-
21
- def to_text_area_tag(options = {})
22
- "<%= text_area '#{@object_name}', '#{@method_name}' #{options.empty? ? '' : ', '+ options.inspect} %>"
23
- end
24
-
25
- def to_date_select_tag(options = {})
26
- "<%= date_select '#{@object_name}', '#{@method_name}' #{options.empty? ? '' : ', '+ options.inspect} %>"
27
- end
28
-
29
- def to_datetime_select_tag(options = {})
30
- "<%= datetime_select '#{@object_name}', '#{@method_name}' #{options.empty? ? '' : ', '+ options.inspect} %>"
31
- end
32
- end
33
-
34
- class AjaxScaffoldGenerator < Rails::Generator::NamedBase
35
- attr_reader :controller_name,
36
- :controller_class_path,
37
- :controller_file_path,
38
- :controller_class_nesting,
39
- :controller_class_nesting_depth,
40
- :controller_class_name,
41
- :controller_singular_name,
42
- :controller_plural_name
43
- alias_method :controller_file_name, :controller_singular_name
44
- alias_method :controller_table_name, :controller_plural_name
45
-
46
- def initialize(runtime_args, runtime_options = {})
47
- super
48
-
49
- @controller_name = args.shift
50
- @controller_name ||= ActiveRecord::Base.pluralize_table_names ? @name.pluralize : @name
51
-
52
- base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
53
- @controller_class_name_without_nesting, @controller_singular_name, @controller_plural_name = inflect_names(base_name)
54
-
55
- if @controller_class_nesting.empty?
56
- @controller_class_name = @controller_class_name_without_nesting
57
- else
58
- @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
59
- end
60
- end
61
-
62
- def manifest
63
- record do |m|
64
- # Check for class naming collisions.
65
- m.class_collisions controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}ControllerTest", "#{controller_class_name}Helper"
66
-
67
- # Controller, helper, views, and test directories.
68
- m.directory File.join('app/controllers', controller_class_path)
69
- m.directory File.join('app/helpers', controller_class_path)
70
- m.directory File.join('app/views', controller_class_path, controller_file_name)
71
- m.directory File.join('test/functional', controller_class_path)
72
-
73
- # Depend on model generator but skip if the model exists.
74
- m.dependency 'model', [singular_name], :collision => :skip
75
-
76
- # Scaffolded forms.
77
- m.complex_template 'form.rhtml',
78
- File.join('app/views',
79
- controller_class_path,
80
- controller_file_name,
81
- '_form.rhtml'),
82
- :insert => 'form_scaffolding.rhtml',
83
- :sandbox => lambda { create_sandbox },
84
- :begin_mark => 'form',
85
- :end_mark => 'eoform',
86
- :mark_id => singular_name
87
-
88
- # Scaffolded partials.
89
- scaffold_partials.each do |action|
90
- m.template "partial_#{action}.rhtml",
91
- File.join('app/views',
92
- controller_class_path,
93
- controller_file_name,
94
- "_#{action}.rhtml"),
95
- :assigns => { :action => action }
96
- end
97
-
98
- # Scaffolded views.
99
- scaffold_views.each do |action|
100
- m.template "view_#{action}.rhtml",
101
- File.join('app/views',
102
- controller_class_path,
103
- controller_file_name,
104
- "#{action}.rhtml"),
105
- :assigns => { :action => action }
106
- end
107
-
108
- # Controller class, functional test, helper, and views.
109
- m.template 'controller.rb',
110
- File.join('app/controllers',
111
- controller_class_path,
112
- "#{controller_file_name}_controller.rb")
113
-
114
- m.template 'functional_test.rb',
115
- File.join('test/functional',
116
- controller_class_path,
117
- "#{controller_file_name}_controller_test.rb")
118
-
119
- m.template 'helper.rb',
120
- File.join('app/helpers',
121
- controller_class_path,
122
- "#{controller_file_name}_helper.rb")
123
-
124
- # Layout and stylesheet.
125
- m.template 'layout.rhtml', "app/views/layouts/#{controller_file_name}.rhtml"
126
- m.template 'script.js', "public/javascripts/#{controller_file_name}.js"
127
- m.template 'style.css', 'public/stylesheets/scaffold.css'
128
-
129
- # Unscaffolded views.
130
- unscaffolded_actions.each do |action|
131
- path = File.join('app/views',
132
- controller_class_path,
133
- controller_file_name,
134
- "#{action}.rhtml")
135
-
136
- m.template 'controller:view.rhtml', path,
137
- :assigns => { :action => action, :path => path }
138
- end
139
- end
140
- end
141
-
142
- protected
143
- # Override with your own usage banner.
144
- def banner
145
- "Usage: #{$0} ajax_scaffold ModelName [ControllerName] [action, ...]"
146
- end
147
-
148
- def model_name
149
- class_name.demodulize
150
- end
151
-
152
- def scaffold_actions
153
- %w( edit destroy index list new )
154
- end
155
-
156
- def scaffold_partials
157
- %w( edit error item new )
158
- end
159
-
160
- def scaffold_views
161
- %w( list )
162
- end
163
-
164
- def unscaffolded_actions
165
- args - scaffold_actions
166
- end
167
-
168
- def suffix
169
- "_#{singular_name}" if options[:suffix]
170
- end
171
-
172
- def create_sandbox
173
- sandbox = ScaffoldingSandbox.new
174
- sandbox.singular_name = singular_name
175
-
176
- begin
177
- sandbox.model_instance = model_instance
178
- sandbox.instance_variable_set("@#{singular_name}", sandbox.model_instance)
179
- rescue ActiveRecord::StatementInvalid => e
180
- logger.error "Before updating scaffolding from new DB schema, try creating a table for your model (#{class_name})"
181
- raise SystemExit
182
- end
183
-
184
- sandbox.suffix = suffix
185
- sandbox
186
- end
187
-
188
- def model_instance
189
- base = class_nesting.split('::').inject(Object) do |base, nested|
190
- break base.const_get(nested) if base.const_defined?(nested)
191
-
192
- base.const_set(nested, Module.new)
193
- end
194
-
195
- unless base.const_defined?(@class_name_without_nesting)
196
- base.const_set(@class_name_without_nesting, Class.new(ActiveRecord::Base))
197
- end
198
-
199
- class_name.constantize.new
200
- end
201
- end
1
+ require "rails_generator/generators/components/scaffold/scaffold_generator"
2
+
3
+ class AjaxScaffoldingSandbox < ScaffoldingSandbox
4
+ def default_input_block
5
+ Proc.new { |record, column| "<div class=\"form-element\">\n <label for=\"#{record}_#{column.name}\">#{column.human_name}</label>\n #{input(record, column.name)}\n</div>\n" }
6
+ end
7
+ end
8
+
9
+ class AjaxScaffoldGenerator < ScaffoldGenerator
10
+
11
+ def manifest
12
+ record do |m|
13
+ # Check for class naming collisions.
14
+ m.class_collisions controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}ControllerTest", "#{controller_class_name}Helper"
15
+
16
+ # Controller, helper, views, and test directories.
17
+ m.directory File.join('app/controllers', controller_class_path)
18
+ m.directory File.join('app/helpers', controller_class_path)
19
+ m.directory File.join('app/views', controller_class_path, controller_file_name)
20
+ m.directory File.join('public/images')
21
+ m.directory File.join('test/functional', controller_class_path)
22
+
23
+ # Depend on model generator but skip if the model exists.
24
+ m.dependency 'model', [singular_name], :collision => :skip
25
+
26
+ # Scaffolded forms.
27
+ m.complex_template 'form.rhtml',
28
+ File.join('app/views',
29
+ controller_class_path,
30
+ controller_file_name,
31
+ '_form.rhtml'),
32
+ :insert => 'form_scaffolding.rhtml',
33
+ :sandbox => lambda { create_sandbox },
34
+ :begin_mark => 'form',
35
+ :end_mark => 'eoform',
36
+ :mark_id => singular_name
37
+
38
+ # Scaffolded partials.
39
+ m.template "partial_item.rhtml",
40
+ File.join('app/views',
41
+ controller_class_path,
42
+ controller_file_name,
43
+ "_#{controller_file_name}.rhtml")
44
+
45
+ m.template "partial_form_errors.rhtml",
46
+ File.join('app/views',
47
+ controller_class_path,
48
+ controller_file_name,
49
+ "_form_errors.rhtml")
50
+
51
+ # Scaffolded views.
52
+ scaffold_views.each do |action|
53
+ m.template "view_#{action}.rhtml",
54
+ File.join('app/views',
55
+ controller_class_path,
56
+ controller_file_name,
57
+ "#{action}.rhtml"),
58
+ :assigns => { :action => action }
59
+ end
60
+
61
+ # Controller class, functional test, helper, and views.
62
+ m.template 'controller.rb',
63
+ File.join('app/controllers',
64
+ controller_class_path,
65
+ "#{controller_file_name}_controller.rb")
66
+
67
+ m.template 'functional_test.rb',
68
+ File.join('test/functional',
69
+ controller_class_path,
70
+ "#{controller_file_name}_controller_test.rb")
71
+
72
+ m.template 'helper.rb',
73
+ File.join('app/helpers',
74
+ controller_class_path,
75
+ "#{controller_file_name}_helper.rb")
76
+
77
+ # Layout and stylesheet.
78
+ m.template 'layout.rhtml', "app/views/layouts/#{controller_file_name}.rhtml"
79
+ m.template 'style.css', 'public/stylesheets/ajax_scaffold.css'
80
+ m.template 'script.js', 'public/javascripts/ajax_scaffold.js'
81
+ m.template 'rico_corner.js', 'public/javascripts/rico_corner.js'
82
+ m.template 'indicator.gif', 'public/images/indicator.gif'
83
+ m.template 'add.gif', 'public/images/add.gif'
84
+ m.template 'error.gif', 'public/images/error.gif'
85
+
86
+ # Unscaffolded views.
87
+ unscaffolded_actions.each do |action|
88
+ path = File.join('app/views',
89
+ controller_class_path,
90
+ controller_file_name,
91
+ "#{action}.rhtml")
92
+
93
+ m.template 'controller:view.rhtml', path,
94
+ :assigns => { :action => action, :path => path }
95
+ end
96
+ end
97
+ end
98
+
99
+ protected
100
+ # Override with your own usage banner.
101
+ def banner
102
+ "Usage: #{$0} ajax_scaffold ModelName [ControllerName] [action, ...]"
103
+ end
104
+
105
+ def scaffold_views
106
+ %w( list edit new index )
107
+ end
108
+
109
+ def create_sandbox
110
+ sandbox = AjaxScaffoldingSandbox.new
111
+ sandbox.singular_name = singular_name
112
+ begin
113
+ sandbox.model_instance = model_instance
114
+ sandbox.instance_variable_set("@#{singular_name}", sandbox.model_instance)
115
+ rescue ActiveRecord::StatementInvalid => e
116
+ logger.error "Before updating scaffolding from new DB schema, try creating a table for your model (#{class_name})"
117
+ raise SystemExit
118
+ end
119
+ sandbox.suffix = suffix
120
+ sandbox
121
+ end
122
+
123
+ end
data/templates/add.gif ADDED
Binary file
@@ -1,49 +1,61 @@
1
- class <%= controller_class_name %>Controller < ApplicationController
2
- verify :method => :post, :only => %w( destroy edit new )
3
-
4
- <% unless suffix -%>
5
- def index
6
- list
7
- render :action => 'list'
8
- end
9
- <% end -%>
10
-
11
- <% for action in unscaffolded_actions -%>
12
- def <%= action %><%= suffix %>
13
- end
14
-
15
- <% end -%>
16
- def list<%= suffix %>
17
- @<%= plural_name %> = <%= model_name %>.find :all
18
- end
19
-
20
- def new<%= suffix %>
21
- @<%= singular_name %> = <%= model_name %>.new params[:<%= singular_name %>]
22
-
23
- if @<%= singular_name %>.save
24
- @headers['<%= model_name %>ID'] = @<%= singular_name %>.id
25
- @headers['Content-Type'] = 'text/html; charset=utf-8'
26
-
27
- render :partial => 'item'
28
- else
29
- render :partial => 'error', :status => 500
30
- end
31
- end
32
-
33
- def edit<%= suffix %>
34
- @<%= singular_name %> = <%= model_name %>.find params[:id]
35
-
36
- if @<%= singular_name %>.update_attributes(params[:<%= singular_name %>])
37
- @headers['Content-Type'] = 'text/html; charset=utf-8'
38
-
39
- render :partial => 'item'
40
- else
41
- render :partial => 'error', :status => 500
42
- end
43
- end
44
-
45
- def destroy<%= suffix %>
46
- <%= model_name %>.find(params[:id]).destroy
47
- render :nothing => true
48
- end
49
- end
1
+ class <%= controller_class_name %>Controller < ApplicationController
2
+
3
+ <% unless suffix -%>
4
+ def index
5
+
6
+ end
7
+ <% end -%>
8
+
9
+ <% for action in unscaffolded_actions -%>
10
+ def <%= action %><%= suffix %>
11
+ end
12
+ <% end -%>
13
+
14
+ def list<%= suffix %>
15
+ @<%= plural_name %> = <%= model_name %>.find :all
16
+ render :layout => false
17
+ end
18
+
19
+ def new<%= suffix %>
20
+ @<%= singular_name %> = <%= model_name %>.new
21
+
22
+ @temp_id = Time.new.to_i
23
+ @headers['<%= singular_name %>-id'] = @temp_id
24
+ @headers['Content-Type'] = 'text/html; charset=utf-8'
25
+
26
+ render :layout => false
27
+
28
+ # If you want to send an error message:
29
+ # render :inline => "Error text goes here", :layout => false, :status => 500
30
+ end
31
+
32
+ def create<%= suffix %>
33
+ @<%= singular_name %> = <%= model_name %>.new(params[:<%= singular_name %>])
34
+ if @<%= singular_name %>.save
35
+ @headers['<%= singular_name %>-id'] = @<%= singular_name %>.id
36
+ @headers['Content-Type'] = 'text/html; charset=utf-8'
37
+ render :partial => '<%= singular_name %><%= suffix %>', :layout => false
38
+ else
39
+ render :partial => 'form_errors', :layout => false, :status => 500
40
+ end
41
+ end
42
+
43
+ def edit<%= suffix %>
44
+ @<%= singular_name %> = <%= model_name %>.find(params[:id])
45
+ render :layout => false
46
+ end
47
+
48
+ def update
49
+ @<%= singular_name %> = <%= model_name %>.find(params[:id])
50
+ if @<%= singular_name %>.update_attributes(params[:<%= singular_name %>])
51
+ render :partial => '<%= singular_name %><%= suffix %>', :layout => false
52
+ else
53
+ render :partial => 'form_errors', :layout => false, :status => 500
54
+ end
55
+ end
56
+
57
+ def destroy<%= suffix %>
58
+ <%= model_name %>.find(params[:id]).destroy
59
+ render :nothing => true
60
+ end
61
+ end
Binary file
data/templates/form.rhtml CHANGED
@@ -1 +1,5 @@
1
- <%= template_for_inclusion %>
1
+ <fieldset>
2
+ <div class="row">
3
+ <%= template_for_inclusion %>
4
+ </div>
5
+ </fieldset>