Skip to content

Refactor deletion of dangling labels #1606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 15, 2024
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
4 changes: 4 additions & 0 deletions app/models/label.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ def to_h
}.compact
end

def to_s
name
end

def self.ransackable_attributes(_auth_object = nil)
%w[name]
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Task < ApplicationRecord # rubocop:disable Metrics/ClassLength
has_many :groups, through: :group_tasks

has_many :task_labels, dependent: :destroy
has_many :labels, through: :task_labels
has_many :labels, through: :task_labels, dependent: :destroy

has_many :tests, dependent: :destroy, inverse_of: :task
has_many :model_solutions, dependent: :destroy, inverse_of: :task
Expand Down Expand Up @@ -226,7 +226,7 @@ def all_files(cached: true)

def label_names=(label_names)
self.labels = label_names.uniq.compact_blank.map {|name| Label.find_or_initialize_by(name:) }
Label.where.not(id: TaskLabel.select(:label_id)).destroy_all
# Labels without tasks will be deleted as part of `TaskLabel#remove_dangling_labels`.
end

def label_names
Expand Down
6 changes: 6 additions & 0 deletions app/models/task_label.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@
class TaskLabel < ApplicationRecord
belongs_to :task
belongs_to :label

after_destroy :remove_dangling_labels

def remove_dangling_labels
label.destroy if label.task_labels.empty?
end
end
13 changes: 10 additions & 3 deletions spec/controllers/tasks_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,8 @@

before { sign_in user }

let!(:existing_label) { create(:label) }
let!(:new_label) { create(:label) }
let(:existing_label) { create(:label) }
let(:new_label) { create(:label) }

let!(:task) { create(:task, valid_attributes) }
let(:valid_attributes) do
Expand Down Expand Up @@ -428,16 +428,23 @@
end

it 'does not create a new label' do
create(:task, title: 'An existing task with the new label', labels: [new_label])
old_labels = Label.all.to_a
put_update
expect(Label.all.to_a.difference(old_labels)).to be_empty
end

it 'does delete the unused label' do
it 'deletes unused labels' do
put_update
expect(Label.all).to not_include(existing_label)
end

it 'does not delete any used label' do
create(:task, title: 'An existing task with the existing label', labels: [existing_label])
put_update
expect(Label.all).to include(existing_label)
end

context 'when requesting a new label to be created' do
let(:changed_attributes) { {label_names: [not_existing_label_name]} }
let(:not_existing_label_name) { 'some new label' }
Expand Down
9 changes: 9 additions & 0 deletions spec/models/label_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@
describe 'validations' do
it { is_expected.to validate_presence_of(:name) }
end

describe '#to_s' do
let(:label_name) { 'Test Label' }
let(:label) { described_class.new(name: label_name) }

it 'returns the label name' do
expect(label.to_s).to eq label_name
end
end
end
Loading