Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial commit of resource_this

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