Sha256: 25df42a668a783eb01c116cca8becc4db204348fb846717d7519d695f5b89532

Contents?: true

Size: 1.61 KB

Versions: 26

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module Effective
  class EventProduct < ActiveRecord::Base
    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) }

    scope :archived, -> { where(archived: true) }
    scope :unarchived, -> { where(archived: false) }

    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.length
    end

  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
effective_events-0.6.13 app/models/effective/event_product.rb
effective_events-0.6.12 app/models/effective/event_product.rb
effective_events-0.6.11 app/models/effective/event_product.rb
effective_events-0.6.10 app/models/effective/event_product.rb
effective_events-0.6.9 app/models/effective/event_product.rb
effective_events-0.6.8 app/models/effective/event_product.rb
effective_events-0.6.7 app/models/effective/event_product.rb
effective_events-0.6.6 app/models/effective/event_product.rb
effective_events-0.6.5 app/models/effective/event_product.rb
effective_events-0.6.4 app/models/effective/event_product.rb
effective_events-0.6.3 app/models/effective/event_product.rb
effective_events-0.6.2 app/models/effective/event_product.rb
effective_events-0.6.1 app/models/effective/event_product.rb
effective_events-0.6.0 app/models/effective/event_product.rb
effective_events-0.5.6 app/models/effective/event_product.rb
effective_events-0.5.5 app/models/effective/event_product.rb
effective_events-0.5.4 app/models/effective/event_product.rb
effective_events-0.5.3 app/models/effective/event_product.rb
effective_events-0.5.2 app/models/effective/event_product.rb
effective_events-0.5.1 app/models/effective/event_product.rb