# # returning a set of objects # included_records = [] json.data do # => data: json.array! orders do |order| # => [] json.type "order" json.(order, :id) json.attributes do json.(order, :name) json.created_at order.created_at.utc end json.relationships do json.customer_organization do # name of the relationship. Not necessarily the name of the object json.data do json.type "organization" json.(customer_organization, :id) included_records << customer_organization end json.relationships do json.admin do json.data do json.type "user" json.(customer_organization.admin_user, :id) end included_records << customer_organization.admin_user end end end end end end json.included do # => data: json.array! included_records do |record| json.data do case record.class.name when Organization # json.partial('organizations/organization', organization: record) json.type "organization" json.id record.id json.attributes do json.(record, :name) json.created_at record.created_at.utc end when User json.type "user" json.id record.id json.attributes do json.(record, :name) json.created_at record.created_at.utc end end end end end # # returning a single object # json.data do # => data: json.type "order" json.(order, :id) json.attributes do json.(order, :name) json.created_at order.created_at.utc end json.relationships do json.customer_organization do # name of the relationship. Not necessarily the name of the object json.data do json.type "organization" json.(customer_organization, :id) json.attributes do json.(customer_organization, :name) json.created_at customer_organization.created_at.utc end end json.relationships do json.admin do json.data do json.type "user" json.(customer_organization.admin_user, :id) json.attributes do json.(customer_organization.admin_user, :name) json.created_at customer_organization.admin_user.created_at.utc end end end end end end end # # specs for the above response # context 'the response' do let(:json_body) { JSON.parse(response.body) } context 'has "data" containing the order' do expect(json_body).to include('data') end context 'the order data' do let(:order_data) { json_body['data'] } it 'includes the attributes of the order' do expect(order_data).to include('created_at' => order.created_at.iso8601) end end context 'relationships' do let(:order_relationships) { json_body['relationships'] } it 'includes the customer_organization' do expect(order_relationships).to include('data' => { 'type' => 'organization', 'id' => order.customer_organization.id }) end end it 'includes the data of the customer organization' do expect(json_body).to include('included') expect(json_body).to include('data' => { 'type' => 'organization', 'id' => order.customer_organization.id }) included_organization = json_body['data'].find { |u| u.type == 'organization' && u.id == 33 } expect(included_organization).to include('attributes') expect(included_organization['attributes']).to match( 'organization_name' => 'Organization Name', ) end end it 'includes data attributes for the organization admin user' do expect(json_body).to include('included') expect(json_body).to include('data' => { 'type' => 'user', 'id' => 33 }) included_user = json_body['data'].find { |u| u.type == 'user' && u.id == 33 } expect(included_user).to include('attributes') expect(included_user['attributes']).to match( 'name' => 'User name', ) end