news.rb
98 lines
| 3.2 KiB
| text/x-ruby
|
RubyLexer
|
r1908 | # Redmine - project management software | ||
|
r14856 | # Copyright (C) 2006-2016 Jean-Philippe Lang | ||
|
r330 | # | ||
# This program is free software; you can redistribute it and/or | ||||
# modify it under the terms of the GNU General Public License | ||||
# as published by the Free Software Foundation; either version 2 | ||||
# of the License, or (at your option) any later version. | ||||
|
r5699 | # | ||
|
r330 | # This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
|
r5699 | # | ||
|
r330 | # You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
class News < ActiveRecord::Base | ||||
|
r9014 | include Redmine::SafeAttributes | ||
|
r330 | belongs_to :project | ||
|
r13102 | belongs_to :author, :class_name => 'User' | ||
|
r13100 | has_many :comments, lambda {order("created_on")}, :as => :commented, :dependent => :delete_all | ||
|
r5699 | |||
|
r330 | validates_presence_of :title, :description | ||
|
r590 | validates_length_of :title, :maximum => 60 | ||
validates_length_of :summary, :maximum => 255 | ||||
|
r13100 | attr_protected :id | ||
|
r663 | |||
|
r13283 | acts_as_attachable :edit_permission => :manage_news, | ||
:delete_permission => :manage_news | ||||
|
r13100 | acts_as_searchable :columns => ['title', 'summary', "#{table_name}.description"], | ||
|
r13357 | :preload => :project | ||
|
r663 | acts_as_event :url => Proc.new {|o| {:controller => 'news', :action => 'show', :id => o.id}} | ||
|
r13100 | acts_as_activity_provider :scope => preload(:project, :author), | ||
|
r2064 | :author_key => :author_id | ||
|
r4883 | acts_as_watchable | ||
|
r5699 | |||
|
r4883 | after_create :add_author_as_watcher | ||
|
r11791 | after_create :send_notification | ||
|
r5699 | |||
|
r10723 | scope :visible, lambda {|*args| | ||
|
r13100 | joins(:project). | ||
where(Project.allowed_to_condition(args.shift || User.current, :view_news, *args)) | ||||
|
r10723 | } | ||
|
r5699 | |||
|
r9014 | safe_attributes 'title', 'summary', 'description' | ||
|
r3055 | def visible?(user=User.current) | ||
!user.nil? && user.allowed_to?(:view_news, project) | ||||
end | ||||
|
r5699 | |||
|
r8734 | # Returns true if the news can be commented by user | ||
def commentable?(user=User.current) | ||||
user.allowed_to?(:comment_news, project) | ||||
end | ||||
|
r13504 | def notified_users | ||
project.users.select {|user| user.notify_about?(self) && user.allowed_to?(:view_news, project)} | ||||
end | ||||
|
r11041 | def recipients | ||
|
r13504 | notified_users.map(&:mail) | ||
|
r11041 | end | ||
|
r13504 | # Returns the users that should be cc'd when a new news is added | ||
def notified_watchers_for_added_news | ||||
watchers = [] | ||||
|
r12591 | if m = project.enabled_module('news') | ||
|
r13504 | watchers = m.notified_watchers | ||
|
r12591 | unless project.is_public? | ||
|
r13504 | watchers = watchers.select {|user| project.users.include?(user)} | ||
|
r12591 | end | ||
end | ||||
|
r13504 | watchers | ||
end | ||||
# Returns the email addresses that should be cc'd when a new news is added | ||||
def cc_for_added_news | ||||
notified_watchers_for_added_news.map(&:mail) | ||||
|
r12591 | end | ||
|
r330 | # returns latest news for projects visible by user | ||
|
r1908 | def self.latest(user = User.current, count = 5) | ||
|
r13184 | visible(user).preload(:author, :project).order("#{News.table_name}.created_on DESC").limit(count).to_a | ||
|
r12 | end | ||
|
r5699 | |||
|
r4883 | private | ||
|
r5699 | |||
|
r4883 | def add_author_as_watcher | ||
Watcher.create(:watchable => self, :user => author) | ||||
end | ||||
|
r11791 | |||
def send_notification | ||||
if Setting.notified_events.include?('news_added') | ||||
Mailer.news_added(self).deliver | ||||
end | ||||
end | ||||
|
r2 | end | ||