Permalink
Browse files

really adding the generator

git-svn-id: http://jnewland.com/svn/public/ruby/rails/plugins/resource_this@37 9b6b69f6-dd27-0410-8144-a0f3c56a22ea
  • Loading branch information...
1 parent 877cd00 commit 6d59c870a669e3ddf7a890e98ccc3ce59761e368 jnewland committed Sep 17, 2007
View
@@ -0,0 +1 @@
+* Added resource_this generator
@@ -0,0 +1,24 @@
+Description:
+ Stubs out a new resource including an empty model and controller suitable
+ for a restful, resource-oriented application. Pass the singular model name,
+ either CamelCased or under_scored, as the first argument, and an optional
+ list of attribute pairs.
+
+ Attribute pairs are column_name:sql_type arguments specifying the
+ model's attributes. Timestamps are added by default, so you don't have to
+ specify them by hand as 'created_at:datetime updated_at:datetime'.
+
+ You don't have to think up every attribute up front, but it helps to
+ sketch out a few so you can start working with the resource immediately.
+
+ This creates a model, controller, tests and fixtures for both, and the
+ corresponding map.resources declaration in config/routes.rb
+
+ This generates a DRY resource controller using resource_this:
+
+ http://jnewland.com/svn/public/ruby/rails/plugins/resource_this/README
+
+Examples:
+ `./script/generate resource_this post` # no attributes
+ `./script/generate resource_this post title:string body:text published:boolean`
+ `./script/generate resource_this purchase order_id:integer amount:decimal`
@@ -0,0 +1,72 @@
+class ResourceThisGenerator < Rails::Generator::NamedBase
+ default_options :skip_migration => false
+
+ attr_reader :controller_name,
+ :controller_class_path,
+ :controller_file_path,
+ :controller_class_nesting,
+ :controller_class_nesting_depth,
+ :controller_class_name,
+ :controller_singular_name,
+ :controller_plural_name
+ alias_method :controller_file_name, :controller_singular_name
+ alias_method :controller_table_name, :controller_plural_name
+
+ def initialize(runtime_args, runtime_options = {})
+ super
+
+ @controller_name = @name.pluralize
+
+ base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
+ @controller_class_name_without_nesting, @controller_singular_name, @controller_plural_name = inflect_names(base_name)
+
+ if @controller_class_nesting.empty?
+ @controller_class_name = @controller_class_name_without_nesting
+ else
+ @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
+ end
+ end
+
+ def manifest
+ record do |m|
+ # Check for class naming collisions.
+ m.class_collisions(controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}Helper")
+ m.class_collisions(class_path, "#{class_name}")
+
+ # Controller, helper, views, and test directories.
+ m.directory(File.join('app/models', class_path))
+ m.directory(File.join('app/controllers', controller_class_path))
+ m.directory(File.join('app/helpers', controller_class_path))
+ m.directory(File.join('app/views', controller_class_path, controller_file_name))
+ m.directory(File.join('test/functional', controller_class_path))
+ m.directory(File.join('test/unit', class_path))
+
+ m.dependency 'model', [singular_name] + @args, :collision => :skip
+
+ m.template(
+ 'controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
+ )
+
+ m.template('functional_test.rb', File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb"))
+ m.template('helper.rb', File.join('app/helpers', controller_class_path, "#{controller_file_name}_helper.rb"))
+
+ m.route_resources controller_file_name
+ end
+ end
+
+ protected
+ def banner
+ "Usage: #{$0} resource_this ModelName [field:type, field:type]"
+ end
+
+ def add_options!(opt)
+ opt.separator ''
+ opt.separator 'Options:'
+ opt.on("--skip-migration",
+ "Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
+ end
+
+ def model_name
+ class_name.demodulize
+ end
+end
@@ -0,0 +1,3 @@
+class <%= controller_class_name %>Controller < ApplicationController
+ resource_this
+end
@@ -0,0 +1,59 @@
+require File.dirname(__FILE__) + '<%= '/..' * controller_class_nesting_depth %>/../test_helper'
+require '<%= controller_file_path %>_controller'
+
+# Re-raise errors caught by the controller.
+class <%= controller_class_name %>Controller; def rescue_action(e) raise e end; end
+
+class <%= controller_class_name %>ControllerTest < Test::Unit::TestCase
+ fixtures :<%= table_name %>
+
+ def setup
+ @controller = <%= controller_class_name %>Controller.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+ end
+
+ def test_should_get_index
+ get :index
+ assert_response :success
+ assert assigns(:<%= table_name %>)
+ end
+
+ def test_should_get_new
+ @request.accept = 'application/xml'
+ get :new
+ assert_response :success
+ end
+
+ def test_should_create_<%= file_name %>
+ assert_difference('<%= class_name %>.count') do
+ post :create, :<%= file_name %> => { }
+ end
+
+ assert_redirected_to <%= file_name %>_path(assigns(:<%= file_name %>))
+ end
+
+ def test_should_show_<%= file_name %>
+ @request.accept = 'application/xml'
+ get :show, :id => 1
+ assert_response :success
+ end
+
+ def test_should_get_edit
+ #TODO: replace this with a real test once views are in place
+ assert true
+ end
+
+ def test_should_update_<%= file_name %>
+ put :update, :id => 1, :<%= file_name %> => { }
+ assert_redirected_to <%= file_name %>_path(assigns(:<%= file_name %>))
+ end
+
+ def test_should_destroy_<%= file_name %>
+ assert_difference('<%= class_name %>.count', -1) do
+ delete :destroy, :id => 1
+ end
+
+ assert_redirected_to <%= table_name %>_path
+ end
+end
@@ -0,0 +1,2 @@
+module <%= controller_class_name %>Helper
+end

0 comments on commit 6d59c87

Please sign in to comment.