Skip to content

Commit

Permalink
Small changes to responder:
Browse files Browse the repository at this point in the history
* resources is always an array;
* Lazy retrieve request and formats;
* Alias api_location and navigation_location to resource_location, making easier to change its behavior without affecting each other and without a need to reimplement any of the behavior methods.
  • Loading branch information
josevalim committed Jun 26, 2010
1 parent 3782010 commit 7eb5766
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions actionpack/lib/action_controller/metal/responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ class Responder

def initialize(controller, resources, options={})
@controller = controller
@request = controller.request
@format = controller.formats.first
@resource = resources.is_a?(Array) ? resources.last : resources
@resource = resources.last
@resources = resources
@options = options
@action = options.delete(:action)
Expand All @@ -101,6 +99,14 @@ def initialize(controller, resources, options={})
delegate :head, :render, :redirect_to, :to => :controller
delegate :get?, :post?, :put?, :delete?, :to => :request

def request
@request ||= @controller.request
end

def format
@format ||= @controller.formats.first
end

# Undefine :to_json and :to_yaml since it's defined on Object
undef_method(:to_json) if method_defined?(:to_json)
undef_method(:to_yaml) if method_defined?(:to_yaml)
Expand Down Expand Up @@ -147,7 +153,7 @@ def navigation_behavior(error)
elsif has_errors? && default_action
render :action => default_action
else
redirect_to resource_location
redirect_to navigation_location
end
end

Expand All @@ -160,7 +166,7 @@ def api_behavior(error)
elsif has_errors?
display resource.errors, :status => :unprocessable_entity
elsif post?
display resource, :status => :created, :location => resource_location
display resource, :status => :created, :location => api_location
else
head :ok
end
Expand All @@ -178,6 +184,8 @@ def resourceful?
def resource_location
options[:location] || resources
end
alias :navigation_location :resource_location
alias :api_location :resource_location

# If a given response block was given, use it, otherwise call render on
# controller.
Expand Down

3 comments on commit 7eb5766

@joaovitor
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request and format attr_readers are not needed after this commit.
The ticket is here: https://rails.lighthouseapp.com/projects/8994/tickets/5378-removed-attr_reader-methods-not-used-in-responders

@josevalim
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks mate! However, I will revert part of these changes, since using attr_reader is actually faster!

@joaovitor
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I never did this benchmark to compare. I was reading the code, saw this and filled the ticket. Thanks for the fast response.

Please sign in to comment.