Skip to content

Instantly share code, notes, and snippets.

@Supro
Last active December 26, 2015 11:09
Show Gist options
  • Save Supro/7141290 to your computer and use it in GitHub Desktop.
Save Supro/7141290 to your computer and use it in GitHub Desktop.
ActiveModelSerializers example
### spec/serializers/building_spec.rb
require "spec_helper"
describe BuildingSerializer do
include Rails.application.routes.url_helpers
let(:room) { create :room }
let(:building) { room.building }
let(:hash) { BuildingSerializer.new(building).serializable_hash }
let(:links_hash) { { rooms: { href: building_rooms_url(building) } } }
let(:rooms) { building.rooms.map{|room| RoomEmbedSerializer.new(room).serializable_hash } }
subject { hash }
its([:id]) { should eql building.id }
its([:name]) { should eql building.name }
its([:address]) { should eql building.address }
its([:floors_count]) { should eql building.floors_count }
its([:description]) { should eql building.description }
its([:_links]) { should eql links_hash }
its([:rooms]){ should be_nil }
context "expand: true" do
let(:hash) { BuildingSerializer.new(building, expand: true).serializable_hash }
its([:rooms]){ should eql rooms }
end
end
### spec/serializers/rooms_embed_spec.rb
require "spec_helper"
describe RoomEmbedSerializer do
let(:room) { create :room }
let(:hash) { RoomEmbedSerializer.new(room).serializable_hash }
subject { hash }
its([:id]) { should eql room.id }
its([:area]) { should eql room.area }
end
### spec/serializers/rooms_spec.rb
require "spec_helper"
describe RoomSerializer do
let(:room) { create :room }
let(:hash) { RoomSerializer.new(room).serializable_hash }
subject { hash }
its([:id]) { should eql room.id }
its([:area]) { should eql room.area }
its([:furnish]) { should eql room.furnish }
its([:building_id]) { should eql room.building_id }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment