lib/kosher/offer.rb in kosher-0.8.0 vs lib/kosher/offer.rb in kosher-0.9.1

- old
+ new

@@ -1,37 +1,35 @@ module Kosher - - # An offer. class Offer < Structure + include ActiveModel::Validations include Comparable - key :id - has_one :venue - has_one :item - has_one :seller - has_one :shipping + key :id + one :seller + one :shipping + one :unit + key :venue_id, Integer - # Compares offer with another offer. - # - # A kosher offer is better than an unkosher offer. If both offers are - # kosher or unkosher, a lower-priced offer is better. + validates_presence_of :seller, :shipping, :unit, :venue + def <=>(other) if kosher? != other.kosher? kosher? ? -1 : 1 else - price <=> other.price + total <=> other.total end end - # Returns whether the offer is kosher. - # - # An offer is kosher if its item, seller, and shipping are kosher. def kosher? - item.kosher? && seller.kosher? && shipping.kosher? + raise Invalid.new(self) unless valid? + seller.kosher? && shipping.kosher? && unit.kosher? end - # The total price of an offer. def price - item.price.to_money + shipping.cost.to_money + unit.price + shipping.cost + end + + def venue + Venue.find(venue_id) end end end