Sha256: 22b72476fd7fd46d824904ec33315852700546b9b944280e33a1625f61bdfe91

Contents?: true

Size: 1.87 KB

Versions: 4

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

module Effective
  class EventTicket < ActiveRecord::Base
    acts_as_archived

    belongs_to :event

    has_many :event_registrants
    has_many :purchased_event_registrants, -> { EventRegistrant.purchased }, class_name: 'Effective::EventRegistrant'

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

    has_rich_text :body

    effective_resource do
      title                 :string
      capacity              :integer

      # Pricing
      regular_price         :integer
      early_bird_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(:event, :purchased_event_registrants) }

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

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

    validates :title, presence: true, uniqueness: { scope: [:event_id] }
    validates :regular_price, presence: true
    validates :early_bird_price, presence: true, if: -> { event&.early_bird_end_at.present? }

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

    def price
      event.early_bird? ? early_bird_price : regular_price
    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_registrants_count), 0].max
    end

    def purchased_event_registrants_count
      purchased_event_registrants.length
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
effective_events-0.6.13 app/models/effective/event_ticket.rb
effective_events-0.6.12 app/models/effective/event_ticket.rb
effective_events-0.6.11 app/models/effective/event_ticket.rb
effective_events-0.6.10 app/models/effective/event_ticket.rb