Sha256: 9d9c10e1e1e22f87adcbc92140ca9bdb8dd76cd67172577abfa7dbc1f27c09eb

Contents?: true

Size: 1.84 KB

Versions: 14

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

module Effective
  class EventTicket < ActiveRecord::Base
    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(: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

14 entries across 14 versions & 1 rubygems

Version Path
effective_events-0.2.8 app/models/effective/event_ticket.rb
effective_events-0.2.7 app/models/effective/event_ticket.rb
effective_events-0.2.6 app/models/effective/event_ticket.rb
effective_events-0.2.5 app/models/effective/event_ticket.rb
effective_events-0.2.4 app/models/effective/event_ticket.rb
effective_events-0.2.3 app/models/effective/event_ticket.rb
effective_events-0.2.2 app/models/effective/event_ticket.rb
effective_events-0.2.1 app/models/effective/event_ticket.rb
effective_events-0.2.0 app/models/effective/event_ticket.rb
effective_events-0.1.10 app/models/effective/event_ticket.rb
effective_events-0.1.9 app/models/effective/event_ticket.rb
effective_events-0.1.8 app/models/effective/event_ticket.rb
effective_events-0.1.7 app/models/effective/event_ticket.rb
effective_events-0.1.6 app/models/effective/event_ticket.rb