|
| 1 | +#-- encoding: UTF-8 |
| 2 | + |
| 3 | +#-- copyright |
| 4 | +# OpenProject is an open source project management software. |
| 5 | +# Copyright (C) 2012-2020 the OpenProject GmbH |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU General Public License version 3. |
| 9 | +# |
| 10 | +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
| 11 | +# Copyright (C) 2006-2017 Jean-Philippe Lang |
| 12 | +# Copyright (C) 2010-2013 the ChiliProject Team |
| 13 | +# |
| 14 | +# This program is free software; you can redistribute it and/or |
| 15 | +# modify it under the terms of the GNU General Public License |
| 16 | +# as published by the Free Software Foundation; either version 2 |
| 17 | +# of the License, or (at your option) any later version. |
| 18 | +# |
| 19 | +# This program is distributed in the hope that it will be useful, |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +# GNU General Public License for more details. |
| 23 | +# |
| 24 | +# You should have received a copy of the GNU General Public License |
| 25 | +# along with this program; if not, write to the Free Software |
| 26 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 27 | +# |
| 28 | +# See docs/COPYRIGHT.rdoc for more details. |
| 29 | +#++ |
| 30 | + |
| 31 | +class Journal::NotificationConfiguration |
| 32 | + class << self |
| 33 | + # Allows controlling whether notifications are sent out for created journals. |
| 34 | + # After the block is executed, the setting is returned to its original state which is true by default. |
| 35 | + # In case the method is called multiple times within itself, the first setting prevails. |
| 36 | + # This allows to control the setting globally without having to pass the setting down the call stack in |
| 37 | + # order to ensure all subsequent code follows the provided setting. |
| 38 | + def with(send_notifications, &block) |
| 39 | + if already_set? |
| 40 | + log_warning(send_notifications) |
| 41 | + yield |
| 42 | + else |
| 43 | + with_first(send_notifications, &block) |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + def active? |
| 48 | + active.value |
| 49 | + end |
| 50 | + |
| 51 | + protected |
| 52 | + |
| 53 | + def with_first(send_notifications) |
| 54 | + old_value = active? |
| 55 | + self.already_set = true |
| 56 | + |
| 57 | + self.active = send_notifications |
| 58 | + |
| 59 | + yield |
| 60 | + ensure |
| 61 | + self.active = old_value |
| 62 | + self.already_set = false |
| 63 | + end |
| 64 | + |
| 65 | + def log_warning(send_notifications) |
| 66 | + return if active == send_notifications |
| 67 | + |
| 68 | + message = <<~MSG |
| 69 | + Ignoring setting journal notifications to '#{send_notifications}' as a parent block already set it to #{active}" |
| 70 | + MSG |
| 71 | + Rails.logger.debug message |
| 72 | + end |
| 73 | + |
| 74 | + def active |
| 75 | + @active ||= Concurrent::ThreadLocalVar.new(true) |
| 76 | + end |
| 77 | + |
| 78 | + def already_set |
| 79 | + @already_set ||= Concurrent::ThreadLocalVar.new(false) |
| 80 | + end |
| 81 | + |
| 82 | + def already_set? |
| 83 | + already_set.value |
| 84 | + end |
| 85 | + |
| 86 | + def active=(value) |
| 87 | + @active.value = value |
| 88 | + end |
| 89 | + |
| 90 | + def already_set=(value) |
| 91 | + @already_set.value = value |
| 92 | + end |
| 93 | + end |
| 94 | +end |
0 commit comments