ryanb-nifty-generators 0.0.3 → 0.1.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/CHANGELOG +3 -0
- data/Manifest +3 -0
- data/README +22 -35
- data/TODO +1 -1
- data/lib/nifty_generators.rb +3 -0
- data/nifty-generators.gemspec +13 -14
- data/rails_generators/nifty_layout/USAGE +16 -12
- data/rails_generators/nifty_layout/nifty_layout_generator.rb +2 -2
- data/rails_generators/nifty_layout/templates/layout.html.erb +0 -1
- data/rails_generators/nifty_scaffold/USAGE +46 -3
- data/rails_generators/nifty_scaffold/nifty_scaffold_generator.rb +69 -29
- data/rails_generators/nifty_scaffold/templates/actions/create.rb +1 -0
- data/rails_generators/nifty_scaffold/templates/actions/destroy.rb +1 -0
- data/rails_generators/nifty_scaffold/templates/actions/update.rb +1 -0
- data/rails_generators/nifty_scaffold/templates/controller.rb +1 -1
- data/rails_generators/nifty_scaffold/templates/helper.rb +3 -0
- data/rails_generators/nifty_scaffold/templates/migration.rb +2 -0
- data/rails_generators/nifty_scaffold/templates/views/_form.html.erb +3 -3
- data/rails_generators/nifty_scaffold/templates/views/edit.html.erb +6 -0
- data/rails_generators/nifty_scaffold/templates/views/index.html.erb +13 -6
- data/rails_generators/nifty_scaffold/templates/views/new.html.erb +2 -0
- data/rails_generators/nifty_scaffold/templates/views/show.html.erb +7 -1
- data/tasks/deployment.rake +2 -0
- data/test/test_helper.rb +1 -1
- data/test/test_nifty_scaffold_generator.rb +121 -22
- metadata +12 -5
data/CHANGELOG
ADDED
data/Manifest
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
CHANGELOG
|
2
|
+
lib/nifty_generators.rb
|
1
3
|
LICENSE
|
2
4
|
Manifest
|
3
5
|
rails_generators/nifty_layout/nifty_layout_generator.rb
|
@@ -24,6 +26,7 @@ rails_generators/nifty_scaffold/templates/views/new.html.erb
|
|
24
26
|
rails_generators/nifty_scaffold/templates/views/show.html.erb
|
25
27
|
rails_generators/nifty_scaffold/USAGE
|
26
28
|
README
|
29
|
+
tasks/deployment.rake
|
27
30
|
test/test_helper.rb
|
28
31
|
test/test_nifty_layout_generator.rb
|
29
32
|
test/test_nifty_scaffold_generator.rb
|
data/README
CHANGED
@@ -1,49 +1,36 @@
|
|
1
1
|
= nifty-generator
|
2
2
|
|
3
|
-
|
3
|
+
A collection of useful Rails generator scripts by Ryan Bates.
|
4
4
|
|
5
|
-
==
|
5
|
+
== Install
|
6
6
|
|
7
|
-
|
7
|
+
gem install ryanb-nifty-generators --source=http://gems.github.com
|
8
|
+
|
9
|
+
== Usage
|
8
10
|
|
9
|
-
|
11
|
+
Once you install the gem, the generators will be available to all Rails
|
12
|
+
applications on your system. If you run script/generate without any
|
13
|
+
additional arguments you should see the available generators listed.
|
10
14
|
|
11
|
-
|
15
|
+
To run the generator, go to your rails project directory and call it
|
16
|
+
using the script/generate or script/destroy command.
|
12
17
|
|
13
|
-
|
18
|
+
script/generate nifty_scaffold Recipe name:string index new
|
19
|
+
|
20
|
+
== Included Generators
|
14
21
|
|
15
|
-
|
22
|
+
* nifty_layout: generates generic layout, stylesheet, and helper files.
|
23
|
+
* nifty_scaffold: generates a controller and optional model/migration.
|
16
24
|
|
17
|
-
|
25
|
+
Run the command with the --help option to learn more.
|
18
26
|
|
19
|
-
|
20
|
-
* Similar opinions as me
|
27
|
+
script/generate nifty_layout --help
|
21
28
|
|
22
|
-
==
|
29
|
+
== Development
|
23
30
|
|
24
|
-
|
31
|
+
This project can be found on github at the following URL.
|
25
32
|
|
26
|
-
|
33
|
+
http://github.com/ryanb/nifty-generators/
|
27
34
|
|
28
|
-
|
29
|
-
|
30
|
-
Copyright (c) 2008 Ryan Bates
|
31
|
-
|
32
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
33
|
-
a copy of this software and associated documentation files (the
|
34
|
-
'Software'), to deal in the Software without restriction, including
|
35
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
36
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
37
|
-
permit persons to whom the Software is furnished to do so, subject to
|
38
|
-
the following conditions:
|
39
|
-
|
40
|
-
The above copyright notice and this permission notice shall be
|
41
|
-
included in all copies or substantial portions of the Software.
|
42
|
-
|
43
|
-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
44
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
45
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
46
|
-
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
47
|
-
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
48
|
-
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
49
|
-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
35
|
+
If you wish the generators behaved differently, please consider
|
36
|
+
forking the project and modifying to your heart's content.
|
data/TODO
CHANGED
data/nifty-generators.gemspec
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
|
2
|
-
# Gem::Specification for Nifty-generators-0.0
|
2
|
+
# Gem::Specification for Nifty-generators-0.1.0
|
3
3
|
# Originally generated by Echoe
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = %q{nifty-generators}
|
7
|
-
s.version = "0.0
|
7
|
+
s.version = "0.1.0"
|
8
8
|
|
9
9
|
s.specification_version = 2 if s.respond_to? :specification_version=
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.authors = ["Ryan Bates"]
|
13
|
-
s.date = %q{2008-05-
|
14
|
-
s.description = %q{A collection of
|
15
|
-
s.email = %q{ryan
|
16
|
-
s.extra_rdoc_files = ["LICENSE", "README", "TODO"]
|
17
|
-
s.files = ["LICENSE", "Manifest", "rails_generators/nifty_layout/nifty_layout_generator.rb", "rails_generators/nifty_layout/templates/helper.rb", "rails_generators/nifty_layout/templates/layout.html.erb", "rails_generators/nifty_layout/templates/stylesheet.css", "rails_generators/nifty_layout/USAGE", "rails_generators/nifty_scaffold/nifty_scaffold_generator.rb", "rails_generators/nifty_scaffold/templates/actions/create.rb", "rails_generators/nifty_scaffold/templates/actions/destroy.rb", "rails_generators/nifty_scaffold/templates/actions/edit.rb", "rails_generators/nifty_scaffold/templates/actions/index.rb", "rails_generators/nifty_scaffold/templates/actions/new.rb", "rails_generators/nifty_scaffold/templates/actions/show.rb", "rails_generators/nifty_scaffold/templates/actions/update.rb", "rails_generators/nifty_scaffold/templates/controller.rb", "rails_generators/nifty_scaffold/templates/migration.rb", "rails_generators/nifty_scaffold/templates/model.rb", "rails_generators/nifty_scaffold/templates/views/_form.html.erb", "rails_generators/nifty_scaffold/templates/views/edit.html.erb", "rails_generators/nifty_scaffold/templates/views/index.html.erb", "rails_generators/nifty_scaffold/templates/views/new.html.erb", "rails_generators/nifty_scaffold/templates/views/show.html.erb", "rails_generators/nifty_scaffold/USAGE", "README", "test/test_helper.rb", "test/test_nifty_layout_generator.rb", "test/test_nifty_scaffold_generator.rb", "TODO", "nifty-generators.gemspec"]
|
13
|
+
s.date = %q{2008-05-08}
|
14
|
+
s.description = %q{A collection of useful generator scripts for Rails.}
|
15
|
+
s.email = %q{ryan (at) railscasts (dot) com}
|
16
|
+
s.extra_rdoc_files = ["CHANGELOG", "lib/nifty_generators.rb", "LICENSE", "README", "tasks/deployment.rake", "TODO"]
|
17
|
+
s.files = ["CHANGELOG", "lib/nifty_generators.rb", "LICENSE", "Manifest", "rails_generators/nifty_layout/nifty_layout_generator.rb", "rails_generators/nifty_layout/templates/helper.rb", "rails_generators/nifty_layout/templates/layout.html.erb", "rails_generators/nifty_layout/templates/stylesheet.css", "rails_generators/nifty_layout/USAGE", "rails_generators/nifty_scaffold/nifty_scaffold_generator.rb", "rails_generators/nifty_scaffold/templates/actions/create.rb", "rails_generators/nifty_scaffold/templates/actions/destroy.rb", "rails_generators/nifty_scaffold/templates/actions/edit.rb", "rails_generators/nifty_scaffold/templates/actions/index.rb", "rails_generators/nifty_scaffold/templates/actions/new.rb", "rails_generators/nifty_scaffold/templates/actions/show.rb", "rails_generators/nifty_scaffold/templates/actions/update.rb", "rails_generators/nifty_scaffold/templates/controller.rb", "rails_generators/nifty_scaffold/templates/helper.rb", "rails_generators/nifty_scaffold/templates/migration.rb", "rails_generators/nifty_scaffold/templates/model.rb", "rails_generators/nifty_scaffold/templates/views/_form.html.erb", "rails_generators/nifty_scaffold/templates/views/edit.html.erb", "rails_generators/nifty_scaffold/templates/views/index.html.erb", "rails_generators/nifty_scaffold/templates/views/new.html.erb", "rails_generators/nifty_scaffold/templates/views/show.html.erb", "rails_generators/nifty_scaffold/USAGE", "README", "tasks/deployment.rake", "test/test_helper.rb", "test/test_nifty_layout_generator.rb", "test/test_nifty_scaffold_generator.rb", "TODO", "nifty-generators.gemspec"]
|
18
18
|
s.has_rdoc = true
|
19
19
|
s.homepage = %q{http://github.com/ryanb/nifty-generators}
|
20
20
|
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Nifty-generators", "--main", "README"]
|
21
21
|
s.require_paths = ["lib"]
|
22
22
|
s.rubyforge_project = %q{nifty-generators}
|
23
23
|
s.rubygems_version = %q{1.0.0}
|
24
|
-
s.summary = %q{A collection of
|
24
|
+
s.summary = %q{A collection of useful generator scripts for Rails.}
|
25
25
|
s.test_files = ["test/test_helper.rb", "test/test_nifty_layout_generator.rb", "test/test_nifty_scaffold_generator.rb"]
|
26
26
|
end
|
27
27
|
|
@@ -34,12 +34,12 @@ end
|
|
34
34
|
# begin
|
35
35
|
# require 'echoe'
|
36
36
|
#
|
37
|
-
# Echoe.new('nifty-generators', '0.0
|
38
|
-
# p.summary = "A collection of
|
39
|
-
# p.description = "A collection of
|
37
|
+
# Echoe.new('nifty-generators', '0.1.0') do |p|
|
38
|
+
# p.summary = "A collection of useful generator scripts for Rails."
|
39
|
+
# p.description = "A collection of useful generator scripts for Rails."
|
40
40
|
# p.url = "http://github.com/ryanb/nifty-generators"
|
41
41
|
# p.author = 'Ryan Bates'
|
42
|
-
# p.email = "ryan
|
42
|
+
# p.email = "ryan (at) railscasts (dot) com"
|
43
43
|
# p.ignore_pattern = ["script/*", "*.gemspec"]
|
44
44
|
# end
|
45
45
|
#
|
@@ -48,5 +48,4 @@ end
|
|
48
48
|
# puts "#{boom.to_s.capitalize}."
|
49
49
|
# end
|
50
50
|
#
|
51
|
-
#
|
52
|
-
# task :build => [:build_manifest, :build_gemspec]
|
51
|
+
# Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
@@ -1,21 +1,25 @@
|
|
1
1
|
Description:
|
2
|
-
The nifty_layout generator creates a basic stylesheet and
|
3
|
-
|
4
|
-
|
2
|
+
The nifty_layout generator creates a basic layout, stylesheet and
|
3
|
+
helper which will give some structure to a starting Rails app.
|
4
|
+
|
5
5
|
The generator takes one argument which will be the name of the
|
6
6
|
layout and stylesheet files. If no argument is passed then it defaults
|
7
7
|
to "application".
|
8
|
+
|
9
|
+
The helper module includes some methods which can be called in any
|
10
|
+
template or partial to set variables to be used in the layout, such as
|
11
|
+
page title and javascript/stylesheet includes.
|
8
12
|
|
9
|
-
|
10
|
-
|
13
|
+
Examples:
|
14
|
+
script/generate nifty_layout
|
11
15
|
|
12
|
-
|
13
|
-
|
14
|
-
|
16
|
+
Layout: app/views/layouts/application.html.erb
|
17
|
+
Stylesheet: public/stylesheets/application.css
|
18
|
+
Helper: app/helpers/layout_helper.rb
|
15
19
|
|
16
20
|
|
17
|
-
|
21
|
+
script/generate nifty_layout admin
|
18
22
|
|
19
|
-
|
20
|
-
|
21
|
-
|
23
|
+
Layout: app/views/layouts/admin.html.erb
|
24
|
+
Stylesheet: public/stylesheets/admin.css
|
25
|
+
Helper: app/helpers/layout_helper.rb
|
@@ -23,9 +23,9 @@ class NiftyLayoutGenerator < Rails::Generator::Base
|
|
23
23
|
protected
|
24
24
|
def banner
|
25
25
|
<<-EOS
|
26
|
-
Creates layout and
|
26
|
+
Creates generic layout, stylesheet, and helper files.
|
27
27
|
|
28
|
-
USAGE: #{$0} #{spec.name}
|
28
|
+
USAGE: #{$0} #{spec.name} [layout_name]
|
29
29
|
EOS
|
30
30
|
end
|
31
31
|
end
|
@@ -1,5 +1,48 @@
|
|
1
1
|
Description:
|
2
|
-
|
3
|
-
|
2
|
+
Scaffolds an entire resource, from model and migration to controller and
|
3
|
+
views. The resource is ready to use as a starting point for your restful,
|
4
|
+
resource-oriented application. Tests are not generated yet, but expect
|
5
|
+
them in future releases.
|
6
|
+
|
4
7
|
Usage:
|
5
|
-
|
8
|
+
Pass the name of the model, either CamelCased or under_scored, as the first
|
9
|
+
argument, and an optional list of attribute pairs and controller actions.
|
10
|
+
|
11
|
+
If no controller actions are specified, they will default to index, show,
|
12
|
+
new, create, edit, update, and destroy.
|
13
|
+
|
14
|
+
IMPORTANT: If no attribute pairs are specified, no model will be generated.
|
15
|
+
It will try to determine the attributes from an existing model.
|
16
|
+
|
17
|
+
Attribute pairs are column_name:sql_type arguments specifying the
|
18
|
+
model's attributes. Timestamps are added by default, so you don't have to
|
19
|
+
specify them by hand as 'created_at:datetime updated_at:datetime'.
|
20
|
+
|
21
|
+
For example, `nifty_scaffold post name:string content:text hidden:boolean`
|
22
|
+
gives you a model with those three attributes, a controller that handles
|
23
|
+
the create/show/update/destroy, forms to create and edit your posts, and
|
24
|
+
an index that lists them all, as well as a map.resources :posts
|
25
|
+
declaration in config/routes.rb.
|
26
|
+
|
27
|
+
Adding an "!" in the mix of arguments will invert the passed controller
|
28
|
+
actions. This will include all 7 controller actitons except the ones
|
29
|
+
mentioned. This option doesn't effect model attributes.
|
30
|
+
|
31
|
+
Examples:
|
32
|
+
script/generate nifty_scaffold post
|
33
|
+
|
34
|
+
Will create a controller called "posts" it will contain all seven
|
35
|
+
CRUD actions along with the views. A model will NOT be created,
|
36
|
+
instead it will look for an existing model and use those attributes.
|
37
|
+
|
38
|
+
script/generate nifty_scaffold post name:string content:text index new edit
|
39
|
+
|
40
|
+
Will create a Post model and migration file with the name and content
|
41
|
+
attributes. It will also create a controller with index, new, create,
|
42
|
+
edit, and update actions. Notice the create and update actions are
|
43
|
+
added automatically with new and edit.
|
44
|
+
|
45
|
+
script/generate nifty_scaffold post ! show new
|
46
|
+
|
47
|
+
Creates a posts controller (no model) with index, edit, update, and
|
48
|
+
destroy actions.
|
@@ -11,20 +11,28 @@ class NiftyScaffoldGenerator < Rails::Generator::Base
|
|
11
11
|
@attributes = []
|
12
12
|
|
13
13
|
@args[1..-1].each do |arg|
|
14
|
-
if arg
|
14
|
+
if arg == '!'
|
15
|
+
options[:invert] = true
|
16
|
+
elsif arg.include? ':'
|
15
17
|
@attributes << Rails::Generator::GeneratedAttribute.new(*arg.split(":"))
|
16
18
|
else
|
17
19
|
@controller_actions << arg
|
20
|
+
@controller_actions << 'create' if arg == 'new'
|
21
|
+
@controller_actions << 'update' if arg == 'edit'
|
18
22
|
end
|
19
23
|
end
|
20
24
|
|
21
|
-
|
22
|
-
|
25
|
+
@controller_actions.uniq!
|
26
|
+
@attributes.uniq!
|
27
|
+
|
28
|
+
if options[:invert] || @controller_actions.empty?
|
29
|
+
@controller_actions = all_actions - @controller_actions
|
23
30
|
end
|
24
31
|
|
25
32
|
if @attributes.empty?
|
33
|
+
options[:skip_model] = true # default to skipping model if no attributes passed
|
26
34
|
if model_exists?
|
27
|
-
|
35
|
+
model_columns_for_attributes.each do |column|
|
28
36
|
@attributes << Rails::Generator::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
|
29
37
|
end
|
30
38
|
else
|
@@ -32,38 +40,54 @@ class NiftyScaffoldGenerator < Rails::Generator::Base
|
|
32
40
|
end
|
33
41
|
end
|
34
42
|
end
|
35
|
-
|
43
|
+
|
36
44
|
def manifest
|
37
45
|
record do |m|
|
38
|
-
|
39
|
-
|
40
|
-
m.directory "app/models"
|
41
|
-
m.directory "db/migrate"
|
42
|
-
m.directory "app/views/#{plural_name}"
|
43
|
-
m.template "controller.rb", "app/controllers/#{plural_name}_controller.rb"
|
44
|
-
m.template "helper.rb", "app/helpers/#{plural_name}_helper.rb"
|
45
|
-
|
46
|
-
unless model_exists?
|
46
|
+
unless options[:skip_model]
|
47
|
+
m.directory "app/models"
|
47
48
|
m.template "model.rb", "app/models/#{singular_name}.rb"
|
48
|
-
|
49
|
+
unless options[:skip_migration]
|
50
|
+
m.migration_template "migration.rb", "db/migrate", :migration_file_name => "create_#{plural_name}"
|
51
|
+
end
|
49
52
|
end
|
50
53
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
+
unless options[:skip_controller]
|
55
|
+
m.directory "app/controllers"
|
56
|
+
m.template "controller.rb", "app/controllers/#{plural_name}_controller.rb"
|
57
|
+
|
58
|
+
m.directory "app/helpers"
|
59
|
+
m.template "helper.rb", "app/helpers/#{plural_name}_helper.rb"
|
60
|
+
|
61
|
+
m.directory "app/views/#{plural_name}"
|
62
|
+
controller_actions.each do |action|
|
63
|
+
if File.exist? source_path("views/#{action}.html.erb")
|
64
|
+
m.template "views/#{action}.html.erb", "app/views/#{plural_name}/#{action}.html.erb"
|
65
|
+
end
|
54
66
|
end
|
55
|
-
end
|
56
67
|
|
57
|
-
|
58
|
-
|
59
|
-
|
68
|
+
if form_partial?
|
69
|
+
m.template "views/_form.html.erb", "app/views/#{plural_name}/_form.html.erb"
|
70
|
+
end
|
60
71
|
|
61
|
-
|
72
|
+
m.route_resources plural_name
|
73
|
+
end
|
62
74
|
end
|
63
75
|
end
|
64
76
|
|
65
77
|
def form_partial?
|
66
|
-
|
78
|
+
actions? :new, :edit
|
79
|
+
end
|
80
|
+
|
81
|
+
def all_actions
|
82
|
+
%w[index show new create edit update destroy]
|
83
|
+
end
|
84
|
+
|
85
|
+
def action?(name)
|
86
|
+
controller_actions.include? name.to_s
|
87
|
+
end
|
88
|
+
|
89
|
+
def actions?(*names)
|
90
|
+
names.all? { |n| action? n.to_s }
|
67
91
|
end
|
68
92
|
|
69
93
|
def singular_name
|
@@ -71,7 +95,7 @@ class NiftyScaffoldGenerator < Rails::Generator::Base
|
|
71
95
|
end
|
72
96
|
|
73
97
|
def plural_name
|
74
|
-
name.pluralize
|
98
|
+
name.underscore.pluralize
|
75
99
|
end
|
76
100
|
|
77
101
|
def class_name
|
@@ -85,7 +109,7 @@ class NiftyScaffoldGenerator < Rails::Generator::Base
|
|
85
109
|
def controller_methods
|
86
110
|
controller_actions.map do |action|
|
87
111
|
controller_method(action)
|
88
|
-
end.join
|
112
|
+
end.join("\n")
|
89
113
|
end
|
90
114
|
|
91
115
|
def controller_method(name)
|
@@ -101,15 +125,31 @@ class NiftyScaffoldGenerator < Rails::Generator::Base
|
|
101
125
|
end
|
102
126
|
|
103
127
|
def item_path
|
104
|
-
if
|
128
|
+
if action? :show
|
105
129
|
"@#{singular_name}"
|
106
130
|
else
|
107
131
|
"#{plural_name}_path"
|
108
132
|
end
|
109
133
|
end
|
110
134
|
|
135
|
+
def model_columns_for_attributes
|
136
|
+
class_name.constantize.columns.reject do |column|
|
137
|
+
column.name.to_s =~ /^(id|created_at|updated_at)$/
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
111
141
|
protected
|
112
142
|
|
143
|
+
def add_options!(opt)
|
144
|
+
opt.separator ''
|
145
|
+
opt.separator 'Options:'
|
146
|
+
opt.on("--skip-model", "Don't generate a model or migration file.") { |v| options[:skip_model] = v }
|
147
|
+
opt.on("--skip-migration", "Don't generate migration file for model.") { |v| options[:skip_migration] = v }
|
148
|
+
opt.on("--skip-timestamps", "Don't add timestamps to migration file.") { |v| options[:skip_timestamps] = v }
|
149
|
+
opt.on("--skip-controller", "Don't generate controller, helper, or views.") { |v| options[:skip_controller] = v }
|
150
|
+
opt.on("--invert", "Generate all controller actions except these mentioned.") { |v| options[:invert] = v }
|
151
|
+
end
|
152
|
+
|
113
153
|
# is there a better way to do this? Perhaps with const_defined?
|
114
154
|
def model_exists?
|
115
155
|
File.exist? destination_path("app/models/#{singular_name}.rb")
|
@@ -121,9 +161,9 @@ protected
|
|
121
161
|
|
122
162
|
def banner
|
123
163
|
<<-EOS
|
124
|
-
Creates a controller and model given the name and attributes.
|
164
|
+
Creates a controller and optional model given the name, actions, and attributes.
|
125
165
|
|
126
|
-
USAGE: #{$0} #{spec.name}
|
166
|
+
USAGE: #{$0} #{spec.name} ModelName [controller_actions and model:attributes] [options]
|
127
167
|
EOS
|
128
168
|
end
|
129
169
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
def update
|
2
2
|
@<%= singular_name %> = <%= class_name %>.find(params[:id])
|
3
3
|
if @<%= singular_name %>.update_attributes(params[:<%= singular_name %>])
|
4
|
+
flash[:notice] = "Successfully updated <%= name.humanize.downcase %>."
|
4
5
|
redirect_to <%= item_path %>
|
5
6
|
else
|
6
7
|
render :action => 'edit'
|
@@ -1,9 +1,9 @@
|
|
1
1
|
<%% form_for @<%= singular_name %> do |f| %>
|
2
|
-
|
2
|
+
<%- for attribute in attributes -%>
|
3
3
|
<p>
|
4
|
-
<%%= f.label :<%= attribute.name
|
4
|
+
<%%= f.label :<%= attribute.name %> %><br />
|
5
5
|
<%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
|
6
6
|
</p>
|
7
|
-
|
7
|
+
<%- end -%>
|
8
8
|
<p><%%= f.submit "Submit" %></p>
|
9
9
|
<%% end %>
|
@@ -2,7 +2,13 @@
|
|
2
2
|
|
3
3
|
<%= render_form %>
|
4
4
|
|
5
|
+
<%- if actions? :show, :index -%>
|
5
6
|
<p>
|
7
|
+
<%- if action? :show -%>
|
6
8
|
<%%= link_to "Show", @<%= singular_name %> %> |
|
9
|
+
<%- end -%>
|
10
|
+
<%- if action? :index -%>
|
7
11
|
<%%= link_to "View All", <%= plural_name %>_path %>
|
12
|
+
<%- end -%>
|
8
13
|
</p>
|
14
|
+
<%- end -%>
|
@@ -3,20 +3,27 @@
|
|
3
3
|
<table>
|
4
4
|
<tr>
|
5
5
|
<%- for attribute in attributes -%>
|
6
|
-
<th><%= attribute.column.human_name %></th>
|
6
|
+
<th><%= attribute.column.human_name.titleize %></th>
|
7
7
|
<%- end -%>
|
8
8
|
</tr>
|
9
|
-
|
10
9
|
<%% for <%= singular_name %> in @<%= plural_name %> %>
|
11
10
|
<tr>
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
<%- for attribute in attributes -%>
|
12
|
+
<td><%%=h <%= singular_name %>.<%= attribute.name %> %></td>
|
13
|
+
<%- end -%>
|
14
|
+
<%- if action? :show -%>
|
15
15
|
<td><%%= link_to "Show", <%= singular_name %> %></td>
|
16
|
+
<%- end -%>
|
17
|
+
<%- if action? :edit -%>
|
16
18
|
<td><%%= link_to "Edit", edit_<%= singular_name %>_path(<%= singular_name %>) %></td>
|
19
|
+
<%- end -%>
|
20
|
+
<%- if action? :destroy -%>
|
17
21
|
<td><%%= link_to "Destroy", <%= singular_name %>, :confirm => 'Are you sure?', :method => :delete %></td>
|
22
|
+
<%- end -%>
|
18
23
|
</tr>
|
19
24
|
<%% end %>
|
20
25
|
</table>
|
21
26
|
|
22
|
-
|
27
|
+
<%- if action? :new -%>
|
28
|
+
<p><%%= link_to "New <%= singular_name.titleize %>", new_<%= singular_name %>_path %></p>
|
29
|
+
<%- end -%>
|
@@ -2,13 +2,19 @@
|
|
2
2
|
|
3
3
|
<%- for attribute in attributes -%>
|
4
4
|
<p>
|
5
|
-
<strong><%= attribute.column.human_name %>:</strong>
|
5
|
+
<strong><%= attribute.column.human_name.titleize %>:</strong>
|
6
6
|
<%%=h @<%= singular_name %>.<%= attribute.name %> %>
|
7
7
|
</p>
|
8
8
|
<%- end -%>
|
9
9
|
|
10
10
|
<p>
|
11
|
+
<%- if action? :edit -%>
|
11
12
|
<%%= link_to "Edit", edit_<%= singular_name %>_path(@<%= singular_name %>) %> |
|
13
|
+
<%- end -%>
|
14
|
+
<%- if action? :destroy -%>
|
12
15
|
<%%= link_to "Destroy", @<%= singular_name %>, :confirm => 'Are you sure?', :method => :delete %> |
|
16
|
+
<%- end -%>
|
17
|
+
<%- if action? :index -%>
|
13
18
|
<%%= link_to "View All", <%= plural_name %>_path %>
|
19
|
+
<%- end -%>
|
14
20
|
</p>
|
data/test/test_helper.rb
CHANGED
@@ -37,7 +37,7 @@ module NiftyGenerators
|
|
37
37
|
protected
|
38
38
|
|
39
39
|
def run_rails_generator(generator, *args)
|
40
|
-
options = args.
|
40
|
+
options = args.pop if args.last.kind_of? Hash
|
41
41
|
options ||= {}
|
42
42
|
run_generator(generator.to_s, args, generator_sources, options.reverse_merge(:quiet => true))
|
43
43
|
end
|
@@ -37,25 +37,8 @@ class TestNiftyScaffoldGenerator < Test::Unit::TestCase
|
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
-
context "generator with no options" do
|
41
|
-
rails_generator :nifty_scaffold, "
|
42
|
-
|
43
|
-
should "generate model with class as camelcase name" do
|
44
|
-
assert_generated_file "app/models/line_item.rb" do |body|
|
45
|
-
assert_match "class LineItem < ActiveRecord::Base", body
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
should "generate migration with default name column" do
|
50
|
-
file = Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").first
|
51
|
-
assert file, "migration file doesn't exist"
|
52
|
-
assert_match(/[0-9]+_create_line_items.rb$/, file)
|
53
|
-
assert_generated_file "db/migrate/#{File.basename(file)}" do |body|
|
54
|
-
assert_match "class CreateLineItems", body
|
55
|
-
assert_match "t.string :name", body
|
56
|
-
assert_match "t.timestamp", body
|
57
|
-
end
|
58
|
-
end
|
40
|
+
context "generator with no options and no existing model" do
|
41
|
+
rails_generator :nifty_scaffold, "LineItem"
|
59
42
|
|
60
43
|
should_generate_file "app/helpers/line_items_helper.rb"
|
61
44
|
|
@@ -83,6 +66,30 @@ class TestNiftyScaffoldGenerator < Test::Unit::TestCase
|
|
83
66
|
assert_match "map.resources :line_items", body
|
84
67
|
end
|
85
68
|
end
|
69
|
+
|
70
|
+
should_not_generate_file "app/models/line_item.rb"
|
71
|
+
end
|
72
|
+
|
73
|
+
context "generator with some attributes" do
|
74
|
+
rails_generator :nifty_scaffold, "line_item", "name:string", "description:text"
|
75
|
+
|
76
|
+
should "generate migration with attribute columns" do
|
77
|
+
file = Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").first
|
78
|
+
assert file, "migration file doesn't exist"
|
79
|
+
assert_match(/[0-9]+_create_line_items.rb$/, file)
|
80
|
+
assert_generated_file "db/migrate/#{File.basename(file)}" do |body|
|
81
|
+
assert_match "class CreateLineItems", body
|
82
|
+
assert_match "t.string :name", body
|
83
|
+
assert_match "t.text :description", body
|
84
|
+
assert_match "t.timestamps", body
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
should "generate model with class as camelcase name" do
|
89
|
+
assert_generated_file "app/models/line_item.rb" do |body|
|
90
|
+
assert_match "class LineItem < ActiveRecord::Base", body
|
91
|
+
end
|
92
|
+
end
|
86
93
|
end
|
87
94
|
|
88
95
|
context "generator with index action" do
|
@@ -131,6 +138,7 @@ class TestNiftyScaffoldGenerator < Test::Unit::TestCase
|
|
131
138
|
assert_match "def create", body
|
132
139
|
assert_match "@line_item = LineItem.new(params[:line_item])", body
|
133
140
|
assert_match "if @line_item.save", body
|
141
|
+
assert_match "flash[:notice] = \"Successfully created line item.\"", body
|
134
142
|
assert_match "redirect_to line_items_path", body
|
135
143
|
assert_match "render :action => 'new'", body
|
136
144
|
end
|
@@ -155,6 +163,7 @@ class TestNiftyScaffoldGenerator < Test::Unit::TestCase
|
|
155
163
|
assert_match "@line_item = LineItem.find(params[:id])", body
|
156
164
|
assert_match "def update", body
|
157
165
|
assert_match "if @line_item.update_attributes(params[:line_item])", body
|
166
|
+
assert_match "flash[:notice] = \"Successfully updated line item.\"", body
|
158
167
|
assert_match "redirect_to line_items_path", body
|
159
168
|
assert_match "render :action => 'edit'", body
|
160
169
|
end
|
@@ -171,6 +180,7 @@ class TestNiftyScaffoldGenerator < Test::Unit::TestCase
|
|
171
180
|
assert_match "def destroy", body
|
172
181
|
assert_match "@line_item = LineItem.find(params[:id])", body
|
173
182
|
assert_match "@line_item.destroy", body
|
183
|
+
assert_match "flash[:notice] = \"Successfully destroyed line item.\"", body
|
174
184
|
assert_match "redirect_to line_items_path", body
|
175
185
|
end
|
176
186
|
end
|
@@ -212,7 +222,84 @@ class TestNiftyScaffoldGenerator < Test::Unit::TestCase
|
|
212
222
|
end
|
213
223
|
end
|
214
224
|
end
|
215
|
-
|
225
|
+
|
226
|
+
context "generator with attributes and skip model option" do
|
227
|
+
rails_generator :nifty_scaffold, "line_item", "foo:string", :skip_model => true
|
228
|
+
|
229
|
+
should "use passed attribute" do
|
230
|
+
assert_generated_file "app/views/line_items/_form.html.erb" do |body|
|
231
|
+
assert_match "<%= f.text_field :foo %>", body
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
should "not generate migration file" do
|
236
|
+
assert Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").empty?
|
237
|
+
end
|
238
|
+
|
239
|
+
should_not_generate_file "app/models/line_item.rb"
|
240
|
+
end
|
241
|
+
|
242
|
+
context "generator with no attributes" do
|
243
|
+
rails_generator :nifty_scaffold, "line_item"
|
244
|
+
|
245
|
+
should "not generate migration file" do
|
246
|
+
assert Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").empty?
|
247
|
+
end
|
248
|
+
|
249
|
+
should_not_generate_file "app/models/line_item.rb"
|
250
|
+
end
|
251
|
+
|
252
|
+
context "generator with only new and edit actions" do
|
253
|
+
rails_generator :nifty_scaffold, "line_item", "new", "edit"
|
254
|
+
|
255
|
+
should "included create and update actions in controller" do
|
256
|
+
assert_generated_file "app/controllers/line_items_controller.rb" do |body|
|
257
|
+
assert_match "def create", body
|
258
|
+
assert_match "def update", body
|
259
|
+
end
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
context "generator with exclemation mark and show, new, and edit actions" do
|
264
|
+
rails_generator :nifty_scaffold, "line_item", "!", "show", "new", "edit"
|
265
|
+
|
266
|
+
should "only include index and destroy actions" do
|
267
|
+
assert_generated_file "app/controllers/line_items_controller.rb" do |body|
|
268
|
+
%w[index destroy].each { |a| assert_match "def #{a}", body }
|
269
|
+
%w[show new create edit update].each do |a|
|
270
|
+
assert_no_match(/def #{a}/, body)
|
271
|
+
end
|
272
|
+
end
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
context "generator with --skip-controller" do
|
277
|
+
rails_generator :nifty_scaffold, "line_item", :skip_controller => true
|
278
|
+
should_not_generate_file "app/controllers/line_items_controller.rb"
|
279
|
+
should_not_generate_file "app/helpers/line_items_helper.rb"
|
280
|
+
should_not_generate_file "app/views/line_items/index.html.erb"
|
281
|
+
end
|
282
|
+
|
283
|
+
context "generator with --skip-migration" do
|
284
|
+
rails_generator :nifty_scaffold, "line_item", "name:string", :skip_migration => true
|
285
|
+
|
286
|
+
should "not generate migration file" do
|
287
|
+
assert Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").empty?
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
context "generator with --skip-timestamps" do
|
292
|
+
rails_generator :nifty_scaffold, "line_item", "name:string", :skip_timestamps => true
|
293
|
+
|
294
|
+
should "generate migration with no timestamps" do
|
295
|
+
file = Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").first
|
296
|
+
assert file, "migration file doesn't exist"
|
297
|
+
assert_generated_file "db/migrate/#{File.basename(file)}" do |body|
|
298
|
+
assert_no_match(/t.timestamps/, body)
|
299
|
+
end
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
216
303
|
context "existing model" do
|
217
304
|
setup do
|
218
305
|
Dir.mkdir("#{RAILS_ROOT}/app") unless File.exists?("#{RAILS_ROOT}/app")
|
@@ -226,15 +313,23 @@ class TestNiftyScaffoldGenerator < Test::Unit::TestCase
|
|
226
313
|
FileUtils.rm_rf "#{RAILS_ROOT}/app"
|
227
314
|
end
|
228
315
|
|
229
|
-
context "generator with
|
230
|
-
rails_generator :nifty_scaffold, "recipe"
|
316
|
+
context "generator with skip model option" do
|
317
|
+
rails_generator :nifty_scaffold, "recipe", :skip_model => true
|
231
318
|
|
232
319
|
should "use model columns for attributes" do
|
233
320
|
assert_generated_file "app/views/recipes/_form.html.erb" do |body|
|
234
321
|
assert_match "<%= f.text_field :foo %>", body
|
235
322
|
assert_match "<%= f.text_field :bar %>", body
|
323
|
+
assert_match "<%= f.text_field :book_id %>", body
|
324
|
+
assert_no_match(/:id/, body)
|
325
|
+
assert_no_match(/:created_at/, body)
|
326
|
+
assert_no_match(/:updated_at/, body)
|
236
327
|
end
|
237
328
|
end
|
329
|
+
|
330
|
+
should "not generate migration file" do
|
331
|
+
assert Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").empty?
|
332
|
+
end
|
238
333
|
end
|
239
334
|
|
240
335
|
context "generator with attribute specified" do
|
@@ -252,6 +347,10 @@ end
|
|
252
347
|
|
253
348
|
# just an example model we can use
|
254
349
|
class Recipe < ActiveRecord::Base
|
350
|
+
add_column :id, :integer
|
255
351
|
add_column :foo, :string
|
256
352
|
add_column :bar, :string
|
353
|
+
add_column :book_id, :integer
|
354
|
+
add_column :created_at, :datetime
|
355
|
+
add_column :updated_at, :datetime
|
257
356
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ryanb-nifty-generators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Bates
|
@@ -9,21 +9,26 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-05-
|
12
|
+
date: 2008-05-08 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
16
|
-
description: A collection of
|
17
|
-
email: ryan
|
16
|
+
description: A collection of useful generator scripts for Rails.
|
17
|
+
email: ryan (at) railscasts (dot) com
|
18
18
|
executables: []
|
19
19
|
|
20
20
|
extensions: []
|
21
21
|
|
22
22
|
extra_rdoc_files:
|
23
|
+
- CHANGELOG
|
24
|
+
- lib/nifty_generators.rb
|
23
25
|
- LICENSE
|
24
26
|
- README
|
27
|
+
- tasks/deployment.rake
|
25
28
|
- TODO
|
26
29
|
files:
|
30
|
+
- CHANGELOG
|
31
|
+
- lib/nifty_generators.rb
|
27
32
|
- LICENSE
|
28
33
|
- Manifest
|
29
34
|
- rails_generators/nifty_layout/nifty_layout_generator.rb
|
@@ -40,6 +45,7 @@ files:
|
|
40
45
|
- rails_generators/nifty_scaffold/templates/actions/show.rb
|
41
46
|
- rails_generators/nifty_scaffold/templates/actions/update.rb
|
42
47
|
- rails_generators/nifty_scaffold/templates/controller.rb
|
48
|
+
- rails_generators/nifty_scaffold/templates/helper.rb
|
43
49
|
- rails_generators/nifty_scaffold/templates/migration.rb
|
44
50
|
- rails_generators/nifty_scaffold/templates/model.rb
|
45
51
|
- rails_generators/nifty_scaffold/templates/views/_form.html.erb
|
@@ -49,6 +55,7 @@ files:
|
|
49
55
|
- rails_generators/nifty_scaffold/templates/views/show.html.erb
|
50
56
|
- rails_generators/nifty_scaffold/USAGE
|
51
57
|
- README
|
58
|
+
- tasks/deployment.rake
|
52
59
|
- test/test_helper.rb
|
53
60
|
- test/test_nifty_layout_generator.rb
|
54
61
|
- test/test_nifty_scaffold_generator.rb
|
@@ -84,7 +91,7 @@ rubyforge_project: nifty-generators
|
|
84
91
|
rubygems_version: 1.0.1
|
85
92
|
signing_key:
|
86
93
|
specification_version: 2
|
87
|
-
summary: A collection of
|
94
|
+
summary: A collection of useful generator scripts for Rails.
|
88
95
|
test_files:
|
89
96
|
- test/test_helper.rb
|
90
97
|
- test/test_nifty_layout_generator.rb
|