diff --git a/.rvmrc b/.rvmrc index a47e4b1..d188f12 100644 --- a/.rvmrc +++ b/.rvmrc @@ -1,23 +1 @@ -#!/usr/bin/env bash - -# adapted from: http://rvm.beginrescueend.com/workflow/rvmrc/ - -ruby_string="1.9.2" -gemset_name="nifty-generators" - -if rvm list strings | grep -q "${ruby_string}" ; then - - # Load or create the specified environment - if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \ - && -s "${rvm_path:-$HOME/.rvm}/environments/${ruby_string}@${gemset_name}" ]] ; then - \. "${rvm_path:-$HOME/.rvm}/environments/${ruby_string}@${gemset_name}" - else - rvm --create "${ruby_string}@${gemset_name}" - fi - -else - - # Notify the user to install the desired interpreter before proceeding. - echo "${ruby_string} was not found, please run 'rvm install ${ruby_string}' and then cd back into the project directory." - -fi +rvm use 1.9.2@nifty-generators --create diff --git a/CHANGELOG b/CHANGELOG index c470e38..cf6631c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,10 @@ +0.4.6 (March 26, 2011) + +* fixing form block in HAML (thanks apocalyptiq) + +* fixing instance variable setting in HAML (thanks nhocki) + + 0.4.5 (February 14, 2011) * fixing user validation tests in nifty:authentication (thanks apocalyptiq) - issue #87 diff --git a/nifty-generators.gemspec b/nifty-generators.gemspec index 74ba8fb..d9b6402 100644 --- a/nifty-generators.gemspec +++ b/nifty-generators.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = "nifty-generators" - s.version = "0.4.5" + s.version = "0.4.6" s.author = "Ryan Bates" s.email = "ryan@railscasts.com" s.homepage = "http://github.com/ryanb/nifty-generators" diff --git a/rails_generators/nifty_authentication/templates/views/haml/login.html.haml b/rails_generators/nifty_authentication/templates/views/haml/login.html.haml index a399d72..964e8da 100644 --- a/rails_generators/nifty_authentication/templates/views/haml/login.html.haml +++ b/rails_generators/nifty_authentication/templates/views/haml/login.html.haml @@ -3,7 +3,7 @@ %p== Don't have an account? #{link_to "Sign up!", signup_path} <%- if options[:authlogic] -%> -- form_for @<%= session_singular_name %> do |f| += form_for @<%= session_singular_name %> do |f| = f.error_messages %p = f.label :username diff --git a/rails_generators/nifty_scaffold/nifty_scaffold_generator.rb b/rails_generators/nifty_scaffold/nifty_scaffold_generator.rb index 6393863..cf45599 100644 --- a/rails_generators/nifty_scaffold/nifty_scaffold_generator.rb +++ b/rails_generators/nifty_scaffold/nifty_scaffold_generator.rb @@ -43,51 +43,71 @@ def initialize(runtime_args, runtime_options = {}) def manifest record do |m| unless options[:skip_model] - m.directory "app/models" - m.template "model.rb", "app/models/#{singular_name}.rb" + destination = "app/models/#{model_name.underscore}.rb" + m.directory File.dirname(destination) + m.template "model.rb", destination unless options[:skip_migration] - m.migration_template "migration.rb", "db/migrate", :migration_file_name => "create_#{plural_name}" + m.migration_template "migration.rb", "db/migrate", :migration_file_name => "create_#{model_name.underscore.pluralize.gsub('/','_')}" end if rspec? - m.directory "spec/models" - m.template "tests/#{test_framework}/model.rb", "spec/models/#{singular_name}_spec.rb" - m.directory "spec/fixtures" - m.template "fixtures.yml", "spec/fixtures/#{plural_name}.yml" + destination = "spec/models/#{model_name.underscore}_spec.rb" + m.directory File.dirname(destination) + m.template "tests/#{test_framework}/model.rb", destination + destination = "spec/fixtures/#{plural_model_name}.yml" + m.directory File.dirname(destination) + m.template "fixtures.yml", destination else - m.directory "test/unit" - m.template "tests/#{test_framework}/model.rb", "test/unit/#{singular_name}_test.rb" - m.directory "test/fixtures" - m.template "fixtures.yml", "test/fixtures/#{plural_name}.yml" + destination = "test/unit/#{model_name.underscore}_test.rb" + m.directory File.dirname(destination) + m.template "tests/#{test_framework}/model.rb", destination + destination = "test/fixtures/#{plural_model_name}.yml" + m.directory File.dirname(destination) + m.template "fixtures.yml", destination end end unless options[:skip_controller] - m.directory "app/controllers" - m.template "controller.rb", "app/controllers/#{plural_name}_controller.rb" + destination = "app/controllers/#{resource_path}_controller.rb" + m.directory File.dirname(destination) + m.template "controller.rb", destination - m.directory "app/helpers" - m.template "helper.rb", "app/helpers/#{plural_name}_helper.rb" + destination = "app/helpers/#{resource_path}_helper.rb" + m.directory File.dirname(destination) + m.template "helper.rb", destination - m.directory "app/views/#{plural_name}" + m.directory "app/views/#{resource_path}" controller_actions.each do |action| if File.exist? source_path("views/#{view_language}/#{action}.html.#{view_language}") - m.template "views/#{view_language}/#{action}.html.#{view_language}", "app/views/#{plural_name}/#{action}.html.#{view_language}" + m.template "views/#{view_language}/#{action}.html.#{view_language}", "app/views/#{resource_path}/#{action}.html.#{view_language}" end end if form_partial? - m.template "views/#{view_language}/_form.html.#{view_language}", "app/views/#{plural_name}/_form.html.#{view_language}" + m.template "views/#{view_language}/_form.html.#{view_language}", "app/views/#{resource_path}/_form.html.#{view_language}" end - m.route_resources plural_name + sentinel = 'ActionController::Routing::Routes.draw do |map|' + namespaces = resource_path.split('/') + resource = namespaces.pop + route = namespaces.reverse.inject("map.resources :#{resource}") { |acc, namespace| + "map.namespace(:#{namespace}){|map| #{acc} }" + } + logger.route route + unless options[:pretend] + m.gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match| + "#{match}\n #{route}\n" + end + end if rspec? - m.directory "spec/controllers" - m.template "tests/#{test_framework}/controller.rb", "spec/controllers/#{plural_name}_controller_spec.rb" + destination = "spec/controllers/#{resource_path}_controller_spec.rb" + m.directory File.dirname(destination) + m.template "tests/#{test_framework}/controller.rb", destination else - m.directory "test/functional" - m.template "tests/#{test_framework}/controller.rb", "test/functional/#{plural_name}_controller_test.rb" + destination = "test/functional/#{resource_path}_controller_test.rb" + m.directory File.dirname(destination) + m.template "tests/#{test_framework}/controller.rb", destination end end end @@ -109,20 +129,58 @@ def actions?(*names) names.all? { |n| action? n.to_s } end - def singular_name - name.underscore + def resource_path + name.underscore.pluralize end - def plural_name - name.underscore.pluralize + def singular_model_name + model_name.underscore.gsub('/', '_') + end + + def plural_model_name + singular_model_name.pluralize end - def class_name - name.camelize + def model_name + if options[:namespace_model] + name.camelize + else + name.split('::').last.camelize + end end - def plural_class_name - plural_name.camelize + def items_path + if action? :index + "#{resource_path.gsub('/', '_')}_path" + else + "root_path" + end + end + + def items_url + if action? :index + "#{resource_path.gsub('/', '_')}_url" + else + "root_url" + end + end + + def item_path(options = {}) + if action?(:show) || options[:action] + name = options[:instance_variable] ? "@#{singular_model_name}" : singular_model_name + if %w(new edit).include? options[:action].to_s + "#{options[:action].to_s}_#{resource_path.singularize.gsub('/', '_')}_path(#{name})" + else + if resource_path.include?('/') && !self.options[:namespace_model] + namespace = resource_path.split('/')[0..-2] + "[ :#{namespace.join(', :')}, #{name} ]" + else + name + end + end + else + items_path + end end def controller_methods(dir_name) @@ -143,40 +201,32 @@ def render_form end end - def items_path(suffix = 'path') - if action? :index - "#{plural_name}_#{suffix}" - else - "root_#{suffix}" - end - end - - def item_path(suffix = 'path') - if action? :show - "@#{singular_name}" - else - items_path(suffix) - end - end - def item_path_for_spec(suffix = 'path') if action? :show - "#{singular_name}_#{suffix}(assigns[:#{singular_name}])" + "#{resource_path.singularize.gsub('/', '_')}_#{suffix}(assigns[:#{singular_model_name}])" else - items_path(suffix) + if suffix == 'path' + items_path + else + items_url + end end end def item_path_for_test(suffix = 'path') if action? :show - "#{singular_name}_#{suffix}(assigns(:#{singular_name}))" + "#{resource_path.singularize.gsub('/', '_')}_#{suffix}(assigns(:#{singular_model_name}))" else - items_path(suffix) + if suffix == 'path' + items_path + else + items_url + end end end def model_columns_for_attributes - class_name.constantize.columns.reject do |column| + model_name.constantize.columns.reject do |column| column.name.to_s =~ /^(id|created_at|updated_at)$/ end end @@ -206,6 +256,7 @@ def add_options!(opt) opt.on("--skip-migration", "Don't generate migration file for model.") { |v| options[:skip_migration] = v } opt.on("--skip-timestamps", "Don't add timestamps to migration file.") { |v| options[:skip_timestamps] = v } opt.on("--skip-controller", "Don't generate controller, helper, or views.") { |v| options[:skip_controller] = v } + opt.on("--namespace-model", "If the resource is namespaced, include the model in the namespace.") { |v| options[:namespace_model] = v } opt.on("--invert", "Generate all controller actions except these mentioned.") { |v| options[:invert] = v } opt.on("--haml", "Generate HAML views instead of ERB.") { |v| options[:haml] = v } opt.on("--testunit", "Use test/unit for test files.") { options[:test_framework] = :testunit } @@ -215,7 +266,7 @@ def add_options!(opt) # is there a better way to do this? Perhaps with const_defined? def model_exists? - File.exist? destination_path("app/models/#{singular_name}.rb") + File.exist? destination_path("app/models/#{model_name.underscore}.rb") end def read_template(relative_path) diff --git a/rails_generators/nifty_scaffold/templates/actions/create.rb b/rails_generators/nifty_scaffold/templates/actions/create.rb index a6fe7fb..e9ed9c7 100644 --- a/rails_generators/nifty_scaffold/templates/actions/create.rb +++ b/rails_generators/nifty_scaffold/templates/actions/create.rb @@ -1,8 +1,8 @@ def create - @<%= singular_name %> = <%= class_name %>.new(params[:<%= singular_name %>]) - if @<%= singular_name %>.save + @<%= singular_model_name %> = <%= model_name %>.new(params[:<%= singular_model_name %>]) + if @<%= singular_model_name %>.save flash[:notice] = "Successfully created <%= name.underscore.humanize.downcase %>." - redirect_to <%= item_path('url') %> + redirect_to <%= item_path :instance_variable => true %> else render :action => 'new' end diff --git a/rails_generators/nifty_scaffold/templates/actions/destroy.rb b/rails_generators/nifty_scaffold/templates/actions/destroy.rb index c25dc5c..7582b7a 100644 --- a/rails_generators/nifty_scaffold/templates/actions/destroy.rb +++ b/rails_generators/nifty_scaffold/templates/actions/destroy.rb @@ -1,6 +1,6 @@ def destroy - @<%= singular_name %> = <%= class_name %>.find(params[:id]) - @<%= singular_name %>.destroy + @<%= singular_model_name %> = <%= model_name %>.find(params[:id]) + @<%= singular_model_name %>.destroy flash[:notice] = "Successfully destroyed <%= name.underscore.humanize.downcase %>." - redirect_to <%= items_path('url') %> + redirect_to <%= items_url %> end diff --git a/rails_generators/nifty_scaffold/templates/actions/edit.rb b/rails_generators/nifty_scaffold/templates/actions/edit.rb index eb220f5..4a89649 100644 --- a/rails_generators/nifty_scaffold/templates/actions/edit.rb +++ b/rails_generators/nifty_scaffold/templates/actions/edit.rb @@ -1,3 +1,3 @@ def edit - @<%= singular_name %> = <%= class_name %>.find(params[:id]) + @<%= singular_model_name %> = <%= model_name %>.find(params[:id]) end diff --git a/rails_generators/nifty_scaffold/templates/actions/index.rb b/rails_generators/nifty_scaffold/templates/actions/index.rb index a72f6c6..8b21845 100644 --- a/rails_generators/nifty_scaffold/templates/actions/index.rb +++ b/rails_generators/nifty_scaffold/templates/actions/index.rb @@ -1,3 +1,3 @@ def index - @<%= plural_name %> = <%= class_name %>.all + @<%= plural_model_name %> = <%= model_name %>.all end diff --git a/rails_generators/nifty_scaffold/templates/actions/new.rb b/rails_generators/nifty_scaffold/templates/actions/new.rb index cf3f68b..fc16649 100644 --- a/rails_generators/nifty_scaffold/templates/actions/new.rb +++ b/rails_generators/nifty_scaffold/templates/actions/new.rb @@ -1,3 +1,3 @@ def new - @<%= singular_name %> = <%= class_name %>.new + @<%= singular_model_name %> = <%= model_name %>.new end diff --git a/rails_generators/nifty_scaffold/templates/actions/show.rb b/rails_generators/nifty_scaffold/templates/actions/show.rb index c8e26f4..c4615d7 100644 --- a/rails_generators/nifty_scaffold/templates/actions/show.rb +++ b/rails_generators/nifty_scaffold/templates/actions/show.rb @@ -1,3 +1,3 @@ def show - @<%= singular_name %> = <%= class_name %>.find(params[:id]) + @<%= singular_model_name %> = <%= model_name %>.find(params[:id]) end diff --git a/rails_generators/nifty_scaffold/templates/actions/update.rb b/rails_generators/nifty_scaffold/templates/actions/update.rb index f1c6db1..069c2a2 100644 --- a/rails_generators/nifty_scaffold/templates/actions/update.rb +++ b/rails_generators/nifty_scaffold/templates/actions/update.rb @@ -1,8 +1,8 @@ def update - @<%= singular_name %> = <%= class_name %>.find(params[:id]) - if @<%= singular_name %>.update_attributes(params[:<%= singular_name %>]) + @<%= singular_model_name %> = <%= model_name %>.find(params[:id]) + if @<%= singular_model_name %>.update_attributes(params[:<%= singular_model_name %>]) flash[:notice] = "Successfully updated <%= name.underscore.humanize.downcase %>." - redirect_to <%= item_path('url') %> + redirect_to <%= item_path :instance_variable => true %> else render :action => 'edit' end diff --git a/rails_generators/nifty_scaffold/templates/controller.rb b/rails_generators/nifty_scaffold/templates/controller.rb index a54de70..52bdf12 100644 --- a/rails_generators/nifty_scaffold/templates/controller.rb +++ b/rails_generators/nifty_scaffold/templates/controller.rb @@ -1,3 +1,3 @@ -class <%= plural_class_name %>Controller < ApplicationController +class <%= resource_path.camelize %>Controller < ApplicationController <%= controller_methods :actions %> end diff --git a/rails_generators/nifty_scaffold/templates/helper.rb b/rails_generators/nifty_scaffold/templates/helper.rb index 084b485..ed48dac 100644 --- a/rails_generators/nifty_scaffold/templates/helper.rb +++ b/rails_generators/nifty_scaffold/templates/helper.rb @@ -1,2 +1,2 @@ -module <%= plural_class_name %>Helper +module <%= resource_path.camelize %>Helper end diff --git a/rails_generators/nifty_scaffold/templates/migration.rb b/rails_generators/nifty_scaffold/templates/migration.rb index 784f0ca..2e48765 100644 --- a/rails_generators/nifty_scaffold/templates/migration.rb +++ b/rails_generators/nifty_scaffold/templates/migration.rb @@ -1,6 +1,6 @@ -class Create<%= plural_class_name %> < ActiveRecord::Migration +class Create<%= model_name.pluralize.delete('::') %> < ActiveRecord::Migration def self.up - create_table :<%= plural_name %> do |t| + create_table :<%= plural_model_name %> do |t| <%- for attribute in attributes -%> t.<%= attribute.type %> :<%= attribute.name %> <%- end -%> @@ -11,6 +11,6 @@ def self.up end def self.down - drop_table :<%= plural_name %> + drop_table :<%= plural_model_name %> end end diff --git a/rails_generators/nifty_scaffold/templates/model.rb b/rails_generators/nifty_scaffold/templates/model.rb index e5c9e81..d6b07f5 100644 --- a/rails_generators/nifty_scaffold/templates/model.rb +++ b/rails_generators/nifty_scaffold/templates/model.rb @@ -1,3 +1,4 @@ -class <%= class_name %> < ActiveRecord::Base +class <%= model_name %> < ActiveRecord::Base + <%= "set_table_name :#{plural_model_name}" if model_name.include?('::') %> attr_accessible <%= attributes.map { |a| ":#{a.name}" }.join(", ") %> end diff --git a/rails_generators/nifty_scaffold/templates/tests/rspec/actions/create.rb b/rails_generators/nifty_scaffold/templates/tests/rspec/actions/create.rb index ef5a906..3e9d9f4 100644 --- a/rails_generators/nifty_scaffold/templates/tests/rspec/actions/create.rb +++ b/rails_generators/nifty_scaffold/templates/tests/rspec/actions/create.rb @@ -1,11 +1,11 @@ it "create action should render new template when model is invalid" do - <%= class_name %>.any_instance.stubs(:valid?).returns(false) + <%= model_name %>.any_instance.stubs(:valid?).returns(false) post :create response.should render_template(:new) end it "create action should redirect when model is valid" do - <%= class_name %>.any_instance.stubs(:valid?).returns(true) + <%= model_name %>.any_instance.stubs(:valid?).returns(true) post :create response.should redirect_to(<%= item_path_for_spec('url') %>) end diff --git a/rails_generators/nifty_scaffold/templates/tests/rspec/actions/destroy.rb b/rails_generators/nifty_scaffold/templates/tests/rspec/actions/destroy.rb index b405fbd..a905e9a 100644 --- a/rails_generators/nifty_scaffold/templates/tests/rspec/actions/destroy.rb +++ b/rails_generators/nifty_scaffold/templates/tests/rspec/actions/destroy.rb @@ -1,6 +1,6 @@ it "destroy action should destroy model and redirect to index action" do - <%= singular_name %> = <%= class_name %>.first - delete :destroy, :id => <%= singular_name %> - response.should redirect_to(<%= items_path('url') %>) - <%= class_name %>.exists?(<%= singular_name %>.id).should be_false + <%= singular_model_name %> = <%= model_name %>.first + delete :destroy, :id => <%= singular_model_name %> + response.should redirect_to(<%= items_url %>) + <%= model_name %>.exists?(<%= singular_model_name %>.id).should be_false end diff --git a/rails_generators/nifty_scaffold/templates/tests/rspec/actions/edit.rb b/rails_generators/nifty_scaffold/templates/tests/rspec/actions/edit.rb index e144904..4b52648 100644 --- a/rails_generators/nifty_scaffold/templates/tests/rspec/actions/edit.rb +++ b/rails_generators/nifty_scaffold/templates/tests/rspec/actions/edit.rb @@ -1,4 +1,4 @@ it "edit action should render edit template" do - get :edit, :id => <%= class_name %>.first + get :edit, :id => <%= model_name %>.first response.should render_template(:edit) end diff --git a/rails_generators/nifty_scaffold/templates/tests/rspec/actions/show.rb b/rails_generators/nifty_scaffold/templates/tests/rspec/actions/show.rb index eaa3d93..b4786b0 100644 --- a/rails_generators/nifty_scaffold/templates/tests/rspec/actions/show.rb +++ b/rails_generators/nifty_scaffold/templates/tests/rspec/actions/show.rb @@ -1,4 +1,4 @@ it "show action should render show template" do - get :show, :id => <%= class_name %>.first + get :show, :id => <%= model_name %>.first response.should render_template(:show) end diff --git a/rails_generators/nifty_scaffold/templates/tests/rspec/actions/update.rb b/rails_generators/nifty_scaffold/templates/tests/rspec/actions/update.rb index c919962..1ef1a6a 100644 --- a/rails_generators/nifty_scaffold/templates/tests/rspec/actions/update.rb +++ b/rails_generators/nifty_scaffold/templates/tests/rspec/actions/update.rb @@ -1,11 +1,11 @@ it "update action should render edit template when model is invalid" do - <%= class_name %>.any_instance.stubs(:valid?).returns(false) - put :update, :id => <%= class_name %>.first + <%= model_name %>.any_instance.stubs(:valid?).returns(false) + put :update, :id => <%= model_name %>.first response.should render_template(:edit) end it "update action should redirect when model is valid" do - <%= class_name %>.any_instance.stubs(:valid?).returns(true) - put :update, :id => <%= class_name %>.first + <%= model_name %>.any_instance.stubs(:valid?).returns(true) + put :update, :id => <%= model_name %>.first response.should redirect_to(<%= item_path_for_spec('url') %>) end diff --git a/rails_generators/nifty_scaffold/templates/tests/rspec/controller.rb b/rails_generators/nifty_scaffold/templates/tests/rspec/controller.rb index 850f9b9..a26a95e 100644 --- a/rails_generators/nifty_scaffold/templates/tests/rspec/controller.rb +++ b/rails_generators/nifty_scaffold/templates/tests/rspec/controller.rb @@ -1,6 +1,6 @@ -require File.dirname(__FILE__) + '/../spec_helper' +require File.dirname(__FILE__) + '<%= resource_path.gsub(/[^\/]+/, '/..') %>/spec_helper' -describe <%= plural_class_name %>Controller do +describe <%= resource_path.camelize %>Controller do fixtures :all integrate_views diff --git a/rails_generators/nifty_scaffold/templates/tests/rspec/model.rb b/rails_generators/nifty_scaffold/templates/tests/rspec/model.rb index 25c3cc4..c73b546 100644 --- a/rails_generators/nifty_scaffold/templates/tests/rspec/model.rb +++ b/rails_generators/nifty_scaffold/templates/tests/rspec/model.rb @@ -1,7 +1,7 @@ -require File.dirname(__FILE__) + '/../spec_helper' +require File.dirname(__FILE__) + '<%= model_name.underscore.gsub(/[^\/]+/, '/..') %>/spec_helper' -describe <%= class_name %> do +describe <%= model_name %> do it "should be valid" do - <%= class_name %>.new.should be_valid + <%= model_name %>.new.should be_valid end end diff --git a/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/create.rb b/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/create.rb index f305367..6f0355b 100644 --- a/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/create.rb +++ b/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/create.rb @@ -1,12 +1,12 @@ context "create action" do should "render new template when model is invalid" do - <%= class_name %>.any_instance.stubs(:valid?).returns(false) + <%= model_name %>.any_instance.stubs(:valid?).returns(false) post :create assert_template 'new' end should "redirect when model is valid" do - <%= class_name %>.any_instance.stubs(:valid?).returns(true) + <%= model_name %>.any_instance.stubs(:valid?).returns(true) post :create assert_redirected_to <%= item_path_for_test('url') %> end diff --git a/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/destroy.rb b/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/destroy.rb index 44888f9..867e605 100644 --- a/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/destroy.rb +++ b/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/destroy.rb @@ -1,8 +1,8 @@ context "destroy action" do should "destroy model and redirect to index action" do - <%= singular_name %> = <%= class_name %>.first - delete :destroy, :id => <%= singular_name %> - assert_redirected_to <%= items_path('url') %> - assert !<%= class_name %>.exists?(<%= singular_name %>.id) + <%= singular_model_name %> = <%= model_name %>.first + delete :destroy, :id => <%= singular_model_name %> + assert_redirected_to <%= items_url %> + assert !<%= model_name %>.exists?(<%= singular_model_name %>.id) end end diff --git a/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/edit.rb b/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/edit.rb index 47597d0..4b0006c 100644 --- a/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/edit.rb +++ b/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/edit.rb @@ -1,6 +1,6 @@ context "edit action" do should "render edit template" do - get :edit, :id => <%= class_name %>.first + get :edit, :id => <%= model_name %>.first assert_template 'edit' end end diff --git a/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/show.rb b/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/show.rb index 75b37f6..908e6f4 100644 --- a/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/show.rb +++ b/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/show.rb @@ -1,6 +1,6 @@ context "show action" do should "render show template" do - get :show, :id => <%= class_name %>.first + get :show, :id => <%= model_name %>.first assert_template 'show' end end diff --git a/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/update.rb b/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/update.rb index fc46f72..968b931 100644 --- a/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/update.rb +++ b/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/update.rb @@ -1,13 +1,13 @@ context "update action" do should "render edit template when model is invalid" do - <%= class_name %>.any_instance.stubs(:valid?).returns(false) - put :update, :id => <%= class_name %>.first + <%= model_name %>.any_instance.stubs(:valid?).returns(false) + put :update, :id => <%= model_name %>.first assert_template 'edit' end should "redirect when model is valid" do - <%= class_name %>.any_instance.stubs(:valid?).returns(true) - put :update, :id => <%= class_name %>.first + <%= model_name %>.any_instance.stubs(:valid?).returns(true) + put :update, :id => <%= model_name %>.first assert_redirected_to <%= item_path_for_test('url') %> end end diff --git a/rails_generators/nifty_scaffold/templates/tests/shoulda/controller.rb b/rails_generators/nifty_scaffold/templates/tests/shoulda/controller.rb index e9f9764..16bb3cc 100644 --- a/rails_generators/nifty_scaffold/templates/tests/shoulda/controller.rb +++ b/rails_generators/nifty_scaffold/templates/tests/shoulda/controller.rb @@ -1,5 +1,5 @@ require 'test_helper' -class <%= plural_class_name %>ControllerTest < ActionController::TestCase +class <%= resource_path.camelize %>ControllerTest < ActionController::TestCase <%= controller_methods 'tests/shoulda/actions' %> end diff --git a/rails_generators/nifty_scaffold/templates/tests/shoulda/model.rb b/rails_generators/nifty_scaffold/templates/tests/shoulda/model.rb index 9065963..c666d0a 100644 --- a/rails_generators/nifty_scaffold/templates/tests/shoulda/model.rb +++ b/rails_generators/nifty_scaffold/templates/tests/shoulda/model.rb @@ -1,7 +1,7 @@ require 'test_helper' -class <%= class_name %>Test < ActiveSupport::TestCase +class <%= model_name %>Test < ActiveSupport::TestCase should "be valid" do - assert <%= class_name %>.new.valid? + assert <%= model_name %>.new.valid? end end diff --git a/rails_generators/nifty_scaffold/templates/tests/testunit/actions/create.rb b/rails_generators/nifty_scaffold/templates/tests/testunit/actions/create.rb index 5a6d2ac..f3ac1a2 100644 --- a/rails_generators/nifty_scaffold/templates/tests/testunit/actions/create.rb +++ b/rails_generators/nifty_scaffold/templates/tests/testunit/actions/create.rb @@ -1,11 +1,11 @@ def test_create_invalid - <%= class_name %>.any_instance.stubs(:valid?).returns(false) + <%= model_name %>.any_instance.stubs(:valid?).returns(false) post :create assert_template 'new' end def test_create_valid - <%= class_name %>.any_instance.stubs(:valid?).returns(true) + <%= model_name %>.any_instance.stubs(:valid?).returns(true) post :create assert_redirected_to <%= item_path_for_test('url') %> end diff --git a/rails_generators/nifty_scaffold/templates/tests/testunit/actions/destroy.rb b/rails_generators/nifty_scaffold/templates/tests/testunit/actions/destroy.rb index bf99ab7..57c936a 100644 --- a/rails_generators/nifty_scaffold/templates/tests/testunit/actions/destroy.rb +++ b/rails_generators/nifty_scaffold/templates/tests/testunit/actions/destroy.rb @@ -1,6 +1,6 @@ def test_destroy - <%= singular_name %> = <%= class_name %>.first - delete :destroy, :id => <%= singular_name %> - assert_redirected_to <%= items_path('url') %> - assert !<%= class_name %>.exists?(<%= singular_name %>.id) + <%= singular_model_name %> = <%= model_name %>.first + delete :destroy, :id => <%= singular_model_name %> + assert_redirected_to <%= items_url %> + assert !<%= model_name %>.exists?(<%= singular_model_name %>.id) end diff --git a/rails_generators/nifty_scaffold/templates/tests/testunit/actions/edit.rb b/rails_generators/nifty_scaffold/templates/tests/testunit/actions/edit.rb index 55ed24a..20e593d 100644 --- a/rails_generators/nifty_scaffold/templates/tests/testunit/actions/edit.rb +++ b/rails_generators/nifty_scaffold/templates/tests/testunit/actions/edit.rb @@ -1,4 +1,4 @@ def test_edit - get :edit, :id => <%= class_name %>.first + get :edit, :id => <%= model_name %>.first assert_template 'edit' end diff --git a/rails_generators/nifty_scaffold/templates/tests/testunit/actions/show.rb b/rails_generators/nifty_scaffold/templates/tests/testunit/actions/show.rb index b74f371..71fe612 100644 --- a/rails_generators/nifty_scaffold/templates/tests/testunit/actions/show.rb +++ b/rails_generators/nifty_scaffold/templates/tests/testunit/actions/show.rb @@ -1,4 +1,4 @@ def test_show - get :show, :id => <%= class_name %>.first + get :show, :id => <%= model_name %>.first assert_template 'show' end diff --git a/rails_generators/nifty_scaffold/templates/tests/testunit/actions/update.rb b/rails_generators/nifty_scaffold/templates/tests/testunit/actions/update.rb index b588bfc..e4b9b47 100644 --- a/rails_generators/nifty_scaffold/templates/tests/testunit/actions/update.rb +++ b/rails_generators/nifty_scaffold/templates/tests/testunit/actions/update.rb @@ -1,11 +1,11 @@ def test_update_invalid - <%= class_name %>.any_instance.stubs(:valid?).returns(false) - put :update, :id => <%= class_name %>.first + <%= model_name %>.any_instance.stubs(:valid?).returns(false) + put :update, :id => <%= model_name %>.first assert_template 'edit' end def test_update_valid - <%= class_name %>.any_instance.stubs(:valid?).returns(true) - put :update, :id => <%= class_name %>.first + <%= model_name %>.any_instance.stubs(:valid?).returns(true) + put :update, :id => <%= model_name %>.first assert_redirected_to <%= item_path_for_test('url') %> end diff --git a/rails_generators/nifty_scaffold/templates/tests/testunit/controller.rb b/rails_generators/nifty_scaffold/templates/tests/testunit/controller.rb index e478b50..48e9e82 100644 --- a/rails_generators/nifty_scaffold/templates/tests/testunit/controller.rb +++ b/rails_generators/nifty_scaffold/templates/tests/testunit/controller.rb @@ -1,5 +1,5 @@ require 'test_helper' -class <%= plural_class_name %>ControllerTest < ActionController::TestCase +class <%= resource_path.camelize %>ControllerTest < ActionController::TestCase <%= controller_methods 'tests/testunit/actions' %> end diff --git a/rails_generators/nifty_scaffold/templates/tests/testunit/model.rb b/rails_generators/nifty_scaffold/templates/tests/testunit/model.rb index 09b835c..d965d0b 100644 --- a/rails_generators/nifty_scaffold/templates/tests/testunit/model.rb +++ b/rails_generators/nifty_scaffold/templates/tests/testunit/model.rb @@ -1,7 +1,7 @@ require 'test_helper' -class <%= class_name %>Test < ActiveSupport::TestCase +class <%= model_name %>Test < ActiveSupport::TestCase def test_should_be_valid - assert <%= class_name %>.new.valid? + assert <%= model_name %>.new.valid? end end diff --git a/rails_generators/nifty_scaffold/templates/views/erb/_form.html.erb b/rails_generators/nifty_scaffold/templates/views/erb/_form.html.erb index 344f277..f9870c9 100644 --- a/rails_generators/nifty_scaffold/templates/views/erb/_form.html.erb +++ b/rails_generators/nifty_scaffold/templates/views/erb/_form.html.erb @@ -1,4 +1,4 @@ -<%% form_for @<%= singular_name %> do |f| %> +<%% form_for <%= item_path :instance_variable => true, :action => :update %> do |f| %> <%%= f.error_messages %> <%- for attribute in attributes -%>

