From 8760d69aadb89041f7b328d82f8d7acaebe87848 Mon Sep 17 00:00:00 2001 From: Han Date: Thu, 20 Jun 2024 19:32:04 +0100 Subject: [PATCH] TOP-203 add in new serializer for database --- app/serializers/indexed_services_serializer.rb | 8 +++++++- .../regular_schedule_service_at_location_serializer.rb | 5 +++++ app/serializers/service_at_location_serializer.rb | 9 +++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 app/serializers/regular_schedule_service_at_location_serializer.rb create mode 100644 app/serializers/service_at_location_serializer.rb diff --git a/app/serializers/indexed_services_serializer.rb b/app/serializers/indexed_services_serializer.rb index e81806c0..a6ac86f6 100644 --- a/app/serializers/indexed_services_serializer.rb +++ b/app/serializers/indexed_services_serializer.rb @@ -31,6 +31,13 @@ class IndexedServicesSerializer < ActiveModel::Serializer object.locations.where(visible: true) end + has_many :service_at_locations do + object.service_at_locations.includes(:location).where(locations: { visible: true }) + end + + # uses a modified version of the RegularScheduleSerializer that includes regular_schedules to prevent loops + has_many :regular_schedules, serializer: RegularScheduleServiceAtLocationSerializer + has_many :contacts do object.contacts.where(visible: true) end @@ -47,7 +54,6 @@ class IndexedServicesSerializer < ActiveModel::Serializer belongs_to :organisation has_many :taxonomies - has_many :regular_schedules has_many :cost_options has_many :links has_many :send_needs diff --git a/app/serializers/regular_schedule_service_at_location_serializer.rb b/app/serializers/regular_schedule_service_at_location_serializer.rb new file mode 100644 index 00000000..cc98f594 --- /dev/null +++ b/app/serializers/regular_schedule_service_at_location_serializer.rb @@ -0,0 +1,5 @@ +include RegularScheduleHelper + +class RegularScheduleServiceAtLocationSerializer < RegularScheduleSerializer + belongs_to :service_at_location, serializer: ServiceAtLocationSerializer +end diff --git a/app/serializers/service_at_location_serializer.rb b/app/serializers/service_at_location_serializer.rb new file mode 100644 index 00000000..f94336f9 --- /dev/null +++ b/app/serializers/service_at_location_serializer.rb @@ -0,0 +1,9 @@ + +class ServiceAtLocationSerializer < ActiveModel::Serializer + attribute :id + attribute :service_id + attribute :location_id + belongs_to :location, serializer: LocationSerializer + has_many :regular_schedules, serializer: RegularScheduleSerializer, key: :regular_schedule + # @TODO add holiday_schedule here (holidayScheduleCollection) +end \ No newline at end of file