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.

wiki_content.rb 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 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. require 'zlib'
  19. class WikiContent < ApplicationRecord
  20. self.locking_column = 'version'
  21. belongs_to :page, :class_name => 'WikiPage'
  22. belongs_to :author, :class_name => 'User'
  23. has_many :versions, :class_name => 'WikiContentVersion', :dependent => :delete_all
  24. acts_as_mentionable :attributes => ['text']
  25. validates_presence_of :text
  26. validates_length_of :comments, :maximum => 1024, :allow_nil => true
  27. after_save :create_version
  28. after_create_commit :send_notification_create
  29. after_update_commit :send_notification_update
  30. scope :without_text, lambda {select(:id, :page_id, :version, :updated_on)}
  31. def initialize(*args)
  32. super
  33. if new_record?
  34. self.version = 1
  35. end
  36. end
  37. def visible?(user=User.current)
  38. page.visible?(user)
  39. end
  40. def project
  41. page.project
  42. end
  43. def attachments
  44. page.nil? ? [] : page.attachments
  45. end
  46. def notified_users
  47. project.notified_users.reject {|user| !visible?(user)}
  48. end
  49. # Returns the mail addresses of users that should be notified
  50. def recipients
  51. notified_users.collect(&:mail)
  52. end
  53. # Return true if the content is the current page content
  54. def current_version?
  55. true
  56. end
  57. # Reverts the record to a previous version
  58. def revert_to!(version)
  59. if version.wiki_content_id == id
  60. update_columns(
  61. :author_id => version.author_id,
  62. :text => version.text,
  63. :comments => version.comments,
  64. :version => version.version,
  65. :updated_on => version.updated_on
  66. ) && reload
  67. end
  68. end
  69. private
  70. def create_version
  71. versions << WikiContentVersion.new(attributes.except("id"))
  72. end
  73. # Notifies users that a wiki page was created
  74. def send_notification_create
  75. if Setting.notified_events.include?('wiki_content_added')
  76. Mailer.deliver_wiki_content_added(self)
  77. end
  78. end
  79. # Notifies users that a wiki page was updated
  80. def send_notification_update
  81. if Setting.notified_events.include?('wiki_content_updated') && saved_change_to_text?
  82. Mailer.deliver_wiki_content_updated(self)
  83. end
  84. end
  85. end