diff --git a/rails_generators/nifty_scaffold/templates/views/erb/edit.html.erb b/rails_generators/nifty_scaffold/templates/views/erb/edit.html.erb index e0e311d..1e3c332 100644 --- a/rails_generators/nifty_scaffold/templates/views/erb/edit.html.erb +++ b/rails_generators/nifty_scaffold/templates/views/erb/edit.html.erb @@ -1,14 +1,14 @@ -<%% title "Edit <%= singular_name.titleize %>" %> +<%% title "Edit <%= singular_model_name.titleize %>" %> <%= render_form %> <%- if actions? :show, :index -%>

<%- if action? :show -%> - <%%= link_to "Show", @<%= singular_name %> %> | + <%%= link_to "Show", <%= item_path :instance_variable => true %> %> | <%- end -%> <%- if action? :index -%> - <%%= link_to "View All", <%= plural_name %>_path %> + <%%= link_to "View All", <%= items_path %> %> <%- end -%>

<%- end -%> diff --git a/rails_generators/nifty_scaffold/templates/views/erb/index.html.erb b/rails_generators/nifty_scaffold/templates/views/erb/index.html.erb index 8850d16..1b923d3 100644 --- a/rails_generators/nifty_scaffold/templates/views/erb/index.html.erb +++ b/rails_generators/nifty_scaffold/templates/views/erb/index.html.erb @@ -1,4 +1,4 @@ -<%% title "<%= plural_name.titleize %>" %> +<%% title "<%= plural_model_name.titleize %>" %> @@ -6,24 +6,24 @@ <%- end -%> - <%% for <%= singular_name %> in @<%= plural_name %> %> + <%% for <%= singular_model_name %> in @<%= plural_model_name %> %> <%- for attribute in attributes -%> - + <%- end -%> <%- if action? :show -%> - + <%- end -%> <%- if action? :edit -%> - + <%- end -%> <%- if action? :destroy -%> - + <%- end -%> <%% end %>
<%= attribute.column.human_name.titleize %>
<%%=h <%= singular_name %>.<%= attribute.name %> %><%%=h <%= singular_model_name %>.<%= attribute.name %> %><%%= link_to "Show", <%= singular_name %> %><%%= link_to "Show", <%= item_path %> %><%%= link_to "Edit", edit_<%= singular_name %>_path(<%= singular_name %>) %><%%= link_to "Edit", <%= item_path :action => :edit %> %><%%= link_to "Destroy", <%= singular_name %>, :confirm => 'Are you sure?', :method => :delete %><%%= link_to "Destroy", <%= item_path %>, :confirm => 'Are you sure?', :method => :delete %>
<%- if action? :new -%> -

