Sha256: 77697b0232cf3e0594e801cfcea8425a85d1e2472985812862041a7644151a2a

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

module Effective
  class Ring < ActiveRecord::Base
    SIZES = [3, 4, 5, 6, 7, 8]
    TITANIUM_SIZES = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
    METALS = ['14k Yellow Gold', 'Sterling Silver', 'Titanium']

    acts_as_purchasable
    acts_as_addressable :shipping

    log_changes if respond_to?(:log_changes)

    # This ring is charged to an owner
    belongs_to :owner, polymorphic: true

    # Through the ring_wizard
    belongs_to :ring_wizard, polymorphic: true, optional: true

    effective_resource do
      size               :integer
      metal              :string

      issued_at          :datetime   # Present when issued by an admin

      # Acts as Purchasable
      price             :integer
      qb_item_name      :string
      tax_exempt        :boolean

      timestamps
    end

    scope :deep, -> { includes(:addresses, owner: [:membership]) }

    scope :ready_to_issue, -> { purchased.where(issued_at: nil) }
    scope :issued, -> { where.not(issued_at: nil) }

    validates :metal, presence: true, inclusion: { in: METALS }

    validates :size, presence: true
    validates :size, inclusion: { in: TITANIUM_SIZES }, if: -> { metal == 'Titanium' }
    validates :size, inclusion: { in: SIZES }, if: -> { metal != 'Titanium' }

    def to_s
      ["Chemist's Ring", (" - #{metal} size #{size}" if metal.present? && size.present?)].compact.join
    end

    def mark_as_issued!
      update!(issued_at: Time.zone.now)
    end

    def submitted?
      purchased?
    end

    def issued?
      issued_at.present?
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
effective_products-0.2.1 app/models/effective/ring.rb
effective_products-0.2.0 app/models/effective/ring.rb