You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

news.rb 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class News < ActiveRecord::Base
  18. include Redmine::SafeAttributes
  19. belongs_to :project
  20. belongs_to :author, :class_name => 'User'
  21. has_many :comments, lambda {order("created_on")}, :as => :commented, :dependent => :delete_all
  22. validates_presence_of :title, :description
  23. validates_length_of :title, :maximum => 60
  24. validates_length_of :summary, :maximum => 255
  25. acts_as_attachable :edit_permission => :manage_news,
  26. :delete_permission => :manage_news
  27. acts_as_searchable :columns => ['title', 'summary', "#{table_name}.description"],
  28. :preload => :project
  29. acts_as_event :url => Proc.new {|o| {:controller => 'news', :action => 'show', :id => o.id}}
  30. acts_as_activity_provider :scope => preload(:project, :author),
  31. :author_key => :author_id
  32. acts_as_watchable
  33. after_create :add_author_as_watcher
  34. after_create_commit :send_notification
  35. scope :visible, lambda {|*args|
  36. joins(:project).
  37. where(Project.allowed_to_condition(args.shift || User.current, :view_news, *args))
  38. }
  39. safe_attributes 'title', 'summary', 'description'
  40. def visible?(user=User.current)
  41. !user.nil? && user.allowed_to?(:view_news, project)
  42. end
  43. # Returns true if the news can be commented by user
  44. def commentable?(user=User.current)
  45. user.allowed_to?(:comment_news, project)
  46. end
  47. def notified_users
  48. project.users.select {|user| user.notify_about?(self) && user.allowed_to?(:view_news, project)}
  49. end
  50. def recipients
  51. notified_users.map(&:mail)
  52. end
  53. # Returns the users that should be cc'd when a new news is added
  54. def notified_watchers_for_added_news
  55. watchers = []
  56. if m = project.enabled_module('news')
  57. watchers = m.notified_watchers
  58. unless project.is_public?
  59. watchers = watchers.select {|user| project.users.include?(user)}
  60. end
  61. end
  62. watchers
  63. end
  64. # Returns the email addresses that should be cc'd when a new news is added
  65. def cc_for_added_news
  66. notified_watchers_for_added_news.map(&:mail)
  67. end
  68. # returns latest news for projects visible by user
  69. def self.latest(user = User.current, count = 5)
  70. visible(user).preload(:author, :project).order("#{News.table_name}.created_on DESC").limit(count).to_a
  71. end
  72. private
  73. def add_author_as_watcher
  74. Watcher.create(:watchable => self, :user => author)
  75. end
  76. def send_notification
  77. if Setting.notified_events.include?('news_added')
  78. Mailer.deliver_news_added(self)
  79. end
  80. end
  81. end