2011-07-12 16:37:16 -07:00
|
|
|
class ClosetHanger < ActiveRecord::Base
|
|
|
|
belongs_to :item
|
2011-07-26 17:27:23 -07:00
|
|
|
belongs_to :list, :class_name => 'ClosetList'
|
2011-07-12 16:37:16 -07:00
|
|
|
belongs_to :user
|
|
|
|
|
2011-07-22 12:31:23 -07:00
|
|
|
attr_accessible :owned, :quantity
|
2011-07-14 09:50:24 -07:00
|
|
|
|
2011-07-22 12:31:23 -07:00
|
|
|
validates :item_id, :uniqueness => {:scope => [:user_id, :owned]}
|
2011-07-14 09:50:24 -07:00
|
|
|
validates :quantity, :numericality => {:greater_than => 0}
|
|
|
|
validates_presence_of :item, :user
|
|
|
|
|
2011-07-12 16:37:16 -07:00
|
|
|
scope :alphabetical_by_item_name, joins(:item).order(Item.arel_table[:name])
|
2011-07-22 12:31:23 -07:00
|
|
|
scope :owned_before_wanted, order(arel_table[:owned].desc)
|
|
|
|
|
2011-07-26 17:27:23 -07:00
|
|
|
before_validation :set_owned_by_list
|
|
|
|
|
|
|
|
def set_owned_by_list
|
|
|
|
self.owned = list.hangers_owned if list?
|
|
|
|
end
|
|
|
|
|
2011-07-22 12:31:23 -07:00
|
|
|
def verb(subject=:someone)
|
2011-07-22 14:55:05 -07:00
|
|
|
self.class.verb(subject, owned?)
|
|
|
|
end
|
|
|
|
|
2011-07-26 15:41:15 -07:00
|
|
|
def self.verb(subject, owned, positive=true)
|
2011-07-22 14:55:05 -07:00
|
|
|
base = (owned) ? 'own' : 'want'
|
2011-07-26 17:27:23 -07:00
|
|
|
base << 's' if positive && subject != :you && subject != :i
|
2011-07-22 12:31:23 -07:00
|
|
|
base
|
|
|
|
end
|
2011-07-12 16:37:16 -07:00
|
|
|
end
|
|
|
|
|