news.rb
63 lines
| 2.4 KiB
| text/x-ruby
|
RubyLexer
|
r1908 | # Redmine - project management software | ||
|
r9453 | # Copyright (C) 2006-2012 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 | ||
belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' | ||||
has_many :comments, :as => :commented, :dependent => :delete_all, :order => "created_on" | ||||
|
r5699 | |||
|
r330 | validates_presence_of :title, :description | ||
|
r590 | validates_length_of :title, :maximum => 60 | ||
validates_length_of :summary, :maximum => 255 | ||||
|
r663 | |||
|
r8608 | acts_as_attachable :delete_permission => :manage_news | ||
|
r2552 | acts_as_searchable :columns => ['title', 'summary', "#{table_name}.description"], :include => :project | ||
|
r663 | acts_as_event :url => Proc.new {|o| {:controller => 'news', :action => 'show', :id => o.id}} | ||
|
r2064 | acts_as_activity_provider :find_options => {:include => [:project, :author]}, | ||
:author_key => :author_id | ||||
|
r4883 | acts_as_watchable | ||
|
r5699 | |||
|
r4883 | after_create :add_author_as_watcher | ||
|
r5699 | |||
|
r9355 | scope :visible, lambda {|*args| { | ||
|
r4391 | :include => :project, | ||
|
r5699 | :conditions => Project.allowed_to_condition(args.shift || User.current, :view_news, *args) | ||
|
r4391 | }} | ||
|
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 | ||||
|
r330 | # returns latest news for projects visible by user | ||
|
r1908 | def self.latest(user = User.current, count = 5) | ||
|
r9450 | visible(user).includes([:author, :project]).order("#{News.table_name}.created_on DESC").limit(count).all | ||
|
r12 | end | ||
|
r5699 | |||
|
r4883 | private | ||
|
r5699 | |||
|
r4883 | def add_author_as_watcher | ||
Watcher.create(:watchable => self, :user => author) | ||||
end | ||||
|
r2 | end | ||