Permalink
Browse files

Initial commit. Extracted from InheritedResources.

  • Loading branch information...
0 parents commit c4bfa33a608355e2976836d4ee3eb18b50df6fe2 @georgeguimaraes georgeguimaraes committed Mar 12, 2010
@@ -0,0 +1,8 @@
+**/*/log/*
+**/*/tmp/*
+*~
+coverage/*
+rdoc/*
+pkg
+log
+.DS_Store
@@ -0,0 +1,3 @@
+# Version 0.1
+
+* First release. Extracted from InheritedResources v1.0 (Rails 2.3.x)
@@ -0,0 +1,20 @@
+Copyright (c) 2009 George Guimarães and José Valim http://blog.plataformatec.com.br
+
+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.
@@ -0,0 +1,5 @@
+== Responders Backport for Rails 2.3.x
+
+
+Copyright (c) 2009 George Guimarães and José Valim http://blog.plataformatec.com.br
+See the attached MIT License.
@@ -0,0 +1,41 @@
+# encoding: UTF-8
+
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+require File.join(File.dirname(__FILE__), 'lib', 'responders_backport', 'version')
+
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |s|
+ s.name = "responders_backport"
+ s.version = RespondersBackport::VERSION
+ s.rubyforge_project = "responders_backport"
+ s.summary = "Bringing Rails 3 Responders to your obsolete Rails 2.3.x apps =)"
+ s.email = "george@plataformatec.com.br"
+ s.homepage = "http://github.com/plataformatec/responders_backport"
+ s.description = "Bringing Rails 3 Responders to your obsolete Rails 2.3.x apps =)"
+ s.authors = ['George Guimarães', 'José Valim']
+ s.files = FileList["[A-Z]*", "init.rb", "{lib}/**/*"]
+ end
+
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install jeweler"
+end
+
+desc 'Run tests for RespondersBackport.'
+Rake::TestTask.new(:test) do |t|
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for RespondersBackport.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'RespondersBackport'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README.rdoc')
+ rdoc.rdoc_files.include('MIT-LICENSE')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1 @@
+require 'responders_backport'
@@ -0,0 +1,6 @@
+unless defined?(ActionController::Responder)
+ require 'responders_backport/responder'
+ require 'responders_backport/respond_to'
+end
+
+require 'responders_backport/version'
@@ -0,0 +1,154 @@
+module ActionController #:nodoc:
+ class Base #:nodoc:
+ attr_accessor :formats
+
+ class_inheritable_accessor :mimes_for_respond_to, :responder, :instance_writer => false
+
+ self.responder = ActionController::Responder
+ self.mimes_for_respond_to = ActiveSupport::OrderedHash.new
+
+ if defined?(ApplicationController)
+ ApplicationController.responder ||= ActionController::Responder
+ ApplicationController.mimes_for_respond_to ||= ActiveSupport::OrderedHash.new
+ end
+
+ # Defines mimes that are rendered by default when invoking respond_with.
+ #
+ # Examples:
+ #
+ # respond_to :html, :xml, :json
+ #
+ # All actions on your controller will respond to :html, :xml and :json.
+ #
+ # But if you want to specify it based on your actions, you can use only and
+ # except:
+ #
+ # respond_to :html
+ # respond_to :xml, :json, :except => [ :edit ]
+ #
+ # The definition above explicits that all actions respond to :html. And all
+ # actions except :edit respond to :xml and :json.
+ #
+ # You can specify also only parameters:
+ #
+ # respond_to :rjs, :only => :create
+ #
+ def self.respond_to(*mimes)
+ options = mimes.extract_options!
+ clear_respond_to unless mimes_for_respond_to
+
+ only_actions = Array(options.delete(:only))
+ except_actions = Array(options.delete(:except))
+
+ mimes.each do |mime|
+ mime = mime.to_sym
+ mimes_for_respond_to[mime] = {}
+ mimes_for_respond_to[mime][:only] = only_actions unless only_actions.empty?
+ mimes_for_respond_to[mime][:except] = except_actions unless except_actions.empty?
+ end
+ end
+
+ # Clear all mimes in respond_to.
+ def self.clear_respond_to
+ write_inheritable_attribute(:mimes_for_respond_to, ActiveSupport::OrderedHash.new)
+ end
+
+ def respond_to(*mimes, &block)
+ raise ArgumentError, "respond_to takes either types or a block, never both" if mimes.any? && block_given?
+ if response = retrieve_response_from_mimes(mimes, &block)
+ response.call
+ end
+ end
+
+ def respond_with(*resources, &block)
+ if response = retrieve_response_from_mimes([], &block)
+ options = resources.extract_options!
+ options.merge!(:default_response => response)
+ (options.delete(:responder) || responder).call(self, resources, options)
+ end
+ end
+
+ protected
+
+ # Collect mimes declared in the class method respond_to valid for the
+ # current action.
+ #
+ def collect_mimes_from_class_level #:nodoc:
+ action = action_name.to_sym
+
+ mimes_for_respond_to.keys.select do |mime|
+ config = mimes_for_respond_to[mime]
+
+ if config[:except]
+ !config[:except].include?(action)
+ elsif config[:only]
+ config[:only].include?(action)
+ else
+ true
+ end
+ end
+ end
+
+ # Collects mimes and return the response for the negotiated format. Returns
+ # nil if :not_acceptable was sent to the client.
+ #
+ def retrieve_response_from_mimes(mimes, &block)
+ responder = ActionController::MimeResponds::Responder.new(self)
+ mimes = collect_mimes_from_class_level if mimes.empty?
+ mimes.each { |mime| responder.send(mime) }
+ block.call(responder) if block_given?
+
+ if format = responder.negotiate_mime
+ self.response.template.template_format = format.to_sym
+ self.response.content_type = format.to_s
+ self.formats = [ format.to_sym ]
+ responder.response_for(format) || proc { default_render }
+ else
+ head :not_acceptable
+ nil
+ end
+ end
+ end
+
+ module MimeResponds
+ class Responder #:nodoc:
+ attr_reader :order
+
+ def any(*args, &block)
+ if args.any?
+ args.each { |type| send(type, &block) }
+ else
+ custom(Mime::ALL, &block)
+ end
+ end
+ alias :all :any
+
+ def custom(mime_type, &block)
+ mime_type = mime_type.is_a?(Mime::Type) ? mime_type : Mime::Type.lookup(mime_type.to_s)
+ @order << mime_type
+ @responses[mime_type] ||= block
+ end
+
+ def response_for(mime)
+ @responses[mime] || @responses[Mime::ALL]
+ end
+
+ def negotiate_mime
+ @mime_type_priority.each do |priority|
+ if priority == Mime::ALL
+ return @order.first
+ elsif @order.include?(priority)
+ return priority
+ end
+ end
+
+ if @order.include?(Mime::ALL)
+ return Mime::SET.first if @mime_type_priority.first == Mime::ALL
+ return @mime_type_priority.first
+ end
+
+ nil
+ end
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit c4bfa33

Please sign in to comment.