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:
initial commit of resource_this

git-svn-id: 
http://jnewland.com/svn/public/ruby/rails/plugins/resource_this@14 
9b6b69f6-dd27-0410-8144-a0f3c56a22ea
jnewland (author)
Sun Sep 16 10:38:32 -0700 2007
commit  e94b80346f96c88ebf944bdbb87969f99edea9c7
tree    9d28ce82fdec06d6ff211a22506b152826cbae24
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
0
@@ -0,0 +1,20 @@
0
+Copyright (c) 2004-2007 Jesse Newland
0
+
0
+Permission is hereby granted, free of charge, to any person obtaining
0
+a copy of this software and associated documentation files (the
0
+"Software"), to deal in the Software without restriction, including
0
+without limitation the rights to use, copy, modify, merge, publish,
0
+distribute, sublicense, and/or sell copies of the Software, and to
0
+permit persons to whom the Software is furnished to do so, subject to
0
+the following conditions:
0
+
0
+The above copyright notice and this permission notice shall be
0
+included in all copies or substantial portions of the Software.
0
+
0
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
0
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
0
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0
\ No newline at end of file
0
...
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
0
@@ -0,0 +1,6 @@
0
+resouce_this
0
+===========
0
+
0
+docs here
0
+
0
+Copyright (c) 2007 Jesse Newland, released under the MIT license
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
0
@@ -0,0 +1,22 @@
0
+require 'rake'
0
+require 'rake/testtask'
0
+require 'rake/rdoctask'
0
+
0
+desc 'Default: run unit tests.'
0
+task :default => :test
0
+
0
+desc 'Test the resource_this plugin.'
0
+Rake::TestTask.new(:test) do |t|
0
+ t.libs << 'lib'
0
+ t.pattern = 'test/**/*_test.rb'
0
+ t.verbose = true
0
+end
0
+
0
+desc 'Generate documentation for the resouce_this plugin.'
0
+Rake::RDocTask.new(:rdoc) do |rdoc|
0
+ rdoc.rdoc_dir = 'rdoc'
0
+ rdoc.title = 'resouce_this'
0
+ rdoc.options << '--line-numbers' << '--inline-source'
0
+ rdoc.rdoc_files.include('README')
0
+ rdoc.rdoc_files.include('lib/**/*.rb')
0
+end
...
 
