Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Move watchable under lib/open_project

Oliver Güntherdbc4fcc0b6c9
diff --git a/app/controllers/favorites_controller.rb b/app/controllers/favorites_controller.rb
new file mode 100644
index 00000000000..59591fb70d5
--- /dev/null
+++ b/app/controllers/favorites_controller.rb
@@ -0,0 +1,34 @@
+class AnnouncementsController < ApplicationController
+ layout "admin"
+
+ before_action :require_admin
+
+ def edit
+ @announcement = Announcement.only_one
+ end
+
+ def update
+ @announcement = Announcement.only_one
+ @announcement.attributes = announcement_params
+
+ if @announcement.save
+ flash[:notice] = t(:notice_successful_update)
+ end
+
+ redirect_to action: "edit"
+ end
+
+ private
+
+ def default_breadcrumb
+ t(:label_announcement)
+ end
+
+ def show_local_breadcrumb
+ true
+ end
+
+ def announcement_params
+ params.require(:announcement).permit("text", "show_until", "active")
+ end
+end
diff --git a/app/controllers/watchers_controller.rb b/app/controllers/watchers_controller.rb
index a02bf428ae2..af71ecb1556 100644
--- a/app/controllers/watchers_controller.rb
+++ b/app/controllers/watchers_controller.rb
@@ -46,15 +46,10 @@ class WatchersController < ApplicationController
private
def find_watched_by_object
- klass = params[:object_type].singularize.camelcase.constantize
-
- return false unless klass.respond_to?(:watched_by) and
- klass.ancestors.include? Redmine::Acts::Watchable and
- params[:object_id].to_s =~ /\A\d+\z/
-
- unless @watched = klass.find(params[:object_id])
- render_404
- end
+ model_name = params[:object_type]
+ klass = ::OpenProject::Acts::Watchable::Registry.instance(model_name)
+ @watched = klass&.find(params[:object_id])
+ render_404 unless @watched
end
def find_project
diff --git a/app/models/application_record.rb b/app/models/application_record.rb
index 98973520079..5e061f47287 100644
--- a/app/models/application_record.rb
+++ b/app/models/application_record.rb
@@ -1,4 +1,6 @@
class ApplicationRecord < ActiveRecord::Base
+ include ::OpenProject::Acts::Watchable
+
self.abstract_class = true
##
diff --git a/app/models/favorite.rb b/app/models/favorite.rb
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/config/initializers/acts_as_watchable.rb b/config/initializers/acts_as_watchable.rb
new file mode 100644
index 00000000000..1b95b09d305
--- /dev/null
+++ b/config/initializers/acts_as_watchable.rb
@@ -0,0 +1,10 @@
+# Be sure to restart your server when you modify this file.
+
+# In development mode, we need to register the acts_as_watchable models manually
+# as no eager loading takes place
+Rails.application.config.after_initialize do
+ if Rails.env.development?
+ OpenProject::Acts::Watchable::Registry
+ .add(WorkPackage, Message, Forum, News, Meeting, Wiki, WikiPage)
+ end
+end
diff --git a/config/routes.rb b/config/routes.rb
index 202af54fff3..04755d93ae1 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -167,9 +167,7 @@ Rails.application.routes.draw do
resource :wiki_menu_item, only: %i[edit update]
end
- # generic route for adding/removing watchers.
- # Models declared as acts_as_watchable will be automatically added to
- # OpenProject::Acts::Watchable::Routes.watched
+ # generic route for adding/removing watchers and favorites.
scope ":object_type/:object_id", constraints: OpenProject::Acts::Watchable::Routes do
post "/watch" => "watchers#watch"
delete "/unwatch" => "watchers#unwatch"
diff --git a/lib_static/plugins/acts_as_watchable/lib/acts_as_watchable.rb b/lib_static/open_project/acts/favorable.rb
similarity index 91%
rename from lib_static/plugins/acts_as_watchable/lib/acts_as_watchable.rb
rename to lib_static/open_project/acts/favorable.rb
index efa6d55a823..a1fe27d2fb0 100644
--- a/lib_static/plugins/acts_as_watchable/lib/acts_as_watchable.rb
+++ b/lib_static/open_project/acts/favorable.rb
@@ -26,10 +26,9 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-# ActsAsWatchable
-module Redmine
+module OpenProject
module Acts
- module Watchable
+ module Favorable
def self.included(base)
base.extend ClassMethods
end
@@ -53,7 +52,7 @@ module Redmine
# is allowed to watch
def acts_as_watchable(options = {})
- return if included_modules.include?(Redmine::Acts::Watchable::InstanceMethods)
+ return if included_modules.include?(::OpenProject::Acts::Watchable::InstanceMethods)
acts_as_watchable_enforce_project_association
@@ -71,7 +70,7 @@ module Redmine
self.acts_as_watchable_options = options
end
- send :prepend, Redmine::Acts::Watchable::InstanceMethods
+ send :prepend, OpenProject::OpenProject::Acts::Watchable::InstanceMethods
end
def acts_as_watchable_enforce_project_association
@@ -112,10 +111,10 @@ module Redmine
active_scope = Principal.not_locked.user
allowed_scope = if project.public?
- User.allowed(self.class.acts_as_watchable_permission, project)
- else
- User.allowed_members_on_work_package(self.class.acts_as_watchable_permission, self)
- end
+ User.allowed(self.class.acts_as_watchable_permission, project)
+ else
+ User.allowed_members_on_work_package(self.class.acts_as_watchable_permission, self)
+ end
active_scope.where(id: allowed_scope)
end
@@ -158,7 +157,7 @@ module Redmine
def watched_by?(user)
user.present? &&
((watchers.loaded? && watchers.map(&:user_id).any? { |uid| uid == user.id }) ||
- watcher_user_ids.any? { |uid| uid == user.id })
+ watcher_user_ids.any? { |uid| uid == user.id })
end
module ClassMethods
diff --git a/lib_static/open_project/acts/watchable.rb b/lib_static/open_project/acts/watchable.rb
new file mode 100644
index 00000000000..a6c963aa6f5
--- /dev/null
+++ b/lib_static/open_project/acts/watchable.rb
@@ -0,0 +1,174 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# 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.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+require_relative "watchable/registry"
+require_relative "watchable/routes"
+
+module OpenProject
+ module Acts
+ module Watchable
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ module ClassMethods
+ # Marks an ActiveRecord::Model as watchable
+ # A watchable model has association with users (watchers) who wish to
+ # be informed of changes on it.
+ #
+ # This also creates the routes necessary for watching/unwatching by
+ # adding the model's name to routes. This e.g leads to the following
+ # routes when marking issues as watchable:
+ # POST: issues/1/watch
+ # DELETE: issues/1/unwatch
+ # GET/POST: issues/1/watchers/new
+ # DELETE: issues/1/watchers/1
+ #
+ # params:
+ # options:
+ # permission: overrides the permission used to determine whether a user
+ # is allowed to watch
+
+ def acts_as_watchable(options = {})
+ return if included_modules.include?(::OpenProject::Acts::Watchable::InstanceMethods)
+
+ acts_as_watchable_enforce_project_association
+
+ class_eval do
+ has_many :watchers, as: :watchable, dependent: :delete_all, validate: false
+ has_many :watcher_users, through: :watchers, source: :user, validate: false
+
+ scope :watched_by, ->(user_id) {
+ includes(:watchers)
+ .where(watchers: { user_id: })
+ }
+
+ class_attribute :acts_as_watchable_options
+
+ self.acts_as_watchable_options = options
+ ::OpenProject::Acts::Watchable::Registry.add(self)
+ end
+
+ send :prepend, ::OpenProject::Acts::Watchable::InstanceMethods
+ end
+
+ def acts_as_watchable_enforce_project_association
+ unless reflect_on_association(:project)
+ message = <<-MESSAGE
+
+ The #{self} model does not have an association to the Project model.
+
+ acts_as_watchable requires the including model to have such an association.
+
+ If no direct association exists, consider adding a
+ has_one :project, through: ...
+ association.
+ MESSAGE
+ raise message
+ end
+ end
+ end
+
+ module InstanceMethods
+ def self.prepended(base)
+ base.extend ClassMethods
+ end
+
+ def possible_watcher?(user)
+ user.allowed_based_on_permission_context?(self.class.acts_as_watchable_permission,
+ project:,
+ entity: self)
+ end
+
+ # Returns all users that could potentially be watchers.
+ # This includes those already added as watchers.
+ #
+ # Admins are excluded at least for non public projects
+ # because while they have the right to be added as watchers having
+ # them pop up in every project would be weird.
+ def possible_watcher_users
+ active_scope = Principal.not_locked.user
+
+ allowed_scope = if project.public?
+ User.allowed(self.class.acts_as_watchable_permission, project)
+ else
+ User.allowed_members_on_work_package(self.class.acts_as_watchable_permission, self)
+ end
+
+ active_scope.where(id: allowed_scope)
+ end
+
+ # Returns an array of users that are proposed as watchers
+ def addable_watcher_users
+ possible_watcher_users.where.not(id: watcher_users.pluck(:id))
+ end
+
+ # Adds user as a watcher
+ def add_watcher(user)
+ watchers << Watcher.new(user:, watchable: self) unless watchers.map(&:user_id).include?(user.id)
+ end
+
+ # Removes user from the watchers list
+ def remove_watcher(user)
+ return nil unless user&.is_a?(User)
+
+ watchers_to_delete = watchers.find_all { |watcher| watcher.user == user }
+ watchers_to_delete.each(&:delete)
+ watchers.reload
+ watchers_to_delete.count
+ end
+
+ # Adds/removes watcher
+ def set_watcher(user, watching = true)
+ watching ? add_watcher(user) : remove_watcher(user)
+ end
+
+ # Overrides watcher_user_ids= to make user_ids uniq
+ def watcher_user_ids=(user_ids)
+ if user_ids.is_a?(Array)
+ user_ids = user_ids.uniq
+ end
+
+ super
+ end
+
+ # Returns true if object is watched by +user+
+ def watched_by?(user)
+ user.present? &&
+ ((watchers.loaded? && watchers.map(&:user_id).any? { |uid| uid == user.id }) ||
+ watcher_user_ids.any? { |uid| uid == user.id })
+ end
+
+ module ClassMethods
+ def acts_as_watchable_permission
+ acts_as_watchable_options[:permission] || :"view_#{name.underscore.pluralize}"
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib_static/plugins/acts_as_watchable/init.rb b/lib_static/open_project/acts/watchable/registry.rb
similarity index 64%
rename from lib_static/plugins/acts_as_watchable/init.rb
rename to lib_static/open_project/acts/watchable/registry.rb
index 0a2265a04af..c233cf1c325 100644
--- a/lib_static/plugins/acts_as_watchable/init.rb
+++ b/lib_static/open_project/acts/watchable/registry.rb
@@ -26,8 +26,32 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-# Include hook code here
-require File.dirname(__FILE__) + "/lib/acts_as_watchable"
-require File.dirname(__FILE__) + "/lib/acts_as_watchable/routes.rb"
+module OpenProject
+ module Acts
+ module Watchable
+ module Registry
+ def self.models
+ @models ||= Set.new
+ end
-ActiveRecord::Base.include Redmine::Acts::Watchable
+ def self.exists?(model)
+ models.include?(model)
+ end
+
+ def self.instance(model_name)
+ models.detect { |cls| cls.name == model_name }
+ end
+
+ def self.add(*models)
+ models.each do |model|
+ unless model.ancestors.include?(::OpenProject::Acts::Watchable)
+ raise ArgumentError.new("Model #{model} does not include acts_as_watchable")
+ end
+
+ self.models << model
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib_static/plugins/acts_as_watchable/lib/acts_as_watchable/routes.rb b/lib_static/open_project/acts/watchable/routes.rb
similarity index 81%
rename from lib_static/plugins/acts_as_watchable/lib/acts_as_watchable/routes.rb
rename to lib_static/open_project/acts/watchable/routes.rb
index 7874be8c495..d6dd092ba8b 100644
--- a/lib_static/plugins/acts_as_watchable/lib/acts_as_watchable/routes.rb
+++ b/lib_static/open_project/acts/watchable/routes.rb
@@ -25,7 +25,6 @@
#
# See COPYRIGHT and LICENSE files for more details.
#++
-
module OpenProject
module Acts
module Watchable
@@ -35,20 +34,9 @@ module OpenProject
def self.matches?(request)
params = request.path_parameters
- watched?(params[:object_type]) &&
+ Registry.exists?(params[:object_type]) &&
/\d+/.match(params[:object_id])
end
-
- def self.watched?(object)
- watchable_object? object
- end
-
- def self.watchable_object?(object)
- klass = object.to_s.classify.constantize
- klass.included_modules.include? Redmine::Acts::Watchable
- rescue StandardError
- false
- end
end
end
end
diff --git a/modules/meeting/spec/models/meeting_spec.rb b/modules/meeting/spec/models/meeting_spec.rb
index 7774235d9e5..5a34ce14fd0 100644
--- a/modules/meeting/spec/models/meeting_spec.rb
+++ b/modules/meeting/spec/models/meeting_spec.rb
@@ -196,7 +196,7 @@ RSpec.describe Meeting do
describe "acts_as_watchable" do
it "is watchable" do
- expect(described_class).to include(Redmine::Acts::Watchable::InstanceMethods)
+ expect(described_class).to include(::OpenProject::Acts::Watchable::InstanceMethods)
end
it "uses the :view_meetings permission" do
diff --git a/spec/lib/acts_as_watchable/lib/acts_as_watchable/routes_spec.rb b/spec/lib/acts_as_watchable/lib/acts_as_watchable/routes_spec.rb
index c089a1b3be9..bc6773c8c12 100644
--- a/spec/lib/acts_as_watchable/lib/acts_as_watchable/routes_spec.rb
+++ b/spec/lib/acts_as_watchable/lib/acts_as_watchable/routes_spec.rb
@@ -28,7 +28,7 @@
require "spec_helper"
-RSpec.describe OpenProject::Acts::Watchable::Routes do
+RSpec.describe ::OpenProject::Acts::Watchable::Routes do
let(:request) do
Struct.new(:type, :id) do
def path_parameters
@@ -43,7 +43,7 @@ RSpec.describe OpenProject::Acts::Watchable::Routes do
let(:id) { "1" }
it "is true" do
- expect(OpenProject::Acts::Watchable::Routes.matches?(request)).to be_truthy
+ expect(::OpenProject::Acts::Watchable::Routes.matches?(request)).to be_truthy
end
end
@@ -51,7 +51,7 @@ RSpec.describe OpenProject::Acts::Watchable::Routes do
let(:id) { "schmu" }
it "is false" do
- expect(OpenProject::Acts::Watchable::Routes.matches?(request)).to be_falsey
+ expect(::OpenProject::Acts::Watchable::Routes.matches?(request)).to be_falsey
end
end
end
@@ -69,7 +69,7 @@ RSpec.describe OpenProject::Acts::Watchable::Routes do
let(:id) { "4" }
it "is false" do
- expect(OpenProject::Acts::Watchable::Routes.matches?(request)).to be_falsey
+ expect(::OpenProject::Acts::Watchable::Routes.matches?(request)).to be_falsey
end
end
end
diff --git a/spec/routing/watchers_spec.rb b/spec/routing/watchers_spec.rb
index fb674bfb7e6..20bfba20a9f 100644
--- a/spec/routing/watchers_spec.rb
+++ b/spec/routing/watchers_spec.rb
@@ -31,7 +31,7 @@ require "spec_helper"
RSpec.describe WatchersController do
shared_examples_for "watched model routes" do
before do
- expect(OpenProject::Acts::Watchable::Routes).to receive(:matches?).and_return(true)
+ expect(::OpenProject::Acts::Watchable::Routes).to receive(:matches?).and_return(true)
end
it "connects POST /:object_type/:object_id/watch to watchers#watch" do