Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Working rails-api/active_model_serializers support.

  • Loading branch information...
commit a5a97636e201816068d2275f4bf14d214d3d1655 1 parent 20b1881
@Sutto authored
View
2  Gemfile
@@ -21,4 +21,6 @@ if (wp_version = ENV['WILL_PAGINATE_VERSION'])
gem 'will_paginate', wp_version
end
+gem 'active_model_serializers'
+
gemspec
View
2  Rakefile
@@ -7,7 +7,7 @@ require 'bundler/gem_tasks'
desc "Run all specs in spec directory (excluding plugin specs)"
RSpec::Core::RakeTask.new(:spec)
-INTEGRATION_LIBS = %w(will_paginate kaminari active_record)
+INTEGRATION_LIBS = %w(will_paginate kaminari active_record active_model_serializers)
namespace :spec do
View
15 lib/rocket_pants/controller/respondable.rb
@@ -2,6 +2,19 @@ module RocketPants
module Respondable
extend ActiveSupport::Concern
+ SerializerWrapper = Struct.new(:serializer, :object) do
+
+ def serializable_hash(options = {})
+ instance = serializer.new(object, options)
+ if instance.respond_to?(:serializable_hash)
+ instance.serializable_hash
+ else
+ instance.as_json options
+ end
+ end
+
+ end
+
def self.pagination_type(object)
if object.respond_to?(:total_entries)
:will_paginate
@@ -59,7 +72,7 @@ def self.normalise_to_serializer(object, options)
serializer = options.delete(:serializer)
serializer = object.active_model_serializer if object.respond_to?(:active_model_serializer) && serializer.nil?
return object unless serializer
- serializer.new object, options
+ SerializerWrapper.new serializer, object
end
RENDERING_OPTIONS = [:status, :content_type]
View
137 spec/integration/active_model_serializers_spec.rb
@@ -0,0 +1,137 @@
+require 'spec_helper'
+require 'active_model_serializers'
+
+describe RocketPants::Base, 'active_model_serializers integration', :integration => true, :target => 'active_model_serializers' do
+ include ControllerHelpers
+
+ use_reversible_tables :fish, :scope => :all
+
+ # t.string :name
+ # t.string :latin_name
+ # t.integer :child_number
+ # t.string :token
+
+ let(:fish) { Fish.create! :name => "Test Fish", :latin_name => "Fishus fishii", :child_number => 1, :token => "xyz" }
+ after(:each) { Fish.delete_all }
+
+ class SerializerA < ActiveModel::Serializer
+ attributes :name, :latin_name
+ end
+
+ class SerializerB < ActiveModel::Serializer
+ attributes :name, :child_number
+ end
+
+ describe 'on instances' do
+
+ it 'should use the active_model_serializer' do
+ mock(TestController).test_data { fish }
+ mock(fish).active_model_serializer { SerializerB }
+ mock.proxy(SerializerB).new(fish, anything) { |r| r }
+ get :test_data
+ content[:response].should be_present
+ content[:response].should be_a Hash
+ content[:response].keys.map(&:to_sym).should =~ [:name, :child_number]
+ end
+
+ it 'should let you specify a custom serializer' do
+ mock(TestController).test_data { fish }
+ mock(TestController).test_options { {:serializer => SerializerA} }
+ mock.proxy(SerializerA).new(fish, anything) { |r| r }
+ get :test_data
+ content[:response].should be_present
+ content[:response].should be_a Hash
+ content[:response].keys.map(&:to_sym).should =~ [:name, :latin_name]
+ end
+
+ it 'should use serializable_hash without a serializer' do
+ dont_allow(SerializerA).new(fish, anything)
+ dont_allow(SerializerB).new(fish, anything)
+ mock(TestController).test_data { fish }
+ expected_keys = fish.serializable_hash.keys.map(&:to_sym)
+ mock.proxy(fish).serializable_hash.with_any_args { |r| r }
+ get :test_data
+ content[:response].should be_present
+ content[:response].should be_a Hash
+ content[:response].keys.map(&:to_sym).should =~ expected_keys
+ end
+
+ it 'should pass through url options' do
+ mock(TestController).test_data { fish }
+ mock(TestController).test_options { {:serializer => SerializerA} }
+ mock.proxy(SerializerA).new(fish, rr_satisfy { |h| h[:url_options].present? }) { |r| r }
+ get :test_data
+ content[:response].should be_present
+ content[:response].should be_a Hash
+ content[:response].keys.map(&:to_sym).should =~ [:name, :latin_name]
+ end
+
+ end
+
+ describe 'on arrays' do
+
+ it 'should work with array serializers' do
+ mock(TestController).test_data { [fish] }
+ mock(fish).active_model_serializer { SerializerB }
+ mock.proxy(SerializerB).new(fish, anything) { |r| r }
+ get :test_data
+ content[:response].should be_present
+ content[:response].should be_a Array
+ serialized_fish = content[:response].first
+ serialized_fish.should be_a Hash
+ serialized_fish.keys.map(&:to_sym).should =~ [:name, :child_number]
+ end
+
+ it 'should support each_serializer' do
+ mock(TestController).test_data { [fish] }
+ mock.proxy(SerializerA).new(fish, anything) { |r| r }
+ mock(TestController).test_options { {:each_serializer => SerializerA} }
+ get :test_data
+ content[:response].should be_present
+ content[:response].should be_a Array
+ serialized_fish = content[:response].first
+ serialized_fish.should be_a Hash
+ serialized_fish.keys.map(&:to_sym).should =~ [:name, :latin_name]
+ end
+
+ it 'should default to the serializable hash version' do
+ dont_allow(SerializerA).new(fish, anything)
+ dont_allow(SerializerB).new(fish, anything)
+ mock(TestController).test_data { [fish] }
+ expected_keys = fish.serializable_hash.keys.map(&:to_sym)
+ mock.proxy(fish).serializable_hash.with_any_args { |r| r }
+ get :test_data
+ content[:response].should be_present
+ content[:response].should be_a Array
+ serialized_fish = content[:response].first
+ serialized_fish.should be_a Hash
+ serialized_fish.keys.map(&:to_sym).should =~ expected_keys
+ end
+
+ it 'should pass through url options' do
+ mock(TestController).test_data { [fish] }
+ mock(TestController).test_options { {:each_serializer => SerializerA} }
+ mock.proxy(SerializerA).new(fish, rr_satisfy { |h| h[:url_options].present? }) { |r| r }
+ get :test_data
+ content[:response].should be_present
+ content[:response].should be_a Array
+ serialized_fish = content[:response].first
+ serialized_fish.should be_a Hash
+ serialized_fish.keys.map(&:to_sym).should =~ [:name, :latin_name]
+ end
+
+ it 'should default to root being false' do
+ mock(TestController).test_data { [fish] }
+ mock(TestController).test_options { {:each_serializer => SerializerA} }
+ mock.proxy(SerializerA).new(fish, rr_satisfy { |h| h[:root] == false }) { |r| r }
+ get :test_data
+ content[:response].should be_present
+ content[:response].should be_a Array
+ serialized_fish = content[:response].first
+ serialized_fish.should be_a Hash
+ serialized_fish.keys.map(&:to_sym).should =~ [:name, :latin_name]
+ end
+
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.