0
...
1
2
0
@@ -0,0 +1 @@
0
+ActionController::Base.send :include, ResourceThis
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
0
@@ -0,0 +1,92 @@
0
+module ResourceThis # :nodoc:
0
+ def self.included(base)
0
+ base.extend(ClassMethods)
0
+ end
0
+
0
+ module ClassMethods
0
+ def resource_this(options = {})
0
+ options.assert_valid_keys(:class_name, :will_paginate, :sort_method)
0
+
0
+ singular_name = options[:class_name].downcase.singularize || controller_name.singularize
0
+ class_name = options[:class_name] || singular_name.camelize
0
+ plural_name = singular_name.pluralize
0
+ will_paginate_index = options[:class_name] || false
0
+
0
+ module_eval <<-"end_eval", __FILE__, __LINE__
0
+
0
+ def index
0
+ if will_paginate_index
0
+ instance_variable_set("@#{plural_name}", class_name.paginate(:page => params[:page]))
0
+ else
0
+ instance_variable_set("@#{plural_name}", class_name.find(:all))
0
+ end
0
+ #TODO: add sorting customizable by subclassed controllers
0
+ respond_to do |format|
0
+ format.html
0
+ format.xml { render :xml => instance_variable_get("@#{plural_name}") }
0
+ end
0
+ end
0
+
0
+ def show
0
+ instance_variable_set("@#{singular_name}", class_name.find(params[:id]))
0
+ respond_to do |format|
0
+ format.html
0
+ format.xml { render :xml => instance_variable_get("@#{singular_name}") }
0
+ end
0
+ end
0
+
0
+ def new
0
+ instance_variable_set("@#{singular_name}", class_name.new)
0
+ respond_to do |format|
0
+ format.html { render :action => :edit }
0
+ format.xml { render :xml => instance_variable_get("@#{singular_name}") }
0
+ end
0
+ end
0
+
0
+ def create
0
+ instance_variable_set("@#{singular_name}", class_name.create!(params_hash))
0
+ flash[:notice] = "#{class_name} was successfully created."
0
+ respond_to do |format|
0
+ format.html { redirect_to :action => :index }
0
+ format.xml { render :xml => instance_variable_get("@#{singular_name}"), :status => :created, :location => instance_variable_get("@#{singular_name}") }
0
+ end
0
+ rescue ActiveRecord::RecordInvalid
0
+ flash[:error] = instance_variable_get("@#{singular_name}").errors
0
+ respond_to do |format|
0
+ format.html { render :action => :new }
0
+ format.xml { render :xml => instance_variable_get("@#{singular_name}").errors, :status => :unprocessable_entity }
0
+ end
0
+ end
0
+
0
+ def edit
0
+ instance_variable_set("@#{singular_name}", class_name.find(params[:id]))
0
+ end
0
+
0
+ def update
0
+ instance_variable_set("@#{singular_name}", class_name.find(params[:id]))
0
+ eval("@#{singular_name}").update_attributes!(params_hash)
0
+ flash[:notice] = "#{class_name} was successfully updated."
0
+ respond_to do |format|
0
+ format.html { redirect_to(instance_variable_get("@#{singular_name}")) }
0
+ format.xml { head :ok }
0
+ end
0
+ rescue ActiveRecord::RecordInvalid
0
+ flash[:error] = instance_variable_get("@#{singular_name}").errors
0
+ respond_to do |format|
0
+ format.html { render :action => :edit }
0
+ format.xml { render :xml => instance_variable_get("@#{singular_name}").errors, :status => :unprocessable_entity }
0
+ end
0
+ end
0
+
0
+ def destroy
0
+ class_name.find(params[:id]).destroy
0
+ respond_to do |format|
0
+ format.html { redirect_to :action => :index }
0
+ format.xml { head :ok }
0
+ end
0
+ end
0
+
0
+ end_eval
0
+ end
0
+ end
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
0
@@ -0,0 +1,83 @@
0
+require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
0
+require 'action_controller/test_process'
0
+require 'active_record/fixtures'
0
+
0
+ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
0
+
0
+ActiveRecord::Schema.define(:version => 1) do
0
+ create_table :posts do |t|
0
+ t.column :title, :string
0
+ t.column :body, :text
0
+ t.column :created_at, :datetime
0
+ t.column :updated_at, :datetime
0
+ end
0
+end
0
+
0
+class Post < ActiveRecord::Base
0
+end
0
+
0
+class PostsController < ActionController::Base
0
+ resource_this
0
+end
0
+
0
+class ResourceThisTest < Test::Unit::TestCase
0
+ def setup
0
+ @controller = PostsController.new
0
+ @request = ActionController::TestRequest.new
0
+ @response = ActionController::TestResponse.new
0
+
0
+ @first = Post.create :title => "Welcome to the weblog", :body => "Such a lovely day"
0
+ end
0
+
0
+ def test_should_get_index
0
+ accept :xml
0
+ get :index
0
+ assert_response :success
0
+ assert assigns(:posts)
0
+ end
0
+
0
+ def test_should_get_new
0
+ accept :xml
0
+ get :new
0
+ assert_response :success
0
+ assert assigns(:post)
0
+ end
0
+
0
+ def test_should_create_post
0
+ accept :xml
0
+ assert_difference('Post.count') do
0
+ post :create, :post => { :title => "test", :body => "test" }
0
+ end
0
+ assert_response :success
0
+ assert assigns(:post)
0
+ end
0
+
0
+ def test_should_show_post
0
+ accept :xml
0
+ get :show, :id => 1
0
+ assert_response :success
0
+ assert assigns(:post)
0
+ end
0
+
0
+ def test_should_get_edit
0
+ accept :xml
0
+ get :edit, :id => 1
0
+ assert_response :success
0
+ assert assigns(:post)
0
+ end
0
+
0
+ def test_should_update_post
0
+ accept :xml
0
+ put :update, :id => 1, :post => { :title => "test", :body => "test" }
0
+ assert_response :success
0
+ assert assigns(:post)
0
+ end
0
+
0
+ def test_should_destroy_post
0
+ accept :xml
0
+ assert_difference('Post.count', -1) do
0
+ delete :destroy, :id => 1
0
+ end
0
+ assert_response :success
0
+ end
0
+end

Comments

    No one has commented yet.