Sha256: 92d35598a951f0f7ff592dd6c2adfa3ac381f3e3d55b44c330759e908759825d

Contents?: true

Size: 1.62 KB

Versions: 9

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module Effective
  class EventProduct < ActiveRecord::Base
    self.table_name = (EffectiveEvents.event_products_table_name || :event_products).to_s

    acts_as_archived

    belongs_to :event

    has_many :event_addons
    has_many :purchased_event_addons, -> { EventAddon.purchased }, class_name: 'Effective::EventAddon'

    log_changes(to: :event) if respond_to?(:log_changes)

    has_rich_text :body

    effective_resource do
      title                 :string
      capacity              :integer

      # Pricing
      price                 :integer

      qb_item_name          :string
      tax_exempt            :boolean

      position              :integer
      archived              :boolean

      timestamps
    end

    scope :sorted, -> { order(:position) }
    scope :deep, -> { with_rich_text_body.includes(:purchased_event_addons) }

    before_validation(if: -> { event.present? }) do
      self.position ||= (event.event_products.map(&:position).compact.max || -1) + 1
    end

    validates :title, presence: true, uniqueness: { scope: [:event_id] }
    validates :price, presence: true

    def to_s
      title.presence || 'New Event Product'
    end

    # Available for purchase
    def available?
      return false if archived?
      capacity_available?
    end

    def capacity_available?
      capacity.blank? || (capacity_available > 0)
    end

    def capacity_available
      return nil if capacity.blank?
      [(capacity - purchased_event_addons_count), 0].max
    end

    def purchased_event_addons_count
      purchased_event_addons.reject(&:archived?).length
    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
effective_events-0.10.2 app/models/effective/event_product.rb
effective_events-0.10.1 app/models/effective/event_product.rb
effective_events-0.10.0 app/models/effective/event_product.rb
effective_events-0.9.3 app/models/effective/event_product.rb
effective_events-0.9.2 app/models/effective/event_product.rb
effective_events-0.9.1 app/models/effective/event_product.rb
effective_events-0.9.0 app/models/effective/event_product.rb
effective_events-0.8.0 app/models/effective/event_product.rb
effective_events-0.7.4 app/models/effective/event_product.rb