README.markdown in roar-rails-0.1.6 vs README.markdown in roar-rails-1.0.0

- old
+ new

@@ -30,11 +30,11 @@ This will create the file `app/representers/band_representer.rb` with the following content, ```ruby module BandRepresenter - include Roar::Representer::JSON + include Roar::JSON property :id property :name end ``` @@ -127,10 +127,11 @@ ## Parsing incoming documents In `#create` and `#update` actions it is often necessary to parse the incoming representation and map it to a model instance. Use the `#consume!` method for this. +The client must provide a `Content-Type` request header with proper MIME type to let `#consume!` know which representer to use. ```ruby class SingersController < ApplicationController respond_to :json @@ -141,33 +142,36 @@ respond_with singer end end ``` -The `consume!` call will roughly do the following. +For instance, if content type is set to `application/xml` the `consume!` call will roughly do the following. ```ruby singer. extend(SingerRepresenter) - from_json(request.body) + from_xml(request.body) ``` -So, `#consume!` helps you figuring out the representer module and reading the incoming document. +So, `#consume!` helps you figuring out the representer module and reading the incoming document. Just like Rails, depending on the registered MIME type for `Content-type` it picks the deserialize method (e.g. `from_json` vs. `from_xml`) -Note that it respects settings from `#represents`. It uses the same mechanics known from `#respond_with` to choose a representer. +It is important to provide a known content type in the request. If it is missing or not supported by the responder +`#consume!` will raise an exception `Roar::Rails::ControllerAdditions::UnsupportedMediaType`. Unless you rescue the exception the action will stop and respond with HTTP status `406 Unsupported Media Type`. +Note that `#consume!` respects settings from `#represents`. It uses the same mechanics known from `#respond_with` to choose a representer. + ```ruby consume!(singer, :represent_with => MusicianRepresenter) ``` ## Using Decorators If you prefer roar's decorator approach over extend, just go for it. roar-rails will figure out automatically which represent strategy to use. Be sure to use roar >= 0.11.17. ```ruby class SingerRepresenter < Roar::Decorator - include Roar::Representer::JSON + include Roar::JSON property :name link :self do singer_url(represented) @@ -209,11 +213,11 @@ Any URL helpers from the Rails app are automatically available in representers. ```ruby module FruitRepresenter - include Roar::Representer::JSON - include Roar::Representer::Feature::Hypermedia + include Roar::JSON + include Roar::Hypermedia link :self do fruit_url self end end