Module: Roar::Rails::ControllerAdditions

Extended by:
ActiveSupport::Concern
Defined in:
lib/roar/rails/controller_additions.rb

Defined Under Namespace

Modules: ClassMethods, Render

Instance Method Summary collapse

Instance Method Details

#consume!(model, options = {}) ⇒ Object

TODO: move into separate class so we don't pollute controller.


28
29
30
31
32
33
34
35
36
37
38
# File 'lib/roar/rails/controller_additions.rb', line 28

def consume!(model, options={})
  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(parsing_method, incoming_string, options) # e.g. from_json("...")
  model
end

#prepare_model_for(format, model, options) ⇒ Object


40
41
42
43
# File 'lib/roar/rails/controller_additions.rb', line 40

def prepare_model_for(format, model, options)
  representer = representer_for(format, model, options)
  representer.prepare(model)
end

#representer_for(format, model, options = {}) ⇒ Object

Central entry-point for finding the appropriate representer.


46
47
48
# File 'lib/roar/rails/controller_additions.rb', line 46

def representer_for(format, model, options={})
  options.delete(:represent_with) || self.class.represents_options.for(format, model, controller_path)
end