Sha256: 375f3203fc44c11f0698a10137a92a32b85ea0ce494784c220e80fc285ce0570

Contents?: true

Size: 1022 Bytes

Versions: 8

Compression:

Stored size: 1022 Bytes

Contents

module Comee
  module Core
    class Product < ApplicationRecord
      after_save :update_parent, if: proc { |product| product.parent && product.parent.leaf? }

      has_ancestry
      has_one_attached :thumbnail_image
      has_many_attached :images

      belongs_to :source, class_name: "Comee::Core::FulfillmentCenter", optional: true

      validates :code, presence: true, uniqueness: true
      validates :name, presence: true

      scope :leafs, -> { where(leaf: true) }

      def update_parent
        obj = parent
        obj.leaf = false
        obj.save!
      end

      def thumbnail_image_url
        thumbnail_image.attached? ? Rails.application.routes.url_helpers.rails_blob_url(thumbnail_image, only_path: false) : nil
      end

      def images_url
        images.attached? ? images.map { |image| Rails.application.routes.url_helpers.rails_blob_url(image, only_path: false) } : []
      end

      def default_unit
        preferred_units.find { |unit| unit["default"] }
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
comee_core-0.1.61 app/models/comee/core/product.rb
comee_core-0.1.60 app/models/comee/core/product.rb
comee_core-0.1.59 app/models/comee/core/product.rb
comee_core-0.1.58 app/models/comee/core/product.rb
comee_core-0.1.57 app/models/comee/core/product.rb
comee_core-0.1.56 app/models/comee/core/product.rb
comee_core-0.1.55 app/models/comee/core/product.rb
comee_core-0.1.54 app/models/comee/core/product.rb