<%%= link_to "New <%= singular_name.titleize %>", new_<%= singular_name %>_path %>

+

<%%= link_to "New <%= singular_model_name.titleize %>", <%= item_path :action => :new %> %>

<%- end -%> diff --git a/rails_generators/nifty_scaffold/templates/views/erb/new.html.erb b/rails_generators/nifty_scaffold/templates/views/erb/new.html.erb index 462caaf..d68972b 100644 --- a/rails_generators/nifty_scaffold/templates/views/erb/new.html.erb +++ b/rails_generators/nifty_scaffold/templates/views/erb/new.html.erb @@ -1,7 +1,7 @@ -<%% title "New <%= singular_name.titleize %>" %> +<%% title "New <%= singular_model_name.titleize %>" %> <%= render_form %> <%- if action? :index -%> -

<%%= link_to "Back to List", <%= plural_name %>_path %>

+

<%%= link_to "Back to List", <%= items_path %> %>

<%- end -%> diff --git a/rails_generators/nifty_scaffold/templates/views/erb/show.html.erb b/rails_generators/nifty_scaffold/templates/views/erb/show.html.erb index acfafd5..fc203ef 100644 --- a/rails_generators/nifty_scaffold/templates/views/erb/show.html.erb +++ b/rails_generators/nifty_scaffold/templates/views/erb/show.html.erb @@ -1,20 +1,20 @@ -<%% title "<%= singular_name.titleize %>" %> +<%% title "<%= singular_model_name.titleize %>" %> <%- for attribute in attributes -%>

