summaryrefslogtreecommitdiff
path: root/app/serializers/product_serializer.rb
blob: 0104ba40c9018a926c87e9bba7a6a02f82e79ec7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# frozen_string_literal: true

# ProductSerializer
class ProductSerializer < BaseSerializer
  include Rails.application.routes.url_helpers
  extend ActionView::RoutingUrlFor

  attributes :name, :public_id, :unitary_price, :bulk_price, :available_quantity, :categories

  attribute :picture do |object|
    object.picture.url
  end

  attribute :quantity, if: Proc.new { |object| object.respond_to?(:quantity) } do |object|
    object.quantity
  end

  attribute :company, if: Proc.new { |object| object.respond_to?(:company_short_name) } do |object|
    { name: object.company_name, short_name: object.company_short_name }
  end
end