1
0
Fork 0
forked from OpenNeo/impress

validate that closet hanger list belongs to the same user

This commit is contained in:
Emi Matchu 2011-07-29 13:47:01 -04:00
parent 811d6df697
commit c76c261444

View file

@ -9,17 +9,14 @@ class ClosetHanger < ActiveRecord::Base
validates :quantity, :numericality => {:greater_than => 0} validates :quantity, :numericality => {:greater_than => 0}
validates_presence_of :item, :user validates_presence_of :item, :user
validate :list_belongs_to_user
scope :alphabetical_by_item_name, joins(:item).order(Item.arel_table[:name]) scope :alphabetical_by_item_name, joins(:item).order(Item.arel_table[:name])
scope :owned_before_wanted, order(arel_table[:owned].desc) scope :owned_before_wanted, order(arel_table[:owned].desc)
scope :unlisted, where(:list_id => nil) scope :unlisted, where(:list_id => nil)
before_validation :set_owned_by_list before_validation :set_owned_by_list
def set_owned_by_list
self.owned = list.hangers_owned if list_id?
true
end
def verb(subject=:someone) def verb(subject=:someone)
self.class.verb(subject, owned?) self.class.verb(subject, owned?)
end end
@ -29,5 +26,22 @@ class ClosetHanger < ActiveRecord::Base
base << 's' if positive && subject != :you && subject != :i base << 's' if positive && subject != :you && subject != :i
base base
end end
protected
def list_belongs_to_user
if list_id?
if list
errors.add(:list_id, "must belong to you") unless list.user_id == user_id
else
errors.add(:list, "must exist")
end
end
end
def set_owned_by_list
self.owned = list.hangers_owned if list
true
end
end end