Class: Exchange

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
app/models/exchange.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order, items, tickets, ticket_type, send_email_confirmation = false) ⇒ Exchange

The original order The items to exchange The tickets that they are being exchanged for



20
21
22
23
24
25
26
# File 'app/models/exchange.rb', line 20

def initialize(order, items, tickets, ticket_type, send_email_confirmation = false)
  self.order        = order
  self.items        = items
  self.tickets      = tickets || []
  self.ticket_type  = ticket_type
  self.send_email_confirmation = send_email_confirmation
end

Instance Attribute Details

#exchange_orderObject (readonly)

Returns the value of attribute exchange_order.



5
6
7
# File 'app/models/exchange.rb', line 5

def exchange_order
  @exchange_order
end

#itemsObject

Returns the value of attribute items.



4
5
6
# File 'app/models/exchange.rb', line 4

def items
  @items
end

#orderObject

Returns the value of attribute order.



4
5
6
# File 'app/models/exchange.rb', line 4

def order
  @order
end

#send_email_confirmationObject

Returns the value of attribute send_email_confirmation.



4
5
6
# File 'app/models/exchange.rb', line 4

def send_email_confirmation
  @send_email_confirmation
end

#ticket_typeObject

Returns the value of attribute ticket_type.



4
5
6
# File 'app/models/exchange.rb', line 4

def ticket_type
  @ticket_type
end

#ticketsObject

Returns the value of attribute tickets.



4
5
6
# File 'app/models/exchange.rb', line 4

def tickets
  @tickets
end

Instance Method Details

#create_order(time = Time.now) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/exchange.rb', line 71

def create_order(time=Time.now)
  exchange_order = ExchangeOrder.new.tap do |exchange_order|
    exchange_order.person = order.person
    exchange_order.parent = order
    exchange_order.payment_method = order.payment_method
    exchange_order.created_at = time
    exchange_order.for_organization order.organization
    exchange_order.details = "Order is the result of an exchange on #{I18n.l time, :format => :slashed_date}"
    exchange_order.skip_email = !send_email_confirmation
    exchange_order << tickets
  end
  exchange_order.record_exchange! items
  exchange_order.save!
  @exchange_order = exchange_order
end

#items_are_exchangeableObject



28
29
30
# File 'app/models/exchange.rb', line 28

def items_are_exchangeable
  errors.add(:items, "are not available to exchange") unless items.all?(&:exchangeable?)
end


65
66
67
68
69
# File 'app/models/exchange.rb', line 65

def legal_ticket_type
  self.tickets.each do |ticket|
    errors.add(:tickets, "cannot be assigned to this ticket type") unless ticket.can_be_assigned_to(self.ticket_type)
  end
end

#return_old_itemsObject



52
53
54
# File 'app/models/exchange.rb', line 52

def return_old_items
  items.map(&:exchange!)
end

#sell_new_itemsObject



56
57
58
59
60
61
62
63
# File 'app/models/exchange.rb', line 56

def sell_new_items
  exchange_order_timestamp = Time.now
  tickets.each_with_index do |ticket, index| 
    ticket.exchange_to(order.person, exchange_order_timestamp)
    ticket.exchange_prices_from items[index].product
  end
  create_order(exchange_order_timestamp)
end

#submitObject



44
45
46
47
48
49
50
# File 'app/models/exchange.rb', line 44

def submit
  ActiveRecord::Base.transaction do
    self.tickets = Ticket.lock(self.tickets, self.ticket_type, Cart.create)
    sell_new_items
    return_old_items
  end
end

#tickets_are_availableObject



36
37
38
# File 'app/models/exchange.rb', line 36

def tickets_are_available
  errors.add(:tickets, "are not available to exchange") if tickets.any?(&:committed?)
end

#tickets_belong_to_organizationObject



40
41
42
# File 'app/models/exchange.rb', line 40

def tickets_belong_to_organization
  errors.add(:tickets, "do not belong to this organization") unless tickets.all? { |ticket| order.organization.can? :manage, ticket }
end

#tickets_match_itemsObject



32
33
34
# File 'app/models/exchange.rb', line 32

def tickets_match_items
  errors.add(:tickets, "must match the items to exchange") unless tickets.length == items.length
end