Sha256: 4ee7587a124e60461a2bdec0e24d6b150998c65d75121e3ebe3ef876a0bc5b15

Contents?: true

Size: 1.18 KB

Versions: 10

Compression:

Stored size: 1.18 KB

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
      belongs_to :weight_unit, class_name: "Comee::Core::Unit", 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 self.ransackable_attributes(_auth_object = nil)
        %w[code name description]
      end

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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
comee_core-0.1.73 app/models/comee/core/product.rb
comee_core-0.1.72 app/models/comee/core/product.rb
comee_core-0.1.71 app/models/comee/core/product.rb
comee_core-0.1.70 app/models/comee/core/product.rb
comee_core-0.1.69 app/models/comee/core/product.rb
comee_core-0.1.68 app/models/comee/core/product.rb
comee_core-0.1.67 app/models/comee/core/product.rb
comee_core-0.1.66 app/models/comee/core/product.rb
comee_core-0.1.65 app/models/comee/core/product.rb
comee_core-0.1.64 app/models/comee/core/product.rb