2014-05-30 20:32:37 +08:00
|
|
|
class Contestnotification < ActiveRecord::Base
|
2014-06-05 11:58:59 +08:00
|
|
|
#attr_accessible :author_id, :notificationcomments_count, :contest_id, :description, :summary, :title
|
2014-05-30 20:32:37 +08:00
|
|
|
|
|
|
|
include Redmine::SafeAttributes
|
2014-06-06 21:40:58 +08:00
|
|
|
#Contestnotification::Notificationcomment
|
2014-05-30 20:32:37 +08:00
|
|
|
belongs_to :contest
|
|
|
|
belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
|
2014-06-05 11:19:59 +08:00
|
|
|
has_many :notificationcomments, :as => :notificationcommented, :dependent => :delete_all, :order => "created_at"
|
2014-05-30 20:32:37 +08:00
|
|
|
# fq
|
|
|
|
has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy
|
|
|
|
|
|
|
|
validates_presence_of :title, :description
|
|
|
|
validates_length_of :title, :maximum => 60
|
|
|
|
validates_length_of :summary, :maximum => 255
|
|
|
|
|
2014-06-03 18:24:55 +08:00
|
|
|
acts_as_attachable :delete_permission => :manage_contestnotifications
|
2014-05-30 20:32:37 +08:00
|
|
|
acts_as_searchable :columns => ['title', 'summary', "#{table_name}.description"], :include => :contest
|
2014-06-03 18:24:55 +08:00
|
|
|
acts_as_event :url => Proc.new {|o| {:controller => 'contestnotifications', :action => 'show', :id => o.id}}
|
2014-05-30 20:32:37 +08:00
|
|
|
acts_as_activity_provider :find_options => {:include => [:contest, :author]},
|
|
|
|
:author_key => :author_id
|
|
|
|
acts_as_watchable
|
|
|
|
|
2014-06-04 20:29:15 +08:00
|
|
|
after_create :add_author_as_watcher
|
2014-05-30 20:32:37 +08:00
|
|
|
|
|
|
|
after_create :act_as_activity
|
|
|
|
|
|
|
|
|
2014-06-03 18:24:55 +08:00
|
|
|
scope :visible, lambda {|*args|
|
|
|
|
nil
|
|
|
|
#includes(:contest).where(Contest.allowed_to_condition(args.shift || User.current, :view_contestnotifications, *args))
|
|
|
|
}
|
2014-05-30 20:32:37 +08:00
|
|
|
|
|
|
|
safe_attributes 'title', 'summary', 'description'
|
|
|
|
|
|
|
|
def visible?(user=User.current)
|
2014-06-03 15:27:04 +08:00
|
|
|
!user.nil? && user.allowed_to?(:view_contestnotifications, contest)
|
2014-05-30 20:32:37 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns true if the news can be commented by user
|
2014-06-05 11:02:32 +08:00
|
|
|
def notificationcommentable?(user=User.current)
|
|
|
|
user.allowed_to?(:notificationcomment_contestnotifications, contest)
|
2014-05-30 20:32:37 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def recipients
|
2014-06-05 15:45:17 +08:00
|
|
|
#contest.users.select {|user| user.notify_about?(self)}.map(&:mail)
|
2014-05-30 20:32:37 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# returns latest news for contests visible by user
|
|
|
|
def self.latest(user = User.current, count = 5)
|
2014-06-05 15:45:17 +08:00
|
|
|
visible(user).includes([:author, :contest]).order("#{Contestnotification.table_name}.created_at DESC").limit(count).all
|
2014-05-30 20:32:37 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def add_author_as_watcher
|
2014-06-05 15:45:17 +08:00
|
|
|
#Watcher.create(:watchable => self, :user => author)
|
2014-05-30 20:32:37 +08:00
|
|
|
end
|
|
|
|
## fq
|
|
|
|
def act_as_activity
|
|
|
|
self.acts << Activity.new(:user_id => self.author_id)
|
|
|
|
end
|
2014-06-03 15:27:04 +08:00
|
|
|
|
2014-05-30 20:32:37 +08:00
|
|
|
end
|