├── .DS_Store ├── MIT-LICENSE ├── README ├── Rakefile ├── generators ├── .DS_Store └── rspec_nested_scaffold │ ├── .DS_Store │ ├── rspec_nested_scaffold_generator.rb │ └── templates │ ├── controller.rb │ ├── controller_spec.rb │ ├── edit_erb_spec.rb │ ├── helper_spec.rb │ ├── index_erb_spec.rb │ ├── new_erb_spec.rb │ ├── routing_spec.rb │ ├── show_erb_spec.rb │ ├── view_edit.html.erb │ ├── view_index.html.erb │ ├── view_new.html.erb │ └── view_show.html.erb ├── init.rb ├── install.rb ├── lib └── rspec_on_rails_nested_routes.rb ├── tasks └── rspec_nested_scaffold_tasks.rake ├── test └── rspec_nested_scaffold_test.rb └── uninstall.rb /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeremyf/rspec_on_rails_nested_scaffold/cf8f68533db4755653e099ac54a4964edaf4f4bb/.DS_Store -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2008 [name of plugin creator] 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. 21 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | RspecNestedScaffold 2 | =================== 3 | 4 | This plugin will generate a fully spec-ed nested resource scaffold. 5 | 6 | 7 | Example 8 | ======= 9 | 10 | ./script/generate rspec_nested_scaffold --owner=User Entry body:text 11 | 12 | 13 | TODO 14 | ==== 15 | [ ] generate the appropriate nested route 16 | 17 | Copyright (c) 2008 Jeremy Friesen, released under the MIT license 18 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'rake' 2 | require 'rake/testtask' 3 | require 'rake/rdoctask' 4 | 5 | desc 'Default: run unit tests.' 6 | task :default => :test 7 | 8 | desc 'Test the rspec_nested_scaffold plugin.' 9 | Rake::TestTask.new(:test) do |t| 10 | t.libs << 'lib' 11 | t.pattern = 'test/**/*_test.rb' 12 | t.verbose = true 13 | end 14 | 15 | desc 'Generate documentation for the rspec_nested_scaffold plugin.' 16 | Rake::RDocTask.new(:rdoc) do |rdoc| 17 | rdoc.rdoc_dir = 'rdoc' 18 | rdoc.title = 'RspecNestedScaffold' 19 | rdoc.options << '--line-numbers' << '--inline-source' 20 | rdoc.rdoc_files.include('README') 21 | rdoc.rdoc_files.include('lib/**/*.rb') 22 | end 23 | -------------------------------------------------------------------------------- /generators/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeremyf/rspec_on_rails_nested_scaffold/cf8f68533db4755653e099ac54a4964edaf4f4bb/generators/.DS_Store -------------------------------------------------------------------------------- /generators/rspec_nested_scaffold/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeremyf/rspec_on_rails_nested_scaffold/cf8f68533db4755653e099ac54a4964edaf4f4bb/generators/rspec_nested_scaffold/.DS_Store -------------------------------------------------------------------------------- /generators/rspec_nested_scaffold/rspec_nested_scaffold_generator.rb: -------------------------------------------------------------------------------- 1 | class RspecNestedScaffoldGenerator < Rails::Generator::NamedBase 2 | default_options :skip_migration => false 3 | 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_singular_name, 11 | :controller_plural_name, 12 | :resource_edit_path, 13 | :default_file_extension, 14 | :nesting_owner 15 | alias_method :controller_file_name, :controller_singular_name 16 | alias_method :controller_table_name, :controller_plural_name 17 | 18 | def initialize(runtime_args, runtime_options = {}) 19 | super 20 | raise "For #{$0} #{File.basename(__FILE__, '.rb')} you must specify the --owner option. See --help" unless options[:owner] 21 | 22 | @nesting_owner = options[:owner].underscore.singularize 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_singular_name, @controller_plural_name = inflect_names(base_name) 28 | 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 | 35 | @resource_generator = "scaffold" 36 | @default_file_extension = "html.erb" 37 | 38 | if ActionController::Base.respond_to?(:resource_action_separator) 39 | @resource_edit_path = "/edit" 40 | else 41 | @resource_edit_path = ";edit" 42 | end 43 | end 44 | 45 | def manifest 46 | record do |m| 47 | 48 | # Check for class naming collisions. 49 | m.class_collisions(controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}Helper") 50 | m.class_collisions(class_path, "#{class_name}") 51 | 52 | # Controller, helper, views, and spec directories. 53 | m.directory(File.join('app/models', class_path)) 54 | m.directory(File.join('app/controllers', controller_class_path)) 55 | m.directory(File.join('app/helpers', controller_class_path)) 56 | m.directory(File.join('app/views', controller_class_path, controller_file_name)) 57 | m.directory(File.join('spec/controllers', controller_class_path)) 58 | m.directory(File.join('spec/models', class_path)) 59 | m.directory(File.join('spec/helpers', class_path)) 60 | m.directory File.join('spec/fixtures', class_path) 61 | m.directory File.join('spec/views', controller_class_path, controller_file_name) 62 | 63 | #Controller spec, class, and helper. 64 | m.template 'rspec_nested_scaffold:routing_spec.rb', 65 | File.join('spec/controllers', controller_class_path, "#{controller_file_name}_routing_spec.rb") 66 | 67 | m.template 'rspec_nested_scaffold:controller_spec.rb', 68 | File.join('spec/controllers', controller_class_path, "#{controller_file_name}_controller_spec.rb") 69 | 70 | m.template "rspec_nested_scaffold:controller.rb", 71 | File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb") 72 | 73 | m.template 'rspec_scaffold:helper_spec.rb', 74 | File.join('spec/helpers', class_path, "#{controller_file_name}_helper_spec.rb") 75 | 76 | m.template "#{@resource_generator}:helper.rb", 77 | File.join('app/helpers', controller_class_path, "#{controller_file_name}_helper.rb") 78 | 79 | for action in scaffold_views 80 | m.template( 81 | "rspec_nested_scaffold:view_#{action}.#{@default_file_extension}", 82 | File.join('app/views', controller_class_path, controller_file_name, "#{action}.#{default_file_extension}") 83 | ) 84 | 85 | m.template "rspec_nested_scaffold:#{action}_erb_spec.rb", 86 | File.join('spec/views', controller_class_path, controller_file_name, "#{action}.#{default_file_extension}_spec.rb") 87 | end 88 | 89 | # Model class, unit test, and fixtures. 90 | m.template 'model:model.rb', File.join('app/models', class_path, "#{file_name}.rb") 91 | m.template 'model:fixtures.yml', File.join('spec/fixtures', class_path, "#{table_name}.yml") 92 | m.template 'rspec_model:model_spec.rb', File.join('spec/models', class_path, "#{file_name}_spec.rb") 93 | 94 | # View specs 95 | 96 | unless options[:skip_migration] 97 | m.migration_template( 98 | 'model:migration.rb', 'db/migrate', 99 | :assigns => { 100 | :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}", 101 | :attributes => attributes 102 | }, 103 | :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}" 104 | ) 105 | end 106 | 107 | m.route_resources controller_file_name 108 | 109 | end 110 | end 111 | 112 | protected 113 | 114 | # Override with your own usage banner. 115 | def banner 116 | "Usage: #{$0} #{File.basename(__FILE__, '.rb')} ModelName --owner=OwnerName field:type field:type" 117 | end 118 | 119 | def add_options!(opt) 120 | opt.separator '' 121 | opt.separator 'Options:' 122 | opt.on("--skip-migration", 123 | "Don't generate a migration file for this model") { |v| options[:skip_migration] = v } 124 | opt.on("--owner=owner", '-o', 'Nested resource owner') { |v| options[:owner] = v } 125 | end 126 | 127 | def scaffold_views 128 | %w[ index show new edit ] 129 | end 130 | 131 | def model_name 132 | class_name.demodulize 133 | end 134 | 135 | def nesting_owner_class 136 | nesting_owner.classify 137 | end 138 | end 139 | 140 | module Rails 141 | module Generator 142 | class GeneratedAttribute 143 | def default_value 144 | @default_value ||= case type 145 | when :int, :integer then "\"1\"" 146 | when :float then "\"1.5\"" 147 | when :decimal then "\"9.99\"" 148 | when :datetime, :timestamp, :time then "Time.now" 149 | when :date then "Date.today" 150 | when :string then "\"MyString\"" 151 | when :text then "\"MyText\"" 152 | when :boolean then "false" 153 | else 154 | "" 155 | end 156 | end 157 | 158 | def input_type 159 | @input_type ||= case type 160 | when :text then "textarea" 161 | else 162 | "input" 163 | end 164 | end 165 | end 166 | end 167 | end 168 | -------------------------------------------------------------------------------- /generators/rspec_nested_scaffold/templates/controller.rb: -------------------------------------------------------------------------------- 1 | class <%= controller_class_name %>Controller < ApplicationController 2 | 3 | protected 4 | 5 | # The nested resource owner. 6 | def <%= nesting_owner %> 7 | @<%=nesting_owner %> ||= <%= nesting_owner_class %>.find(params[:<%= nesting_owner %>_id]) 8 | end 9 | 10 | public 11 | 12 | # GET /<%=nesting_owner.pluralize%>/:<%=nesting_owner%>_id/<%= table_name %> 13 | # GET /<%=nesting_owner.pluralize%>/:<%=nesting_owner%>_id/<%= table_name %>.xml 14 | def index 15 | @<%= table_name %> = <%= nesting_owner %>.<%= table_name %>.find(:all) 16 | 17 | respond_to do |format| 18 | format.html # index.html.erb 19 | format.xml { render :xml => @<%= table_name %> } 20 | end 21 | end 22 | 23 | # GET /<%=nesting_owner.pluralize%>/:<%=nesting_owner%>_id/<%= table_name %>/1 24 | # GET /<%=nesting_owner.pluralize%>/:<%=nesting_owner%>_id/<%= table_name %>/1.xml 25 | def show 26 | @<%= file_name %> = <%= nesting_owner %>.<%= table_name %>.find(params[:id]) 27 | 28 | respond_to do |format| 29 | format.html # show.html.erb 30 | format.xml { render :xml => @<%= file_name %> } 31 | end 32 | end 33 | 34 | # GET /<%=nesting_owner.pluralize%>/:<%=nesting_owner%>_id/<%= table_name %>/new 35 | # GET /<%=nesting_owner.pluralize%>/:<%=nesting_owner%>_id/<%= table_name %>/new.xml 36 | def new 37 | @<%= file_name %> = <%= nesting_owner %>.<%= table_name %>.build 38 | 39 | respond_to do |format| 40 | format.html # new.html.erb 41 | format.xml { render :xml => @<%= file_name %> } 42 | end 43 | end 44 | 45 | # GET /<%=nesting_owner.pluralize%>/:<%=nesting_owner%>_id/<%= table_name %>/edit 46 | def edit 47 | @<%= file_name %> = <%= nesting_owner %>.<%= table_name %>.find(params[:id]) 48 | end 49 | 50 | # POST /<%=nesting_owner.pluralize%>/:<%=nesting_owner%>_id/<%= table_name %> 51 | # POST /<%=nesting_owner.pluralize%>/:<%=nesting_owner%>_id/<%= table_name %>.xml 52 | def create 53 | @<%= file_name %> = <%= nesting_owner %>.<%= table_name %>.build(params[:<%= file_name %>]) 54 | 55 | respond_to do |format| 56 | if @<%= file_name %>.save 57 | flash[:notice] = '<%= class_name %> was successfully created.' 58 | format.html { redirect_to(<%= nesting_owner %>_<%= file_name %>_url(<%= nesting_owner %>, @<%= file_name %>)) } 59 | format.xml { render :xml => @<%= file_name %>, :status => :created, :location => <%= nesting_owner %>_<%= file_name %>_path(<%= nesting_owner %>, @<%= file_name %>) } 60 | else 61 | format.html { render :action => "new" } 62 | format.xml { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity } 63 | end 64 | end 65 | end 66 | 67 | # PUT /<%=nesting_owner.pluralize%>/:<%=nesting_owner%>_id/<%= table_name %>/1 68 | # PUT /<%=nesting_owner.pluralize%>/:<%=nesting_owner%>_id/<%= table_name %>/1.xml 69 | def update 70 | @<%= file_name %> = <%= nesting_owner %>.<%= table_name %>.find(params[:id]) 71 | 72 | respond_to do |format| 73 | if @<%= file_name %>.update_attributes(params[:<%= file_name %>]) 74 | flash[:notice] = '<%= class_name %> was successfully updated.' 75 | format.html { redirect_to(<%= nesting_owner %>_<%= file_name %>_url(<%= nesting_owner %>, @<%= file_name %>)) } 76 | format.xml { head :ok } 77 | else 78 | format.html { render :action => "edit" } 79 | format.xml { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity } 80 | end 81 | end 82 | end 83 | 84 | # DELETE /<%=nesting_owner.pluralize%>/:<%=nesting_owner%>_id/<%= table_name %>/1 85 | # DELETE /<%=nesting_owner.pluralize%>/:<%=nesting_owner%>_id/<%= table_name %>/1.xml 86 | def destroy 87 | @<%= file_name %> = <%= nesting_owner %>.<%= table_name %>.find(params[:id]) 88 | @<%= file_name %>.destroy 89 | 90 | respond_to do |format| 91 | format.html { redirect_to(<%= nesting_owner %>_<%= table_name %>_path(<%= nesting_owner %>)) } 92 | format.xml { head :ok } 93 | end 94 | end 95 | end 96 | -------------------------------------------------------------------------------- /generators/rspec_nested_scaffold/templates/controller_spec.rb: -------------------------------------------------------------------------------- 1 | require File.dirname(__FILE__) + '/../spec_helper' 2 | 3 | describe <%= controller_class_name %>Controller do 4 | before(:each) do 5 | @<%= file_name %> = mock_model(<%= class_name %>, :to_param => '1') 6 | @<%= nesting_owner %> = mock_model(<%= nesting_owner_class %>, :<%= table_name %> => [@<%= file_name %>], :to_param => '2') 7 | <%= nesting_owner_class %>.stub!(:find).with('2').and_return(@<%= nesting_owner %>) 8 | end 9 | 10 | describe "handling GET /<%= nesting_owner %>s/:<%= nesting_owner %>_id/<%= table_name %>" do 11 | before(:each) do 12 | @<%= nesting_owner %>.<%= table_name %>.stub!(:find).with(:all).and_return([@<%= file_name %>]) 13 | end 14 | def do_get 15 | get :index, :<%= nesting_owner %>_id => '2' 16 | end 17 | 18 | it "should be successful" do 19 | do_get 20 | response.should be_success 21 | end 22 | 23 | it "should assign the found <%= nesting_owner %> for the view" do 24 | do_get 25 | assigns[:<%= nesting_owner %>].should == @<%= nesting_owner %> 26 | end 27 | 28 | it "should render index template" do 29 | do_get 30 | response.should render_template('index') 31 | end 32 | 33 | it "should find all <%= table_name %> owned by <%= nesting_owner %>" do 34 | @<%= nesting_owner %>.<%= table_name %>.stub!(:find).with(:all).and_return([@<%= file_name %>]) 35 | do_get 36 | end 37 | 38 | it "should assign the found <%= table_name %> for the view" do 39 | do_get 40 | assigns[:<%= table_name %>].should == [@<%= file_name %>] 41 | end 42 | end 43 | 44 | describe "handling GET /<%= nesting_owner %>s/:<%= nesting_owner %>_id/<%= table_name %>.xml" do 45 | 46 | before(:each) do 47 | @<%= nesting_owner %>.<%= table_name %>.stub!(:find).and_return([@<%= file_name %>]) 48 | end 49 | 50 | def do_get 51 | @<%= nesting_owner %>.<%= table_name %>.stub!(:to_xml).and_return("XML") 52 | @<%= file_name %>.stub!(:to_xml).and_return("XML") 53 | @request.env["HTTP_ACCEPT"] = "application/xml" 54 | get :index, :<%= nesting_owner %>_id => '2' 55 | end 56 | 57 | it "should be successful" do 58 | do_get 59 | response.should be_success 60 | end 61 | 62 | it "should find all <%= table_name %>" do 63 | @<%= nesting_owner %>.<%= table_name %>.should_receive(:find).and_return([@<%= file_name %>]) 64 | do_get 65 | end 66 | 67 | it "should render the found <%= table_name %> as xml" do 68 | @<%= file_name %>.should_receive(:to_xml).and_return("XML") 69 | do_get 70 | end 71 | end 72 | 73 | describe "handling GET /<%= nesting_owner %>s/:<%= nesting_owner %>_id/<%= table_name %>/1" do 74 | 75 | before(:each) do 76 | @<%= nesting_owner %>.<%= table_name %>.stub!(:find).with('1').and_return(@<%= file_name %>) 77 | end 78 | 79 | def do_get 80 | get :show, :<%= nesting_owner %>_id => '2', :id => '1' 81 | end 82 | 83 | it "should be successful" do 84 | do_get 85 | response.should be_success 86 | end 87 | 88 | it "should render show template" do 89 | do_get 90 | response.should render_template('show') 91 | end 92 | 93 | it "should find the <%= file_name %> requested" do 94 | @<%= nesting_owner %>.<%= table_name %>.stub!(:find).with('1').and_return(@<%= file_name %>) 95 | do_get 96 | end 97 | 98 | it "should assign the found <%= file_name %> for the view" do 99 | do_get 100 | assigns[:<%= file_name %>].should equal(@<%= file_name %>) 101 | end 102 | 103 | it "should assign the found <%= nesting_owner %> for the view" do 104 | do_get 105 | assigns[:<%= nesting_owner %>].should == @<%= nesting_owner %> 106 | end 107 | 108 | end 109 | 110 | describe "handling GET /<%= nesting_owner %>s/:<%= nesting_owner %>_id/<%= table_name %>/1.xml" do 111 | 112 | before(:each) do 113 | @<%= nesting_owner %>.<%= table_name %>.stub!(:find).with('1').and_return(@<%= file_name %>) 114 | end 115 | 116 | def do_get 117 | @request.env["HTTP_ACCEPT"] = "application/xml" 118 | get :show, :<%= nesting_owner %>_id => '2', :id => '1' 119 | end 120 | 121 | it "should be successful" do 122 | do_get 123 | response.should be_success 124 | end 125 | 126 | it "should find the <%= file_name %> requested" do 127 | @<%= nesting_owner %>.<%= table_name %>.stub!(:find).with('1').and_return(@<%= file_name %>) 128 | do_get 129 | end 130 | 131 | it "should render the found <%= file_name %> as xml" do 132 | @<%= file_name %>.stub!(:to_xml).and_return("XML") 133 | do_get 134 | response.body.should == "XML" 135 | end 136 | end 137 | 138 | describe "handling GET /<%= nesting_owner %>s/:<%= nesting_owner %>_id/<%= table_name %>/new" do 139 | 140 | before(:each) do 141 | @<%= nesting_owner %>.<%= table_name %>.stub!(:build).and_return(@<%= file_name %>) 142 | end 143 | 144 | def do_get 145 | get :new, :<%= nesting_owner %>_id => '2' 146 | end 147 | 148 | it "should be successful" do 149 | do_get 150 | response.should be_success 151 | end 152 | 153 | it "should render new template" do 154 | do_get 155 | response.should render_template('new') 156 | end 157 | 158 | it "should create an new <%= file_name %>" do 159 | @<%= nesting_owner %>.<%= table_name %>.should_receive(:build).and_return(@<%= file_name %>) 160 | do_get 161 | end 162 | 163 | it "should not save the new <%= file_name %>" do 164 | @<%= file_name %>.should_not_receive(:save) 165 | do_get 166 | end 167 | 168 | it "should assign the new <%= file_name %> for the view" do 169 | do_get 170 | assigns[:<%= file_name %>].should equal(@<%= file_name %>) 171 | end 172 | 173 | it "should assign the <%= nesting_owner_class %> for the view" do 174 | do_get 175 | assigns[:<%= nesting_owner %>].should equal(@<%= nesting_owner %>) 176 | end 177 | end 178 | 179 | describe "handling GET /<%= nesting_owner %>s/:<%= nesting_owner %>_id/<%= table_name %>/1/edit" do 180 | 181 | before(:each) do 182 | @<%= nesting_owner %>.<%= table_name %>.stub!(:find).and_return(@<%= file_name %>) 183 | end 184 | 185 | def do_get 186 | get :edit, :<%= nesting_owner %>_id => '2', :id => '1' 187 | end 188 | 189 | it "should be successful" do 190 | do_get 191 | response.should be_success 192 | end 193 | 194 | it "should render edit template" do 195 | do_get 196 | response.should render_template('edit') 197 | end 198 | 199 | it "should find the <%= file_name %> requested" do 200 | @<%= nesting_owner %>.<%= table_name %>.should_receive(:find).with('1').and_return(@<%= file_name %>) 201 | do_get 202 | end 203 | 204 | it "should assign the found <%= class_name %> for the view" do 205 | do_get 206 | assigns[:<%= file_name %>].should equal(@<%= file_name %>) 207 | end 208 | 209 | it "should assign the <%= nesting_owner_class %> for the view" do 210 | do_get 211 | assigns[:<%= nesting_owner %>].should equal(@<%= nesting_owner %>) 212 | end 213 | end 214 | 215 | describe "handling POST /<%= nesting_owner.pluralize %>/:<%= nesting_owner %>_id/<%= table_name %>" do 216 | 217 | before(:each) do 218 | @<%= nesting_owner %>.<%= table_name %>.stub!(:build).and_return(@<%= file_name %>) 219 | end 220 | 221 | describe "with successful save" do 222 | 223 | def do_post 224 | @<%= file_name %>.should_receive(:save).and_return(true) 225 | post :create, :<%= nesting_owner %>_id => '2', :<%= file_name %> => {} 226 | end 227 | 228 | it "should create a new <%= file_name %>" do 229 | @<%= nesting_owner %>.<%= table_name %>.should_receive(:build).with({}).and_return(@<%= file_name %>) 230 | do_post 231 | end 232 | 233 | it "should redirect to the new <%= file_name %>" do 234 | do_post 235 | response.should redirect_to(<%= nesting_owner %>_<%= file_name %>_path(@<%= nesting_owner %>, @<%= file_name %>)) 236 | end 237 | 238 | end 239 | 240 | describe "with failed save" do 241 | 242 | def do_post 243 | @<%= file_name %>.should_receive(:save).and_return(false) 244 | post :create, :<%= nesting_owner %>_id => '2', :<%= file_name %> => {} 245 | end 246 | 247 | it "should re-render 'new'" do 248 | do_post 249 | response.should render_template('new') 250 | end 251 | 252 | end 253 | end 254 | 255 | describe "handling PUT /<%= nesting_owner.pluralize %>/:<%= nesting_owner %>_id/<%= table_name %>/1" do 256 | 257 | before(:each) do 258 | @<%= nesting_owner %>.<%= table_name %>.stub!(:find).and_return(@<%= file_name %>) 259 | end 260 | 261 | describe "with successful update" do 262 | 263 | def do_put 264 | @<%= file_name %>.should_receive(:update_attributes).and_return(true) 265 | put :update, :<%= nesting_owner %>_id => '2', :id => '1' 266 | end 267 | 268 | it "should find the <%= file_name %> requested" do 269 | @<%= nesting_owner %>.<%= table_name %>.should_receive(:find).with('1').and_return(@<%= file_name %>) 270 | do_put 271 | end 272 | 273 | it "should update the found <%= file_name %>" do 274 | do_put 275 | assigns(:<%= file_name %>).should equal(@<%= file_name %>) 276 | end 277 | 278 | it "should assign the found <%= file_name %> for the view" do 279 | do_put 280 | assigns(:<%= file_name %>).should equal(@<%= file_name %>) 281 | end 282 | 283 | it "should assign the <%= nesting_owner %> for the view" do 284 | do_put 285 | assigns(:<%= nesting_owner %>).should equal(@<%= nesting_owner %>) 286 | end 287 | 288 | it "should redirect to the <%= file_name %>" do 289 | do_put 290 | response.should redirect_to(<%= nesting_owner %>_<%= file_name %>_url(@<%= nesting_owner %>, @<%= file_name %>)) 291 | end 292 | 293 | end 294 | 295 | describe "with failed update" do 296 | 297 | def do_put 298 | @<%= file_name %>.should_receive(:update_attributes).and_return(false) 299 | put :update, :<%= nesting_owner %>_id => '2', :id => '1' 300 | end 301 | 302 | it "should re-render 'edit'" do 303 | do_put 304 | response.should render_template('edit') 305 | end 306 | 307 | end 308 | end 309 | 310 | describe "handling DELETE /<%= nesting_owner.pluralize %>/:<%= nesting_owner %>_id/<%= table_name %>/1" do 311 | 312 | before(:each) do 313 | @<%= nesting_owner %>.<%= table_name %>.stub!(:find).and_return(@<%= file_name %>) 314 | @<%= file_name %>.stub!(:destroy).and_return(true) 315 | end 316 | 317 | def do_delete 318 | delete :destroy, :<%= nesting_owner %>_id => '2', :id => '1' 319 | end 320 | 321 | it "should find the <%= file_name %> requested" do 322 | @<%= nesting_owner %>.<%= table_name %>.should_receive(:find).with('1').and_return(@<%= file_name %>) 323 | do_delete 324 | end 325 | 326 | it "should call destroy on the found <%= file_name %>" do 327 | @<%= file_name %>.should_receive(:destroy) 328 | do_delete 329 | end 330 | 331 | it "should redirect to the <%= nesting_owner %>'s <%= table_name %> list" do 332 | do_delete 333 | response.should redirect_to(<%= nesting_owner %>_<%= table_name %>_url(@<%= nesting_owner %>)) 334 | end 335 | end 336 | end 337 | -------------------------------------------------------------------------------- /generators/rspec_nested_scaffold/templates/edit_erb_spec.rb: -------------------------------------------------------------------------------- 1 | require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../../spec_helper' 2 | 3 | describe "/<%= table_name %>/edit.<%= default_file_extension %>" do 4 | include <%= controller_class_name %>Helper 5 | 6 | before do 7 | @<%= file_name %> = mock_model(<%= class_name %>) 8 | @<%= nesting_owner %> = mock_model(<%= nesting_owner_class %>) 9 | <% for attribute in attributes -%> 10 | @<%= file_name %>.stub!(:<%= attribute.name %>).and_return(<%= attribute.default_value %>) 11 | <% end -%> 12 | assigns[:<%= file_name %>] = @<%= file_name %> 13 | assigns[:<%= nesting_owner %>] = @<%= nesting_owner %> 14 | end 15 | 16 | it "should render edit form" do 17 | render "/<%= table_name %>/edit.<%= default_file_extension %>" 18 | 19 | response.should have_tag("form[action=#{<%= nesting_owner %>_<%= file_name %>_path(@<%= nesting_owner %>, @<%= file_name %>)}][method=post]") do 20 | with_tag('div[style=?]', "margin:0;padding:0") do 21 | with_tag('input[name=_method][type=hidden][value=put]') 22 | end 23 | <% for attribute in attributes -%><% unless attribute.name =~ /_id/ || [:datetime, :timestamp, :time, :date].index(attribute.type) -%> 24 | with_tag('<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>[name=?]', "<%= file_name %>[<%= attribute.name %>]") 25 | <% end -%><% end -%> 26 | end 27 | end 28 | end 29 | 30 | 31 | -------------------------------------------------------------------------------- /generators/rspec_nested_scaffold/templates/helper_spec.rb: -------------------------------------------------------------------------------- 1 | require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../spec_helper' 2 | 3 | describe <%= controller_class_name %>Helper do 4 | 5 | #Delete this example and add some real ones or delete this file 6 | it "should include the <%= class_name %>Helper" do 7 | included_modules = self.metaclass.send :included_modules 8 | included_modules.should include(<%= controller_class_name %>Helper) 9 | end 10 | 11 | end 12 | -------------------------------------------------------------------------------- /generators/rspec_nested_scaffold/templates/index_erb_spec.rb: -------------------------------------------------------------------------------- 1 | require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../../spec_helper' 2 | 3 | describe "/<%= table_name %>/index.<%= default_file_extension %>" do 4 | include <%= controller_class_name %>Helper 5 | 6 | before(:each) do 7 | <% [98,99].each do |id| -%> 8 | <%= file_name %>_<%= id %> = mock_model(<%= class_name %>) 9 | <% for attribute in attributes -%> 10 | <%= file_name %>_<%= id %>.should_receive(:<%= attribute.name %>).and_return(<%= attribute.default_value %>) 11 | <% end -%><% end %> 12 | @<%= nesting_owner %> = mock_model(<%= nesting_owner_class %>) 13 | assigns[:<%= table_name %>] = [<%= file_name %>_98, <%= file_name %>_99] 14 | assigns[:<%= nesting_owner %>] = @<%= nesting_owner %> 15 | end 16 | 17 | it "should render list of <%= table_name %>" do 18 | render "/<%= table_name %>/index.<%= default_file_extension %>" 19 | <% for attribute in attributes -%><% unless attribute.name =~ /_id/ || [:datetime, :timestamp, :time, :date].index(attribute.type) -%> 20 | response.should have_tag("tr>td", <%= attribute.default_value %>, 2) 21 | <% end -%><% end -%> 22 | end 23 | end 24 | 25 | -------------------------------------------------------------------------------- /generators/rspec_nested_scaffold/templates/new_erb_spec.rb: -------------------------------------------------------------------------------- 1 | require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../../spec_helper' 2 | 3 | describe "/<%= table_name %>/new.<%= default_file_extension %>" do 4 | include <%= controller_class_name %>Helper 5 | 6 | before(:each) do 7 | @<%= file_name %> = mock_model(<%= class_name %>) 8 | @<%= file_name %>.stub!(:new_record?).and_return(true) 9 | @<%= nesting_owner %> = mock_model(<%= nesting_owner_class %>) 10 | <% for attribute in attributes -%> 11 | @<%= file_name %>.stub!(:<%= attribute.name %>).and_return(<%= attribute.default_value %>) 12 | <% end -%> 13 | assigns[:<%= file_name %>] = @<%= file_name %> 14 | assigns[:<%= nesting_owner %>] = @<%= nesting_owner %> 15 | end 16 | 17 | it "should render new form" do 18 | render "/<%= table_name %>/new.<%= default_file_extension %>" 19 | 20 | response.should have_tag("form[action=?][method=post]", <%= nesting_owner %>_<%= table_name %>_path(@<%= nesting_owner %>)) do 21 | with_tag('input[name=_method][type=hidden][value=put]', :count => 0) 22 | <% for attribute in attributes -%><% unless attribute.name =~ /_id/ || [:datetime, :timestamp, :time, :date].index(attribute.type) -%> 23 | with_tag("<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>[name=?]", "<%= file_name %>[<%= attribute.name %>]") 24 | <% end -%><% end -%> 25 | end 26 | end 27 | end 28 | 29 | 30 | -------------------------------------------------------------------------------- /generators/rspec_nested_scaffold/templates/routing_spec.rb: -------------------------------------------------------------------------------- 1 | require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../spec_helper' 2 | 3 | describe <%= controller_class_name %>Controller do 4 | describe "route generation" do 5 | 6 | it "should map { :controller => '<%= table_name %>', :action => 'index', :<%= nesting_owner %>_id => '2' } to /<%= nesting_owner.pluralize %>/2/<%= table_name %>" do 7 | route_for(:controller => "<%= table_name %>", :action => 'index', :<%= nesting_owner %>_id => '2').should == "/<%= nesting_owner.pluralize %>/2/<%= table_name %>" 8 | end 9 | 10 | it "should map { :controller => '<%= table_name %>', :action => 'new', :<%= nesting_owner %>_id => '2' } to /<%= nesting_owner.pluralize %>/2/<%= table_name %>/new" do 11 | route_for(:controller => "<%= table_name %>", :action => 'new', :<%= nesting_owner %>_id => '2').should == "/<%= nesting_owner.pluralize %>/2/<%= table_name %>/new" 12 | end 13 | 14 | it "should map { :controller => '<%= table_name %>', :action => 'show', :<%= nesting_owner %>_id => '2', :id => 1 } to /<%= nesting_owner.pluralize %>/2/<%= table_name %>/1" do 15 | route_for(:controller => "<%= table_name %>", :action => 'show', :<%= nesting_owner %>_id => '2', :id => 1).should == "/<%= nesting_owner.pluralize %>/2/<%= table_name %>/1" 16 | end 17 | 18 | it "should map { :controller => '<%= table_name %>', :action => 'edit', :<%= nesting_owner %>_id => '2', :id => 1 } to /<%= nesting_owner.pluralize %>/2/<%= table_name %>/1<%= resource_edit_path %>" do 19 | route_for(:controller => "<%= table_name %>", :action => 'edit', :<%= nesting_owner %>_id => '2', :id => 1).should == "/<%= nesting_owner.pluralize %>/2/<%= table_name %>/1<%= resource_edit_path %>" 20 | end 21 | 22 | it "should map { :controller => '<%= table_name %>', :action => 'update', :<%= nesting_owner %>_id => '2', :id => 1} to /<%= nesting_owner.pluralize %>/2/<%= table_name %>/1" do 23 | route_for(:controller => "<%= table_name %>", :action => 'update', :<%= nesting_owner %>_id => '2', :id => 1).should == "/<%= nesting_owner.pluralize %>/2/<%= table_name %>/1" 24 | end 25 | 26 | it "should map { :controller => '<%= table_name %>', :action => 'destroy', :<%= nesting_owner %>_id => '2', :id => 1} to /<%= nesting_owner.pluralize %>/2/<%= table_name %>/1" do 27 | route_for(:controller => "<%= table_name %>", :action => 'destroy', :<%= nesting_owner %>_id => '2', :id => 1).should == "/<%= nesting_owner.pluralize %>/2/<%= table_name %>/1" 28 | end 29 | end 30 | 31 | describe "route recognition" do 32 | 33 | it "should generate params { :controller => '<%= table_name %>', action => 'index', :<%= nesting_owner %>_id => '2' } from GET /<%= nesting_owner.pluralize %>/2/<%= table_name %>" do 34 | params_from(:get, "/<%= nesting_owner.pluralize %>/2/<%= table_name %>").should == {:controller => "<%= table_name %>", :action => 'index', :<%= nesting_owner %>_id => '2'} 35 | end 36 | 37 | it "should generate params { :controller => '<%= table_name %>', action => 'new', :<%= nesting_owner %>_id => '2' } from GET /<%= nesting_owner.pluralize %>/2/<%= table_name %>/new" do 38 | params_from(:get, "/<%= nesting_owner.pluralize %>/2/<%= table_name %>/new").should == {:controller => "<%= table_name %>", :action => 'new', :<%= nesting_owner %>_id => '2'} 39 | end 40 | 41 | it "should generate params { :controller => '<%= table_name %>', action => 'create', :<%= nesting_owner %>_id => '2' } from POST /<%= nesting_owner.pluralize %>/2/<%= table_name %>" do 42 | params_from(:post, "/<%= nesting_owner.pluralize %>/2/<%= table_name %>").should == {:controller => "<%= table_name %>", :action => 'create', :<%= nesting_owner %>_id => '2'} 43 | end 44 | 45 | it "should generate params { :controller => '<%= table_name %>', action => 'show', :<%= nesting_owner %>_id => '2', id => '1' } from GET /<%= nesting_owner.pluralize %>/2/<%= table_name %>/1" do 46 | params_from(:get, "/<%= nesting_owner.pluralize %>/2/<%= table_name %>/1").should == {:controller => "<%= table_name %>", :action => 'show', :<%= nesting_owner %>_id => '2', :id => "1"} 47 | end 48 | 49 | it "should generate params { :controller => '<%= table_name %>', action => 'edit', :<%= nesting_owner %>_id => '2', id => '1' } from GET /<%= nesting_owner.pluralize %>/2/<%= table_name %>/1;edit" do 50 | params_from(:get, "/<%= nesting_owner.pluralize %>/2/<%= table_name %>/1<%= resource_edit_path %>").should == {:controller => "<%= table_name %>", :action => 'edit', :<%= nesting_owner %>_id => '2', :id => "1"} 51 | end 52 | 53 | it "should generate params { :controller => '<%= table_name %>', action => 'update', :<%= nesting_owner %>_id => '2', id => '1' } from PUT /<%= nesting_owner.pluralize %>/2/<%= table_name %>/1" do 54 | params_from(:put, "/<%= nesting_owner.pluralize %>/2/<%= table_name %>/1").should == {:controller => "<%= table_name %>", :action => 'update', :<%= nesting_owner %>_id => '2', :id => "1"} 55 | end 56 | 57 | it "should generate params { :controller => '<%= table_name %>', action => 'destroy', :<%= nesting_owner %>_id => '2', id => '1' } from DELETE /<%= nesting_owner.pluralize %>/2/<%= table_name %>/1" do 58 | params_from(:delete, "/<%= nesting_owner.pluralize %>/2/<%= table_name %>/1").should == {:controller => "<%= table_name %>", :action => 'destroy', :<%= nesting_owner %>_id => '2', :id => "1"} 59 | end 60 | end 61 | end -------------------------------------------------------------------------------- /generators/rspec_nested_scaffold/templates/show_erb_spec.rb: -------------------------------------------------------------------------------- 1 | require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../../spec_helper' 2 | 3 | describe "/<%= table_name %>/show.<%= default_file_extension %>" do 4 | include <%= controller_class_name %>Helper 5 | 6 | before(:each) do 7 | @<%= file_name %> = mock_model(<%= class_name %>) 8 | @<%= nesting_owner %> = mock_model(<%= nesting_owner_class %>) 9 | <% for attribute in attributes -%> 10 | @<%= file_name %>.stub!(:<%= attribute.name %>).and_return(<%= attribute.default_value %>) 11 | <% end -%> 12 | assigns[:<%= nesting_owner %>] = @<%= nesting_owner %> 13 | assigns[:<%= file_name %>] = @<%= file_name %> 14 | end 15 | 16 | it "should render attributes in
" do 17 | render "/<%= table_name %>/show.<%= default_file_extension %>" 18 | <% for attribute in attributes -%><% unless attribute.name =~ /_id/ || [:datetime, :timestamp, :time, :date].index(attribute.type) -%> 19 | response.should have_text(/<%= Regexp.escape(attribute.default_value)[1..-2]%>/) 20 | <% end -%><% end -%> 21 | end 22 | end 23 | 24 | -------------------------------------------------------------------------------- /generators/rspec_nested_scaffold/templates/view_edit.html.erb: -------------------------------------------------------------------------------- 1 |
8 | <%%= f.label :<%= attribute.name %> %>
9 | <%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
10 |
13 | <%%= f.submit "Update" %> 14 |
15 | <%% end %> 16 | 17 | <%%= link_to 'Show', <%= nesting_owner %>_<%= singular_name %>_path(@<%= nesting_owner %>, @<%= singular_name %>) %> | 18 | <%%= link_to 'Back', <%= nesting_owner %>_<%= plural_name %>_path(@<%= nesting_owner %>) %> 19 | -------------------------------------------------------------------------------- /generators/rspec_nested_scaffold/templates/view_index.html.erb: -------------------------------------------------------------------------------- 1 |<%= attribute.column.human_name %> | 7 | <% end -%> 8 ||||
---|---|---|---|
<%%=h <%= singular_name %>.<%= attribute.name %> %> | 14 | <% end -%> 15 |<%%= link_to 'Show', <%= nesting_owner %>_<%= singular_name %>_path(@<%= nesting_owner %>, <%= singular_name %>) %> | 16 |<%%= link_to 'Edit', edit_<%= nesting_owner %>_<%= singular_name %>_path(@<%= nesting_owner %>, <%= singular_name %>) %> | 17 |<%%= link_to 'Destroy', <%= nesting_owner %>_<%= singular_name %>_path(@<%= nesting_owner %>, <%= singular_name %>), :confirm => 'Are you sure?', :method => :delete %> | 18 |
8 | <%%= f.label :<%= attribute.name %> %>
9 | <%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
10 |
13 | <%%= f.submit "Create" %> 14 |
15 | <%% end %> 16 | 17 | <%%= link_to 'Back', <%= nesting_owner %>_<%= plural_name %>_path(@<%= nesting_owner %>) %> -------------------------------------------------------------------------------- /generators/rspec_nested_scaffold/templates/view_show.html.erb: -------------------------------------------------------------------------------- 1 | <% for attribute in attributes -%> 2 |3 | <%= attribute.column.human_name %>: 4 | <%%=h @<%= singular_name %>.<%= attribute.name %> %> 5 |
6 | 7 | <% end -%> 8 | 9 | <%%= link_to 'Edit', edit_<%= nesting_owner %>_<%= singular_name %>_path(@<%= nesting_owner %>, @<%= singular_name %>) %> | 10 | <%%= link_to 'Back', <%= nesting_owner %>_<%= plural_name %>_path(@<%= nesting_owner %>) %> 11 | -------------------------------------------------------------------------------- /init.rb: -------------------------------------------------------------------------------- 1 | # Include hook code here 2 | -------------------------------------------------------------------------------- /install.rb: -------------------------------------------------------------------------------- 1 | # Install hook code here 2 | -------------------------------------------------------------------------------- /lib/rspec_on_rails_nested_routes.rb: -------------------------------------------------------------------------------- 1 | # RspecNestedScaffold 2 | -------------------------------------------------------------------------------- /tasks/rspec_nested_scaffold_tasks.rake: -------------------------------------------------------------------------------- 1 | # desc "Explaining what the task does" 2 | # task :rspec_nested_scaffold do 3 | # # Task goes here 4 | # end 5 | -------------------------------------------------------------------------------- /test/rspec_nested_scaffold_test.rb: -------------------------------------------------------------------------------- 1 | require 'test/unit' 2 | 3 | class RspecNestedScaffoldTest < Test::Unit::TestCase 4 | # Replace this with your real tests. 5 | def test_this_plugin 6 | flunk 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /uninstall.rb: -------------------------------------------------------------------------------- 1 | # Uninstall hook code here 2 | --------------------------------------------------------------------------------