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.3KB

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