nifty-generators 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. data/CHANGELOG +43 -0
  2. data/LICENSE +20 -0
  3. data/Manifest +74 -0
  4. data/README +75 -0
  5. data/Rakefile +16 -0
  6. data/TODO +7 -0
  7. data/lib/nifty_generators.rb +3 -0
  8. data/nifty-generators.gemspec +142 -0
  9. data/rails_generators/nifty_config/USAGE +23 -0
  10. data/rails_generators/nifty_config/nifty_config_generator.rb +32 -0
  11. data/rails_generators/nifty_config/templates/config.yml +8 -0
  12. data/rails_generators/nifty_config/templates/load_config.rb +2 -0
  13. data/rails_generators/nifty_layout/USAGE +25 -0
  14. data/rails_generators/nifty_layout/nifty_layout_generator.rb +44 -0
  15. data/rails_generators/nifty_layout/templates/helper.rb +23 -0
  16. data/rails_generators/nifty_layout/templates/layout.html.erb +22 -0
  17. data/rails_generators/nifty_layout/templates/layout.html.haml +19 -0
  18. data/rails_generators/nifty_layout/templates/stylesheet.css +80 -0
  19. data/rails_generators/nifty_layout/templates/stylesheet.sass +66 -0
  20. data/rails_generators/nifty_scaffold/USAGE +51 -0
  21. data/rails_generators/nifty_scaffold/nifty_scaffold_generator.rb +224 -0
  22. data/rails_generators/nifty_scaffold/templates/actions/create.rb +9 -0
  23. data/rails_generators/nifty_scaffold/templates/actions/destroy.rb +6 -0
  24. data/rails_generators/nifty_scaffold/templates/actions/edit.rb +3 -0
  25. data/rails_generators/nifty_scaffold/templates/actions/index.rb +3 -0
  26. data/rails_generators/nifty_scaffold/templates/actions/new.rb +3 -0
  27. data/rails_generators/nifty_scaffold/templates/actions/show.rb +3 -0
  28. data/rails_generators/nifty_scaffold/templates/actions/update.rb +9 -0
  29. data/rails_generators/nifty_scaffold/templates/controller.rb +3 -0
  30. data/rails_generators/nifty_scaffold/templates/fixtures.yml +9 -0
  31. data/rails_generators/nifty_scaffold/templates/helper.rb +2 -0
  32. data/rails_generators/nifty_scaffold/templates/migration.rb +16 -0
  33. data/rails_generators/nifty_scaffold/templates/model.rb +2 -0
  34. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/create.rb +11 -0
  35. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/destroy.rb +6 -0
  36. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/edit.rb +4 -0
  37. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/index.rb +4 -0
  38. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/new.rb +4 -0
  39. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/show.rb +4 -0
  40. data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/update.rb +11 -0
  41. data/rails_generators/nifty_scaffold/templates/tests/rspec/controller.rb +8 -0
  42. data/rails_generators/nifty_scaffold/templates/tests/rspec/model.rb +7 -0
  43. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/create.rb +13 -0
  44. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/destroy.rb +8 -0
  45. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/edit.rb +6 -0
  46. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/index.rb +6 -0
  47. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/new.rb +6 -0
  48. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/show.rb +6 -0
  49. data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/update.rb +13 -0
  50. data/rails_generators/nifty_scaffold/templates/tests/shoulda/controller.rb +5 -0
  51. data/rails_generators/nifty_scaffold/templates/tests/shoulda/model.rb +7 -0
  52. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/create.rb +11 -0
  53. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/destroy.rb +6 -0
  54. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/edit.rb +4 -0
  55. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/index.rb +4 -0
  56. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/new.rb +4 -0
  57. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/show.rb +4 -0
  58. data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/update.rb +11 -0
  59. data/rails_generators/nifty_scaffold/templates/tests/testunit/controller.rb +5 -0
  60. data/rails_generators/nifty_scaffold/templates/tests/testunit/model.rb +7 -0
  61. data/rails_generators/nifty_scaffold/templates/views/erb/_form.html.erb +10 -0
  62. data/rails_generators/nifty_scaffold/templates/views/erb/edit.html.erb +14 -0
  63. data/rails_generators/nifty_scaffold/templates/views/erb/index.html.erb +29 -0
  64. data/rails_generators/nifty_scaffold/templates/views/erb/new.html.erb +7 -0
  65. data/rails_generators/nifty_scaffold/templates/views/erb/show.html.erb +20 -0
  66. data/rails_generators/nifty_scaffold/templates/views/haml/_form.html.haml +11 -0
  67. data/rails_generators/nifty_scaffold/templates/views/haml/edit.html.haml +14 -0
  68. data/rails_generators/nifty_scaffold/templates/views/haml/index.html.haml +25 -0
  69. data/rails_generators/nifty_scaffold/templates/views/haml/new.html.haml +7 -0
  70. data/rails_generators/nifty_scaffold/templates/views/haml/show.html.haml +20 -0
  71. data/tasks/deployment.rake +2 -0
  72. data/test/test_helper.rb +117 -0
  73. data/test/test_nifty_config_generator.rb +37 -0
  74. data/test/test_nifty_layout_generator.rb +42 -0
  75. data/test/test_nifty_scaffold_generator.rb +532 -0
  76. metadata +140 -0
