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 +20 -20
- data/{USAGE → README} +28 -31
- data/ajax_scaffold_generator.rb +123 -201
- data/templates/add.gif +0 -0
- data/templates/controller.rb +61 -49
- data/templates/error.gif +0 -0
- data/templates/form.rhtml +5 -1
- data/templates/functional_test.rb +88 -71
- data/templates/helper.rb +12 -2
- data/templates/indicator.gif +0 -0
- data/templates/layout.rhtml +15 -14
- data/templates/partial_form_errors.rhtml +9 -0
- data/templates/partial_item.rhtml +25 -16
- data/templates/rico_corner.js +781 -0
- data/templates/script.js +327 -47
- data/templates/style.css +324 -74
- data/templates/view_edit.rhtml +22 -0
- data/templates/view_index.rhtml +1 -0
- data/templates/view_list.rhtml +53 -15
- data/templates/view_new.rhtml +22 -0
- metadata +32 -39
- data/templates/partial_edit.rhtml +0 -13
- data/templates/partial_error.rhtml +0 -3
- data/templates/partial_new.rhtml +0 -13
data/MIT-LICENSE
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
-
Copyright (c)
|
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
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
+
|
data/ajax_scaffold_generator.rb
CHANGED
@@ -1,201 +1,123 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
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
|
data/templates/controller.rb
CHANGED
@@ -1,49 +1,61 @@
|
|
1
|
-
class <%= controller_class_name %>Controller < ApplicationController
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
<%
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
@<%= singular_name
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
render :partial => '
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
data/templates/error.gif
ADDED
Binary file
|
data/templates/form.rhtml
CHANGED