2011-07-26 17:27:23 -07:00
|
|
|
class ClosetList < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
2011-07-29 07:52:04 -07:00
|
|
|
has_many :hangers, :class_name => 'ClosetHanger', :foreign_key => 'list_id',
|
|
|
|
:dependent => :nullify
|
2011-07-26 17:27:23 -07:00
|
|
|
|
2011-07-30 19:34:27 -07:00
|
|
|
attr_accessible :description, :hangers_owned, :name, :visibility
|
2011-07-26 17:27:23 -07:00
|
|
|
|
|
|
|
validates :name, :presence => true, :uniqueness => {:scope => :user_id}
|
|
|
|
validates :user, :presence => true
|
|
|
|
validates :hangers_owned, :inclusion => {:in => [true, false], :message => "can't be blank"}
|
|
|
|
|
2011-07-29 07:52:04 -07:00
|
|
|
scope :alphabetical, order(:name)
|
2011-07-30 23:48:16 -07:00
|
|
|
scope :public, where(arel_table[:visibility].gteq(ClosetVisibility[:public].id))
|
2011-07-30 19:34:27 -07:00
|
|
|
scope :visible_to, lambda { |user|
|
|
|
|
condition = arel_table[:visibility].gteq(ClosetVisibility[:public].id)
|
|
|
|
condition = condition.or(arel_table[:user_id].eq(user.id)) if user
|
|
|
|
where(condition)
|
|
|
|
}
|
2011-07-29 07:52:04 -07:00
|
|
|
|
2011-07-26 17:27:23 -07:00
|
|
|
after_save :sync_hangers_owned!
|
|
|
|
|
|
|
|
def sync_hangers_owned!
|
|
|
|
if hangers_owned_changed?
|
|
|
|
hangers.each do |hanger|
|
|
|
|
hanger.owned = hangers_owned
|
|
|
|
hanger.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|