grimen-dry_scaffold 0.1.2 → 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/README.textile +81 -21
- data/TODO.textile +5 -5
- data/generators/dmodel/dmodel_generator.rb +15 -0
- data/generators/dry_model/USAGE +8 -0
- data/generators/dry_model/dry_model_generator.rb +201 -0
- data/generators/dry_model/templates/factories_factory_girl.rb +5 -0
- data/generators/dry_model/templates/factories_machinist.rb +9 -0
- data/generators/dry_model/templates/fixtures_standard.yml +6 -0
- data/generators/dry_model/templates/migration_standard.rb +23 -0
- data/generators/dry_model/templates/model_standard.rb +15 -0
- data/generators/dry_model/templates/unit_test_standard.rb +9 -0
- data/generators/dry_scaffold/USAGE +1 -1
- data/generators/dry_scaffold/dry_scaffold_generator.rb +72 -29
- data/generators/dry_scaffold/templates/controller_inherited_resources.rb +1 -0
- data/generators/dry_scaffold/templates/controller_standard.rb +1 -0
- data/generators/dry_scaffold/templates/{controller_test_standard.rb → functional_test_standard.rb} +0 -0
- data/generators/dry_scaffold/templates/view_show.html.haml +1 -1
- metadata +14 -10
data/README.textile
CHANGED
@@ -6,12 +6,13 @@ h2. Description
|
|
6
6
|
|
7
7
|
DryScaffold is a replacement for the Rails scaffold generator that generates code that most people end up deleting or rewriting anyway because of the unusable code. The scaffold concept is powerful, but it has more potential than generating messy and almost useless code. The goal with DryScaffold is to generate DRY, beautiful, and standards compliant code based on common patterns without adding a lot of magic.
|
8
8
|
|
9
|
-
|
9
|
+
h3. Key concepts:
|
10
10
|
|
11
|
-
* Controllers that are DRY, RESTful, no code-smells, following conventions, and implementing VERY common patterns
|
12
|
-
* Views that are DRY, semantic, standards compliant, valid, and more useful in development
|
13
|
-
*
|
14
|
-
*
|
11
|
+
* Controllers that are DRY, RESTful, no code-smells, following conventions, and implementing VERY common patterns.
|
12
|
+
* Views that are DRY, semantic, standards compliant, valid, and more useful in development.
|
13
|
+
* Factories instead of fixtures.
|
14
|
+
* Generator that gets smart with additional arguments - but not stupid without them.
|
15
|
+
* Any Rails developer should be able to switch generator with no effort - follow current conventions, but extend them.
|
15
16
|
|
16
17
|
h2. Dependencies
|
17
18
|
|
@@ -21,10 +22,18 @@ h3. Required:
|
|
21
22
|
|
22
23
|
h3. Optional:
|
23
24
|
|
25
|
+
h4. Controllers/Views
|
26
|
+
|
24
27
|
* "*will_paginate*":http://github.com/mislav/will_paginate - Pagination
|
25
28
|
* "*formtastic*":http://github.com/justinfrench/formtastic - DRY and semantic forms
|
26
29
|
* "*inherited_resources*":http://github.com/josevalim/inherited_resources - DRY/Resourceful controllers
|
27
30
|
|
31
|
+
h4. Models
|
32
|
+
|
33
|
+
* "*factory_girl*":http://github.com/thoughtbot/factory_girl - Fixture-replacement
|
34
|
+
* "*machinist*":http://github.com/notahat/machinist - Fixture-replacement
|
35
|
+
* "*object_daddy*":http://github.com/flogic/object_daddy - Fixture-replacement
|
36
|
+
|
28
37
|
h2. Features
|
29
38
|
|
30
39
|
h3. Overview
|
@@ -94,7 +103,7 @@ Find out more about *inherited_resources*: "http://github.com/josevalim/inherite
|
|
94
103
|
|
95
104
|
h3. Pagination
|
96
105
|
|
97
|
-
Pagination is such a common feature that always seems to be implemented anyway, so
|
106
|
+
Pagination is such a common feature that always seems to be implemented anyway, so DryScaffold will generate a DRY solution for this in each controller that you can tweak - even thought that will not be needed in most cases. See DRY Patterns beneath for more details how it's done. Pagination - using WillPaginate - can be turned off.
|
98
107
|
|
99
108
|
Find out more about *will_paginate*: "http://github.com/mislav/will_paginate":http://github.com/mislav/will_paginate
|
100
109
|
|
@@ -102,11 +111,13 @@ h3. DRY Patterns
|
|
102
111
|
|
103
112
|
Either if you choosing default or resourceful controllers, this pattern is used to DRYing up controllers a bit more, and at the same time loading resources in the same place using before_filter while adding automatic pagination for collections.
|
104
113
|
|
105
|
-
h4. Default (ActionController
|
114
|
+
h4. Default (ActionController with pagination - not using InheritedResources)
|
106
115
|
|
107
116
|
<pre>before_filter :load_resource, :only => [:show, :edit, :update, :destroy]
|
108
117
|
before_filter :load_and_paginate_resources, :only => [:index]
|
109
118
|
|
119
|
+
...
|
120
|
+
|
110
121
|
protected
|
111
122
|
|
112
123
|
def collection
|
@@ -122,7 +133,7 @@ protected
|
|
122
133
|
end
|
123
134
|
alias :load_resource :resource</pre>
|
124
135
|
|
125
|
-
h4. Resourceful (InheritedResources)
|
136
|
+
h4. Resourceful (InheritedResources with pagination)
|
126
137
|
|
127
138
|
<pre>protected
|
128
139
|
|
@@ -139,7 +150,7 @@ h4. Resourceful (InheritedResources)
|
|
139
150
|
|
140
151
|
h3. View Partials
|
141
152
|
|
142
|
-
A very common pattern is to break up views in partials,
|
153
|
+
A very common pattern is to break up views in partials, which is also what DryScaffold does:
|
143
154
|
|
144
155
|
* new/edit => _form
|
145
156
|
* index => _item
|
@@ -148,8 +159,8 @@ h3. More To Come... (TODO)
|
|
148
159
|
|
149
160
|
These are things I have in mind:
|
150
161
|
|
151
|
-
* Generation of
|
152
|
-
*
|
162
|
+
* Generation of Rails builder stubs automatically for formats atom/rss.
|
163
|
+
* Read default scaffold options for current Rails porject from a config (say "config/scaffold.yml")
|
153
164
|
* Break up into multiple generators as dependencies (like Rails generators): dry_controller, dry_views, dry_scaffold, ...
|
154
165
|
|
155
166
|
h2. Setup
|
@@ -179,14 +190,24 @@ h3. 3. Install Dependencies (Partly optional)
|
|
179
190
|
|
180
191
|
Install dependencies to release the full power of dry_scaffold. Only HAML is really required of these, but how could anyone resist candy? =)
|
181
192
|
|
182
|
-
h4.
|
193
|
+
h4. Express
|
194
|
+
|
195
|
+
For us lazy ones... =)
|
196
|
+
|
197
|
+
<pre>rake dry_scaffold:setup</pre>
|
198
|
+
|
199
|
+
Will install the dependencies, initialize HAML within current Rails project if not already done, and automatically referencing the dependency gems within the current Rails project environment config if they are not already in there (note: nothing will be overwritten).
|
200
|
+
|
201
|
+
h4. Installation
|
202
|
+
|
203
|
+
Get the gems...
|
183
204
|
|
184
205
|
<pre>sudo gem install haml</pre>
|
185
206
|
<pre>sudo gem install will_paginate</pre>
|
186
207
|
<pre>sudo gem install justinfrench-formtastic</pre>
|
187
208
|
<pre>sudo gem install josevalim-inherited_resources</pre>
|
188
209
|
|
189
|
-
h4. Configuration
|
210
|
+
h4. B2. Configuration
|
190
211
|
|
191
212
|
In: @config/environment.rb@
|
192
213
|
|
@@ -199,21 +220,31 @@ h2. Usage
|
|
199
220
|
|
200
221
|
<pre>./script/generate dry_scaffold ModelName [attribute:type attribute:type] [_actions:new,create,...] [_formats:html,json,...] [--skip-pagination] [--skip-resourceful] [--skip-formtastic] [--skip-views] [--skip-helpers] [--skip-tests] [--include-layout]</pre>
|
201
222
|
|
223
|
+
...or use the alias (instead of @dry_scaffold@):
|
224
|
+
|
225
|
+
<pre>./script/generate dscaffold ...</pre>
|
226
|
+
|
202
227
|
h3. Model Name
|
203
228
|
|
204
|
-
Example:
|
229
|
+
Example:
|
230
|
+
|
231
|
+
<pre>Duck</pre>
|
205
232
|
|
206
233
|
Same as in the default scaffold/model generator; the name of a new/existing model.
|
207
234
|
|
208
235
|
h3. Model Attributes
|
209
236
|
|
210
|
-
Example:
|
237
|
+
Example:
|
238
|
+
|
239
|
+
<pre>name:string about:text ...</pre>
|
211
240
|
|
212
241
|
Same as in the default scaffold/model generator; model attributes and database migration column types.
|
213
242
|
|
214
243
|
h3. Controller Actions
|
215
244
|
|
216
|
-
Usage:
|
245
|
+
Usage:
|
246
|
+
|
247
|
+
<pre>_actions:new,create,quack,index,...</pre>
|
217
248
|
|
218
249
|
You can override what actions that should be generated directly - including custom actions.
|
219
250
|
|
@@ -233,13 +264,20 @@ If no actions are specified, the following REST-actions will be generated by def
|
|
233
264
|
|
234
265
|
Default controller action stubs, controller action test stubs, and corresponding views (and required partials), are generated for all of these actions.
|
235
266
|
|
267
|
+
These default actions can also be included using the alias symbol @*@, which makes it easier to add new actions without having to specify all the default actions explicit. Example:
|
268
|
+
|
269
|
+
<pre>_actions:quack # => quack</pre>
|
270
|
+
<pre>_actions:*,quack # => show,index,new,edit,create,update,destroy,quack</pre>
|
271
|
+
|
236
272
|
h4. Custom actions
|
237
273
|
|
238
274
|
The above REST actions are in many RESTful applications the only ones needed. Any other specified actions will generate empty action function stubs for manual implementation. No views will be generated for custom actions.
|
239
275
|
|
240
276
|
h3. Controller Formats
|
241
277
|
|
242
|
-
Usage:
|
278
|
+
Usage:
|
279
|
+
|
280
|
+
<pre>_formats:html,xml,txt,... # <=> _respond_to:html,xml,txt,...</pre>
|
243
281
|
|
244
282
|
You can override what respond_to-formats that should be generated directly - only for REST-actions right now because I tried to avoid bad assumptions.
|
245
283
|
|
@@ -256,6 +294,11 @@ If no formats are specified, the following formats will be generated by default:
|
|
256
294
|
|
257
295
|
Default respond block stubs are generated for all of these formats - for each generated REST-action.
|
258
296
|
|
297
|
+
Like for actions, these default respond_to-formats can also be included using the alias symbol @*@, which makes it easier to add new formats without having to specify all the default formats explicit. Example:
|
298
|
+
|
299
|
+
<pre>_formats:iphone # => _formats:iphone</pre>
|
300
|
+
<pre>_formats:*,iphone # => _formats:html,js,xml,json,iphone</pre>
|
301
|
+
|
259
302
|
h4. Additional formats
|
260
303
|
|
261
304
|
Also, default respond block stubs are generated for any of these formats - for each generated REST-action - if they are specified:
|
@@ -263,13 +306,19 @@ Also, default respond block stubs are generated for any of these formats - for e
|
|
263
306
|
* @yaml@ or @yml@ => Render: resource.to_yaml
|
264
307
|
* @txt@ or @text@ => Render: resource.to_s
|
265
308
|
|
309
|
+
NOTE: Only for Non-InheritedResources controllers for now.
|
310
|
+
|
266
311
|
h4. Custom formats
|
267
312
|
|
268
313
|
The above formats are the most commonly used ones, and respond blocks are already implemented using Rails default dependencies. Any other specified formats (such as PDF, CSV, etc.) will generate empty respond block stubs for manual implementation with help of additional dependencies.
|
269
314
|
|
270
315
|
h3. Options/Flags
|
271
316
|
|
272
|
-
Example:
|
317
|
+
Example:
|
318
|
+
|
319
|
+
<pre>--skip-resourceful --layout</pre>
|
320
|
+
|
321
|
+
h4. General
|
273
322
|
|
274
323
|
* @--skip-pagination@ - Don't generate pagination of collections in controller and views, i.e. don't use *will_paginate*.
|
275
324
|
* @--skip-resourceful@ - Don't generate resourceful controller, i.e. don't use *inherited_resources*.
|
@@ -277,20 +326,31 @@ Example: <pre>--skip-resourceful --include-layout</pre>
|
|
277
326
|
* @--skip-views@ - Don't generate views.
|
278
327
|
* @--skip-helpers@ - Don't generate helpers.
|
279
328
|
* @--skip-tests@ - Don't generate tests (functional/unit/...).
|
280
|
-
* @--
|
329
|
+
* @--layout@ - Generate layout.
|
330
|
+
|
331
|
+
h4. Model-specific
|
332
|
+
|
333
|
+
* @--fixtures@ - Generate fixtures.
|
334
|
+
* @--fgirl@ - Generate *factory_girl* factories.
|
335
|
+
* @--machinist@ - Generate *machinist* blueprints (factories).
|
336
|
+
* @--odaddy@ - Generate *object_daddy* generator/factory methods.
|
337
|
+
* @--skip-timestamps@ - Don't add timestamps to the migration file.
|
338
|
+
* @--skip-migration@ - Skip generation of migration file.
|
281
339
|
|
282
340
|
As DryScaffold is built upon Rails generator, the default generator options is available as well. For more details; see Rails documentation.
|
283
341
|
|
284
342
|
h2. Examples
|
285
343
|
|
286
|
-
No need for more samples here, just create a new Rails project, install
|
344
|
+
No need for more samples here, just create a new Rails project, install DryScaffold and it's dependencies, and try it out!
|
287
345
|
|
288
346
|
For your inspiration, you could try the following:
|
289
347
|
|
290
348
|
<pre>./script/generate dry_scaffold Zebra name:string about:text --skip-resourceful
|
291
349
|
./script/generate dry_scaffold Kangaroo name:string about:text --skip-formtastic
|
292
350
|
./script/generate dry_scaffold Duck name:string about:text _actions:new,create,index,quack
|
293
|
-
./script/generate dry_scaffold Parrot name:string about:text _formats:html,xml,yml
|
351
|
+
./script/generate dry_scaffold Parrot name:string about:text _formats:html,xml,yml
|
352
|
+
./script/generate dry_scaffold Parrot name:string about:text _indexes:name --fgirl
|
353
|
+
./script/generate dry_scaffold Parrot name:string about:text _indexes:name,name+about --fixtures</pre>
|
294
354
|
|
295
355
|
...or just go crazy!
|
296
356
|
|
data/TODO.textile
CHANGED
@@ -2,11 +2,11 @@ h1. TODO
|
|
2
2
|
|
3
3
|
h2. Next
|
4
4
|
|
5
|
-
*
|
6
|
-
*
|
7
|
-
* Optionally generate builders (Atom/RSS/...)
|
5
|
+
* Feature: Generate Atom/RSS builder stubs for formats - if specified - atom/rss for index action. (Reference: http://gatezero.org/~tim/2008/04/22/generating-valid-atom-feeds-from-rails-202/)
|
6
|
+
* Feature: Read default scaffold options from a config in: "RAILS_ROOT/config/scaffold.yml"
|
8
7
|
|
9
8
|
h2. Maybe later
|
10
9
|
|
11
|
-
* Comment the code a bit more =)
|
12
|
-
* Check for overridden templates in: RAILS_ROOT/lib/dry_scaffold/templates/
|
10
|
+
* Clean-up: Comment the code a bit more =)
|
11
|
+
* Feature: Check for overridden templates in: RAILS_ROOT/lib/dry_scaffold/templates/
|
12
|
+
* Refactor: Break up in different generators: dry_controller, dry_views, etc., and use as dependencies in dry_scaffold
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'dry_model', 'dry_model_generator')
|
2
|
+
|
3
|
+
class DmodelGenerator < DryModelGenerator
|
4
|
+
|
5
|
+
def initialize(runtime_args, runtime_options = {})
|
6
|
+
super
|
7
|
+
# Make Rails look for templates within generator "dry_model" path
|
8
|
+
@source_root = options[:source] || File.join(spec.path, '..', 'dry_model', 'templates')
|
9
|
+
end
|
10
|
+
|
11
|
+
def usage_message
|
12
|
+
File.read(File.join(spec.path, '..', 'dry_model', 'USAGE')) rescue ''
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
NAME
|
2
|
+
dry_model a.k.a. dmodel - A Rails scaffold generator that generates DRYer, cleaner, and more useful code - for models.
|
3
|
+
|
4
|
+
DESCRIPTION
|
5
|
+
A replacement for the Rails model generator - a part of the dry_generator toolset.
|
6
|
+
|
7
|
+
EXAMPLE
|
8
|
+
./script/generate dry_model ModelName [attribute:type attribute:type] [_index:name,owner_id+owner_type,active,...] [--fixtures] [--fgirl] [--machinist] [--odaddy] [--skip-timestamps] [--skip-migration] [--skip-tests]
|
@@ -0,0 +1,201 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
begin
|
3
|
+
require 'factory_girl'
|
4
|
+
FACTORY_GIRL = true
|
5
|
+
rescue MissingSourceFile
|
6
|
+
FACTORY_GIRL = false
|
7
|
+
end
|
8
|
+
begin
|
9
|
+
require 'machinist'
|
10
|
+
MACHINIST = true
|
11
|
+
rescue MissingSourceFile
|
12
|
+
MACHINIST = false
|
13
|
+
end
|
14
|
+
begin
|
15
|
+
require 'object_daddy'
|
16
|
+
OBJECT_DADDY = true
|
17
|
+
rescue MissingSourceFile
|
18
|
+
OBJECT_DADDY = false
|
19
|
+
end
|
20
|
+
|
21
|
+
class DryModelGenerator < Rails::Generator::NamedBase
|
22
|
+
|
23
|
+
MODELS_PATH = File.join('app', 'models').freeze
|
24
|
+
MIGRATIONS_PATH = File.join('db', 'migrate').freeze
|
25
|
+
TESTS_PATH = File.join('test').freeze
|
26
|
+
UNIT_TESTS_PATH = File.join(TESTS_PATH, 'unit').freeze
|
27
|
+
FIXTURES_PATH = File.join(TESTS_PATH, 'fixtures').freeze
|
28
|
+
FACTORY_GIRL_FACTORIES_PATH = File.join(TESTS_PATH, 'factories').freeze
|
29
|
+
MACHINIST_FACTORIES_PATH = File.join(TESTS_PATH, 'blueprints').freeze
|
30
|
+
|
31
|
+
NON_ATTR_ARG_KEY_PREFIX = '_'.freeze
|
32
|
+
|
33
|
+
default_options :fixtures => false,
|
34
|
+
:factory_girl => false,
|
35
|
+
:machinist => false,
|
36
|
+
:object_daddy => false,
|
37
|
+
:faker => false,
|
38
|
+
:skip_timestamps => false,
|
39
|
+
:skip_migration => false,
|
40
|
+
:skip_tests => false
|
41
|
+
|
42
|
+
attr_reader :indexes,
|
43
|
+
:references
|
44
|
+
|
45
|
+
def initialize(runtime_args, runtime_options = {})
|
46
|
+
super
|
47
|
+
|
48
|
+
@attributes ||= []
|
49
|
+
args_for_model = []
|
50
|
+
|
51
|
+
@args.each do |arg|
|
52
|
+
arg_entities = arg.split(':')
|
53
|
+
if arg =~ /^#{NON_ATTR_ARG_KEY_PREFIX}/
|
54
|
+
if arg =~ /^#{NON_ATTR_ARG_KEY_PREFIX}index/
|
55
|
+
arg_indexes = arg_entities[1].split(',').compact.uniq
|
56
|
+
@indexes = arg_indexes.collect do |index|
|
57
|
+
if index =~ /\+/
|
58
|
+
index.split('+').collect { |i| i.downcase.to_sym }
|
59
|
+
else
|
60
|
+
index.downcase.to_sym
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
else
|
65
|
+
@attributes << Rails::Generator::GeneratedAttribute.new(*arg_entities)
|
66
|
+
args_for_model << arg
|
67
|
+
end
|
68
|
+
end
|
69
|
+
@args = args_for_model
|
70
|
+
@references = attributes.select(&:reference?)
|
71
|
+
end
|
72
|
+
|
73
|
+
def manifest
|
74
|
+
record do |m|
|
75
|
+
# Check for class naming collisions.
|
76
|
+
m.class_collisions class_name, "#{class_name}"
|
77
|
+
m.class_collisions class_name, "#{class_name}Test"
|
78
|
+
|
79
|
+
# Directories.
|
80
|
+
m.directory File.join(MODELS_PATH, class_path)
|
81
|
+
m.directory File.join(UNIT_TESTS_PATH, class_path) unless options[:skip_tests]
|
82
|
+
m.directory File.join(FIXTURES_PATH, class_path) if options[:fixtures]
|
83
|
+
m.directory File.join(FACTORY_GIRL_FACTORIES_PATH, class_path) if options[:factory_girl]
|
84
|
+
m.directory File.join(MACHINIST_FACTORIES_PATH, class_path) if options[:machinist]
|
85
|
+
|
86
|
+
# Model Class + Unit Test.
|
87
|
+
m.template 'model_standard.rb', File.join(MODELS_PATH, class_path, "#{file_name}.rb")
|
88
|
+
unless options[:skip_tests]
|
89
|
+
m.template 'unit_test_standard.rb',
|
90
|
+
File.join(UNIT_TESTS_PATH, class_path, "#{file_name}_test.rb")
|
91
|
+
end
|
92
|
+
|
93
|
+
# Fixtures/Factories.
|
94
|
+
if options[:fixtures]
|
95
|
+
m.template 'fixtures_standard.yml',
|
96
|
+
File.join(FIXTURES_PATH, "#{file_name}.yml")
|
97
|
+
end
|
98
|
+
if options[:factory_girl]
|
99
|
+
m.template 'factories_factory_girl.rb',
|
100
|
+
File.join(FACTORY_GIRL_FACTORIES_PATH, "#{file_name}.rb")
|
101
|
+
end
|
102
|
+
if options[:machinist]
|
103
|
+
m.template 'factories_machinist.rb',
|
104
|
+
File.join(MACHINIST_FACTORIES_PATH, "#{file_name}.rb")
|
105
|
+
end
|
106
|
+
# NOTE: :object_daddy handled in model
|
107
|
+
|
108
|
+
# Migration.
|
109
|
+
unless options[:skip_migration]
|
110
|
+
m.migration_template 'migration_standard.rb', MIGRATIONS_PATH,
|
111
|
+
:assigns => {:migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}"},
|
112
|
+
:migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
protected
|
118
|
+
|
119
|
+
def add_options!(opt)
|
120
|
+
opt.separator ''
|
121
|
+
opt.separator 'Options:'
|
122
|
+
|
123
|
+
opt.on("--fixtures", "Generate fixtures.") do |v|
|
124
|
+
options[:fixtures] = v
|
125
|
+
end
|
126
|
+
|
127
|
+
opt.on("--fgirl", "Generate \"factory_girl\" factories.") do |v|
|
128
|
+
options[:factory_girl] = v
|
129
|
+
end
|
130
|
+
|
131
|
+
opt.on("--machinist", "Generate \"machinist\" blueprints (factories).") do |v|
|
132
|
+
options[:machinist] = v
|
133
|
+
end
|
134
|
+
|
135
|
+
opt.on("--odaddy", "Generate \"object_daddy\" generator/factory methods.") do |v|
|
136
|
+
options[:object_daddy] = v
|
137
|
+
end
|
138
|
+
|
139
|
+
opt.on("--skip-timestamps", "Don't add timestamps to the migration file.") do |v|
|
140
|
+
options[:skip_timestamps] = v
|
141
|
+
end
|
142
|
+
|
143
|
+
opt.on("--skip-migration", "Skip generation of migration file.") do |v|
|
144
|
+
options[:skip_migration] = v
|
145
|
+
end
|
146
|
+
|
147
|
+
opt.on("--skip-tests", "Skip generation of tests.") do |v|
|
148
|
+
options[:skip_tests] = v
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def banner
|
153
|
+
["Usage: #{$0} #{spec.name} ModelName",
|
154
|
+
"[field:type field:type]",
|
155
|
+
"[_indexes:name,owner_id+owner_type,active,...]",
|
156
|
+
"[--fixtures]",
|
157
|
+
"[--fgirl]",
|
158
|
+
"[--machinist]",
|
159
|
+
"[--odaddy]",
|
160
|
+
"[--skip_timestamps]",
|
161
|
+
"[--skip-migration]",
|
162
|
+
"[--skip-tests]"
|
163
|
+
].join(' ')
|
164
|
+
end
|
165
|
+
|
166
|
+
end
|
167
|
+
|
168
|
+
module Rails
|
169
|
+
module Generator
|
170
|
+
class GeneratedAttribute
|
171
|
+
def default_for_fixture
|
172
|
+
@default ||= case type
|
173
|
+
when :integer then 1
|
174
|
+
when :float then 1.5
|
175
|
+
when :decimal then '9.99'
|
176
|
+
when :datetime, :timestamp, :time then Time.now.to_s(:db)
|
177
|
+
when :date then Date.today.to_s(:db)
|
178
|
+
when :string then 'Hello'
|
179
|
+
when :text then 'Lorem ipsum dolor sit amet...'
|
180
|
+
when :boolean then false
|
181
|
+
else
|
182
|
+
''
|
183
|
+
end
|
184
|
+
end
|
185
|
+
def default_for_factory
|
186
|
+
@default ||= case type
|
187
|
+
when :integer then 1
|
188
|
+
when :float then 1.5
|
189
|
+
when :decimal then '9.99'
|
190
|
+
when :datetime, :timestamp, :time then 'Time.now'
|
191
|
+
when :date then 'Date.today'
|
192
|
+
when :string then '"Hello"'
|
193
|
+
when :text then '"Lorem ipsum dolor sit amet..."'
|
194
|
+
when :boolean then false
|
195
|
+
else
|
196
|
+
''
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class <%= migration_name %> < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :<%= table_name %>, :force => true do |t|
|
4
|
+
<% attributes.each do |attribute| -%>
|
5
|
+
t.<%= attribute.type %> :<%= attribute.name %>
|
6
|
+
<% end -%>
|
7
|
+
|
8
|
+
<% unless options[:skip_timestamps] -%>
|
9
|
+
t.timestamps
|
10
|
+
<% end -%>
|
11
|
+
end
|
12
|
+
<% unless indexes.blank? -%>
|
13
|
+
|
14
|
+
<% indexes.each do |index| -%>
|
15
|
+
add_index :<%= table_name %>, <%= index.is_a?(Array) ? "[:#{index.join(', :')}]" : ":#{index}" %>
|
16
|
+
<% end -%>
|
17
|
+
<% end -%>
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.down
|
21
|
+
drop_table :<%= table_name %>
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class <%= class_name %> < ActiveRecord::Base
|
2
|
+
|
3
|
+
<% unless references.empty? -%>
|
4
|
+
<% references.each do |attribute| -%>
|
5
|
+
belongs_to :<%= attribute.name %>
|
6
|
+
<% end -%>
|
7
|
+
|
8
|
+
<% end -%>
|
9
|
+
<% if options[:object_daddy] -%>
|
10
|
+
<% attributes.each do |attribute| -%>
|
11
|
+
generator_for(:<%= attribute.name %>) { <%= attribute.default_for_factory %> }
|
12
|
+
<% end -%>
|
13
|
+
|
14
|
+
<% end -%>
|
15
|
+
end
|
@@ -9,4 +9,4 @@ EXAMPLE
|
|
9
9
|
|
10
10
|
...or the shortcut version:
|
11
11
|
|
12
|
-
./script/generate dscaffold ModelName [attribute:type attribute:type] [_actions:new,create,...] [_formats:html,json,...] [--skip-pagination] [--skip-resourceful] [--skip-formtastic] [--skip-views] [--skip-helpers] [--skip-tests] [--
|
12
|
+
./script/generate dscaffold ModelName [attribute:type attribute:type] [_actions:new,create,...] [_formats:html,json,...] [--skip-pagination] [--skip-resourceful] [--skip-formtastic] [--skip-views] [--skip-helpers] [--skip-tests] [--layout]
|
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'rubygems'
|
2
|
-
|
3
2
|
begin
|
4
3
|
require 'formtastic'
|
5
4
|
FORMTASTIC = true
|
@@ -41,9 +40,7 @@ class DryScaffoldGenerator < Rails::Generator::NamedBase
|
|
41
40
|
RESOURCEFUL_COLLECTION_NAME = 'collection'.freeze
|
42
41
|
RESOURCEFUL_SINGULAR_NAME = 'resource'.freeze
|
43
42
|
|
44
|
-
ARG_KEY_VALUE_DIVIDER = ':'.freeze
|
45
43
|
NON_ATTR_ARG_KEY_PREFIX = '_'.freeze
|
46
|
-
NON_ATTR_ARG_VALUE_DIVIDER = ','.freeze
|
47
44
|
|
48
45
|
# :{action} => [:{partial}, ...]
|
49
46
|
VIEW_TEMPLATES = {
|
@@ -99,20 +96,28 @@ class DryScaffoldGenerator < Rails::Generator::NamedBase
|
|
99
96
|
@attributes ||= []
|
100
97
|
@args_for_model ||= []
|
101
98
|
|
102
|
-
# Non-attribute args, i.e. "_actions:new,create".
|
99
|
+
# Non-attribute args, i.e. "_actions:new,create".
|
103
100
|
@args.each do |arg|
|
104
|
-
arg_entities = arg.split(
|
105
|
-
if arg =~ /^#{NON_ATTR_ARG_KEY_PREFIX}
|
106
|
-
|
107
|
-
|
108
|
-
|
101
|
+
arg_entities = arg.split(':')
|
102
|
+
if arg =~ /^#{NON_ATTR_ARG_KEY_PREFIX}/
|
103
|
+
if arg =~ /^#{NON_ATTR_ARG_KEY_PREFIX}action/
|
104
|
+
# Replace a '*' with default actions
|
105
|
+
arg_entities[1].gsub!(/\*/, DEFAULT_CONTROLLER_ACTIONS.join(','))
|
106
|
+
arg_actions = arg_entities[1].split(',').compact.uniq
|
107
|
+
@actions = arg_actions.collect { |action| action.downcase.to_sym }
|
108
|
+
elsif arg =~ /^#{NON_ATTR_ARG_KEY_PREFIX}(format|respond_to)/
|
109
|
+
# Replace a '*' with default respond_to-formats
|
110
|
+
arg_entities[1].gsub!(/\*/, DEFAULT_RESPOND_TO_FORMATS.join(','))
|
111
|
+
arg_formats = arg_entities[1].split(',').compact.uniq
|
112
|
+
@formats = arg_formats.collect { |format| format.downcases.to_sym }
|
113
|
+
end
|
109
114
|
else
|
110
115
|
@attributes << Rails::Generator::GeneratedAttribute.new(*arg_entities)
|
111
116
|
@args_for_model << arg
|
112
117
|
end
|
113
118
|
end
|
114
119
|
|
115
|
-
@actions ||=
|
120
|
+
@actions ||= DEFAULT_CONTROLLER_ACTIONS
|
116
121
|
@formats ||= DEFAULT_RESPOND_TO_FORMATS
|
117
122
|
end
|
118
123
|
|
@@ -137,7 +142,7 @@ class DryScaffoldGenerator < Rails::Generator::NamedBase
|
|
137
142
|
|
138
143
|
# Controller Tests.
|
139
144
|
unless options[:skip_tests]
|
140
|
-
m.template '
|
145
|
+
m.template 'functional_test_standard.rb',
|
141
146
|
File.join(FUNCTIONAL_TESTS_PATH, controller_class_path, "#{controller_file_name}_controller_test.rb")
|
142
147
|
end
|
143
148
|
|
@@ -175,7 +180,17 @@ class DryScaffoldGenerator < Rails::Generator::NamedBase
|
|
175
180
|
m.route_resources controller_file_name
|
176
181
|
|
177
182
|
# Models - use Rails default generator.
|
178
|
-
m.dependency '
|
183
|
+
m.dependency 'dry_model',
|
184
|
+
[name] + @args_for_model +
|
185
|
+
options.slice(:skip_tests,
|
186
|
+
:fixtures,
|
187
|
+
:factory_girl,
|
188
|
+
:machinist,
|
189
|
+
:object_daddy,
|
190
|
+
:skip_timestamps,
|
191
|
+
:skip_migration
|
192
|
+
),
|
193
|
+
:collision => :skip
|
179
194
|
end
|
180
195
|
end
|
181
196
|
|
@@ -213,6 +228,10 @@ class DryScaffoldGenerator < Rails::Generator::NamedBase
|
|
213
228
|
options[:formtastic] = !v
|
214
229
|
end
|
215
230
|
|
231
|
+
opt.on('--layout', "Generate layout.") do |v|
|
232
|
+
options[:include_layout] = v
|
233
|
+
end
|
234
|
+
|
216
235
|
opt.on('--skip-views', "Skip generation of views.") do |v|
|
217
236
|
options[:skip_views] = v
|
218
237
|
end
|
@@ -225,27 +244,51 @@ class DryScaffoldGenerator < Rails::Generator::NamedBase
|
|
225
244
|
options[:skip_tests] = v
|
226
245
|
end
|
227
246
|
|
228
|
-
opt.on('--
|
229
|
-
options[:
|
247
|
+
opt.on('--fixtures', "Model: Generate fixtures.") do |v|
|
248
|
+
options[:fixtures] = v
|
249
|
+
end
|
250
|
+
|
251
|
+
opt.on('--fgirl', "Model: Generate \"factory_girl\" factories.") do |v|
|
252
|
+
options[:machinist] = v
|
253
|
+
end
|
254
|
+
|
255
|
+
opt.on('--machinist', "Model: Generate \"machinist\" blueprints (factories).") do |v|
|
256
|
+
options[:factory_girl] = v
|
257
|
+
end
|
258
|
+
|
259
|
+
opt.on('--odaddy', "Model: Generate \"object_daddy\" generator/factory methods.") do |v|
|
260
|
+
options[:object_daddy] = v
|
261
|
+
end
|
262
|
+
|
263
|
+
opt.on('--skip-timestamps', "Model: Don't add timestamps to the migration file.") do |v|
|
264
|
+
options[:skip_timestamps] = v
|
265
|
+
end
|
266
|
+
|
267
|
+
opt.on('--skip-migration', "Model: Skip generation of migration file.") do |v|
|
268
|
+
options[:skip_migration] = v
|
230
269
|
end
|
231
|
-
end
|
232
|
-
|
233
|
-
def model_name
|
234
|
-
class_name.demodulize
|
235
270
|
end
|
236
271
|
|
237
272
|
def banner
|
238
|
-
"Usage: #{$0}
|
239
|
-
"
|
240
|
-
"
|
241
|
-
"
|
242
|
-
"
|
243
|
-
"
|
244
|
-
"
|
245
|
-
"
|
246
|
-
"
|
247
|
-
"
|
248
|
-
|
273
|
+
["Usage: #{$0} #{spec.name} ModelName",
|
274
|
+
"[field:type field:type ...]",
|
275
|
+
"[_actions:new,create,...]",
|
276
|
+
"[_formats:html,json,...]",
|
277
|
+
"[_indexes:field,field+field,field,...]",
|
278
|
+
"[--skip-pagination]",
|
279
|
+
"[--skip-resourceful]",
|
280
|
+
"[--skip-formtastic]",
|
281
|
+
"[--skip-views]",
|
282
|
+
"[--skip-helpers]",
|
283
|
+
"[--skip-tests]",
|
284
|
+
"[--layout]",
|
285
|
+
"[--fixtures]",
|
286
|
+
"[--factory_girl]",
|
287
|
+
"[--machinist]",
|
288
|
+
"[--object_daddy]",
|
289
|
+
"[--skip-timestamps]",
|
290
|
+
"[--skip-migration]",
|
291
|
+
].join(' ')
|
249
292
|
end
|
250
293
|
|
251
294
|
end
|
@@ -17,6 +17,7 @@ class <%= controller_class_name %>Controller < InheritedResources::Base
|
|
17
17
|
<% (actions - DryScaffoldGenerator::DEFAULT_CONTROLLER_ACTIONS).each do |action| -%>
|
18
18
|
# GET /<%= plural_name %>/<%= action.to_s %>
|
19
19
|
def <%= action.to_s %>
|
20
|
+
# TODO: Implement action "<%= plural_name %>/<%= action.to_s %>"
|
20
21
|
end
|
21
22
|
|
22
23
|
<% end -%>
|
@@ -217,6 +217,7 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
217
217
|
<% (actions - DryScaffoldGenerator::DEFAULT_CONTROLLER_ACTIONS).each do |action| -%>
|
218
218
|
# GET /<%= plural_name %>/<%= action.to_s %>
|
219
219
|
def <%= action.to_s %>
|
220
|
+
# TODO: Implement action "<%= plural_name %>/<%= action.to_s %>"
|
220
221
|
end
|
221
222
|
|
222
223
|
<% end -%>
|
data/generators/dry_scaffold/templates/{controller_test_standard.rb → functional_test_standard.rb}
RENAMED
File without changes
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grimen-dry_scaffold
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonas Grimfelt
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-05-01 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -24,17 +24,24 @@ extra_rdoc_files:
|
|
24
24
|
files:
|
25
25
|
- MIT-LICENSE
|
26
26
|
- README.textile
|
27
|
-
- TODO.textile
|
28
27
|
- Rakefile
|
29
|
-
-
|
28
|
+
- TODO.textile
|
29
|
+
- generators/dmodel/dmodel_generator.rb
|
30
|
+
- generators/dry_model/USAGE
|
31
|
+
- generators/dry_model/dry_model_generator.rb
|
32
|
+
- generators/dry_model/templates/factories_factory_girl.rb
|
33
|
+
- generators/dry_model/templates/factories_machinist.rb
|
34
|
+
- generators/dry_model/templates/fixtures_standard.yml
|
35
|
+
- generators/dry_model/templates/migration_standard.rb
|
36
|
+
- generators/dry_model/templates/model_standard.rb
|
37
|
+
- generators/dry_model/templates/unit_test_standard.rb
|
38
|
+
- generators/dry_scaffold/USAGE
|
30
39
|
- generators/dry_scaffold/dry_scaffold_generator.rb
|
31
|
-
- generators/dry_scaffold/templates
|
32
40
|
- generators/dry_scaffold/templates/controller_inherited_resources.rb
|
33
41
|
- generators/dry_scaffold/templates/controller_standard.rb
|
34
|
-
- generators/dry_scaffold/templates/
|
42
|
+
- generators/dry_scaffold/templates/functional_test_standard.rb
|
35
43
|
- generators/dry_scaffold/templates/helper_standard.rb
|
36
44
|
- generators/dry_scaffold/templates/helper_test_standard.rb
|
37
|
-
- generators/dry_scaffold/templates/prototypes
|
38
45
|
- generators/dry_scaffold/templates/prototypes/controller_inherited_resources.rb
|
39
46
|
- generators/dry_scaffold/templates/prototypes/controller_standard.rb
|
40
47
|
- generators/dry_scaffold/templates/prototypes/controller_test_standard.rb
|
@@ -54,8 +61,6 @@ files:
|
|
54
61
|
- generators/dry_scaffold/templates/view_layout.html.haml
|
55
62
|
- generators/dry_scaffold/templates/view_new.html.haml
|
56
63
|
- generators/dry_scaffold/templates/view_show.html.haml
|
57
|
-
- generators/dry_scaffold/USAGE
|
58
|
-
- generators/dscaffold
|
59
64
|
- generators/dscaffold/dscaffold_generator.rb
|
60
65
|
- rails/init.rb
|
61
66
|
- tasks/dry_scaffold.rake
|
@@ -63,7 +68,6 @@ has_rdoc: true
|
|
63
68
|
homepage: http://github.com/grimen/dry_scaffold/tree/master
|
64
69
|
post_install_message:
|
65
70
|
rdoc_options:
|
66
|
-
- --inline-source
|
67
71
|
- --charset=UTF-8
|
68
72
|
require_paths:
|
69
73
|
- lib
|