Skip to content

Commit

Permalink
Fix replacing references of presenter_id
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverguenther committed Sep 22, 2024
1 parent f7d7a19 commit 1f4d3a3
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 23 deletions.
11 changes: 10 additions & 1 deletion app/services/principals/replace_references_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def rewrite_active_models(from, to)
rewrite_actor(from, to)
rewrite_owner(from, to)
rewrite_logged_by(from, to)
rewrite_presenter(from, to)
end

def rewrite_custom_value(from, to)
Expand Down Expand Up @@ -135,12 +136,20 @@ def rewrite_logged_by(from, to)
end
end

def rewrite_presenter(from, to)
[
MeetingAgendaItem
].each do |klass|
rewrite(klass, :presenter_id, from, to)
end
end

def journal_classes
[Journal] + Journal::BaseJournal.subclasses
end

def foreign_keys
%w[author_id user_id assigned_to_id responsible_id logged_by_id]
%w[author_id user_id assigned_to_id responsible_id logged_by_id presenter_id]
end

def rewrite(klass, attribute, from, to)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,43 @@

require "spec_helper"
require_module_spec_helper
require Rails.root.join('spec/services/principals/replace_references_context')

Check notice on line 33 in modules/meeting/spec/services/principals/replace_references_service_call_integration_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] modules/meeting/spec/services/principals/replace_references_service_call_integration_spec.rb#L33 <Style/StringLiterals>

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Raw output
modules/meeting/spec/services/principals/replace_references_service_call_integration_spec.rb:33:25: C: Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
subject(:service_call) { instance.call(from: principal, to: to_principal) }

shared_let(:other_user) { create(:user) }
shared_let(:principal) { create(:user) }
shared_let(:to_principal) { create(:user) }

subject(:service_call) { instance.call(from: principal, to: to_principal) }

let(:instance) do
described_class.new
end

shared_examples "replaces the creator" do
before do
model
end

it "is successful" do
expect(service_call)
.to be_success
context "with MeetingAgendaItem" do
it_behaves_like "rewritten record",
:meeting_agenda_item,
:author_id do
let(:attributes) do
{
author_id: principal.id,
created_at: "NOW()",
updated_at: "NOW()"
}
end
end

it "replaces principal with to_principal" do
service_call
model.reload
it_behaves_like "rewritten record",
:meeting_agenda_item,
:presenter_id do
let(:attributes) do
{
presenter_id: principal.id,
created_at: "NOW()",
updated_at: "NOW()"
}

expect(model.author).to eql to_principal
end
end

context "with MeetingAgendaItem" do
it_behaves_like "replaces the creator" do
let(:model) { create(:meeting_agenda_item, author: principal) }
end

Check notice on line 69 in modules/meeting/spec/services/principals/replace_references_service_call_integration_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] modules/meeting/spec/services/principals/replace_references_service_call_integration_spec.rb#L68-L69 <Layout/EmptyLinesAroundBlockBody>

Extra empty line detected at block body end.
Raw output
modules/meeting/spec/services/principals/replace_references_service_call_integration_spec.rb:68:1: C: Layout/EmptyLinesAroundBlockBody: Extra empty line detected at block body end.
end
end
end
86 changes: 86 additions & 0 deletions spec/services/principals/replace_references_context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 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.
#++

RSpec.shared_examples_for "rewritten record" do |factory, attribute, format = Integer|
let!(:model) do
klass = FactoryBot.factories.find(factory).build_class
all_attributes = other_attributes.merge(attribute => principal_id)

inserted = ActiveRecord::Base.connection.select_one <<~SQL.squish
INSERT INTO #{klass.table_name}
(#{all_attributes.keys.join(', ')})
VALUES
(#{all_attributes.values.join(', ')})
RETURNING id
SQL

klass.find(inserted["id"])
end

let(:other_attributes) do
defined?(attributes) ? attributes : {}
end

def expected(user, format)
if format == String
user.id.to_s
else
user.id
end
end

context "for #{factory}" do
context "with the replaced user" do
let(:principal_id) { principal.id }

before do
service_call
model.reload
end

it "replaces #{attribute}" do
expect(model.send(attribute))
.to eql expected(to_principal, format)
end
end

context "with a different user" do
let(:principal_id) { other_user.id }

before do
service_call
model.reload
end

it "keeps #{attribute}" do
expect(model.send(attribute))
.to eql expected(other_user, format)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#++

require "spec_helper"
require_relative "replace_references_context"

RSpec.describe Principals::ReplaceReferencesService, "#call", type: :model do
subject(:service_call) { instance.call(from: principal, to: to_principal) }
Expand Down Expand Up @@ -330,8 +331,7 @@ def expected(user, format)

it_behaves_like "rewritten record",
:journal_wiki_page_journal,
:author_id do
end
:author_id
end

context "with WorkPackage" do
Expand Down

0 comments on commit 1f4d3a3

Please sign in to comment.