mconnell-generators 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/CHANGELOG +43 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +46 -0
- data/Rakefile +17 -0
- data/TODO +11 -0
- data/bin/rubaidh_rails +11 -0
- data/generators.gemspec +59 -0
- data/generators/rubaidh_controller/USAGE +31 -0
- data/generators/rubaidh_controller/rubaidh_controller_generator.rb +40 -0
- data/generators/rubaidh_controller/templates/controller.rb +17 -0
- data/generators/rubaidh_controller/templates/controller_spec.rb +29 -0
- data/generators/rubaidh_controller/templates/view.html.erb +17 -0
- data/generators/rubaidh_helper/USAGE +23 -0
- data/generators/rubaidh_helper/rubaidh_helper_generator.rb +27 -0
- data/generators/rubaidh_helper/templates/helper.rb +12 -0
- data/generators/rubaidh_helper/templates/helper_spec.rb +14 -0
- data/generators/rubaidh_layout/rubaidh_layout_generator.rb +27 -0
- data/generators/rubaidh_layout/templates/base.css +334 -0
- data/generators/rubaidh_layout/templates/layout.html.erb +36 -0
- data/generators/rubaidh_layout/templates/style.css +321 -0
- data/generators/rubaidh_model/USAGE +25 -0
- data/generators/rubaidh_model/rubaidh_model_generator.rb +29 -0
- data/generators/rubaidh_model/templates/migration.rb +25 -0
- data/generators/rubaidh_model/templates/model.rb +15 -0
- data/generators/rubaidh_model/templates/model_exemplar.rb +13 -0
- data/generators/rubaidh_model/templates/model_spec.rb +31 -0
- data/generators/rubaidh_named_base.rb +31 -0
- data/generators/rubaidh_scaffold/USAGE +29 -0
- data/generators/rubaidh_scaffold/rubaidh_scaffold_generator.rb +90 -0
- data/generators/rubaidh_scaffold/templates/controller.rb +76 -0
- data/generators/rubaidh_scaffold/templates/controller_spec.rb +251 -0
- data/generators/rubaidh_scaffold/templates/partial_form.html.erb +13 -0
- data/generators/rubaidh_scaffold/templates/partial_layout.html.erb +13 -0
- data/generators/rubaidh_scaffold/templates/partial_model.html.erb +8 -0
- data/generators/rubaidh_scaffold/templates/routing_spec.rb +73 -0
- data/generators/rubaidh_scaffold/templates/view_edit.html.erb +10 -0
- data/generators/rubaidh_scaffold/templates/view_index.html.erb +10 -0
- data/generators/rubaidh_scaffold/templates/view_new.html.erb +9 -0
- data/generators/rubaidh_scaffold/templates/view_show.html.erb +17 -0
- data/templates/rubaidh.rb +115 -0
- metadata +113 -0
@@ -0,0 +1,25 @@
|
|
1
|
+
Description:
|
2
|
+
Stubs out a new model. Pass the model name, either CamelCased or
|
3
|
+
under_scored, and an optional list of attribute pairs as arguments.
|
4
|
+
|
5
|
+
Attribute pairs are column_name:sql_type arguments specifying the
|
6
|
+
model's attributes. Timestamps are added by default, so you don't have to
|
7
|
+
specify them by hand as 'created_at:datetime updated_at:datetime'.
|
8
|
+
|
9
|
+
You don't have to think up every attribute up front, but it helps to
|
10
|
+
sketch out a few so you can start working with the model immediately.
|
11
|
+
|
12
|
+
This generates a model class in app/models, an rspec specification in
|
13
|
+
spec/models and a migration in db/migrate.
|
14
|
+
|
15
|
+
Examples:
|
16
|
+
`./script/generate model account`
|
17
|
+
|
18
|
+
creates an Account model, spec and migration:
|
19
|
+
Model: app/models/account.rb
|
20
|
+
Spec: spec/models/account_spec.rb
|
21
|
+
Migration: db/migrate/XXX_add_accounts.rb
|
22
|
+
|
23
|
+
`./script/generate model post title:string body:text published:boolean`
|
24
|
+
|
25
|
+
creates a Post model with a string title, text body, and published flag.
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'rubaidh_named_base')
|
2
|
+
|
3
|
+
class RubaidhModelGenerator < RubaidhNamedBase
|
4
|
+
def manifest
|
5
|
+
record do |m|
|
6
|
+
# Check for class naming collisions.
|
7
|
+
m.class_collisions class_name
|
8
|
+
|
9
|
+
# Model, test, and fixture directories.
|
10
|
+
m.directory File.join('app/models', class_path)
|
11
|
+
m.directory File.join('spec/models', class_path)
|
12
|
+
m.directory File.join('spec/exemplars', class_path)
|
13
|
+
|
14
|
+
# Model class, unit test, and fixtures.
|
15
|
+
m.template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
|
16
|
+
m.template 'model_spec.rb', File.join('spec/models', class_path, "#{file_name}_spec.rb")
|
17
|
+
m.template 'model_exemplar.rb', File.join('spec/exemplars', class_path, "#{file_name}_exemplar.rb")
|
18
|
+
|
19
|
+
m.migration_template 'migration.rb', 'db/migrate', :assigns => {
|
20
|
+
:migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}"
|
21
|
+
}, :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
protected
|
26
|
+
def banner
|
27
|
+
"Usage: #{$0} #{spec.name} ModelName [field:type] ..."
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# <%= migration_name %> Migration
|
2
|
+
#
|
3
|
+
# Created on <%= Time.now.to_s :long %> by <%= user_full_name %> as part
|
4
|
+
# of the "<%= project_name %>" project.
|
5
|
+
#
|
6
|
+
#--
|
7
|
+
# Copyright (c) 2006-<%= Time.now.year %> Rubaidh Ltd. All rights reserved.
|
8
|
+
# See LICENSE in the top level source code folder for permissions.
|
9
|
+
#++
|
10
|
+
|
11
|
+
class <%= migration_name %> < ActiveRecord::Migration
|
12
|
+
def self.up
|
13
|
+
create_table :<%= table_name %> do |t|
|
14
|
+
<% attributes.each do |attribute| -%>
|
15
|
+
t.<%= attribute.type %> :<%= attribute.name %>
|
16
|
+
<% end -%>
|
17
|
+
|
18
|
+
t.timestamps
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.down
|
23
|
+
drop_table :<%= table_name %>
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# <%= class_name %> Model
|
2
|
+
#
|
3
|
+
# Created on <%= Time.now.to_s :long %> by <%= user_full_name %> as part
|
4
|
+
# of the "<%= project_name %>" project.
|
5
|
+
#
|
6
|
+
#--
|
7
|
+
# Copyright (c) 2006-<%= Time.now.year %> Rubaidh Ltd. All rights reserved.
|
8
|
+
# See LICENSE in the top level source code folder for permissions.
|
9
|
+
#++
|
10
|
+
|
11
|
+
class <%= class_name %> < ActiveRecord::Base
|
12
|
+
<% attributes.select(&:reference?).each do |attribute| -%>
|
13
|
+
belongs_to :<%= attribute.name %>
|
14
|
+
<% end -%>
|
15
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# <%= class_name %> Exemplar
|
2
|
+
#
|
3
|
+
# Created on <%= Time.now.to_s :long %> by <%= user_full_name %> as part
|
4
|
+
# of the "<%= project_name %>" project.
|
5
|
+
#
|
6
|
+
#--
|
7
|
+
# Copyright (c) 2006-<%= Time.now.year %> Rubaidh Ltd. All rights reserved.
|
8
|
+
# See LICENSE in the top level source code folder for permissions.
|
9
|
+
#++
|
10
|
+
|
11
|
+
<%= class_name %>.class_eval do
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# <%= class_name %> Specification
|
2
|
+
#
|
3
|
+
# Created on <%= Time.now.to_s :long %> by <%= user_full_name %> as part
|
4
|
+
# of the "<%= project_name %>" project.
|
5
|
+
#
|
6
|
+
#--
|
7
|
+
# Copyright (c) 2006-<%= Time.now.year %> Rubaidh Ltd. All rights reserved.
|
8
|
+
# See LICENSE in the top level source code folder for permissions.
|
9
|
+
#++
|
10
|
+
|
11
|
+
require File.expand_path(File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../spec_helper')
|
12
|
+
|
13
|
+
describe <%= class_name %> do
|
14
|
+
describe "generator" do
|
15
|
+
it "should successfully create a new instance" do
|
16
|
+
lambda { <%= class_name %>.generate! }.should_not raise_error
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should persist the new instance in the database" do
|
20
|
+
lambda { <%= class_name %>.generate }.should change(<%= class_name %>, :count).by(1)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should be valid" do
|
24
|
+
<%= class_name %>.generate.should be_valid
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "validations" do
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'grit'
|
2
|
+
|
3
|
+
class RubaidhNamedBase < Rails::Generator::NamedBase
|
4
|
+
attr_reader :project_name, :user_full_name
|
5
|
+
|
6
|
+
def initialize(runtime_args, runtime_options = {})
|
7
|
+
super
|
8
|
+
|
9
|
+
assign_additional_names!
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
def assign_additional_names!
|
14
|
+
@user_full_name = figure_out_full_name_from_git_repository
|
15
|
+
@project_name = figure_out_project_name_from_rails_root
|
16
|
+
end
|
17
|
+
|
18
|
+
def figure_out_full_name_from_git_repository
|
19
|
+
repos = Grit::Repo.new(RAILS_ROOT)
|
20
|
+
|
21
|
+
repos.config['user.name']
|
22
|
+
rescue StandardError => e
|
23
|
+
# If Grit::Repo raises an error, chances are it's not a git repository.
|
24
|
+
# It's not super-important, so just give up.
|
25
|
+
"a Rubaidh staff member"
|
26
|
+
end
|
27
|
+
|
28
|
+
def figure_out_project_name_from_rails_root
|
29
|
+
File.basename(RAILS_ROOT).humanize
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
Description:
|
2
|
+
Scaffolds an entire resource, from model and migration to controller and
|
3
|
+
views, along with a full rspec test suite. The resource is ready to use as
|
4
|
+
a starting point for your RESTful, resource-oriented application.
|
5
|
+
|
6
|
+
Pass the name of the model (in singular form), either CamelCased or
|
7
|
+
under_scored, as the first argument, and an optional list of attribute
|
8
|
+
pairs.
|
9
|
+
|
10
|
+
Attribute pairs are column_name:sql_type arguments specifying the
|
11
|
+
model's attributes. Timestamps are added by default, so you don't have to
|
12
|
+
specify them by hand as 'created_at:datetime updated_at:datetime'.
|
13
|
+
|
14
|
+
You don't have to think up every attribute up front, but it helps to
|
15
|
+
sketch out a few so you can start working with the resource immediately.
|
16
|
+
|
17
|
+
For example, 'scaffold post title:string body:text published:boolean'
|
18
|
+
gives you a model with those three attributes, a controller that handles
|
19
|
+
the create/show/update/destroy, forms to create and edit your posts, and
|
20
|
+
an index that lists them all, as well as a map.resources :posts
|
21
|
+
declaration in config/routes.rb.
|
22
|
+
|
23
|
+
If you want to remove all the generated files, run
|
24
|
+
'script/destroy scaffold ModelName'.
|
25
|
+
|
26
|
+
Examples:
|
27
|
+
`./script/generate scaffold post`
|
28
|
+
`./script/generate scaffold post title:string body:text published:boolean`
|
29
|
+
`./script/generate scaffold purchase order_id:integer amount:decimal`
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'rubaidh_named_base')
|
2
|
+
|
3
|
+
class RubaidhScaffoldGenerator < RubaidhNamedBase
|
4
|
+
attr_reader :controller_name,
|
5
|
+
:controller_class_path,
|
6
|
+
:controller_file_path,
|
7
|
+
:controller_class_nesting,
|
8
|
+
:controller_class_nesting_depth,
|
9
|
+
:controller_class_name,
|
10
|
+
:controller_underscore_name,
|
11
|
+
:controller_singular_name,
|
12
|
+
:controller_plural_name
|
13
|
+
alias_method :controller_file_name, :controller_underscore_name
|
14
|
+
alias_method :controller_table_name, :controller_plural_name
|
15
|
+
|
16
|
+
def initialize(runtime_args, runtime_options = {})
|
17
|
+
super
|
18
|
+
|
19
|
+
if @name == @name.pluralize && !options[:force_plural]
|
20
|
+
logger.warning "Plural version of the model detected, using singularized version. Override with --force-plural."
|
21
|
+
@name = @name.singularize
|
22
|
+
end
|
23
|
+
|
24
|
+
@controller_name = @name.pluralize
|
25
|
+
|
26
|
+
base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
|
27
|
+
@controller_class_name_without_nesting, @controller_underscore_name, @controller_plural_name = inflect_names(base_name)
|
28
|
+
@controller_singular_name=base_name.singularize
|
29
|
+
if @controller_class_nesting.empty?
|
30
|
+
@controller_class_name = @controller_class_name_without_nesting
|
31
|
+
else
|
32
|
+
@controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def manifest
|
37
|
+
record do |m|
|
38
|
+
# Check for class naming collisions.
|
39
|
+
m.class_collisions "#{controller_class_name}Controller"
|
40
|
+
|
41
|
+
# Controller, helper, views, test and stylesheets directories.
|
42
|
+
m.directory(File.join('app/controllers', controller_class_path))
|
43
|
+
m.directory(File.join('app/views', controller_class_path, controller_file_name))
|
44
|
+
m.directory(File.join('spec/controllers', controller_class_path))
|
45
|
+
|
46
|
+
[ :index, :show, :new, :edit ].each do |view|
|
47
|
+
m.template(
|
48
|
+
"view_#{view}.html.erb",
|
49
|
+
File.join('app/views', controller_class_path, controller_file_name, "#{view}.html.erb")
|
50
|
+
)
|
51
|
+
end
|
52
|
+
|
53
|
+
m.template(
|
54
|
+
"partial_form.html.erb",
|
55
|
+
File.join('app/views', controller_class_path, controller_file_name, "_form.html.erb")
|
56
|
+
)
|
57
|
+
|
58
|
+
m.template(
|
59
|
+
"partial_layout.html.erb",
|
60
|
+
File.join('app/views', controller_class_path, controller_file_name, "_#{plural_name}.html.erb")
|
61
|
+
)
|
62
|
+
|
63
|
+
m.template(
|
64
|
+
"partial_model.html.erb",
|
65
|
+
File.join('app/views', controller_class_path, controller_file_name, "_#{singular_name}.html.erb")
|
66
|
+
)
|
67
|
+
|
68
|
+
m.template(
|
69
|
+
'controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
|
70
|
+
)
|
71
|
+
|
72
|
+
m.template('routing_spec.rb', File.join('spec/controllers', controller_class_path, "#{controller_file_name}_routing_spec.rb"))
|
73
|
+
m.template('controller_spec.rb', File.join('spec/controllers', controller_class_path, "#{controller_file_name}_controller_spec.rb"))
|
74
|
+
|
75
|
+
m.route_resources controller_file_name
|
76
|
+
|
77
|
+
m.dependency 'rubaidh_model', [name] + @args
|
78
|
+
m.dependency 'rubaidh_helper', [plural_name] + @args
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
protected
|
83
|
+
def banner
|
84
|
+
"Usage: #{$0} #{spec.name} ModelName [field:type] ..."
|
85
|
+
end
|
86
|
+
|
87
|
+
def model_name
|
88
|
+
class_name.demodulize
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# <%= class_name %> Controller
|
2
|
+
#
|
3
|
+
# Created on <%= Time.now.to_s :long %> by <%= user_full_name %> as part
|
4
|
+
# of the "<%= project_name %>" project.
|
5
|
+
#
|
6
|
+
#--
|
7
|
+
# Copyright (c) 2006-<%= Time.now.year %> Rubaidh Ltd. All rights reserved.
|
8
|
+
# See LICENSE in the top level source code folder for permissions.
|
9
|
+
#++
|
10
|
+
|
11
|
+
class <%= controller_class_name %>Controller < ApplicationController
|
12
|
+
def index
|
13
|
+
@<%= table_name %> = <%= class_name %>.all
|
14
|
+
|
15
|
+
respond_to do |format|
|
16
|
+
format.html # index.html.erb
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def show
|
21
|
+
@<%= file_name %> = <%= class_name %>.find(params[:id])
|
22
|
+
|
23
|
+
respond_to do |format|
|
24
|
+
format.html # show.html.erb
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def new
|
29
|
+
@<%= file_name %> = <%= class_name %>.new
|
30
|
+
|
31
|
+
respond_to do |format|
|
32
|
+
format.html # new.html.erb
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def edit
|
37
|
+
@<%= file_name %> = <%= class_name %>.find(params[:id])
|
38
|
+
|
39
|
+
respond_to do |format|
|
40
|
+
format.html # edit.html.erb
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def create
|
45
|
+
@<%= file_name %> = <%= class_name %>.new(params[:<%= file_name %>])
|
46
|
+
|
47
|
+
respond_to do |format|
|
48
|
+
if @<%= file_name %>.save
|
49
|
+
format.html { redirect_to(@<%= file_name %>) }
|
50
|
+
else
|
51
|
+
format.html { render :action => "new" }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def update
|
57
|
+
@<%= file_name %> = <%= class_name %>.find(params[:id])
|
58
|
+
|
59
|
+
respond_to do |format|
|
60
|
+
if @<%= file_name %>.update_attributes(params[:<%= file_name %>])
|
61
|
+
format.html { redirect_to(@<%= file_name %>) }
|
62
|
+
else
|
63
|
+
format.html { render :action => "edit" }
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def destroy
|
69
|
+
@<%= file_name %> = <%= class_name %>.find(params[:id])
|
70
|
+
@<%= file_name %>.destroy
|
71
|
+
|
72
|
+
respond_to do |format|
|
73
|
+
format.html { redirect_to(<%= table_name %>_url) }
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,251 @@
|
|
1
|
+
# <%= class_name %> Controller Spec
|
2
|
+
#
|
3
|
+
# Created on <%= Time.now.to_s :long %> by <%= user_full_name %> as part
|
4
|
+
# of the "<%= project_name %>" project.
|
5
|
+
#
|
6
|
+
#--
|
7
|
+
# Copyright (c) 2006-<%= Time.now.year %> Rubaidh Ltd. All rights reserved.
|
8
|
+
# See LICENSE in the top level source code folder for permissions.
|
9
|
+
#++
|
10
|
+
|
11
|
+
require File.expand_path(File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../spec_helper')
|
12
|
+
|
13
|
+
describe <%= controller_class_name %>Controller do
|
14
|
+
def mock_<%= file_name %>(stubs={})
|
15
|
+
@mock_<%= file_name %> ||= mock_model(<%= class_name %>, stubs)
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "responding to GET index" do
|
19
|
+
before(:each) do
|
20
|
+
@<%= table_name %> = [mock_<%= file_name %>]
|
21
|
+
<%= class_name %>.stub!(:find).and_return(@<%= table_name %>)
|
22
|
+
end
|
23
|
+
|
24
|
+
def do_get
|
25
|
+
get :index
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should be successful" do
|
29
|
+
do_get
|
30
|
+
response.should be_success
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should render the index template" do
|
34
|
+
do_get
|
35
|
+
response.should render_template(:index)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should query the model for a list of <%= table_name %>" do
|
39
|
+
<%= class_name %>.should_receive(:find).with(:all).and_return([mock_<%= file_name %>])
|
40
|
+
do_get
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should expose all <%= table_name %> as @<%= table_name %>" do
|
44
|
+
do_get
|
45
|
+
assigns[:<%= table_name %>].should == [mock_<%= file_name %>]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "responding to GET show" do
|
50
|
+
before(:each) do
|
51
|
+
<%= class_name %>.stub!(:find).and_return(mock_<%= file_name %>)
|
52
|
+
end
|
53
|
+
|
54
|
+
def do_get
|
55
|
+
get :show, :id => "37"
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should be successful" do
|
59
|
+
do_get
|
60
|
+
response.should be_success
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should render the show template" do
|
64
|
+
do_get
|
65
|
+
response.should render_template(:show)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should query the model for the requested <%= file_name %>" do
|
69
|
+
<%= class_name %>.should_receive(:find).with("37").and_return(mock_<%= file_name %>)
|
70
|
+
do_get
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should expose the requested <%= file_name %> as @<%= file_name %>" do
|
74
|
+
do_get
|
75
|
+
assigns[:<%= file_name %>].should equal(mock_<%= file_name %>)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "responding to GET new" do
|
80
|
+
before(:each) do
|
81
|
+
<%= class_name %>.stub!(:new).and_return(mock_<%= file_name %>)
|
82
|
+
end
|
83
|
+
|
84
|
+
def do_get
|
85
|
+
get :new
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should ask the model for a new <%= file_name %>" do
|
89
|
+
<%= class_name %>.should_receive(:new).and_return(mock_<%= file_name %>)
|
90
|
+
do_get
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should expose a new <%= file_name %> as @<%= file_name %>" do
|
94
|
+
do_get
|
95
|
+
assigns[:<%= file_name %>].should equal(mock_<%= file_name %>)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe "responding to GET edit" do
|
100
|
+
before(:each) do
|
101
|
+
<%= class_name %>.stub!(:find).and_return(mock_<%= file_name %>)
|
102
|
+
end
|
103
|
+
|
104
|
+
def do_get
|
105
|
+
get :edit, :id => "37"
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should be successful" do
|
109
|
+
do_get
|
110
|
+
response.should be_success
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should render the edit template" do
|
114
|
+
do_get
|
115
|
+
response.should render_template(:edit)
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should query the model for the requested <%= file_name %>" do
|
119
|
+
<%= class_name %>.should_receive(:find).with("37").and_return(mock_<%= file_name %>)
|
120
|
+
do_get
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should expose the requested <%= file_name %> as @<%= file_name %>" do
|
124
|
+
do_get
|
125
|
+
assigns[:<%= file_name %>].should equal(mock_<%= file_name %>)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe "responding to POST create" do
|
130
|
+
before(:each) do
|
131
|
+
<%= class_name %>.stub!(:new).and_return(mock_<%= file_name %>)
|
132
|
+
mock_<%= file_name %>.stub!(:save)
|
133
|
+
end
|
134
|
+
|
135
|
+
def do_post
|
136
|
+
post :create, :<%= file_name %> => { "dummy" => "parameters" }
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should build a new <%= file_name %> with the supplied parameters" do
|
140
|
+
<%= class_name %>.should_receive(:new).with({ "dummy" => "parameters" })
|
141
|
+
do_post
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should attempt to save the new <%= file_name %>" do
|
145
|
+
mock_<%= file_name %>.should_receive(:save)
|
146
|
+
do_post
|
147
|
+
end
|
148
|
+
|
149
|
+
describe "with valid params" do
|
150
|
+
before(:each) do
|
151
|
+
mock_<%= file_name %>.stub!(:save).and_return(true)
|
152
|
+
end
|
153
|
+
|
154
|
+
it "should redirect to the created <%= file_name %>" do
|
155
|
+
do_post
|
156
|
+
response.should redirect_to(<%= file_name %>_url(mock_<%= file_name %>))
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
describe "with invalid params" do
|
161
|
+
before(:each) do
|
162
|
+
mock_<%= file_name %>.stub!(:save).and_return(false)
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should expose a newly created but unsaved <%= file_name %> as @<%= file_name %>" do
|
166
|
+
do_post
|
167
|
+
assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
|
168
|
+
end
|
169
|
+
|
170
|
+
it "should re-render the 'new' template" do
|
171
|
+
do_post
|
172
|
+
response.should render_template(:new)
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
describe "responding to PUT udpate" do
|
178
|
+
before(:each) do
|
179
|
+
<%= class_name %>.stub!(:find).and_return(mock_<%= file_name %>)
|
180
|
+
mock_<%= file_name %>.stub!(:update_attributes)
|
181
|
+
end
|
182
|
+
|
183
|
+
def do_put
|
184
|
+
put :update, :id => "37", :<%= file_name %> => { "dummy" => "parameters" }
|
185
|
+
end
|
186
|
+
|
187
|
+
it "should query the model for the requested <%= file_name %>" do
|
188
|
+
<%= class_name %>.should_receive(:find).with("37").and_return(mock_<%= file_name %>)
|
189
|
+
do_put
|
190
|
+
end
|
191
|
+
|
192
|
+
it "should attempt to update the attributes of the requested <%= file_name %>" do
|
193
|
+
mock_<%= file_name %>.should_receive(:update_attributes).with({ "dummy" => "parameters" })
|
194
|
+
do_put
|
195
|
+
end
|
196
|
+
|
197
|
+
describe "with valid params" do
|
198
|
+
before(:each) do
|
199
|
+
mock_<%= file_name %>.stub!(:update_attributes).and_return(true)
|
200
|
+
end
|
201
|
+
|
202
|
+
it "should redirect to the <%= file_name %>" do
|
203
|
+
do_put
|
204
|
+
response.should redirect_to(<%= file_name %>_url(mock_<%= file_name %>))
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
describe "with invalid params" do
|
209
|
+
before(:each) do
|
210
|
+
mock_<%= file_name %>.stub!(:update_attributes).and_return(false)
|
211
|
+
end
|
212
|
+
|
213
|
+
it "should expose the <%= file_name %> as @<%= file_name %>" do
|
214
|
+
do_put
|
215
|
+
assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
|
216
|
+
end
|
217
|
+
|
218
|
+
it "should re-render the 'edit' template" do
|
219
|
+
do_put
|
220
|
+
response.should render_template(:edit)
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
end
|
225
|
+
|
226
|
+
describe "responding to DELETE destroy" do
|
227
|
+
before(:each) do
|
228
|
+
<%= class_name %>.stub!(:find).and_return(mock_<%= file_name %>)
|
229
|
+
mock_<%= file_name %>.stub!(:destroy)
|
230
|
+
end
|
231
|
+
|
232
|
+
def do_delete(params = {})
|
233
|
+
delete :destroy, { :id => "37" }.merge(params)
|
234
|
+
end
|
235
|
+
|
236
|
+
it "should query the model for the requested <%= file_name %>" do
|
237
|
+
<%= class_name %>.should_receive(:find).with("37").and_return(mock_<%= file_name %>)
|
238
|
+
do_delete
|
239
|
+
end
|
240
|
+
|
241
|
+
it "should destroy the requested <%= file_name %>" do
|
242
|
+
mock_<%= file_name %>.should_receive(:destroy)
|
243
|
+
do_delete
|
244
|
+
end
|
245
|
+
|
246
|
+
it "should redirect to the <%= table_name %> index action" do
|
247
|
+
do_delete
|
248
|
+
response.should redirect_to(<%= table_name %>_url)
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|