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

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