annotate .svn/pristine/2d/2dbde491b7a799c190d49fd409652b2fbab77ca3.svn-base @ 1464:261b3d9a4903 redmine-2.4

Update to Redmine 2.4 branch rev 12663
author Chris Cannam
date Tue, 14 Jan 2014 14:37:42 +0000
parents
children
rev   line source
Chris@1464 1 # Redmine - project management software
Chris@1464 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@1464 3 #
Chris@1464 4 # This program is free software; you can redistribute it and/or
Chris@1464 5 # modify it under the terms of the GNU General Public License
Chris@1464 6 # as published by the Free Software Foundation; either version 2
Chris@1464 7 # of the License, or (at your option) any later version.
Chris@1464 8 #
Chris@1464 9 # This program is distributed in the hope that it will be useful,
Chris@1464 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1464 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1464 12 # GNU General Public License for more details.
Chris@1464 13 #
Chris@1464 14 # You should have received a copy of the GNU General Public License
Chris@1464 15 # along with this program; if not, write to the Free Software
Chris@1464 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1464 17
Chris@1464 18 class News < ActiveRecord::Base
Chris@1464 19 include Redmine::SafeAttributes
Chris@1464 20 belongs_to :project
Chris@1464 21 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
Chris@1464 22 has_many :comments, :as => :commented, :dependent => :delete_all, :order => "created_on"
Chris@1464 23
Chris@1464 24 validates_presence_of :title, :description
Chris@1464 25 validates_length_of :title, :maximum => 60
Chris@1464 26 validates_length_of :summary, :maximum => 255
Chris@1464 27
Chris@1464 28 acts_as_attachable :delete_permission => :manage_news
Chris@1464 29 acts_as_searchable :columns => ['title', 'summary', "#{table_name}.description"], :include => :project
Chris@1464 30 acts_as_event :url => Proc.new {|o| {:controller => 'news', :action => 'show', :id => o.id}}
Chris@1464 31 acts_as_activity_provider :find_options => {:include => [:project, :author]},
Chris@1464 32 :author_key => :author_id
Chris@1464 33 acts_as_watchable
Chris@1464 34
Chris@1464 35 after_create :add_author_as_watcher
Chris@1464 36
Chris@1464 37 scope :visible, lambda {|*args|
Chris@1464 38 includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_news, *args))
Chris@1464 39 }
Chris@1464 40
Chris@1464 41 safe_attributes 'title', 'summary', 'description'
Chris@1464 42
Chris@1464 43 def visible?(user=User.current)
Chris@1464 44 !user.nil? && user.allowed_to?(:view_news, project)
Chris@1464 45 end
Chris@1464 46
Chris@1464 47 # Returns true if the news can be commented by user
Chris@1464 48 def commentable?(user=User.current)
Chris@1464 49 user.allowed_to?(:comment_news, project)
Chris@1464 50 end
Chris@1464 51
Chris@1464 52 def recipients
Chris@1464 53 project.users.select {|user| user.notify_about?(self)}.map(&:mail)
Chris@1464 54 end
Chris@1464 55
Chris@1464 56 # returns latest news for projects visible by user
Chris@1464 57 def self.latest(user = User.current, count = 5)
Chris@1464 58 visible(user).includes([:author, :project]).order("#{News.table_name}.created_on DESC").limit(count).all
Chris@1464 59 end
Chris@1464 60
Chris@1464 61 private
Chris@1464 62
Chris@1464 63 def add_author_as_watcher
Chris@1464 64 Watcher.create(:watchable => self, :user => author)
Chris@1464 65 end
Chris@1464 66 end