Sha256: 0760e0635cef14ff18e2a194688e970999fe1a9aa1c599b19454207a58c0de2a

Contents?: true

Size: 1.93 KB

Versions: 2

Compression:

Stored size: 1.93 KB

Contents

class Comp
  include ActiveModel::Conversion 
  include ActiveModel::Validations
  extend ActiveModel::Naming

  validate :valid_recipient_and_benefactor

  attr_accessor :show, :tickets, :recipient, :benefactor, :reason, :order
  attr_accessor :comped_count, :uncomped_count

  #tickets can be an array of tickets_ids or an array of tickets
  def initialize(show, tickets_or_ids, recipient, benefactor)
    @show = show
    @tickets = []
    load_tickets(tickets_or_ids)
    @recipient = Person.find(recipient) unless recipient.blank?
    @benefactor = benefactor
  end
  
  def valid_recipient_and_benefactor
    if @recipient.nil?
      errors.add(:base, "Please select a person to comp to or create a new person record")
      return
    end
    
    if @benefactor.nil?
      errors.add(:base, "Please select a benefactor")
      return
    end
    
    unless @benefactor.current_organization.eql? @recipient.organization
      errors.add(:base, "Recipient and benefactor are from different organizations")
    end
  end

  def has_recipient?
    !recipient.blank?
  end

  def persisted?
    false
  end

  def submit
    ActiveRecord::Base.transaction do
      comped_tickets = []
      @tickets.each do |t|
        t.comp_to recipient
        comped_tickets << t
      end
      create_order(comped_tickets, @benefactor)
      self.comped_count    = tickets.size
      self.uncomped_count  = 0
    end
  end

  private
    def load_tickets(tickets_or_ids)
      tickets_or_ids.each do |t|
        t = Ticket.find(t) unless t.kind_of? Ticket
        @tickets << t
      end
    end
  
    def create_order(comped_tickets, benefactor)
      @order = CompOrder.new
      @order << comped_tickets
      @order.person = recipient
      @order.organization = benefactor.current_organization
      @order.details = "Comped by: #{benefactor.email} Reason: #{reason}"
      @order.to_comp!
    
    
      if 0 < comped_tickets.size
        @order.save
      end
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
artfully_ose-1.0.0.rc4 app/models/comp.rb
artfully_ose-1.0.0.rc3 app/models/comp.rb