@@ -0,0 +1,7 @@
1
+ - title "New <%= singular_name.titleize %>"
2
+
3
+ <%= render_form %>
4
+
5
+ <%- if action? :index -%>
6
+ %p= link_to "Back to List", <%= plural_name %>_path
7
+ <%- end -%>
@@ -0,0 +1,20 @@
1
+ - title "<%= singular_name.titleize %>"
2
+
3
+ <%- for attribute in attributes -%>
4
+ %p
5
+ %strong <%= attribute.column.human_name.titleize %>:
6
+ =h @<%= singular_name %>.<%= attribute.name %>
7
+ <%- end -%>
8
+
9
+ %p
10
+ <%- if action? :edit -%>
11
+ = link_to "Edit", edit_<%= singular_name %>_path(@<%= singular_name %>)
12
+ |
13
+ <%- end -%>
14
+ <%- if action? :destroy -%>
15
+ = link_to "Destroy", @<%= singular_name %>, :confirm => 'Are you sure?', :method => :delete
16
+ |
17
+ <%- end -%>
18
+ <%- if action? :index -%>
19
+ = link_to "View All", <%= plural_name %>_path
20
+ <%- end -%>
@@ -0,0 +1,2 @@
1
+ desc "Build the manifest and gemspec files."
2
+ task :build => [:build_manifest, :build_gemspec]
@@ -0,0 +1,117 @@
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
+ gem 'rails', '2.0.2' # getting a Rails.configuration error with 2.1
20
+ require 'rubigen' # gem install rubigen
21
+ require 'rubigen/helpers/generator_test_helper'
22
+ require 'rails_generator'
23
+ require 'shoulda' # gem install Shoulda
24
+ require 'mocha'
25
+
26
+ module NiftyGenerators
27
+ module TestHelper
28
+ include RubiGen::GeneratorTestHelper
29
+
30
+ def setup
31
+ bare_setup
32
+ end
33
+
34
+ def teardown
35
+ bare_teardown
36
+ end
37
+
38
+ protected
39
+
40
+ def run_rails_generator(generator, *args)
41
+ options = args.pop if args.last.kind_of? Hash
42
+ options ||= {}
43
+ run_generator(generator.to_s, args, generator_sources, options.reverse_merge(:quiet => true))
44
+ end
45
+
46
+ def generator_sources
47
+ [RubiGen::PathSource.new(:test, File.join(File.dirname(__FILE__), "..", "rails_generators"))]
48
+ end
49
+ end
50
+
51
+ module ShouldaAdditions
52
+ def rails_generator(*args)
53
+ setup do
54
+ run_rails_generator(*args)
55
+ end
56
+ end
57
+
58
+ def should_generate_file(file, &block)
59
+ should "generate file #{file}" do
60
+ yield("foo") if block_given?
61
+ assert_generated_file(file)
62
+ end
63
+ end
64
+
65
+ def should_not_generate_file(file)
66
+ should "not generate file #{file}" do
67
+ assert !File.exists?("#{APP_ROOT}/#{file}"),"The file '#{file}' should not exist"
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ class Thoughtbot::Shoulda::Context
74
+ include NiftyGenerators::ShouldaAdditions
75
+ end
76
+
77
+ # Mock out what we need from AR::Base.
78
+ module ActiveRecord
79
+ class Base
80
+ class << self
81
+ attr_accessor :pluralize_table_names, :columns
82
+
83
+ def add_column(name, type = :string)
84
+ returning ActiveRecord::ConnectionAdapters::Column.new(name, nil) do |column|
85
+ column.type = type
86
+ @columns ||= []
87
+ @columns << column
88
+ end
89
+ end
90
+ end
91
+ self.pluralize_table_names = true
92
+ end
93
+
94
+ module ConnectionAdapters
95
+ class Column
96
+ attr_accessor :name, :default, :type, :limit, :null, :sql_type, :precision, :scale
97
+
98
+ def initialize(name, default, sql_type = nil)
99
+ @name=name
100
+ @default=default
101
+ @type=@sql_type=sql_type
102
+ end
103
+
104
+ def human_name
105
+ @name.humanize
106
+ end
107
+ end
108
+ end
109
+ end
110
+
111
+ # And what we need from ActionView
112
+ module ActionView
113
+ module Helpers
114
+ module ActiveRecordHelper; end
115
+ class InstanceTag; end
116
+ end
117
+ end
@@ -0,0 +1,37 @@
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_config
22
+ should_generate_file 'config/app_config.yml'
23
+ should_generate_file 'config/initializers/load_app_config.rb'
24
+ end
25
+
26
+ context "generator with name" do
27
+ rails_generator :nifty_config, "FooBar"
28
+ should_generate_file 'config/foo_bar_config.yml'
29
+
30
+ should "use the name as a constant in the initializer" do
31
+ assert_generated_file 'config/initializers/load_foo_bar_config.rb' do |body|
32
+ assert_match "config/foo_bar_config.yml", body
33
+ assert_match "FOO_BAR_CONFIG = ", body
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,42 @@
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
+
37
+ context "generator with haml option" do
38
+ rails_generator :nifty_layout, "foobar", :haml => true
39
+ should_generate_file 'app/views/layouts/foobar.html.haml'
40
+ should_generate_file 'public/stylesheets/sass/foobar.sass'
41
+ end
42
+ end
@@ -0,0 +1,532 @@
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 and no existing model" do
41
+ rails_generator :nifty_scaffold, "LineItem"
42
+
43
+ should_generate_file "app/helpers/line_items_helper.rb"
44
+
45
+ should "generate controller with class as camelcase name pluralized and all actions" do
46
+ assert_generated_file "app/controllers/line_items_controller.rb" do |body|
47
+ assert_match "class LineItemsController < ApplicationController", body
48
+ %w[index show new create edit update destroy].each do |action|
49
+ assert_match "def #{action}", body
50
+ end
51
+ end
52
+ end
53
+
54
+ %w[index show new edit].each do |action|
55
+ should_generate_file "app/views/line_items/#{action}.html.erb"
56
+ end
57
+
58
+ should "have name attribute" do
59
+ assert_generated_file "app/views/line_items/_form.html.erb" do |body|
60
+ assert_match "<%= f.text_field :name %>", body
61
+ end
62
+ end
63
+
64
+ should "add map.resources line to routes" do
65
+ assert_generated_file "config/routes.rb" do |body|
66
+ assert_match "map.resources :line_items", body
67
+ end
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
93
+ end
94
+
95
+ context "generator with index action" do
96
+ rails_generator :nifty_scaffold, "line_item", "index"
97
+
98
+ should_generate_file "app/views/line_items/index.html.erb"
99
+
100
+ should "generate controller with index action" do
101
+ assert_generated_file "app/controllers/line_items_controller.rb" do |body|
102
+ assert_match "def index", body
103
+ assert_match "@line_items = LineItem.find(:all)", body
104
+ assert_no_match(/ def index/, body)
105
+ end
106
+ end
107
+ end
108
+
109
+ context "generator with show action" do
110
+ rails_generator :nifty_scaffold, "line_item", "show"
111
+
112
+ should_generate_file "app/views/line_items/show.html.erb"
113
+
114
+ should "generate controller with show action" do
115
+ assert_generated_file "app/controllers/line_items_controller.rb" do |body|
116
+ assert_match "def show", body
117
+ assert_match "@line_item = LineItem.find(params[:id])", body
118
+ end
119
+ end
120
+ end
121
+
122
+ context "generator with new and create actions" do
123
+ rails_generator :nifty_scaffold, "line_item", "new", "create"
124
+
125
+ should_not_generate_file "app/views/line_items/create.html.erb"
126
+ should_not_generate_file "app/views/line_items/_form.html.erb"
127
+
128
+ should "render form in 'new' template" do
129
+ assert_generated_file "app/views/line_items/new.html.erb" do |body|
130
+ assert_match "<% form_for @line_item do |f| %>", body
131
+ end
132
+ end
133
+
134
+ should "generate controller with actions" do
135
+ assert_generated_file "app/controllers/line_items_controller.rb" do |body|
136
+ assert_match "def new", body
137
+ assert_match "@line_item = LineItem.new\n", body
138
+ assert_match "def create", body
139
+ assert_match "@line_item = LineItem.new(params[:line_item])", body
140
+ assert_match "if @line_item.save", body
141
+ assert_match "flash[:notice] = \"Successfully created line item.\"", body
142
+ assert_match "redirect_to line_items_url", body
143
+ assert_match "render :action => 'new'", body
144
+ end
145
+ end
146
+ end
147
+
148
+ context "generator with edit and update actions" do
149
+ rails_generator :nifty_scaffold, "line_item", "edit", "update"
150
+
151
+ should_not_generate_file "app/views/line_items/update.html.erb"
152
+ should_not_generate_file "app/views/line_items/_form.html.erb"
153
+
154
+ should "render form in 'edit' template" do
155
+ assert_generated_file "app/views/line_items/edit.html.erb" do |body|
156
+ assert_match "<% form_for @line_item do |f| %>", body
157
+ end
158
+ end
159
+
160
+ should "generate controller with actions" do
161
+ assert_generated_file "app/controllers/line_items_controller.rb" do |body|
162
+ assert_match "def edit", body
163
+ assert_match "@line_item = LineItem.find(params[:id])", body
164
+ assert_match "def update", body
165
+ assert_match "if @line_item.update_attributes(params[:line_item])", body
166
+ assert_match "flash[:notice] = \"Successfully updated line item.\"", body
167
+ assert_match "redirect_to line_items_url", body
168
+ assert_match "render :action => 'edit'", body
169
+ end
170
+ end
171
+ end
172
+
173
+ context "generator with edit and update actions" do
174
+ rails_generator :nifty_scaffold, "line_item", "destroy"
175
+
176
+ should_not_generate_file "app/views/line_items/destroy.html.erb"
177
+
178
+ should "generate controller with action" do
179
+ assert_generated_file "app/controllers/line_items_controller.rb" do |body|
180
+ assert_match "def destroy", body
181
+ assert_match "@line_item = LineItem.find(params[:id])", body
182
+ assert_match "@line_item.destroy", body
183
+ assert_match "flash[:notice] = \"Successfully destroyed line item.\"", body
184
+ assert_match "redirect_to line_items_url", body
185
+ end
186
+ end
187
+ end
188
+
189
+ context "generator with new and edit actions" do
190
+ rails_generator :nifty_scaffold, "line_item", "new", "edit"
191
+
192
+ should_generate_file "app/views/line_items/_form.html.erb"
193
+
194
+ should "render the form partial in views" do
195
+ %w[new edit].each do |action|
196
+ assert_generated_file "app/views/line_items/#{action}.html.erb" do |body|
197
+ assert_match "<%= render :partial => 'form' %>", body
198
+ end
199
+ end
200
+ end
201
+ end
202
+
203
+ context "generator with attributes and actions" do
204
+ rails_generator :nifty_scaffold, "line_item", "name:string", "new", "price:float", "index", "available:boolean"
205
+
206
+ should "render a form field for each attribute in 'new' template" do
207
+ assert_generated_file "app/views/line_items/new.html.erb" do |body|
208
+ assert_match "<%= f.text_field :name %>", body
209
+ assert_match "<%= f.text_field :price %>", body
210
+ assert_match "<%= f.check_box :available %>", body
211
+ end
212
+ end
213
+ end
214
+
215
+ context "generator with show, create, and update actions" do
216
+ rails_generator :nifty_scaffold, "line_item", "show", "create", "update"
217
+
218
+ should "redirect to line item show page, not index" do
219
+ assert_generated_file "app/controllers/line_items_controller.rb" do |body|
220
+ assert_match "redirect_to @line_item", body
221
+ assert_no_match(/redirect_to line_items_url/, body)
222
+ end
223
+ end
224
+ end
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
+
303
+ context "existing model" do
304
+ setup do
305
+ Dir.mkdir("#{RAILS_ROOT}/app") unless File.exists?("#{RAILS_ROOT}/app")
306
+ Dir.mkdir("#{RAILS_ROOT}/app/models") unless File.exists?("#{RAILS_ROOT}/app/models")
307
+ File.open("#{RAILS_ROOT}/app/models/recipe.rb", 'w') do |f|
308
+ f.puts "raise 'should not be loaded'"
309
+ end
310
+ end
311
+
312
+ teardown do
313
+ FileUtils.rm_rf "#{RAILS_ROOT}/app"
314
+ end
315
+
316
+ context "generator with skip model option" do
317
+ rails_generator :nifty_scaffold, "recipe", :skip_model => true
318
+
319
+ should "use model columns for attributes" do
320
+ assert_generated_file "app/views/recipes/_form.html.erb" do |body|
321
+ assert_match "<%= f.text_field :foo %>", body
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)
327
+ end
328
+ end
329
+
330
+ should "not generate migration file" do
331
+ assert Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").empty?
332
+ end
333
+ end
334
+
335
+ context "generator with attribute specified" do
336
+ rails_generator :nifty_scaffold, "recipe", "zippo:string"
337
+
338
+ should "use specified attribute" do
339
+ assert_generated_file "app/views/recipes/_form.html.erb" do |body|
340
+ assert_match "<%= f.text_field :zippo %>", body
341
+ end
342
+ end
343
+ end
344
+ end
345
+
346
+ context "with spec dir" do
347
+ setup do
348
+ Dir.mkdir("#{RAILS_ROOT}/spec") unless File.exists?("#{RAILS_ROOT}/spec")
349
+ end
350
+
351
+ teardown do
352
+ FileUtils.rm_rf "#{RAILS_ROOT}/spec"
353
+ end
354
+
355
+ context "generator with some attributes" do
356
+ rails_generator :nifty_scaffold, "line_item", "name:string", "description:text"
357
+
358
+ should_generate_file "spec/models/line_item_spec.rb"
359
+
360
+ should "have controller specs for each action" do
361
+ assert_generated_file "spec/controllers/line_items_controller_spec.rb" do |body|
362
+ assert_match "get :index", body
363
+ assert_match "get :show", body
364
+ assert_match "get :new", body
365
+ assert_match "get :edit", body
366
+ assert_match "post :create", body
367
+ assert_match "put :update", body
368
+ assert_match "delete :destroy", body
369
+ end
370
+ end
371
+
372
+ should "have fixture with attributes" do
373
+ assert_generated_file "spec/fixtures/line_items.yml" do |body|
374
+ assert_match "name: MyString", body
375
+ assert_match "description: MyText", body
376
+ end
377
+ end
378
+ end
379
+
380
+ context "generator with new and index actions" do
381
+ rails_generator :nifty_scaffold, "line_item", "new", "index"
382
+
383
+ should "have controller spec with only mentioned actions" do
384
+ assert_generated_file "spec/controllers/line_items_controller_spec.rb" do |body|
385
+ assert_match "get :index", body
386
+ assert_match "get :new", body
387
+ assert_match "post :create", body
388
+ assert_no_match(/get :show/, body)
389
+ assert_no_match(/get :edit/, body)
390
+ assert_no_match(/put :update/, body)
391
+ assert_no_match(/delete :destroy/, body)
392
+ end
393
+ end
394
+
395
+ should "redirect to index action on successful create" do
396
+ assert_generated_file "spec/controllers/line_items_controller_spec.rb" do |body|
397
+ assert_match "redirect_to(line_items_url)", body
398
+ end
399
+ end
400
+ end
401
+
402
+ context "generator with edit and index actions" do
403
+ rails_generator :nifty_scaffold, "line_item", "edit", "index"
404
+
405
+ should "redirect to index action on successful update" do
406
+ assert_generated_file "spec/controllers/line_items_controller_spec.rb" do |body|
407
+ assert_match "redirect_to(line_items_url)", body
408
+ end
409
+ end
410
+ end
411
+
412
+ context "generator with testunit specified" do
413
+ rails_generator :nifty_scaffold, "line_item", "name:string", :test_framework => :testunit
414
+
415
+ should_generate_file "test/unit/line_item_test.rb"
416
+ end
417
+ end
418
+
419
+ context "with test dir" do
420
+ setup do
421
+ Dir.mkdir("#{RAILS_ROOT}/test") unless File.exists?("#{RAILS_ROOT}/test")
422
+ end
423
+
424
+ teardown do
425
+ FileUtils.rm_rf "#{RAILS_ROOT}/test"
426
+ end
427
+
428
+ context "generator with some attributes" do
429
+ rails_generator :nifty_scaffold, "line_item", "name:string", "description:text"
430
+
431
+ should_generate_file "test/unit/line_item_test.rb"
432
+
433
+ should "have controller tests for each action" do
434
+ assert_generated_file "test/functional/line_items_controller_test.rb" do |body|
435
+ assert_match "get :index", body
436
+ assert_match "get :show", body
437
+ assert_match "get :new", body
438
+ assert_match "get :edit", body
439
+ assert_match "post :create", body
440
+ assert_match "put :update", body
441
+ assert_match "delete :destroy", body
442
+ end
443
+ end
444
+
445
+ should "have fixture with attributes" do
446
+ assert_generated_file "test/fixtures/line_items.yml" do |body|
447
+ assert_match "name: MyString", body
448
+ assert_match "description: MyText", body
449
+ end
450
+ end
451
+ end
452
+
453
+ context "generator with new and index actions" do
454
+ rails_generator :nifty_scaffold, "line_item", "new", "index"
455
+
456
+ should "have controller test with only mentioned actions" do
457
+ assert_generated_file "test/functional/line_items_controller_test.rb" do |body|
458
+ assert_match "get :index", body
459
+ assert_match "get :new", body
460
+ assert_match "post :create", body
461
+ assert_no_match(/get :show/, body)
462
+ assert_no_match(/get :edit/, body)
463
+ assert_no_match(/put :update/, body)
464
+ assert_no_match(/delete :destroy/, body)
465
+ end
466
+ end
467
+
468
+ should "redirect to index action on successful create" do
469
+ assert_generated_file "test/functional/line_items_controller_test.rb" do |body|
470
+ assert_match "assert_redirected_to line_items_url", body
471
+ end
472
+ end
473
+ end
474
+
475
+ context "generator with edit and index actions" do
476
+ rails_generator :nifty_scaffold, "line_item", "edit", "index"
477
+
478
+ should "redirect to index action on successful update" do
479
+ assert_generated_file "test/functional/line_items_controller_test.rb" do |body|
480
+ assert_match "assert_redirected_to line_items_url", body
481
+ end
482
+ end
483
+ end
484
+
485
+ context "generator with rspec specified" do
486
+ rails_generator :nifty_scaffold, "line_item", "name:string", :test_framework => :rspec
487
+
488
+ should_generate_file "spec/models/line_item_spec.rb"
489
+ end
490
+
491
+ context "generator with shoulda specified" do
492
+ rails_generator :nifty_scaffold, "line_item", "name:string", :test_framework => :shoulda
493
+
494
+ should "have controller and model tests using shoulda syntax" do
495
+ assert_generated_file "test/functional/line_items_controller_test.rb" do |body|
496
+ assert_match " should ", body
497
+ end
498
+
499
+ assert_generated_file "test/unit/line_item_test.rb" do |body|
500
+ assert_match " should ", body
501
+ end
502
+ end
503
+ end
504
+ end
505
+
506
+ context "generator with haml option" do
507
+ rails_generator :nifty_scaffold, "LineItem", :haml => true
508
+
509
+ %w[index show new edit _form].each do |action|
510
+ should_generate_file "app/views/line_items/#{action}.html.haml"
511
+ end
512
+
513
+ should "render the form partial in views" do
514
+ %w[new edit].each do |action|
515
+ assert_generated_file "app/views/line_items/#{action}.html.haml" do |body|
516
+ assert_match /^= render :partial => 'form'$/, body
517
+ end
518
+ end
519
+ end
520
+ end
521
+ end
522
+ end
523
+
524
+ # just an example model we can use
525
+ class Recipe < ActiveRecord::Base
526
+ add_column :id, :integer
527
+ add_column :foo, :string
528
+ add_column :bar, :string
529
+ add_column :book_id, :integer
530
+ add_column :created_at, :datetime
531
+ add_column :updated_at, :datetime
532
+ end