lib/roar/rails/controller_additions.rb in roar-rails-0.1.6 vs lib/roar/rails/controller_additions.rb in roar-rails-1.0.0
- old
+ new
@@ -15,21 +15,26 @@
end
module ClassMethods
def represents(format, options)
- represents_options.add(format,options)
+ represents_options.add(format, options)
respond_to format
end
end
+ # TODO: move into separate class so we don't pollute controller.
def consume!(model, options={})
- format = formats.first # FIXME: i expected request.content_mime_type to do the job. copied from responder.rb. this will return the wrong format when the controller responds to :json and :xml and the Content-type is :xml (?)
+ content_type = request.content_type
+
+ format = Mime::Type.lookup(content_type).try(:symbol) or raise UnsupportedMediaType.new("Cannot consume unregistered media type '#{content_type.inspect}'")
+
+ parsing_method = compute_parsing_method(format)
representer = prepare_model_for(format, model, options)
- representer.send(compute_parsing_method(format), incoming_string, options) # e.g. from_json("...")
+ representer.send(parsing_method, incoming_string, options) # e.g. from_json("...")
model
end
def prepare_model_for(format, model, options)
representer = representer_for(format, model, options)
@@ -50,24 +55,35 @@
body = request.body
body.rewind
body.read
end
- def render_to_body(options)
- if res = options[formats.first] and res.is_a?(Roar::Rails::Responder::Response)
- response.content_type = res.content_type
- return res.body
- end
+ # These methods deal with interfacing between the Roar Response object and
+ # ActionController, they simply pass the body of the Roar response up or do nothing
+ def _render_option_json(resource, options)
+ super(_resource_or_body(resource), options)
+ end
- super
+ def _render_option_xml(resource, options)
+ super(_resource_or_body(resource), options)
end
+ def _render_option_hal(resource, options)
+ super(_resource_or_body(resource), options)
+ end
+ def _resource_or_body(resource)
+ resource.is_a?(Roar::Rails::Responder::Response) ? resource.body : resource
+ end
+
# Include if you intend to use roar-rails with <tt>render json: model</tt>.
module Render
def render(options)
format = options.keys.first
super format => prepare_model_for(format, options.values.first, options)
end
end
end
-end
\ No newline at end of file
+
+ class UnsupportedMediaType < StandardError #:nodoc:
+ end
+end