class Contestnotification < ActiveRecord::Base #attr_accessible :author_id, :notificationcomments_count, :contest_id, :description, :summary, :title include Redmine::SafeAttributes #Contestnotification::Notificationcomment belongs_to :contest belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' has_many :notificationcomments, as: :notificationcommented, :dependent => :delete_all, :order => "created_at" # fq has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy validates :title, length: {maximum: 60}, presence: true validates :description, presence: true validates :summary, length: {maximum: 255} acts_as_attachable :delete_permission => :manage_contestnotifications acts_as_searchable :columns => ['title', 'summary', "#{table_name}.description"], :include => :contest acts_as_event :url => Proc.new {|o| {:controller => 'contestnotifications', :action => 'show', :id => o.id}} acts_as_activity_provider :find_options => {:include => [:contest, :author]}, :author_key => :author_id acts_as_watchable after_create :add_author_as_watcher after_create :act_as_activity scope :visible, lambda {|*args| nil #includes(:contest).where(Contest.allowed_to_condition(args.shift || User.current, :view_contestnotifications, *args)) } safe_attributes 'title', 'summary', 'description' def visible?(user=User.current) !user.nil? && user.allowed_to?(:view_contestnotifications, contest) end # Returns true if the news can be commented by user def notificationcommentable?(user=User.current) user.allowed_to?(:notificationcomment_contestnotifications, contest) end def recipients #contest.users.select {|user| user.notify_about?(self)}.map(&:mail) end # returns latest news for contests visible by user def self.latest(user = User.current, count = 5) visible(user).includes([:author, :contest]).order("#{Contestnotification.table_name}.created_at DESC").limit(count).all end private def add_author_as_watcher #Watcher.create(:watchable => self, :user => author) end ## fq def act_as_activity self.acts << Activity.new(:user_id => self.author_id) end end