mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-06 22:36:07 +00:00
Update use of links on relationships
This commit is contained in:
committed by
Maikel Linke
parent
3dbf00f302
commit
d66d6d6bd6
13
app/serializers/api/v1/base_serializer.rb
Normal file
13
app/serializers/api/v1/base_serializer.rb
Normal 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
|
||||
@@ -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
|
||||
|
||||
15
app/serializers/api/v1/enterprise_serializer.rb
Normal file
15
app/serializers/api/v1/enterprise_serializer.rb
Normal 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
|
||||
@@ -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}"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user