ryanb-nifty-generators 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. data/LICENSE +20 -0
  2. data/Manifest +30 -0
  3. data/README +49 -0
  4. data/TODO +4 -0
  5. data/nifty-generators.gemspec +52 -0
  6. data/rails_generators/nifty_layout/USAGE +21 -0
  7. data/rails_generators/nifty_layout/nifty_layout_generator.rb +31 -0
  8. data/rails_generators/nifty_layout/templates/helper.rb +23 -0
  9. data/rails_generators/nifty_layout/templates/layout.html.erb +23 -0
  10. data/rails_generators/nifty_layout/templates/stylesheet.css +82 -0
  11. data/rails_generators/nifty_scaffold/USAGE +5 -0
  12. data/rails_generators/nifty_scaffold/nifty_scaffold_generator.rb +129 -0
  13. data/rails_generators/nifty_scaffold/templates/actions/create.rb +8 -0
  14. data/rails_generators/nifty_scaffold/templates/actions/destroy.rb +5 -0
  15. data/rails_generators/nifty_scaffold/templates/actions/edit.rb +3 -0
  16. data/rails_generators/nifty_scaffold/templates/actions/index.rb +3 -0
  17. data/rails_generators/nifty_scaffold/templates/actions/new.rb +3 -0
  18. data/rails_generators/nifty_scaffold/templates/actions/show.rb +3 -0
  19. data/rails_generators/nifty_scaffold/templates/actions/update.rb +8 -0
  20. data/rails_generators/nifty_scaffold/templates/controller.rb +3 -0
  21. data/rails_generators/nifty_scaffold/templates/migration.rb +14 -0
  22. data/rails_generators/nifty_scaffold/templates/model.rb +3 -0
  23. data/rails_generators/nifty_scaffold/templates/views/_form.html.erb +9 -0
  24. data/rails_generators/nifty_scaffold/templates/views/edit.html.erb +8 -0
  25. data/rails_generators/nifty_scaffold/templates/views/index.html.erb +22 -0
  26. data/rails_generators/nifty_scaffold/templates/views/new.html.erb +5 -0
  27. data/rails_generators/nifty_scaffold/templates/views/show.html.erb +14 -0
  28. data/test/test_helper.rb +116 -0
  29. data/test/test_nifty_layout_generator.rb +36 -0
  30. data/test/test_nifty_scaffold_generator.rb +257 -0
  31. metadata +91 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Ryan Bates
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/Manifest ADDED
@@ -0,0 +1,30 @@
1
+ LICENSE
2
+ Manifest
3
+ rails_generators/nifty_layout/nifty_layout_generator.rb
4
+ rails_generators/nifty_layout/templates/helper.rb
5
+ rails_generators/nifty_layout/templates/layout.html.erb
6
+ rails_generators/nifty_layout/templates/stylesheet.css
7
+ rails_generators/nifty_layout/USAGE
8
+ rails_generators/nifty_scaffold/nifty_scaffold_generator.rb
9
+ rails_generators/nifty_scaffold/templates/actions/create.rb
10
+ rails_generators/nifty_scaffold/templates/actions/destroy.rb
11
+ rails_generators/nifty_scaffold/templates/actions/edit.rb
12
+ rails_generators/nifty_scaffold/templates/actions/index.rb
13
+ rails_generators/nifty_scaffold/templates/actions/new.rb
14
+ rails_generators/nifty_scaffold/templates/actions/show.rb
15
+ rails_generators/nifty_scaffold/templates/actions/update.rb
16
+ rails_generators/nifty_scaffold/templates/controller.rb
17
+ rails_generators/nifty_scaffold/templates/helper.rb
18
+ rails_generators/nifty_scaffold/templates/migration.rb
19
+ rails_generators/nifty_scaffold/templates/model.rb
20
+ rails_generators/nifty_scaffold/templates/views/_form.html.erb
21
+ rails_generators/nifty_scaffold/templates/views/edit.html.erb
22
+ rails_generators/nifty_scaffold/templates/views/index.html.erb
23
+ rails_generators/nifty_scaffold/templates/views/new.html.erb
24
+ rails_generators/nifty_scaffold/templates/views/show.html.erb
25
+ rails_generators/nifty_scaffold/USAGE
26
+ README
27
+ test/test_helper.rb
28
+ test/test_nifty_layout_generator.rb
29
+ test/test_nifty_scaffold_generator.rb
30
+ TODO
data/README ADDED
@@ -0,0 +1,49 @@
1
+ = nifty-generator
2
+
3
+ * http://github.com/ryanb/nifty-generators/
4
+
5
+ == DESCRIPTION:
6
+
7
+ A collection of Ryan's generator scripts for Rails.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Still in very early development
12
+
13
+ == SYNOPSIS:
14
+
15
+ script/generate nifty_layout
16
+
17
+ == REQUIREMENTS:
18
+
19
+ * Rails
20
+ * Similar opinions as me
21
+
22
+ == INSTALL:
23
+
24
+ * gem install ryanb-nifty-generators --source=http://gems.github.com
25
+
26
+ == LICENSE:
27
+
28
+ (The MIT License)
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.
data/TODO ADDED
@@ -0,0 +1,4 @@
1
+
2
+ Nifty Scaffold Generator
3
+ - generate tests/specs
4
+ + support custom actions
@@ -0,0 +1,52 @@
1
+
2
+ # Gem::Specification for Nifty-generators-0.0.3
3
+ # Originally generated by Echoe
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = %q{nifty-generators}
7
+ s.version = "0.0.3"
8
+
9
+ s.specification_version = 2 if s.respond_to? :specification_version=
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.authors = ["Ryan Bates"]
13
+ s.date = %q{2008-05-07}
14
+ s.description = %q{A collection of Ryan's generator scripts for Rails.}
15
+ s.email = %q{ryan@railscasts.com}
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"]
18
+ s.has_rdoc = true
19
+ s.homepage = %q{http://github.com/ryanb/nifty-generators}
20
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Nifty-generators", "--main", "README"]
21
+ s.require_paths = ["lib"]
22
+ s.rubyforge_project = %q{nifty-generators}
23
+ s.rubygems_version = %q{1.0.0}
24
+ s.summary = %q{A collection of Ryan's generator scripts for Rails.}
25
+ s.test_files = ["test/test_helper.rb", "test/test_nifty_layout_generator.rb", "test/test_nifty_scaffold_generator.rb"]
26
+ end
27
+
28
+
29
+ # # Original Rakefile source (requires the Echoe gem):
30
+ #
31
+ # require 'rubygems'
32
+ # require 'rake'
33
+ #
34
+ # begin
35
+ # require 'echoe'
36
+ #
37
+ # Echoe.new('nifty-generators', '0.0.3') do |p|
38
+ # p.summary = "A collection of Ryan's generator scripts for Rails."
39
+ # p.description = "A collection of Ryan's generator scripts for Rails."
40
+ # p.url = "http://github.com/ryanb/nifty-generators"
41
+ # p.author = 'Ryan Bates'
42
+ # p.email = "ryan@railscasts.com"
43
+ # p.ignore_pattern = ["script/*", "*.gemspec"]
44
+ # end
45
+ #
46
+ # rescue LoadError => boom
47
+ # puts "You are missing a dependency required for meta-operations on this gem."
48
+ # puts "#{boom.to_s.capitalize}."
49
+ # end
50
+ #
51
+ # desc "Build the manifest and gemspec files."
52
+ # task :build => [:build_manifest, :build_gemspec]
@@ -0,0 +1,21 @@
1
+ Description:
2
+ The nifty_layout generator creates a basic stylesheet and layout
3
+ which will give some structure to a starting Rails app.
4
+
5
+ The generator takes one argument which will be the name of the
6
+ layout and stylesheet files. If no argument is passed then it defaults
7
+ to "application".
8
+
9
+ Usage:
10
+ ./script/generate nifty_layout
11
+
12
+ This will create two files:
13
+ Layout: app/views/layouts/application.html.erb
14
+ Stylesheet: public/stylesheets/application.css
15
+
16
+
17
+ ./script/generate nifty_layout admin
18
+
19
+ This will create two files:
20
+ Layout: app/views/layouts/admin.html.erb
21
+ Stylesheet: public/stylesheets/admin.css
@@ -0,0 +1,31 @@
1
+ class NiftyLayoutGenerator < Rails::Generator::Base
2
+ def initialize(runtime_args, runtime_options = {})
3
+ super
4
+ @name = @args.first || 'application'
5
+ end
6
+
7
+ def manifest
8
+ record do |m|
9
+ m.directory 'app/views/layouts'
10
+ m.directory 'public/stylesheets'
11
+ m.directory 'app/helpers'
12
+
13
+ m.template "layout.html.erb", "app/views/layouts/#{file_name}.html.erb"
14
+ m.file "stylesheet.css", "public/stylesheets/#{file_name}.css"
15
+ m.file "helper.rb", "app/helpers/layout_helper.rb"
16
+ end
17
+ end
18
+
19
+ def file_name
20
+ @name.underscore
21
+ end
22
+
23
+ protected
24
+ def banner
25
+ <<-EOS
26
+ Creates layout and stylesheet files.
27
+
28
+ USAGE: #{$0} #{spec.name} application
29
+ EOS
30
+ end
31
+ end
@@ -0,0 +1,23 @@
1
+ # These helper methods can be called in your template to set variables to be used in the layout
2
+ # This module should be included in all views globally,
3
+ # to do so you may need to add this line to your ApplicationController
4
+ # helper :layout
5
+ module LayoutHelper
6
+ def title(page_title, show_title = true)
7
+ @content_for_title = page_title.to_s
8
+ @show_title = show_title
9
+ end
10
+
11
+ def show_title?
12
+ @show_title
13
+ end
14
+
15
+ def stylesheet(*args)
16
+ content_for(:head) { stylesheet_link_tag(*args.map(&:to_s)) }
17
+ end
18
+
19
+ def javascript(*args)
20
+ args = args.map { |arg| arg == :defaults ? arg : arg.to_s }
21
+ content_for(:head) { javascript_include_tag(*args) }
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
+ <html>
4
+ <head>
5
+ <title><%%= h(yield(:title) || "Untitled") %></title>
6
+ <%%= stylesheet_link_tag '<%= file_name %>' %>
7
+ <%%= javascript_include_tag :defaults %>
8
+ <%%= yield(:head) %>
9
+ </head>
10
+ <body>
11
+ <div id="container">
12
+ <%%- flash.each do |name, msg| -%>
13
+ <%%= content_tag :div, msg, :id => "flash_#{name}" %>
14
+ <%%- end -%>
15
+
16
+ <%%- if show_title? -%>
17
+ <h1><%%=h yield(:title) %></h1>
18
+ <%%- end -%>
19
+
20
+ <%%= yield %>
21
+ </div>
22
+ </body>
23
+ </html>
@@ -0,0 +1,82 @@
1
+ body {
2
+ background-color: #FFF;
3
+ font-family: Verdana, Helvetica, Arial;
4
+ font-size: 14px;
5
+ }
6
+
7
+ a img {
8
+ border: none;
9
+ }
10
+
11
+ a {
12
+ color: #0000FF;
13
+ }
14
+
15
+ body {
16
+ background-color: #4B7399;
17
+ }
18
+
19
+ #container {
20
+ width: 75%;
21
+ margin: 0 auto;
22
+ background-color: #FFF;
23
+ padding: 20px 40px;
24
+ border: solid 1px black;
25
+ margin-top: 20px;
26
+ }
27
+
28
+ #flash_notice, #flash_error {
29
+ padding: 5px 8px;
30
+ margin: 10px 0;
31
+ }
32
+
33
+ #flash_notice {
34
+ background-color: #CFC;
35
+ border: solid 1px #6C6;
36
+ }
37
+
38
+ #flash_error {
39
+ background-color: #FCC;
40
+ border: solid 1px #C66;
41
+ }
42
+
43
+ .clear {
44
+ clear: both;
45
+ }
46
+
47
+
48
+
49
+ .fieldWithErrors {
50
+ display: inline;
51
+ }
52
+
53
+ #errorExplanation {
54
+ width: 400px;
55
+ border: 2px solid #CF0000;
56
+ padding: 0px;
57
+ padding-bottom: 12px;
58
+ margin-bottom: 20px;
59
+ background-color: #f0f0f0;
60
+ }
61
+
62
+ #errorExplanation h2 {
63
+ text-align: left;
64
+ font-weight: bold;
65
+ padding: 5px 5px 5px 15px;
66
+ font-size: 12px;
67
+ margin: 0px;
68
+ background-color: #c00;
69
+ color: #fff;
70
+ }
71
+ #errorExplanation p {
72
+ color: #333;
73
+ margin-bottom: 0;
74
+ padding: 8px;
75
+ }
76
+ #errorExplanation ul {
77
+ margin: 2px 24px;
78
+ }
79
+ #errorExplanation ul li {
80
+ font-size: 12px;
81
+ list-style: disc;
82
+ }
@@ -0,0 +1,5 @@
1
+ Description:
2
+
3
+
4
+ Usage:
5
+
@@ -0,0 +1,129 @@
1
+ class NiftyScaffoldGenerator < Rails::Generator::Base
2
+ attr_accessor :name, :attributes, :controller_actions
3
+
4
+ def initialize(runtime_args, runtime_options = {})
5
+ super
6
+ usage if @args.empty?
7
+
8
+
9
+ @name = @args.first
10
+ @controller_actions = []
11
+ @attributes = []
12
+
13
+ @args[1..-1].each do |arg|
14
+ if arg.include? ':'
15
+ @attributes << Rails::Generator::GeneratedAttribute.new(*arg.split(":"))
16
+ else
17
+ @controller_actions << arg
18
+ end
19
+ end
20
+
21
+ if @controller_actions.empty?
22
+ @controller_actions = %w[index show new create edit update destroy]
23
+ end
24
+
25
+ if @attributes.empty?
26
+ if model_exists?
27
+ class_name.constantize.columns.each do |column|
28
+ @attributes << Rails::Generator::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
29
+ end
30
+ else
31
+ @attributes << Rails::Generator::GeneratedAttribute.new('name', 'string')
32
+ end
33
+ end
34
+ end
35
+
36
+ def manifest
37
+ record do |m|
38
+ m.directory "app/controllers"
39
+ m.directory "app/helpers"
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?
47
+ m.template "model.rb", "app/models/#{singular_name}.rb"
48
+ m.migration_template "migration.rb", "db/migrate", :migration_file_name => "create_#{plural_name}"
49
+ end
50
+
51
+ controller_actions.each do |action|
52
+ if File.exist? source_path("views/#{action}.html.erb")
53
+ m.template "views/#{action}.html.erb", "app/views/#{plural_name}/#{action}.html.erb"
54
+ end
55
+ end
56
+
57
+ if form_partial?
58
+ m.template "views/_form.html.erb", "app/views/#{plural_name}/_form.html.erb"
59
+ end
60
+
61
+ m.route_resources plural_name
62
+ end
63
+ end
64
+
65
+ def form_partial?
66
+ controller_actions.include?('new') && controller_actions.include?('edit')
67
+ end
68
+
69
+ def singular_name
70
+ name.underscore
71
+ end
72
+
73
+ def plural_name
74
+ name.pluralize
75
+ end
76
+
77
+ def class_name
78
+ name.camelize
79
+ end
80
+
81
+ def plural_class_name
82
+ plural_name.camelize
83
+ end
84
+
85
+ def controller_methods
86
+ controller_actions.map do |action|
87
+ controller_method(action)
88
+ end.join
89
+ end
90
+
91
+ def controller_method(name)
92
+ read_template("actions/#{name}.rb")
93
+ end
94
+
95
+ def render_form
96
+ if form_partial?
97
+ "<%= render :partial => 'form' %>"
98
+ else
99
+ read_template("views/_form.html.erb")
100
+ end
101
+ end
102
+
103
+ def item_path
104
+ if controller_actions.include? 'show'
105
+ "@#{singular_name}"
106
+ else
107
+ "#{plural_name}_path"
108
+ end
109
+ end
110
+
111
+ protected
112
+
113
+ # is there a better way to do this? Perhaps with const_defined?
114
+ def model_exists?
115
+ File.exist? destination_path("app/models/#{singular_name}.rb")
116
+ end
117
+
118
+ def read_template(relative_path)
119
+ ERB.new(File.read(source_path(relative_path)), nil, '-').result(binding)
120
+ end
121
+
122
+ def banner
123
+ <<-EOS
124
+ Creates a controller and model given the name and attributes.
125
+
126
+ USAGE: #{$0} #{spec.name} name
127
+ EOS
128
+ end
129
+ end
@@ -0,0 +1,8 @@
1
+ def create
2
+ @<%= singular_name %> = <%= class_name %>.new(params[:<%= singular_name %>])
3
+ if @<%= singular_name %>.save
4
+ redirect_to <%= item_path %>
5
+ else
6
+ render :action => 'new'
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ def destroy
2
+ @<%= singular_name %> = <%= class_name %>.find(params[:id])
3
+ @<%= singular_name %>.destroy
4
+ redirect_to <%= plural_name %>_path
5
+ end
@@ -0,0 +1,3 @@
1
+ def edit
2
+ @<%= singular_name %> = <%= class_name %>.find(params[:id])
3
+ end
@@ -0,0 +1,3 @@
1
+ def index
2
+ @<%= plural_name %> = <%= class_name %>.find(:all)
3
+ end
@@ -0,0 +1,3 @@
1
+ def new
2
+ @<%= singular_name %> = <%= class_name %>.new
3
+ end
@@ -0,0 +1,3 @@
1
+ def show
2
+ @<%= singular_name %> = <%= class_name %>.find(params[:id])
3
+ end
@@ -0,0 +1,8 @@
1
+ def update
2
+ @<%= singular_name %> = <%= class_name %>.find(params[:id])
3
+ if @<%= singular_name %>.update_attributes(params[:<%= singular_name %>])
4
+ redirect_to <%= item_path %>
5
+ else
6
+ render :action => 'edit'
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ class <%= plural_class_name %>Controller < ApplicationController
2
+ <%= controller_methods.lstrip %>
3
+ end
@@ -0,0 +1,14 @@
1
+ class Create<%= plural_class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :<%= plural_name %> do |t|
4
+ <%- for attribute in attributes -%>
5
+ t.<%= attribute.type %> :<%= attribute.name %>
6
+ <%- end -%>
7
+ t.timestamps
8
+ end
9
+ end
10
+
11
+ def self.down
12
+ drop_table :<%= plural_name %>
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ class <%= class_name %> < ActiveRecord::Base
2
+
3
+ end
@@ -0,0 +1,9 @@
1
+ <%% form_for @<%= singular_name %> do |f| %>
2
+ <% for attribute in attributes %>
3
+ <p>
4
+ <%%= f.label :<%= attribute.name %>%><br />
5
+ <%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
6
+ </p>
7
+ <% end %>
8
+ <p><%%= f.submit "Submit" %></p>
9
+ <%% end %>
@@ -0,0 +1,8 @@
1
+ <%% title "Edit <%= singular_name.titleize %>" %>
2
+
3
+ <%= render_form %>
4
+
5
+ <p>
6
+ <%%= link_to "Show", @<%= singular_name %> %> |
7
+ <%%= link_to "View All", <%= plural_name %>_path %>
8
+ </p>
@@ -0,0 +1,22 @@
1
+ <%% title "<%= plural_name.titleize %>" %>
2
+
3
+ <table>
4
+ <tr>
5
+ <%- for attribute in attributes -%>
6
+ <th><%= attribute.column.human_name %></th>
7
+ <%- end -%>
8
+ </tr>
9
+
10
+ <%% for <%= singular_name %> in @<%= plural_name %> %>
11
+ <tr>
12
+ <%- for attribute in attributes -%>
13
+ <td><%%=h <%= singular_name %>.<%= attribute.name %> %></td>
14
+ <%- end -%>
15
+ <td><%%= link_to "Show", <%= singular_name %> %></td>
16
+ <td><%%= link_to "Edit", edit_<%= singular_name %>_path(<%= singular_name %>) %></td>
17
+ <td><%%= link_to "Destroy", <%= singular_name %>, :confirm => 'Are you sure?', :method => :delete %></td>
18
+ </tr>
19
+ <%% end %>
20
+ </table>
21
+
22
+ <p><%%= link_to "New <%= singular_name %>", new_<%= singular_name %>_path %></p>
@@ -0,0 +1,5 @@
1
+ <%% title "New <%= singular_name.titleize %>" %>
2
+
3
+ <%= render_form %>
4
+
5
+ <p><%%= link_to "Back to List", <%= plural_name %>_path %></p>
@@ -0,0 +1,14 @@
1
+ <%% title "<%= singular_name.titleize %>" %>
2
+
3
+ <%- for attribute in attributes -%>
4
+ <p>
5
+ <strong><%= attribute.column.human_name %>:</strong>
6
+ <%%=h @<%= singular_name %>.<%= attribute.name %> %>
7
+ </p>
8
+ <%- end -%>
9
+
10
+ <p>
11
+ <%%= link_to "Edit", edit_<%= singular_name %>_path(@<%= singular_name %>) %> |
12
+ <%%= link_to "Destroy", @<%= singular_name %>, :confirm => 'Are you sure?', :method => :delete %> |
13
+ <%%= link_to "View All", <%= plural_name %>_path %>
14
+ </p>
@@ -0,0 +1,116 @@
1
+ require 'test/unit'
2
+
3
+ # Must set before requiring generator libs.
4
+ TMP_ROOT = File.dirname(__FILE__) + "/tmp" unless defined?(TMP_ROOT)
5
+ PROJECT_NAME = "myproject" unless defined?(PROJECT_NAME)
6
+ app_root = File.join(TMP_ROOT, PROJECT_NAME)
7
+ if defined?(APP_ROOT)
8
+ APP_ROOT.replace(app_root)
9
+ else
10
+ APP_ROOT = app_root
11
+ end
12
+ if defined?(RAILS_ROOT)
13
+ RAILS_ROOT.replace(app_root)
14
+ else
15
+ RAILS_ROOT = app_root
16
+ end
17
+
18
+ require 'rubygems'
19
+ require 'rubigen' # gem install rubigen
20
+ require 'rubigen/helpers/generator_test_helper'
21
+ require 'rails_generator'
22
+ require 'shoulda' # gem install Shoulda
23
+ require 'mocha'
24
+
25
+ module NiftyGenerators
26
+ module TestHelper
27
+ include RubiGen::GeneratorTestHelper
28
+
29
+ def setup
30
+ bare_setup
31
+ end
32
+
33
+ def teardown
34
+ bare_teardown
35
+ end
36
+
37
+ protected
38
+
39
+ def run_rails_generator(generator, *args)
40
+ options = args.shift! if args.last.kind_of? Hash
41
+ options ||= {}
42
+ run_generator(generator.to_s, args, generator_sources, options.reverse_merge(:quiet => true))
43
+ end
44
+
45
+ def generator_sources
46
+ [RubiGen::PathSource.new(:test, File.join(File.dirname(__FILE__), "..", "rails_generators"))]
47
+ end
48
+ end
49
+
50
+ module ShouldaAdditions
51
+ def rails_generator(*args)
52
+ setup do
53
+ run_rails_generator(*args)
54
+ end
55
+ end
56
+
57
+ def should_generate_file(file, &block)
58
+ should "generate file #{file}" do
59
+ yield("foo") if block_given?
60
+ assert_generated_file(file)
61
+ end
62
+ end
63
+
64
+ def should_not_generate_file(file)
65
+ should "not generate file #{file}" do
66
+ assert !File.exists?("#{APP_ROOT}/#{file}"),"The file '#{file}' should not exist"
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ class Thoughtbot::Shoulda::Context
73
+ include NiftyGenerators::ShouldaAdditions
74
+ end
75
+
76
+ # Mock out what we need from AR::Base.
77
+ module ActiveRecord
78
+ class Base
79
+ class << self
80
+ attr_accessor :pluralize_table_names, :columns
81
+
82
+ def add_column(name, type = :string)
83
+ returning ActiveRecord::ConnectionAdapters::Column.new(name, nil) do |column|
84
+ column.type = type
85
+ @columns ||= []
86
+ @columns << column
87
+ end
88
+ end
89
+ end
90
+ self.pluralize_table_names = true
91
+ end
92
+
93
+ module ConnectionAdapters
94
+ class Column
95
+ attr_accessor :name, :default, :type, :limit, :null, :sql_type, :precision, :scale
96
+
97
+ def initialize(name, default, sql_type = nil)
98
+ @name=name
99
+ @default=default
100
+ @type=@sql_type=sql_type
101
+ end
102
+
103
+ def human_name
104
+ @name.humanize
105
+ end
106
+ end
107
+ end
108
+ end
109
+
110
+ # And what we need from ActionView
111
+ module ActionView
112
+ module Helpers
113
+ module ActiveRecordHelper; end
114
+ class InstanceTag; end
115
+ end
116
+ end
@@ -0,0 +1,36 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper.rb")
2
+
3
+ class TestNiftyLayoutGenerator < Test::Unit::TestCase
4
+ include NiftyGenerators::TestHelper
5
+
6
+ # Some generator-related assertions:
7
+ # assert_generated_file(name, &block) # block passed the file contents
8
+ # assert_directory_exists(name)
9
+ # assert_generated_class(name, &block)
10
+ # assert_generated_module(name, &block)
11
+ # assert_generated_test_for(name, &block)
12
+ # The assert_generated_(class|module|test_for) &block is passed the body of the class/module within the file
13
+ # assert_has_method(body, *methods) # check that the body has a list of methods (methods with parentheses not supported yet)
14
+ #
15
+ # Other helper methods are:
16
+ # app_root_files - put this in teardown to show files generated by the test method (e.g. p app_root_files)
17
+ # bare_setup - place this in setup method to create the APP_ROOT folder for each test
18
+ # bare_teardown - place this in teardown method to destroy the TMP_ROOT or APP_ROOT folder after each test
19
+
20
+ context "generator without name" do
21
+ rails_generator :nifty_layout
22
+ should_generate_file 'app/views/layouts/application.html.erb'
23
+ end
24
+
25
+ context "generator with name" do
26
+ rails_generator :nifty_layout, "foobar"
27
+ should_generate_file 'app/views/layouts/foobar.html.erb'
28
+ should_generate_file 'public/stylesheets/foobar.css'
29
+ should_generate_file 'app/helpers/layout_helper.rb'
30
+ end
31
+
32
+ context "generator with CamelCase name" do
33
+ rails_generator :nifty_layout, "FooBar"
34
+ should_generate_file 'app/views/layouts/foo_bar.html.erb'
35
+ end
36
+ end
@@ -0,0 +1,257 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper.rb")
2
+
3
+ class TestNiftyScaffoldGenerator < Test::Unit::TestCase
4
+ include NiftyGenerators::TestHelper
5
+
6
+ # Some generator-related assertions:
7
+ # assert_generated_file(name, &block) # block passed the file contents
8
+ # assert_directory_exists(name)
9
+ # assert_generated_class(name, &block)
10
+ # assert_generated_module(name, &block)
11
+ # assert_generated_test_for(name, &block)
12
+ # The assert_generated_(class|module|test_for) &block is passed the body of the class/module within the file
13
+ # assert_has_method(body, *methods) # check that the body has a list of methods (methods with parentheses not supported yet)
14
+ #
15
+ # Other helper methods are:
16
+ # app_root_files - put this in teardown to show files generated by the test method (e.g. p app_root_files)
17
+ # bare_setup - place this in setup method to create the APP_ROOT folder for each test
18
+ # bare_teardown - place this in teardown method to destroy the TMP_ROOT or APP_ROOT folder after each test
19
+
20
+ context "routed" do
21
+ setup do
22
+ Dir.mkdir("#{RAILS_ROOT}/config") unless File.exists?("#{RAILS_ROOT}/config")
23
+ File.open("#{RAILS_ROOT}/config/routes.rb", 'w') do |f|
24
+ f.puts "ActionController::Routing::Routes.draw do |map|\n\nend"
25
+ end
26
+ end
27
+
28
+ teardown do
29
+ FileUtils.rm_rf "#{RAILS_ROOT}/config"
30
+ end
31
+
32
+ context "generator without name" do
33
+ should "raise usage error" do
34
+ assert_raise Rails::Generator::UsageError do
35
+ run_rails_generator :nifty_scaffold
36
+ end
37
+ end
38
+ end
39
+
40
+ context "generator with no options" do
41
+ rails_generator :nifty_scaffold, "line_item"
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
59
+
60
+ should_generate_file "app/helpers/line_items_helper.rb"
61
+
62
+ should "generate controller with class as camelcase name pluralized and all actions" do
63
+ assert_generated_file "app/controllers/line_items_controller.rb" do |body|
64
+ assert_match "class LineItemsController < ApplicationController", body
65
+ %w[index show new create edit update destroy].each do |action|
66
+ assert_match "def #{action}", body
67
+ end
68
+ end
69
+ end
70
+
71
+ %w[index show new edit].each do |action|
72
+ should_generate_file "app/views/line_items/#{action}.html.erb"
73
+ end
74
+
75
+ should "have name attribute" do
76
+ assert_generated_file "app/views/line_items/_form.html.erb" do |body|
77
+ assert_match "<%= f.text_field :name %>", body
78
+ end
79
+ end
80
+
81
+ should "add map.resources line to routes" do
82
+ assert_generated_file "config/routes.rb" do |body|
83
+ assert_match "map.resources :line_items", body
84
+ end
85
+ end
86
+ end
87
+
88
+ context "generator with index action" do
89
+ rails_generator :nifty_scaffold, "line_item", "index"
90
+
91
+ should_generate_file "app/views/line_items/index.html.erb"
92
+
93
+ should "generate controller with index action" do
94
+ assert_generated_file "app/controllers/line_items_controller.rb" do |body|
95
+ assert_match "def index", body
96
+ assert_match "@line_items = LineItem.find(:all)", body
97
+ assert_no_match(/ def index/, body)
98
+ end
99
+ end
100
+ end
101
+
102
+ context "generator with show action" do
103
+ rails_generator :nifty_scaffold, "line_item", "show"
104
+
105
+ should_generate_file "app/views/line_items/show.html.erb"
106
+
107
+ should "generate controller with show action" do
108
+ assert_generated_file "app/controllers/line_items_controller.rb" do |body|
109
+ assert_match "def show", body
110
+ assert_match "@line_item = LineItem.find(params[:id])", body
111
+ end
112
+ end
113
+ end
114
+
115
+ context "generator with new and create actions" do
116
+ rails_generator :nifty_scaffold, "line_item", "new", "create"
117
+
118
+ should_not_generate_file "app/views/line_items/create.html.erb"
119
+ should_not_generate_file "app/views/line_items/_form.html.erb"
120
+
121
+ should "render form in 'new' template" do
122
+ assert_generated_file "app/views/line_items/new.html.erb" do |body|
123
+ assert_match "<% form_for @line_item do |f| %>", body
124
+ end
125
+ end
126
+
127
+ should "generate controller with actions" do
128
+ assert_generated_file "app/controllers/line_items_controller.rb" do |body|
129
+ assert_match "def new", body
130
+ assert_match "@line_item = LineItem.new\n", body
131
+ assert_match "def create", body
132
+ assert_match "@line_item = LineItem.new(params[:line_item])", body
133
+ assert_match "if @line_item.save", body
134
+ assert_match "redirect_to line_items_path", body
135
+ assert_match "render :action => 'new'", body
136
+ end
137
+ end
138
+ end
139
+
140
+ context "generator with edit and update actions" do
141
+ rails_generator :nifty_scaffold, "line_item", "edit", "update"
142
+
143
+ should_not_generate_file "app/views/line_items/update.html.erb"
144
+ should_not_generate_file "app/views/line_items/_form.html.erb"
145
+
146
+ should "render form in 'edit' template" do
147
+ assert_generated_file "app/views/line_items/edit.html.erb" do |body|
148
+ assert_match "<% form_for @line_item do |f| %>", body
149
+ end
150
+ end
151
+
152
+ should "generate controller with actions" do
153
+ assert_generated_file "app/controllers/line_items_controller.rb" do |body|
154
+ assert_match "def edit", body
155
+ assert_match "@line_item = LineItem.find(params[:id])", body
156
+ assert_match "def update", body
157
+ assert_match "if @line_item.update_attributes(params[:line_item])", body
158
+ assert_match "redirect_to line_items_path", body
159
+ assert_match "render :action => 'edit'", body
160
+ end
161
+ end
162
+ end
163
+
164
+ context "generator with edit and update actions" do
165
+ rails_generator :nifty_scaffold, "line_item", "destroy"
166
+
167
+ should_not_generate_file "app/views/line_items/destroy.html.erb"
168
+
169
+ should "generate controller with action" do
170
+ assert_generated_file "app/controllers/line_items_controller.rb" do |body|
171
+ assert_match "def destroy", body
172
+ assert_match "@line_item = LineItem.find(params[:id])", body
173
+ assert_match "@line_item.destroy", body
174
+ assert_match "redirect_to line_items_path", body
175
+ end
176
+ end
177
+ end
178
+
179
+ context "generator with new and edit actions" do
180
+ rails_generator :nifty_scaffold, "line_item", "new", "edit"
181
+
182
+ should_generate_file "app/views/line_items/_form.html.erb"
183
+
184
+ should "render the form partial in views" do
185
+ %w[new edit].each do |action|
186
+ assert_generated_file "app/views/line_items/#{action}.html.erb" do |body|
187
+ assert_match "<%= render :partial => 'form' %>", body
188
+ end
189
+ end
190
+ end
191
+ end
192
+
193
+ context "generator with attributes and actions" do
194
+ rails_generator :nifty_scaffold, "line_item", "name:string", "new", "price:float", "index", "available:boolean"
195
+
196
+ should "render a form field for each attribute in 'new' template" do
197
+ assert_generated_file "app/views/line_items/new.html.erb" do |body|
198
+ assert_match "<%= f.text_field :name %>", body
199
+ assert_match "<%= f.text_field :price %>", body
200
+ assert_match "<%= f.check_box :available %>", body
201
+ end
202
+ end
203
+ end
204
+
205
+ context "generator with show, create, and update actions" do
206
+ rails_generator :nifty_scaffold, "line_item", "show", "create", "update"
207
+
208
+ should "redirect to line item show page, not index" do
209
+ assert_generated_file "app/controllers/line_items_controller.rb" do |body|
210
+ assert_match "redirect_to @line_item", body
211
+ assert_no_match(/redirect_to line_items_path/, body)
212
+ end
213
+ end
214
+ end
215
+
216
+ context "existing model" do
217
+ setup do
218
+ Dir.mkdir("#{RAILS_ROOT}/app") unless File.exists?("#{RAILS_ROOT}/app")
219
+ Dir.mkdir("#{RAILS_ROOT}/app/models") unless File.exists?("#{RAILS_ROOT}/app/models")
220
+ File.open("#{RAILS_ROOT}/app/models/recipe.rb", 'w') do |f|
221
+ f.puts "raise 'should not be loaded'"
222
+ end
223
+ end
224
+
225
+ teardown do
226
+ FileUtils.rm_rf "#{RAILS_ROOT}/app"
227
+ end
228
+
229
+ context "generator with no options" do
230
+ rails_generator :nifty_scaffold, "recipe"
231
+
232
+ should "use model columns for attributes" do
233
+ assert_generated_file "app/views/recipes/_form.html.erb" do |body|
234
+ assert_match "<%= f.text_field :foo %>", body
235
+ assert_match "<%= f.text_field :bar %>", body
236
+ end
237
+ end
238
+ end
239
+
240
+ context "generator with attribute specified" do
241
+ rails_generator :nifty_scaffold, "recipe", "zippo:string"
242
+
243
+ should "use specified attribute" do
244
+ assert_generated_file "app/views/recipes/_form.html.erb" do |body|
245
+ assert_match "<%= f.text_field :zippo %>", body
246
+ end
247
+ end
248
+ end
249
+ end
250
+ end
251
+ end
252
+
253
+ # just an example model we can use
254
+ class Recipe < ActiveRecord::Base
255
+ add_column :foo, :string
256
+ add_column :bar, :string
257
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ryanb-nifty-generators
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Bates
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-07 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A collection of Ryan's generator scripts for Rails.
17
+ email: ryan@railscasts.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README
25
+ - TODO
26
+ files:
27
+ - LICENSE
28
+ - Manifest
29
+ - rails_generators/nifty_layout/nifty_layout_generator.rb
30
+ - rails_generators/nifty_layout/templates/helper.rb
31
+ - rails_generators/nifty_layout/templates/layout.html.erb
32
+ - rails_generators/nifty_layout/templates/stylesheet.css
33
+ - rails_generators/nifty_layout/USAGE
34
+ - rails_generators/nifty_scaffold/nifty_scaffold_generator.rb
35
+ - rails_generators/nifty_scaffold/templates/actions/create.rb
36
+ - rails_generators/nifty_scaffold/templates/actions/destroy.rb
37
+ - rails_generators/nifty_scaffold/templates/actions/edit.rb
38
+ - rails_generators/nifty_scaffold/templates/actions/index.rb
39
+ - rails_generators/nifty_scaffold/templates/actions/new.rb
40
+ - rails_generators/nifty_scaffold/templates/actions/show.rb
41
+ - rails_generators/nifty_scaffold/templates/actions/update.rb
42
+ - rails_generators/nifty_scaffold/templates/controller.rb
43
+ - rails_generators/nifty_scaffold/templates/migration.rb
44
+ - rails_generators/nifty_scaffold/templates/model.rb
45
+ - rails_generators/nifty_scaffold/templates/views/_form.html.erb
46
+ - rails_generators/nifty_scaffold/templates/views/edit.html.erb
47
+ - rails_generators/nifty_scaffold/templates/views/index.html.erb
48
+ - rails_generators/nifty_scaffold/templates/views/new.html.erb
49
+ - rails_generators/nifty_scaffold/templates/views/show.html.erb
50
+ - rails_generators/nifty_scaffold/USAGE
51
+ - README
52
+ - test/test_helper.rb
53
+ - test/test_nifty_layout_generator.rb
54
+ - test/test_nifty_scaffold_generator.rb
55
+ - TODO
56
+ - nifty-generators.gemspec
57
+ has_rdoc: true
58
+ homepage: http://github.com/ryanb/nifty-generators
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --line-numbers
62
+ - --inline-source
63
+ - --title
64
+ - Nifty-generators
65
+ - --main
66
+ - README
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ version:
81
+ requirements: []
82
+
83
+ rubyforge_project: nifty-generators
84
+ rubygems_version: 1.0.1
85
+ signing_key:
86
+ specification_version: 2
87
+ summary: A collection of Ryan's generator scripts for Rails.
88
+ test_files:
89
+ - test/test_helper.rb
90
+ - test/test_nifty_layout_generator.rb
91
+ - test/test_nifty_scaffold_generator.rb