<%= attribute.column.human_name.titleize %>: - <%%=h @<%= singular_name %>.<%= attribute.name %> %> + <%%=h @<%= singular_model_name %>.<%= attribute.name %> %>

<%- end -%>

<%- if action? :edit -%> - <%%= link_to "Edit", edit_<%= singular_name %>_path(@<%= singular_name %>) %> | + <%%= link_to "Edit", <%= item_path :action => :edit, :instance_variable => true %> %> | <%- end -%> <%- if action? :destroy -%> - <%%= link_to "Destroy", @<%= singular_name %>, :confirm => 'Are you sure?', :method => :delete %> | + <%%= link_to "Destroy", <%= item_path :instance_variable => true %>, :confirm => 'Are you sure?', :method => :delete %> | <%- end -%> <%- if action? :index -%> - <%%= link_to "View All", <%= plural_name %>_path %> + <%%= link_to "View All", <%= items_path %> %> <%- end -%>

diff --git a/rails_generators/nifty_scaffold/templates/views/haml/_form.html.haml b/rails_generators/nifty_scaffold/templates/views/haml/_form.html.haml index 1bca6dc..8726e9d 100644 --- a/rails_generators/nifty_scaffold/templates/views/haml/_form.html.haml +++ b/rails_generators/nifty_scaffold/templates/views/haml/_form.html.haml @@ -1,4 +1,4 @@ -- form_for @<%= singular_name %> do |f| +- form_for <%= item_path :instance_variable => true, :action => :update %> do |f| = f.error_messages <%- for attribute in attributes -%> %p diff --git a/rails_generators/nifty_scaffold/templates/views/haml/edit.html.haml b/rails_generators/nifty_scaffold/templates/views/haml/edit.html.haml index c7b13b6..42c6423 100644 --- a/rails_generators/nifty_scaffold/templates/views/haml/edit.html.haml +++ b/rails_generators/nifty_scaffold/templates/views/haml/edit.html.haml @@ -1,14 +1,14 @@ -- title "Edit <%= singular_name.titleize %>" +- title "Edit <%= singular_model_name.titleize %>" <%= render_form %> <%- if actions? :show, :index -%> %p <%- if action? :show -%> - = link_to "Show", <%= singular_name %>_path(@<%= singular_name %>) + = link_to "Show", <%= item_path :instance_variable => true %> | <%- end -%> <%- if action? :index -%> - = link_to "View All", <%= plural_name %>_path + = link_to "View All", <%= items_path %> <%- end -%> <%- end -%> diff --git a/rails_generators/nifty_scaffold/templates/views/haml/index.html.haml b/rails_generators/nifty_scaffold/templates/views/haml/index.html.haml index b6d6478..5fc71e7 100644 --- a/rails_generators/nifty_scaffold/templates/views/haml/index.html.haml +++ b/rails_generators/nifty_scaffold/templates/views/haml/index.html.haml @@ -1,25 +1,25 @@ -- title "<%= plural_name.titleize %>" +- title "<%= plural_model_name.titleize %>" %table %tr <%- for attribute in attributes -%> %th <%= attribute.column.human_name %> <%- end -%> - - for <%= singular_name %> in @<%= plural_name %> + - for <%= singular_model_name %> in @<%= plural_model_name %> %tr <%- for attribute in attributes -%> - %td= h <%= singular_name %>.<%= attribute.name %> + %td= h <%= singular_model_name %>.<%= attribute.name %> <%- end -%> <%- if action? :show -%> - %td= link_to 'Show', <%= singular_name %> + %td= link_to 'Show', <%= item_path %> <%- end -%> <%- if action? :edit -%> - %td= link_to 'Edit', edit_<%= singular_name %>_path(<%= singular_name %>) + %td= link_to 'Edit', <%= item_path :action => :edit %> <%- end -%> <%- if action? :destroy -%> - %td= link_to 'Destroy', <%= singular_name %>, :confirm => 'Are you sure?', :method => :delete + %td= link_to 'Destroy', <%= item_path %>, :confirm => 'Are you sure?', :method => :delete <%- end -%> <%- if actions? :new -%> -%p= link_to "New <%= singular_name.titleize %>", new_<%= singular_name %>_path +%p= link_to "New <%= singular_model_name.titleize %>", <%= item_path :action => :new %> <%- end -%> diff --git a/rails_generators/nifty_scaffold/templates/views/haml/new.html.haml b/rails_generators/nifty_scaffold/templates/views/haml/new.html.haml index 2eabd32..c071f4e 100644 --- a/rails_generators/nifty_scaffold/templates/views/haml/new.html.haml +++ b/rails_generators/nifty_scaffold/templates/views/haml/new.html.haml @@ -1,7 +1,7 @@ -- title "New <%= singular_name.titleize %>" +- title "New <%= singular_model_name.titleize %>" <%= render_form %> <%- if action? :index -%> -%p= link_to "Back to List", <%= plural_name %>_path +%p= link_to "Back to List", <%= items_path %> <%- end -%> diff --git a/rails_generators/nifty_scaffold/templates/views/haml/show.html.haml b/rails_generators/nifty_scaffold/templates/views/haml/show.html.haml index a67dd27..f4b7fb3 100644 --- a/rails_generators/nifty_scaffold/templates/views/haml/show.html.haml +++ b/rails_generators/nifty_scaffold/templates/views/haml/show.html.haml @@ -1,20 +1,20 @@ -- title "<%= singular_name.titleize %>" +- title "<%= singular_model_name.titleize %>" <%- for attribute in attributes -%> %p %strong <%= attribute.column.human_name.titleize %>: - =h @<%= singular_name %>.<%= attribute.name %> + =h @<%= singular_model_name %>.<%= attribute.name %> <%- end -%> %p <%- if action? :edit -%> - = link_to "Edit", edit_<%= singular_name %>_path(@<%= singular_name %>) + = link_to "Edit", <%= item_path :action => :edit, :instance_variable => true %> | <%- end -%> <%- if action? :destroy -%> - = link_to "Destroy", @<%= singular_name %>, :confirm => 'Are you sure?', :method => :delete + = link_to "Destroy", <%= item_path :instance_variable => true %>, :confirm => 'Are you sure?', :method => :delete | <%- end -%> <%- if action? :index -%> - = link_to "View All", <%= plural_name %>_path + = link_to "View All", <%= items_path %> <%- end -%> diff --git a/test/test_nifty_scaffold_generator.rb b/test/test_nifty_scaffold_generator.rb index e7071f8..fc296dc 100644 --- a/test/test_nifty_scaffold_generator.rb +++ b/test/test_nifty_scaffold_generator.rb @@ -140,7 +140,7 @@ class TestNiftyScaffoldGenerator < Test::Unit::TestCase assert_match "@line_item = LineItem.new(params[:line_item])", body assert_match "if @line_item.save", body assert_match "flash[:notice] = \"Successfully created line item.\"", body - assert_match "redirect_to root_url", body + assert_match "redirect_to root_path", body assert_match "render :action => 'new'", body end end @@ -165,7 +165,7 @@ class TestNiftyScaffoldGenerator < Test::Unit::TestCase assert_match "def update", body assert_match "if @line_item.update_attributes(params[:line_item])", body assert_match "flash[:notice] = \"Successfully updated line item.\"", body - assert_match "redirect_to root_url", body + assert_match "redirect_to root_path", body assert_match "render :action => 'edit'", body end end @@ -257,7 +257,7 @@ class TestNiftyScaffoldGenerator < Test::Unit::TestCase assert_generated_file "app/controllers/line_items_controller.rb" do |body| assert_match "def create", body assert_match "def update", body - assert_match "root_url", body + assert_match "root_path", body end end end @@ -520,6 +520,35 @@ class TestNiftyScaffoldGenerator < Test::Unit::TestCase end end end + + context "generator with namespaced resource" do + rails_generator :nifty_scaffold, "Admin::User", "name:string" + + should "generate file 'app/models/user.rb'" do + assert_generated_file "app/models/user.rb" do |body| + assert_match "class User <", body + end + end + should_generate_file "app/controllers/admin/users_controller.rb" + should_generate_file "app/helpers/admin/users_helper.rb" + should_generate_file "app/views/admin/users/index.html.erb" + should_generate_file "app/views/admin/users/show.html.erb" + should_generate_file "app/views/admin/users/new.html.erb" + should_generate_file "app/views/admin/users/edit.html.erb" + should_generate_file "test/fixtures/users.yml" + end + + context "generator with namespaced model" do + rails_generator :nifty_scaffold, "Admin::User", "name:string", :namespace_model => true + + should_generate_file "test/fixtures/admin_users.yml" + should "generate file 'app/models/admin/user.rb'" do + assert_generated_file "app/models/admin/user.rb" do |body| + assert_match "class Admin::User <", body + assert_match "set_table_name :admin_user", body + end + end + end end end