Sha256: b0b2478d7be5c97ae3278d76b5501a8e1970e7e9dedc0143495d8feb5af29097

Contents?: true

Size: 1.26 KB

Versions: 7

Compression:

Stored size: 1.26 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[id code name 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

7 entries across 7 versions & 1 rubygems

Version Path
comee_core-0.2.75 app/models/comee/core/product.rb
comee_core-0.2.70 app/models/comee/core/product.rb
comee_core-0.2.69 app/models/comee/core/product.rb
comee_core-0.2.68 app/models/comee/core/product.rb
comee_core-0.2.67 app/models/comee/core/product.rb
comee_core-0.2.66 app/models/comee/core/product.rb
comee_core-0.2.65 app/models/comee/core/product.rb