public
Description: Not scaffolding. Resourcing. Creates extremely customizable resource controllers with one line of code.
Clone URL: git://github.com/jnewland/resource_this.git
Search Repo:
really adding the generator

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

Comments

    No one has commented yet.