lib/roar/rails/responder.rb in roar-rails-0.0.7 vs lib/roar/rails/responder.rb in roar-rails-0.0.8
- old
+ new
@@ -1,45 +1,33 @@
module Roar::Rails
module ModelMethods
# DISCUSS: move this into a generic namespace as we could need that in Sinatra as well.
- def extend_with_representer!(model, representer=nil)
- representer ||= representer_for_model(model)
+ def extend_with!(model, representer)
model.extend(representer)
end
-
- private
- def representer_for_model(model)
- class_name = model.class.name
- "#{class_name}Representer".constantize
- end
end
module Responder
include ModelMethods
# DISCUSS: why THE FUCK is options not passed as a method argument but kept as an internal instance variable in the responder? this is something i will never understand about Rails.
def display(model, *args)
- if representer = options.delete(:represent_with)
- # this is the new behaviour.
- model.extend(representer) # FIXME: move to method.
+ if representer = options.delete(:represent_items_with)
+ render_items_with(model, representer) # convenience API, not recommended since it's missing hypermedia.
return super
end
-
- representer = options.delete(:with_representer) and ActiveSupport::Deprecation.warn(":with_representer is deprecated and will be removed in roar-rails 1.0. Use :represent_with or :represent_items_with.")
- representer ||= options.delete(:represent_items_with) # new API.
-
- if model.respond_to?(:map!)
- ActiveSupport::Deprecation.warn("Calling #respond_with with a collection will misbehave in future versions of roar-rails. Use :represent_items_with to get the old behaviour.")
-
- model.map! do |m|
- extend_with_representer!(m, representer)
- m.to_hash
- end
- else
- extend_with_representer!(model, representer)
- end
-
+ representer = controller.representer_for(format, model, options)
+ extend_with!(model, representer)
super
end
+
+ private
+ def render_items_with(collection, representer)
+ collection.map! do |m| # DISCUSS: i don't like changing the method argument here.
+ extend_with!(m, representer)
+ m.to_hash # FIXME: huh? and what about XML?
+ end
+ end
+
end
end