Commit ffa73739 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre
Browse files

Rename Labels::CreateService to Labels::FindOrCreateService

parent f53cc60ac39d
......@@ -734,7 +734,7 @@
def create_labels
Label.templates.each do |label|
params = label.attributes.except('id', 'template', 'created_at', 'updated_at')
Labels::CreateService.new(owner, self, params).execute
Labels::FindOrCreateService.new(owner, self, params).execute
end
end
......
......@@ -19,7 +19,7 @@
end
def find_or_create_label(params)
::Labels::CreateService.new(current_user, project, params).execute
::Labels::FindOrCreateService.new(current_user, project, params).execute
end
def label_params
......
......@@ -88,7 +88,7 @@
return unless labels
params[:label_ids] = labels.split(',').map do |label_name|
service = Labels::CreateService.new(current_user, project, title: label_name.strip)
service = Labels::FindOrCreateService.new(current_user, project, title: label_name.strip)
label = service.execute
label.id
......
module Labels
class CreateService
def initialize(current_user, project, params = {})
@current_user = current_user
@group = project.group
@project = project
@params = params.dup
end
def execute
find_or_create_label
end
private
attr_reader :current_user, :group, :project, :params
def available_labels
@available_labels ||= LabelsFinder.new(current_user, project_id: project.id).execute
end
def find_or_create_label
new_label = available_labels.find_by(title: title)
new_label ||= project.labels.create(params)
new_label
end
def title
params[:title] || params[:name]
end
end
end
module Labels
class FindOrCreateService
def initialize(current_user, project, params = {})
@current_user = current_user
@group = project.group
@project = project
@params = params.dup
end
def execute
find_or_create_label
end
private
attr_reader :current_user, :group, :project, :params
def available_labels
@available_labels ||= LabelsFinder.new(current_user, project_id: project.id).execute
end
def find_or_create_label
new_label = available_labels.find_by(title: title)
new_label ||= project.labels.create(params)
new_label
end
def title
params[:title] || params[:name]
end
end
end
......@@ -43,7 +43,7 @@
def find_or_create_label!(label)
params = label.attributes.slice('title', 'description', 'color')
new_label = CreateService.new(current_user, project, params).execute
new_label = FindOrCreateService.new(current_user, project, params).execute
new_label.id
end
......
......@@ -75,7 +75,7 @@
def create_label(name)
params = { title: name, color: nice_label_color(name) }
::Labels::CreateService.new(project.owner, project, params).execute
::Labels::FindOrCreateService.new(project.owner, project, params).execute
end
def user_info(person_id)
......
......@@ -15,7 +15,7 @@
def create!
params = attributes.except(:project)
service = ::Labels::CreateService.new(project.owner, project, params)
service = ::Labels::FindOrCreateService.new(project.owner, project, params)
label = service.execute
raise ActiveRecord::RecordInvalid.new(label) unless label.persisted?
......
......@@ -234,7 +234,7 @@
def create_label(name)
params = { name: name, color: nice_label_color(name) }
::Labels::CreateService.new(project.owner, project, params).execute
::Labels::FindOrCreateService.new(project.owner, project, params).execute
end
def format_content(raw_content)
......
......@@ -19,7 +19,7 @@
]
labels.each do |params|
::Labels::CreateService.new(project.owner, project).execute(params)
::Labels::FindOrCreateService.new(project.owner, project).execute(params)
end
end
end
......
require 'spec_helper'
describe Labels::CreateService, services: true do
describe '#execute' do
let(:user) { create(:user) }
let(:group) { create(:group) }
let(:project) { create(:project, namespace: group) }
let(:params) do
{
title: 'Security',
description: 'Security related stuff.',
color: '#FF0000'
}
end
subject(:service) { described_class.new(user, project, params) }
before do
project.team << [user, :developer]
end
context 'when label does not exist at group level' do
it 'creates a new label at project level' do
expect { service.execute }.to change(project.labels, :count).by(1)
end
end
context 'when label exists at group level' do
it 'returns the group label' do
group_label = create(:group_label, group: group, title: 'Security')
expect(service.execute).to eq group_label
end
end
context 'when label does not exist at group level' do
it 'creates a new label at project leve' do
expect { service.execute }.to change(project.labels, :count).by(1)
end
end
context 'when label exists at project level' do
it 'returns the project label' do
project_label = create(:label, project: project, title: 'Security')
expect(service.execute).to eq project_label
end
end
end
end
require 'spec_helper'
describe Labels::FindOrCreateService, services: true do
describe '#execute' do
let(:user) { create(:user) }
let(:group) { create(:group) }
let(:project) { create(:project, namespace: group) }
let(:params) do
{
title: 'Security',
description: 'Security related stuff.',
color: '#FF0000'
}
end
subject(:service) { described_class.new(user, project, params) }
before do
project.team << [user, :developer]
end
context 'when label does not exist at group level' do
it 'creates a new label at project level' do
expect { service.execute }.to change(project.labels, :count).by(1)
end
end
context 'when label exists at group level' do
it 'returns the group label' do
group_label = create(:group_label, group: group, title: 'Security')
expect(service.execute).to eq group_label
end
end
context 'when label does not exist at group level' do
it 'creates a new label at project leve' do
expect { service.execute }.to change(project.labels, :count).by(1)
end
end
context 'when label exists at project level' do
it 'returns the project label' do
project_label = create(:label, project: project, title: 'Security')
expect(service.execute).to eq project_label
end
end
end
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment