| Class | Order |
| In: |
app/models/order.rb
|
| Parent: | ActiveRecord::Base |
# File app/models/order.rb, line 47
47: def self.attribute_block_set_groups
48: super + [['paid']]
49: end
Add a new purchase of the given item.
# File app/models/order.rb, line 81
81: def add_item( offer )
82: old_item = line_items.find_by_offer_id( offer )
83: if old_item.nil?
84: LineItem.create! :order => self, :offer => offer, :quantity => 1
85: else
86: old_item.update_attributes! :quantity => (old_item.quantity + 1)
87: end
88: end
Managing payments, or at least mocking it out…
# File app/models/order.rb, line 147
147: def pay_with( payment_code )
148: write_attribute( :payment_authenticator, payment_code )
149: write_attribute( :paid, true )
150: end
Wrapped version of update_attributes. In addition to setting attrs on the Order itself, can also tweak quantities on the line items, given a hash with a key/value pair like so:
:line_item => {id => {:quantity => ...}, id => ..., ... }
If any quantity is blank, the corresponding line item is simply deleted (assuming all else is good). Returns true on success, false if some invalid object could not be saved (in which case nothing happens).
# File app/models/order.rb, line 101
101: def update_attributes( new_attrs )
102:
103: new_attrs = new_attrs.clone
104: lines_attrs = new_attrs.delete( :line_item )
105:
106: self.attributes = new_attrs
107:
108: (lines_attrs || []).each do |id, line_attrs|
109: item = line_items.detect { |it| it.id == id.to_i }
110: item.attributes = line_attrs
111: end
112:
113: line_items_ok = line_items.all? do |it|
114: it.valid? || it.quantity.blank? || it.quantity == 0
115: end
116:
117: unless self.valid? && line_items_ok
118: return false
119: end
120:
121: self.save!
122:
123: line_items.each do |item|
124: if item.quantity.blank? || item.quantity == 0
125: item.destroy
126: else
127: item.save!
128: end
129: end
130:
131: return true
132:
133: end
Wrapped version of update_attributes!. Args as for the wrapped update_attributes, but raises RecordNotSaved if something was wrong with the supplied attributes.
# File app/models/order.rb, line 139
139: def update_attributes!( new_attrs )
140: unless update_attributes( new_attrs )
141: raise( RecordNotSaved )
142: end
143: end