From 6e0ac748e41e8de8111095f7188c75f673779f93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 8 Aug 2009 11:07:07 +0200 Subject: Renamed ActionController::Renderer to ActionController::Responder and ActionController::MimeResponds::Responder to ActionController::MimeResponds::Collector. --- actionpack/lib/action_controller.rb | 2 +- .../lib/action_controller/metal/mime_responds.rb | 26 +-- actionpack/lib/action_controller/metal/renderer.rb | 181 --------------------- .../lib/action_controller/metal/responder.rb | 181 +++++++++++++++++++++ actionpack/test/controller/mime_responds_test.rb | 10 +- 5 files changed, 200 insertions(+), 200 deletions(-) delete mode 100644 actionpack/lib/action_controller/metal/renderer.rb create mode 100644 actionpack/lib/action_controller/metal/responder.rb (limited to 'actionpack') diff --git a/actionpack/lib/action_controller.rb b/actionpack/lib/action_controller.rb index 8343a87936..37ff618edd 100644 --- a/actionpack/lib/action_controller.rb +++ b/actionpack/lib/action_controller.rb @@ -7,10 +7,10 @@ module ActionController autoload :RackConvenience, "action_controller/metal/rack_convenience" autoload :Rails2Compatibility, "action_controller/metal/compatibility" autoload :Redirector, "action_controller/metal/redirector" - autoload :Renderer, "action_controller/metal/renderer" autoload :RenderingController, "action_controller/metal/rendering_controller" autoload :RenderOptions, "action_controller/metal/render_options" autoload :Rescue, "action_controller/metal/rescuable" + autoload :Responder, "action_controller/metal/responder" autoload :Testing, "action_controller/metal/testing" autoload :UrlFor, "action_controller/metal/url_for" autoload :Session, "action_controller/metal/session" diff --git a/actionpack/lib/action_controller/metal/mime_responds.rb b/actionpack/lib/action_controller/metal/mime_responds.rb index d823dd424a..c8d042acb5 100644 --- a/actionpack/lib/action_controller/metal/mime_responds.rb +++ b/actionpack/lib/action_controller/metal/mime_responds.rb @@ -179,15 +179,15 @@ module ActionController #:nodoc: def respond_to(*mimes, &block) raise ArgumentError, "respond_to takes either types or a block, never both" if mimes.any? && block_given? - responder = Responder.new + collector = Collector.new mimes = collect_mimes_from_class_level if mimes.empty? - mimes.each { |mime| responder.send(mime) } - block.call(responder) if block_given? + mimes.each { |mime| collector.send(mime) } + block.call(collector) if block_given? - if format = request.negotiate_mime(responder.order) + if format = request.negotiate_mime(collector.order) self.formats = [format.to_sym] - if response = responder.response_for(format) + if response = collector.response_for(format) response.call else default_render @@ -197,10 +197,10 @@ module ActionController #:nodoc: end end - # respond_with wraps a resource around a renderer for default representation. + # respond_with wraps a resource around a responder for default representation. # First it invokes respond_to, if a response cannot be found (ie. no block # for the request was given and template was not available), it instantiates - # an ActionController::Renderer with the controller and resource. + # an ActionController::Responder with the controller and resource. # # ==== Example # @@ -221,19 +221,19 @@ module ActionController #:nodoc: # end # end # - # All options given to respond_with are sent to the underlying renderer, - # except for the option :renderer itself. Since the renderer interface + # All options given to respond_with are sent to the underlying responder, + # except for the option :responder itself. Since the responder interface # is quite simple (it just needs to respond to call), you can even give # a proc to it. # def respond_with(resource, options={}, &block) respond_to(&block) rescue ActionView::MissingTemplate - (options.delete(:renderer) || renderer).call(self, resource, options) + (options.delete(:responder) || responder).call(self, resource, options) end - def renderer - ActionController::Renderer + def responder + ActionController::Responder end protected @@ -257,7 +257,7 @@ module ActionController #:nodoc: end end - class Responder #:nodoc: + class Collector #:nodoc: attr_accessor :order def initialize diff --git a/actionpack/lib/action_controller/metal/renderer.rb b/actionpack/lib/action_controller/metal/renderer.rb deleted file mode 100644 index 39ab2407aa..0000000000 --- a/actionpack/lib/action_controller/metal/renderer.rb +++ /dev/null @@ -1,181 +0,0 @@ -module ActionController #:nodoc: - # Renderer is responsible to expose a resource for different mime requests, - # usually depending on the HTTP verb. The renderer is triggered when - # respond_with is called. The simplest case to study is a GET request: - # - # class PeopleController < ApplicationController - # respond_to :html, :xml, :json - # - # def index - # @people = Person.find(:all) - # respond_with(@people) - # end - # end - # - # When a request comes, for example with format :xml, three steps happen: - # - # 1) respond_with searches for a template at people/index.xml; - # - # 2) if the template is not available, it will create a renderer, passing - # the controller and the resource and invoke :to_xml on it; - # - # 3) if the renderer does not respond_to :to_xml, call to_format on it. - # - # === Builtin HTTP verb semantics - # - # Rails default renderer holds semantics for each HTTP verb. Depending on the - # content type, verb and the resource status, it will behave differently. - # - # Using Rails default renderer, a POST request for creating an object could - # be written as: - # - # def create - # @user = User.new(params[:user]) - # flash[:notice] = 'User was successfully created.' if @user.save - # respond_with(@user) - # end - # - # Which is exactly the same as: - # - # def create - # @user = User.new(params[:user]) - # - # respond_to do |format| - # if @user.save - # flash[:notice] = 'User was successfully created.' - # format.html { redirect_to(@user) } - # format.xml { render :xml => @user, :status => :created, :location => @user } - # else - # format.html { render :action => "new" } - # format.xml { render :xml => @user.errors, :status => :unprocessable_entity } - # end - # end - # end - # - # The same happens for PUT and DELETE requests. - # - # === Nested resources - # - # You can given nested resource as you do in form_for and polymorphic_url. - # Consider the project has many tasks example. The create action for - # TasksController would be like: - # - # def create - # @project = Project.find(params[:project_id]) - # @task = @project.comments.build(params[:task]) - # flash[:notice] = 'Task was successfully created.' if @task.save - # respond_with([@project, @task]) - # end - # - # Giving an array of resources, you ensure that the renderer will redirect to - # project_task_url instead of task_url. - # - # Namespaced and singleton resources requires a symbol to be given, as in - # polymorphic urls. If a project has one manager which has many tasks, it - # should be invoked as: - # - # respond_with([@project, :manager, @task]) - # - # Check polymorphic_url documentation for more examples. - # - class Renderer - attr_reader :controller, :request, :format, :resource, :resource_location, :options - - def initialize(controller, resource, options={}) - @controller = controller - @request = controller.request - @format = controller.formats.first - @resource = resource.is_a?(Array) ? resource.last : resource - @resource_location = options[:location] || resource - @options = options - end - - delegate :head, :render, :redirect_to, :to => :controller - delegate :get?, :post?, :put?, :delete?, :to => :request - - # Undefine :to_json since it's defined on Object - undef_method :to_json - - # Initializes a new renderer an invoke the proper format. If the format is - # not defined, call to_format. - # - def self.call(*args) - renderer = new(*args) - method = :"to_#{renderer.format}" - renderer.respond_to?(method) ? renderer.send(method) : renderer.to_format - end - - # HTML format does not render the resource, it always attempt to render a - # template. - # - def to_html - if get? - render - elsif has_errors? - render :action => default_action - else - redirect_to resource_location - end - end - - # All others formats try to render the resource given instead. For this - # purpose a helper called display as a shortcut to render a resource with - # the current format. - # - def to_format - return render unless resourceful? - - if get? - display resource - elsif has_errors? - display resource.errors, :status => :unprocessable_entity - elsif post? - display resource, :status => :created, :location => resource_location - else - head :ok - end - end - - protected - - # Checks whether the resource responds to the current format or not. - # - def resourceful? - resource.respond_to?(:"to_#{format}") - end - - # display is just a shortcut to render a resource with the current format. - # - # display @user, :status => :ok - # - # For xml request is equivalent to: - # - # render :xml => @user, :status => :ok - # - # Options sent by the user are also used: - # - # respond_with(@user, :status => :created) - # display(@user, :status => :ok) - # - # Results in: - # - # render :xml => @user, :status => :created - # - def display(resource, given_options={}) - render given_options.merge!(options).merge!(format => resource) - end - - # Check if the resource has errors or not. - # - def has_errors? - resource.respond_to?(:errors) && !resource.errors.empty? - end - - # By default, render the :edit action for html requests with failure, unless - # the verb is post. - # - def default_action - request.post? ? :new : :edit - end - end -end diff --git a/actionpack/lib/action_controller/metal/responder.rb b/actionpack/lib/action_controller/metal/responder.rb new file mode 100644 index 0000000000..9ed99ca623 --- /dev/null +++ b/actionpack/lib/action_controller/metal/responder.rb @@ -0,0 +1,181 @@ +module ActionController #:nodoc: + # Responder is responsible to expose a resource for different mime requests, + # usually depending on the HTTP verb. The responder is triggered when + # respond_with is called. The simplest case to study is a GET request: + # + # class PeopleController < ApplicationController + # respond_to :html, :xml, :json + # + # def index + # @people = Person.find(:all) + # respond_with(@people) + # end + # end + # + # When a request comes, for example with format :xml, three steps happen: + # + # 1) respond_with searches for a template at people/index.xml; + # + # 2) if the template is not available, it will create a responder, passing + # the controller and the resource and invoke :to_xml on it; + # + # 3) if the responder does not respond_to :to_xml, call to_format on it. + # + # === Builtin HTTP verb semantics + # + # Rails default responder holds semantics for each HTTP verb. Depending on the + # content type, verb and the resource status, it will behave differently. + # + # Using Rails default responder, a POST request for creating an object could + # be written as: + # + # def create + # @user = User.new(params[:user]) + # flash[:notice] = 'User was successfully created.' if @user.save + # respond_with(@user) + # end + # + # Which is exactly the same as: + # + # def create + # @user = User.new(params[:user]) + # + # respond_to do |format| + # if @user.save + # flash[:notice] = 'User was successfully created.' + # format.html { redirect_to(@user) } + # format.xml { render :xml => @user, :status => :created, :location => @user } + # else + # format.html { render :action => "new" } + # format.xml { render :xml => @user.errors, :status => :unprocessable_entity } + # end + # end + # end + # + # The same happens for PUT and DELETE requests. + # + # === Nested resources + # + # You can given nested resource as you do in form_for and polymorphic_url. + # Consider the project has many tasks example. The create action for + # TasksController would be like: + # + # def create + # @project = Project.find(params[:project_id]) + # @task = @project.comments.build(params[:task]) + # flash[:notice] = 'Task was successfully created.' if @task.save + # respond_with([@project, @task]) + # end + # + # Giving an array of resources, you ensure that the responder will redirect to + # project_task_url instead of task_url. + # + # Namespaced and singleton resources requires a symbol to be given, as in + # polymorphic urls. If a project has one manager which has many tasks, it + # should be invoked as: + # + # respond_with([@project, :manager, @task]) + # + # Check polymorphic_url documentation for more examples. + # + class Responder + attr_reader :controller, :request, :format, :resource, :resource_location, :options + + def initialize(controller, resource, options={}) + @controller = controller + @request = controller.request + @format = controller.formats.first + @resource = resource.is_a?(Array) ? resource.last : resource + @resource_location = options[:location] || resource + @options = options + end + + delegate :head, :render, :redirect_to, :to => :controller + delegate :get?, :post?, :put?, :delete?, :to => :request + + # Undefine :to_json since it's defined on Object + undef_method :to_json + + # Initializes a new responder an invoke the proper format. If the format is + # not defined, call to_format. + # + def self.call(*args) + responder = new(*args) + method = :"to_#{responder.format}" + responder.respond_to?(method) ? responder.send(method) : responder.to_format + end + + # HTML format does not render the resource, it always attempt to render a + # template. + # + def to_html + if get? + render + elsif has_errors? + render :action => default_action + else + redirect_to resource_location + end + end + + # All others formats try to render the resource given instead. For this + # purpose a helper called display as a shortcut to render a resource with + # the current format. + # + def to_format + return render unless resourceful? + + if get? + display resource + elsif has_errors? + display resource.errors, :status => :unprocessable_entity + elsif post? + display resource, :status => :created, :location => resource_location + else + head :ok + end + end + + protected + + # Checks whether the resource responds to the current format or not. + # + def resourceful? + resource.respond_to?(:"to_#{format}") + end + + # display is just a shortcut to render a resource with the current format. + # + # display @user, :status => :ok + # + # For xml request is equivalent to: + # + # render :xml => @user, :status => :ok + # + # Options sent by the user are also used: + # + # respond_with(@user, :status => :created) + # display(@user, :status => :ok) + # + # Results in: + # + # render :xml => @user, :status => :created + # + def display(resource, given_options={}) + render given_options.merge!(options).merge!(format => resource) + end + + # Check if the resource has errors or not. + # + def has_errors? + resource.respond_to?(:errors) && !resource.errors.empty? + end + + # By default, render the :edit action for html requests with failure, unless + # the verb is post. + # + def default_action + request.post? ? :new : :edit + end + end +end diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index 00ad90ff68..8319b5c573 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -505,9 +505,9 @@ class RespondWithController < ActionController::Base respond_with(Customer.new("david", 13), :location => "http://test.host/", :status => :created) end - def using_resource_with_renderer - renderer = proc { |c, r, o| c.render :text => "Resource name is #{r.name}" } - respond_with(Customer.new("david", 13), :renderer => renderer) + def using_resource_with_responder + responder = proc { |c, r, o| c.render :text => "Resource name is #{r.name}" } + respond_with(Customer.new("david", 13), :responder => responder) end protected @@ -743,8 +743,8 @@ class RespondWithControllerTest < ActionController::TestCase assert_equal 201, @response.status end - def test_using_resource_with_renderer - get :using_resource_with_renderer + def test_using_resource_with_responder + get :using_resource_with_responder assert_equal "Resource name is david", @response.body end -- cgit v1.2.3