Project

General

Profile

Download (694 Bytes) Statistics
| Branch: | Tag: | Revision:
27210309 Paul Kelly
class Notice < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table =>'user_notices'

TYPES = %w{message warning}
before_validation :set_default_notice_level
validates_inclusion_of :level, :in => TYPES

validates_presence_of :content
ffbdaa7c Ohad Levy
before_save :add_to_users
27210309 Paul Kelly
def to_s
"#{global? ? "global" : "individual"} #{content}"
end

9fd7478e Paul Kelly
def destroy_notice
if global
destroy
else
users.delete(User.current)
destroy unless users.any?
end
end
27210309 Paul Kelly
private

ffbdaa7c Ohad Levy
def add_to_users
if global
self.users = [User.current]
36f93e4d Ohad Levy
else
self.users = User.all
ffbdaa7c Ohad Levy
end
27210309 Paul Kelly
end

def set_default_notice_level
self.level ||= TYPES.first
end
end