Sha256: 8c292f7f4b07e77424ca88cd55a0242daf93639af9fda2b5618f952663278954

Contents?: true

Size: 1.34 KB

Versions: 80

Compression:

Stored size: 1.34 KB

Contents

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

      has_ancestry

      has_many :product_lookups
      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, :customs_description, 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[id code name description customs_description leaf]
      end

      def self.ransackable_associations(_auth_object = nil)
        []
      end

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

Version data entries

80 entries across 80 versions & 1 rubygems

Version Path
comee_core-0.3.62 app/models/comee/core/product.rb
comee_core-0.3.61 app/models/comee/core/product.rb
comee_core-0.3.60 app/models/comee/core/product.rb
comee_core-0.3.59 app/models/comee/core/product.rb
comee_core-0.3.58 app/models/comee/core/product.rb
comee_core-0.3.57 app/models/comee/core/product.rb
comee_core-0.3.56 app/models/comee/core/product.rb
comee_core-0.3.55 app/models/comee/core/product.rb
comee_core-0.3.54 app/models/comee/core/product.rb
comee_core-0.3.53 app/models/comee/core/product.rb
comee_core-0.3.51 app/models/comee/core/product.rb
comee_core-0.3.50 app/models/comee/core/product.rb
comee_core-0.3.49 app/models/comee/core/product.rb
comee_core-0.3.48 app/models/comee/core/product.rb
comee_core-0.3.47 app/models/comee/core/product.rb
comee_core-0.3.46 app/models/comee/core/product.rb
comee_core-0.3.45 app/models/comee/core/product.rb
comee_core-0.3.44 app/models/comee/core/product.rb
comee_core-0.3.43 app/models/comee/core/product.rb
comee_core-0.3.42 app/models/comee/core/product.rb