Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding tests for the responder, delegating most of the which-represen…

…ter-for-who to the controller and deprecating the old API. still WIP, bro!
  • Loading branch information...
commit 57e2965045d3f2455849cd462f985eca900d3ede 1 parent 3c49cd6
@apotonick authored
View
8 lib/roar/rails/controller_additions.rb
@@ -39,12 +39,18 @@ def consume!(model)
model
end
+ def representer_for(*args)
+ name = representer_name_for(*args)
+ name = name.constantize unless name.is_a?(Module)
+ name
+ end
+
private
def compute_parsing_method(format)
"from_#{format}"
end
- def representer_name_for(format, model, options={})
+ def representer_name_for(format, model) # DISCUSS: should we pass and process options here?
if self.class.represents_options == {}
model_name = model.class.name
model_name = controller_path.camelize if model.kind_of?(Array)
View
16 lib/roar/rails/responder.rb
@@ -24,21 +24,17 @@ def display(model, *args)
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.")
-
+ if representer = options.delete(:represent_items_with)
model.map! do |m|
- extend_with_representer!(m, representer)
+ m.extend(representer) # FIXME: move to method.
m.to_hash
end
- else
- extend_with_representer!(model, representer)
+ return super
end
+ representer = controller.representer_for(format, model)
+ model.extend(representer) # FIXME: move to method.
+
super
end
end
View
148 test/responder_test.rb
@@ -98,19 +98,26 @@ class SingersController
assert_equal "SingersRepresenter", @controller.send(:representer_name_for, :json, [])
end
end
-
+ end
+
+ describe "#representer_for" do
describe "respond_with model, :represent_with => SingerRepresenter" do
before do
@controller = class ::BooController
include Roar::Rails::ControllerAdditions
- represents :json, :entity => Object, :collection => SingersRepresenter
+ represents :json, :entity => Object, :collection => "SingersRepresenter"
self
end.new
end
- it "uses passed class" do
- assert_equal SingerRepresenter, @controller.send(:representer_for, :json, Singer.new, :represent_with => SingerRepresenter)
+ it "returns module" do
+ assert_equal Object, @controller.send(:representer_for, :json, Singer.new)
+ end
+
+ it "returns constant when string configured" do
+ assert_equal SingersRepresenter, @controller.send(:representer_for, :json, [])
end
+
end
end
end
@@ -119,108 +126,109 @@ class SingersController
class ResponderTest < ActionController::TestCase
include Roar::Rails::TestCase
- class SingersController < ActionController::Base
+ class BaseController < ActionController::Base
include Roar::Rails::ControllerAdditions
respond_to :json
-
+
def execute
instance_exec &@block
end
end
- def get(&block)
- @controller.instance_eval do
- @block = block
+ class UnconfiguredControllerTest < ResponderTest
+ class SingersController < BaseController
end
- super :execute, :format => 'json'
- end
-
-
- tests SingersController
-
- test ":with_representer is deprecated" do
- assert_deprecated do
+ tests SingersController
+
+ test "responder finds SingerRepresenter representer by convention" do
get do
- singer = Musician.new("Bumi")
- respond_with singer, :with_representer => SingerRepresenter
+ singer = Singer.new("Bumi")
+ respond_with singer
end
+
+ assert_equal singer.to_json, @response.body
+ end
+
+ test "responder finds SingersRepresenter for collections by convention" do
+ get do
+ singers = [Singer.new("Bumi"), Singer.new("Bjork"), Singer.new("Sinead")]
+ respond_with singers
+ end
+
+ assert_equal({:singers => singers.collect {|s| s.extend(SingerRepresenter).to_hash }}.to_json, @response.body)
end
end
-
- test "responder allows specifying representer" do # TODO: remove in 1.0.
- get do
- singer = Musician.new("Bumi")
- respond_with singer, :with_representer => SingerRepresenter
+ class RespondToOptionsOverridingConfigurationTest < ResponderTest
+ class SingersController < BaseController
+ represents :json, Object
end
- assert_equal singer.to_json, @response.body
- end
-
- test "responder finds representer by convention" do
- get do
- singer = Singer.new("Bumi")
- respond_with singer
+ tests SingersController
+
+ test "responder uses passed representer" do
+ get do
+ singer = Singer.new("Bumi")
+ respond_with singer, :represent_with => SingerRepresenter
+ end
+
+ assert_equal singer.to_json, @response.body
end
- assert_equal singer.to_json, @response.body
- end
-
-
-
- test "responder works with collections" do # TODO: remove in 1.0.
- assert_deprecated do
+ test "responder uses passed representer for collection" do
get do
singers = [Singer.new("Bumi"), Singer.new("Bjork"), Singer.new("Sinead")]
- respond_with singers
+ respond_with singers, :represent_with => SingersRepresenter
end
+
+ assert_equal({:singers => singers.collect {|s| s.extend(SingerRepresenter).to_hash }}.to_json, @response.body)
end
- assert_equal singers.map(&:to_hash).to_json, @response.body
- end
-
- test "custom responder works with collections" do # TODO: remove in 1.0.
- get do
- singers = [Singer.new("Bumi"), Singer.new("Bjork"), Singer.new("Sinead")]
- respond_with singers, :with_representer => SingerAliasRepresenter
+ test "responder uses passed representer for collection items when :represent_items_with set" do
+ get do
+ singers = [Singer.new("Bumi"), Singer.new("Bjork"), Singer.new("Sinead")]
+ respond_with singers, :represent_items_with => SingerRepresenter
+ end
+
+ assert_equal(singers.collect {|s| s.extend(SingerRepresenter).to_hash }.to_json, @response.body)
end
-
- assert_equal singers.map {|s| s.extend(SingerAliasRepresenter).to_hash }.to_json, @response.body
end
-
-
- test "use passed :represent_with representer for single model" do
- get do
- singer = Musician.new("Bumi")
- respond_with singer, :with_representer => SingerRepresenter
+ class ConfiguredControllerTest < ResponderTest
+ class MusicianController < BaseController
+ represents :json, :entity => SingerRepresenter, :collection => SingersRepresenter
end
- assert_equal singer.extend(SingerRepresenter).to_json, @response.body
- end
-
- test "use passed :represent_with representer for collection" do
- get do
- singers = [Singer.new("Bumi"), Singer.new("Bjork"), Singer.new("Sinead")]
- respond_with singers, :represent_with => SingersRepresenter
+ tests MusicianController
+
+ test "responder uses configured representer" do
+ get do
+ singer = Singer.new("Bumi")
+ respond_with singer
+ end
+
+ assert_equal singer.to_json, @response.body
end
- assert_equal({:singers => singers.collect {|s| s.extend(SingerRepresenter).to_hash }}.to_json, @response.body)
+ test "responder uses configured representer for collection" do
+ get do
+ singers = [Singer.new("Bumi"), Singer.new("Bjork"), Singer.new("Sinead")]
+ respond_with singers
+ end
+
+ assert_equal({:singers => singers.collect {|s| s.extend(SingerRepresenter).to_hash }}.to_json, @response.body)
+ end
end
- test "use passed :represent_items_with for collection items" do
- get do
- singers = [Singer.new("Bumi"), Singer.new("Bjork"), Singer.new("Sinead")]
- respond_with singers, :represent_items_with => SingerRepresenter
+ def get(&block)
+ @controller.instance_eval do
+ @block = block
end
- assert_equal(singers.collect {|s| s.extend(SingerRepresenter).to_hash }.to_json, @response.body)
+ super :execute, :format => 'json'
end
-
-
-
def singer(name="Bumi")
singer = Musician.new(name)
singer.extend SingerRepresenter
Please sign in to comment.
Something went wrong with that request. Please try again.