Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions app/models/concerns/labelable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

#-- 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.
#++

module Labelable
extend ActiveSupport::Concern

included do
has_many :labelings, as: :labelable, dependent: :delete_all
has_many :labels, -> { order(:id) }, through: :labelings

scope :labeled_with, ->(label) { joins(:labelings).where(labelings: { label_id: label }) }
end
end
45 changes: 45 additions & 0 deletions app/models/label.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

#-- 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.
#++

class Label < ApplicationRecord
belongs_to :author, class_name: "User"
has_many :labelings, dependent: :delete_all

scope :with_usage_count, -> {
left_joins(:labelings)
.select("labels.*, COUNT(labelings.id) AS usage_count")
.group(:id)
}

validates :name,
presence: true,
uniqueness: { case_sensitive: false },
length: { maximum: 255 }
end
36 changes: 36 additions & 0 deletions app/models/labeling.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

#-- 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.
#++

class Labeling < ApplicationRecord
belongs_to :label
belongs_to :labelable, polymorphic: true

validates :label_id, uniqueness: { scope: %i[labelable_type labelable_id] }
end
1 change: 1 addition & 0 deletions app/models/work_package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class WorkPackage < ApplicationRecord
include ::Scopes::Scoped
include HasMembers
include Remindable
include Labelable

include OpenProject::Journal::AttachmentHelper

Expand Down
1 change: 1 addition & 0 deletions config/initializers/replace_references_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"PersistedQuery" => :principal_id,
"PersistedView" => :principal_id,
"::Doorkeeper::Application" => :owner_id,
"Label" => :author_id,
"Message" => :author_id,
"News" => :author_id,
"::Notification" => :actor_id,
Expand Down
50 changes: 50 additions & 0 deletions db/migrate/20260914120000_create_labels.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

#-- 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.
#++

class CreateLabels < ActiveRecord::Migration[8.1]
def change
create_table :labels do |t|
t.string :name, null: false
t.references :author, null: false, foreign_key: { to_table: :users }
t.timestamps
end
add_index :labels, "LOWER(name)", unique: true, name: "index_labels_on_LOWER_name"

create_table :labelings do |t|
t.references :label, null: false, foreign_key: { on_delete: :cascade }, index: false
t.references :labelable, polymorphic: true, null: false, index: false
t.timestamps
end
add_index :labelings, %i[labelable_type labelable_id label_id],
unique: true,
name: "index_labelings_on_labelable_and_label"
add_index :labelings, :label_id
end
end
4 changes: 2 additions & 2 deletions modules/bim/app/services/bim/bcf/issues/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ def use_work_package(links:, params:)

::WorkPackages::UpdateService
.new(user:, model: work_package)
.call(**params)
.call(**params.except(*Bim::Bcf::Issue::SETTABLE_ATTRIBUTES))
Comment thread
akabiru marked this conversation as resolved.
end

def create_work_package(params)
::WorkPackages::CreateService
.new(user:)
.call(**params)
.call(**params.except(*Bim::Bcf::Issue::SETTABLE_ATTRIBUTES))
end

def work_package_identifier_from_links(links)
Expand Down
2 changes: 1 addition & 1 deletion modules/bim/app/services/bim/bcf/issues/update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def before_perform(service_result)
.new(model: model.work_package,
user:,
contract_class: ::WorkPackages::UpdateContract)
.call(**params)
.call(**params.except(*Bim::Bcf::Issue::SETTABLE_ATTRIBUTES))

if wp_call.success?
self.params = params.slice(*Bim::Bcf::Issue::SETTABLE_ATTRIBUTES)
Expand Down
36 changes: 36 additions & 0 deletions spec/factories/label_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

#-- 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.
#++

FactoryBot.define do
factory :label do
sequence(:name) { "Label #{it}" }
author factory: :user
end
end
36 changes: 36 additions & 0 deletions spec/factories/labeling_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

#-- 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.
#++

FactoryBot.define do
factory :labeling do
label
labelable factory: :work_package
end
end
73 changes: 73 additions & 0 deletions spec/models/label_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

#-- 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.
#++

require "spec_helper"

RSpec.describe Label do
describe "validations" do
subject { build(:label) }

it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_length_of(:name).is_at_most(255) }
it { is_expected.to validate_uniqueness_of(:name).case_insensitive }
it { is_expected.to belong_to(:author).class_name("User") }

it "is backed by a case-insensitive unique index" do
existing = create(:label, name: "hello")
duplicate = { name: "HELLO", author_id: existing.author_id, created_at: Time.current, updated_at: Time.current }

expect { described_class.insert_all!([duplicate]) }
.to raise_error(ActiveRecord::RecordNotUnique)
end
end

describe ".with_usage_count" do
it "counts the labelings of each label, including unused ones" do
used = create(:label)
unused = create(:label)
create_list(:labeling, 2, label: used)

counts = described_class.with_usage_count.index_by(&:id).transform_values(&:usage_count)

expect(counts).to eq(used.id => 2, unused.id => 0)
end
end

describe "#destroy" do
it "removes its labelings" do
label = create(:label)
labeling = create(:labeling, label:)

label.destroy!

expect(Labeling.where(id: labeling.id)).not_to exist
end
end
end
Loading
Loading