From 0f132310b037812e142fe9bf7906f07966eab334 Mon Sep 17 00:00:00 2001 From: Sophat Sophana <106464006+phana25@users.noreply.github.com> Date: Thu, 9 Jan 2025 16:17:13 +0700 Subject: [PATCH] Close #2216 create event api for organizer dashboard --- .../spree/api/v2/organizer/base_controller.rb | 13 ------------- .../spree/api/v2/organizer/events_controller.rb | 14 ++++++++++---- .../spree_cm_commissioner/taxon_decorator.rb | 14 +++++++++++--- .../spree/v2/organizer/event_serializer.rb | 6 +----- 4 files changed, 22 insertions(+), 25 deletions(-) diff --git a/app/controllers/spree/api/v2/organizer/base_controller.rb b/app/controllers/spree/api/v2/organizer/base_controller.rb index 6a468e6cb..57c195009 100644 --- a/app/controllers/spree/api/v2/organizer/base_controller.rb +++ b/app/controllers/spree/api/v2/organizer/base_controller.rb @@ -13,16 +13,3 @@ def render_serialized_payload(status = 200) end end end -module Spree - module Api - module V2 - module Organizer - class BaseController < ::Spree::Api::V2::BaseController - def render_serialized_payload(status = 200) - render json: yield, status: status, content_type: content_type - end - end - end - end - end -end diff --git a/app/controllers/spree/api/v2/organizer/events_controller.rb b/app/controllers/spree/api/v2/organizer/events_controller.rb index 8a881a9b3..95ea6170f 100644 --- a/app/controllers/spree/api/v2/organizer/events_controller.rb +++ b/app/controllers/spree/api/v2/organizer/events_controller.rb @@ -10,7 +10,10 @@ def index .page(params[:page]) .per(params[:per_page]) render_serialized_payload do - serialize_collection(collection) + serialize_collection.new( + collection, + collection_options(collection) + ).serializable_hash end end @@ -20,11 +23,14 @@ def show end def serialize_resource(resource) - Spree::V2::Organizer::EventSerializer.new(resource).serializable_hash + Spree::V2::Organizer::EventSerializer.new( + resource, + include: resource_includes + ).serializable_hash end - def serialize_collection(collection) - Spree::V2::Organizer::EventSerializer.new(collection).serializable_hash + def serialize_collection + ::Spree::V2::Organizer::EventSerializer end end end diff --git a/app/models/spree_cm_commissioner/taxon_decorator.rb b/app/models/spree_cm_commissioner/taxon_decorator.rb index 04cf87fb4..b35d1e4c2 100644 --- a/app/models/spree_cm_commissioner/taxon_decorator.rb +++ b/app/models/spree_cm_commissioner/taxon_decorator.rb @@ -1,6 +1,6 @@ module SpreeCmCommissioner module TaxonDecorator - def self.prepended(base) + def self.prepended(base) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize base.include SpreeCmCommissioner::TaxonKind base.preference :background_color, :string @@ -45,8 +45,16 @@ def self.prepended(base) base.whitelisted_ransackable_attributes |= %w[kind] - base.scope :is_child, -> (value) { value ? where.not(parent_id: nil) : where(parent_id: nil) } - base.scope :is_leaf, -> (value) { value ? where.not(id: Spree::Taxon.joins(:children).select(:id)) : where(id: Spree::Taxon.joins(:children).select(:id)) } + base.scope :is_child, lambda { |value| + value ? where.not(parent_id: nil) : where(parent_id: nil) + } + base.scope :is_leaf, lambda { |value| + if value + where.not(id: Spree::Taxon.joins(:children).select(:id)) + else + where(id: Spree::Taxon.joins(:children).select(:id)) + end + } base.enum purchasable_on: { both: 0, web: 1, app: 2 } end diff --git a/app/serializers/spree/v2/organizer/event_serializer.rb b/app/serializers/spree/v2/organizer/event_serializer.rb index 7786c332c..fdcc8813f 100644 --- a/app/serializers/spree/v2/organizer/event_serializer.rb +++ b/app/serializers/spree/v2/organizer/event_serializer.rb @@ -2,23 +2,19 @@ module Spree module V2 module Organizer class EventSerializer < BaseSerializer - attributes :name, :subtitle, :from_date, :to_date, :description, :kind + attributes :name, :subtitle, :from_date, :to_date, :description, :kind, :vendor_id, :permalink attribute :is_child, &:child? - attribute :is_leaf, &:leaf? - # Parent Association belongs_to :parent, record_type: :taxon, serializer: Spree::V2::Storefront::TaxonSerializer - # Taxonomy Association belongs_to :taxonomy, record_type: :taxonomy, serializer: Spree::V2::Storefront::TaxonomySerializer - # Children Association has_many :children, record_type: :taxon, serializer: Spree::V2::Storefront::TaxonSerializer