summaryrefslogtreecommitdiffstats
path: root/app/models/wiki_content.rb
blob: 7fa61560ffe69a2da1daeff463954514c3602593 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# frozen_string_literal: true

# Redmine - project management software
# Copyright (C) 2006-2022  Jean-Philippe Lang
#
# 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.
#
# 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.
#
# 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.

require 'zlib'

class WikiContent < ActiveRecord::Base
  self.locking_column = 'version'
  belongs_to :page, :class_name => 'WikiPage'
  belongs_to :author, :class_name => 'User'
  has_many :versions, :class_name => 'WikiContentVersion', :dependent => :delete_all

  acts_as_mentionable :attributes => ['text']

  validates_presence_of :text
  validates_length_of :comments, :maximum => 1024, :allow_nil => true

  after_save :create_version
  after_create_commit :send_notification_create
  after_update_commit :send_notification_update

  scope :without_text, lambda {select(:id, :page_id, :version, :updated_on)}

  def initialize(*args)
    super
    if new_record?
      self.version = 1
    end
  end

  def visible?(user=User.current)
    page.visible?(user)
  end

  def project
    page.project
  end

  def attachments
    page.nil? ? [] : page.attachments
  end

  def notified_users
    project.notified_users.reject {|user| !visible?(user)}
  end

  # Returns the mail addresses of users that should be notified
  def recipients
    notified_users.collect(&:mail)
  end

  # Return true if the content is the current page content
  def current_version?
    true
  end

  # Reverts the record to a previous version
  def revert_to!(version)
    if version.wiki_content_id == id
      update_columns(
        :author_id => version.author_id,
        :text => version.text,
        :comments => version.comments,
        :version => version.version,
        :updated_on => version.updated_on
      ) && reload
    end
  end

  private

  def create_version
    versions << WikiContentVersion.new(attributes.except("id"))
  end

  # Notifies users that a wiki page was created
  def send_notification_create
    if Setting.notified_events.include?('wiki_content_added')
      Mailer.deliver_wiki_content_added(self)
    end
  end

  # Notifies users that a wiki page was updated
  def send_notification_update
    if Setting.notified_events.include?('wiki_content_updated') && saved_change_to_text?
      Mailer.deliver_wiki_content_updated(self)
    end
  end
end