Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

first commit

  • Loading branch information...
commit 907ec61abb111302864178db8dd4082afd4e5fad 0 parents
@sleistner authored
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2008 [name of plugin creator]
+
+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.
15 README
@@ -0,0 +1,15 @@
+ActsAsResourceController
+========================
+
+Introduction goes here.
+
+
+Example
+=======
+
+class MyModelController < ApplicationController
+
+ acts_as_resource_controller
+
+end
+
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 acts_as_resource_controller plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the acts_as_resource_controller plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'ActsAsResourceController'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
5 init.rb
@@ -0,0 +1,5 @@
+require 'acts_as_resource_controller'
+
+if defined?(ActionController) and defined?(ActionController::Base)
+ ActionController::Base.send :include, ActsAsResourceController
+end
1  install.rb
@@ -0,0 +1 @@
+# Install hook code here
172 lib/acts_as_resource_controller.rb
@@ -0,0 +1,172 @@
+module ActsAsResourceController
+
+ def self.included(base) # :nodoc:
+ base.extend ClassMethods
+ end
+
+ module ClassMethods
+
+ def acts_as_resource_controller options = {}
+ include ResourceMethods
+
+ if block_given?
+ options.instance_eval do
+ def method_missing(method, *args)
+ self[method.to_sym] = args.first
+ end
+ end
+ yield(options)
+ end
+
+ rescue_from ActiveRecord::RecordInvalid, :with => :render_invalid_record
+ rescue_from ActiveRecord::RecordNotFound, :with => :render_record_not_found
+
+ %w(belongs_to after_create after_update format_options filter_update_params).each do |o|
+ define_method(o) { options[o.to_sym] }
+ end
+ define_method(:order) { options[:order] || 'id ASC' }
+ define_method(:conditions) { (options[:conditions] ||= []).map { |v| v.is_a?(Proc) ? v.call(self) : v } }
+ define_method(:joins) { options[:joins].is_a?(Symbol) ? ",#{options[:joins]}" : options[:joins] }
+ end
+
+ end# ClassMethods
+
+ module ResourceMethods
+
+ def index
+ self.instances = with_scope do
+ options = returning(:order => order) do |o|
+ o[:conditions] = ["#{belongs_to_id} = ?", params[belongs_to_id]] if belongs_to?
+ o[:joins] = joins unless joins.nil?
+ end
+ model.find :all, options
+ end
+ render_formats instances, true
+ end
+
+ def show
+ self.instance = with_scope { model.find params[:id] }
+ render_formats instance
+ end
+
+ def create
+ self.instance = model.new params[model_name]
+ self.instance.send("#{belongs_to_id}=", params[belongs_to_id]) if belongs_to?
+ self.instance.save!
+ headers['Location'] = send "#{model_name}_url", instance
+ send(after_create) unless after_create.nil?
+ head :created
+ end
+
+ def edit
+ self.instance = model.find params[:id]
+ end
+
+ def update
+ self.instance = model.find params[:id]
+ instance.update_attributes!(filter_update_params.nil? ? params[model_name] : send(filter_update_params, params[model_name]))
+ send(after_update) unless after_update.nil?
+ render_formats instance
+ end
+
+ def destroy
+ model.destroy with_scope { model.find params[:id] }
+ head :ok
+ end
+
+ private
+
+ def with_scope
+ opts = { :conditions => conditions }
+ opts[:joins] = joins unless joins.nil?
+ model.send(:with_scope, :find => opts) { yield }
+ end
+
+ def belongs_to?
+ !belongs_to.nil? && !params[belongs_to_id].nil?
+ end
+
+ def belongs_to_id
+ "#{belongs_to}_id"
+ end
+
+ def model_name
+ @model_name ||= controller_name.singularize
+ end
+
+ def model
+ @model ||= model_name.camelize.constantize
+ end
+
+ def instance=(value)
+ instance_variable_set "@#{model_name}", value
+ end
+
+ def instance
+ instance_variable_get "@#{model_name}"
+ end
+
+ def instances=(objects)
+ instance_variable_set "@#{model_name.pluralize}", objects
+ end
+
+ def instances
+ instance_variable_get "@#{model_name.pluralize}"
+ end
+
+ def extend_to_format obj
+ obj.instance_variable_set "@format_options", format_options
+
+ # TODO: refactore the lines below to an simple :format for loop
+ obj.class.send(:alias_method, :to_json_orig, :to_json)
+ def obj.to_json options = {}
+ to_json_orig options.merge(@format_options)
+ end
+ obj.class.send(:alias_method, :to_xml_orig, :to_xml)
+ def obj.to_xml options = {}
+ to_xml_orig options.merge(@format_options)
+ end
+ end
+
+ def render_formats data, list = false
+
+ unless format_options.nil?
+ if data.is_a? Array
+ data.each {|obj| extend_to_format(obj) }
+ else
+ extend_to_format(data)
+ end
+ end
+
+ respond_to do |format|
+ format.html
+ format.json { render :json => data }
+ format.js { render :json => data, :content_type => 'application/json' }
+ format.xml { render :xml => data }
+ end
+ end
+
+ def render_invalid_record exception
+ record = exception.record
+ respond_to do |format|
+ format.json { render :json => errors_as_hash(record.errors), :status => :unprocessable_entity }
+ format.js { render :json => errors_as_hash(record.errors), :status => :unprocessable_entity, :content_type => 'application/json' }
+ format.xml { render :xml => record.errors.full_messages, :status => :unprocessable_entity }
+ end
+ end
+
+ def errors_as_hash errors
+ returning({}) {|h| errors.each {|attr, msg| h[attr] = msg.gsub(/%\{/, '#{') }}
+ end
+
+ def render_record_not_found exception
+ respond_to do |format|
+ format.json { render :json => exception.to_s, :status => :unprocessable_entity }
+ format.js { render :json => exception.to_s, :status => :unprocessable_entity, :content_type => 'application/json' }
+ format.xml { render :xml => "<error>#{exception}</error>", :status => :unprocessable_entity }
+ end
+ end
+
+ end# ResourceMethods
+
+end# ActsAsResourceController
4 tasks/acts_as_resource_controller_tasks.rake
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :acts_as_resource_controller do
+# # Task goes here
+# end
8 test/acts_as_resource_controller_test.rb
@@ -0,0 +1,8 @@
+require 'test/unit'
+
+class ActsAsResourceControllerTest < Test::Unit::TestCase
+ # Replace this with your real tests.
+ def test_this_plugin
+ flunk
+ end
+end
1  uninstall.rb
@@ -0,0 +1 @@
+# Uninstall hook code here
Please sign in to comment.
Something went wrong with that request. Please try again.