cirandas.net

ref: master

plugins/suppliers/serializers/suppliers_plugin/product_serializer.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
module SuppliersPlugin
  class ProductSerializer < ApplicationSerializer

    attribute :id
    attribute :name
    attribute :description
    attribute :supplier_id
    attribute :product_category_id

    attribute :unit_id
    attribute :price
    attribute :supplier_price
    attribute :supplier_product_name
    attribute :margin_percentage

    attribute :stored
    attribute :use_stock

    attribute :available

    has_one   :image_portrait
    has_one   :image_big

    def price
      product[:price]
    end

    def name
      product[:name] || product[:from_product_name]
    end
    def description
      product[:description] || product[:from_product_description]
    end
    def unit_id
      product[:unit_id] || product[:from_product_unit_id]
    end
    def supplier_price
      product[:from_product_price] || product.supplier_price
    end

    def supplier_product_name
      product[:from_product_name] || (product.supplier_product && product.supplier_product.name)
    end

    def available
      product[:available]
    end

    def supplier_id
      product[:supplier_id] || product.supplier_id
    end

    def margin_percentage
      product.own_margin_percentage
    end

    def image_portrait
      i = product.own_image || product.supplier_image
      return unless i

      i.public_filename :portrait
    end

    def image_big
      i = product.own_image || product.supplier_image
      return unless i

      i.public_filename :big
    end

    protected

    alias_method :product, :object
    alias_method :params, :instance_options

  end
end