2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2009-10-26 20:13:43 -04:00
|
|
|
module ActionController
|
|
|
|
module Head
|
2013-12-03 09:04:25 -05:00
|
|
|
# Returns a response that has no content (merely headers). The options
|
2009-10-26 20:13:43 -04:00
|
|
|
# argument is interpreted to be a hash of header names and values.
|
|
|
|
# This allows you to easily return a response that consists only of
|
|
|
|
# significant headers:
|
|
|
|
#
|
2012-10-27 16:05:27 -04:00
|
|
|
# head :created, location: person_path(@person)
|
2009-10-26 20:13:43 -04:00
|
|
|
#
|
2012-10-27 16:05:27 -04:00
|
|
|
# head :created, location: @person
|
2011-10-11 04:31:27 -04:00
|
|
|
#
|
2009-10-26 20:13:43 -04:00
|
|
|
# It can also be used to return exceptional conditions:
|
|
|
|
#
|
|
|
|
# return head(:method_not_allowed) unless request.post?
|
|
|
|
# return head(:bad_request) unless valid_request?
|
|
|
|
# render
|
2014-07-25 09:30:38 -04:00
|
|
|
#
|
|
|
|
# See Rack::Utils::SYMBOL_TO_STATUS_CODE for a full list of valid +status+ symbols.
|
2009-10-26 20:13:43 -04:00
|
|
|
def head(status, options = {})
|
2015-06-15 16:53:45 -04:00
|
|
|
if status.is_a?(Hash)
|
2016-05-10 03:54:17 -04:00
|
|
|
raise ArgumentError, "#{status.inspect} is not a valid value for `status`."
|
2015-06-15 16:53:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
status ||= :ok
|
2015-08-25 20:23:10 -04:00
|
|
|
|
2009-10-26 20:13:43 -04:00
|
|
|
location = options.delete(:location)
|
2012-05-07 18:56:36 -04:00
|
|
|
content_type = options.delete(:content_type)
|
2009-10-26 20:13:43 -04:00
|
|
|
|
|
|
|
options.each do |key, value|
|
2016-08-06 12:51:43 -04:00
|
|
|
headers[key.to_s.dasherize.split("-").each { |v| v[0] = v[0].chr.upcase }.join("-")] = value.to_s
|
2009-10-26 20:13:43 -04:00
|
|
|
end
|
|
|
|
|
2009-12-20 21:50:47 -05:00
|
|
|
self.status = status
|
|
|
|
self.location = url_for(location) if location
|
2012-05-03 18:47:02 -04:00
|
|
|
|
2014-12-30 10:04:18 -05:00
|
|
|
self.response_body = ""
|
|
|
|
|
2016-08-07 19:05:28 -04:00
|
|
|
if include_content?(response_code)
|
2017-04-30 11:05:20 -04:00
|
|
|
self.content_type = content_type || (Mime[formats.first] if formats) || Mime[:html]
|
2017-01-05 03:20:57 -05:00
|
|
|
response.charset = false
|
2012-05-03 18:47:02 -04:00
|
|
|
end
|
2015-08-25 20:23:10 -04:00
|
|
|
|
2015-03-11 14:31:14 -04:00
|
|
|
true
|
2009-10-26 20:13:43 -04:00
|
|
|
end
|
2012-05-03 18:47:02 -04:00
|
|
|
|
|
|
|
private
|
2016-08-06 13:55:02 -04:00
|
|
|
def include_content?(status)
|
|
|
|
case status
|
|
|
|
when 100..199
|
|
|
|
false
|
|
|
|
when 204, 205, 304
|
|
|
|
false
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
2012-05-03 18:47:02 -04:00
|
|
|
end
|
2009-10-26 20:13:43 -04:00
|
|
|
end
|
2010-10-10 03:51:52 -04:00
|
|
|
end
|