Update use of links on relationships

This commit is contained in:
Matt-Yorkley
2021-12-12 19:13:16 +00:00
committed by Maikel Linke
parent 3dbf00f302
commit d66d6d6bd6
4 changed files with 37 additions and 4 deletions

View File

@@ -0,0 +1,13 @@
# frozen_string_literal: true
module Api
module V1
class BaseSerializer
include JSONAPI::Serializer
def self.url_helpers
Rails.application.routes.url_helpers
end
end
end
end

View File

@@ -2,12 +2,14 @@
module Api
module V1
class CustomerSerializer
include JSONAPI::Serializer
class CustomerSerializer < BaseSerializer
attributes :id, :enterprise_id, :first_name, :last_name, :code, :email
belongs_to :enterprise, record_type: :enterprise, serializer: :id
belongs_to :enterprise, links: {
related: ->(object) {
url_helpers.api_v1_enterprise_url(id: object.enterprise_id)
}
}
end
end
end

View File

@@ -0,0 +1,15 @@
# frozen_string_literal: true
module Api
module V1
class EnterpriseSerializer < BaseSerializer
attributes :id, :name
has_many :customers, links: {
related: ->(object) {
url_helpers.api_v1_enterprise_customers_url(enterprise_id: object.id)
}
}
end
end
end

View File

@@ -128,6 +128,9 @@ describe "Customers", type: :request do
"data" => {
"id" => customer1.enterprise_id.to_s,
"type" => "enterprise"
},
"links" => {
"related" => "http://test.host/api/v1/enterprises/#{customer1.enterprise_id}"
}
}