Commit dcc30fec authored by Brett Walker's avatar Brett Walker

Refactor first_board method into the Board model

and add proper specs
parent c8812965
......@@ -12,6 +12,7 @@ class Board < ApplicationRecord
scope :with_associations, -> { preload(:destroyable_lists) }
scope :order_by_name_asc, -> { order(arel_table[:name].lower.asc) }
scope :first_board, -> { where(id: self.order_by_name_asc.limit(1).select(:id)) }
def project_needed?
!group
......
......@@ -20,12 +20,9 @@ module Boards
parent.boards.order_by_name_asc
end
# rubocop: disable CodeReuse/ActiveRecord
def first_board
# We could use just one query but MySQL does not support nested queries using LIMIT
boards.where(id: boards.first).reorder(nil)
parent.boards.first_board
end
# rubocop: enable CodeReuse/ActiveRecord
def create_board!
Boards::CreateService.new(parent, current_user).execute
......
......@@ -3,6 +3,9 @@
require 'spec_helper'
describe Board do
let(:project) { create(:project) }
let(:other_project) { create(:project) }
describe 'relationships' do
it { is_expected.to belong_to(:project) }
it { is_expected.to have_many(:lists).order(list_type: :asc, position: :asc).dependent(:delete_all) }
......@@ -11,4 +14,28 @@ describe Board do
describe 'validations' do
it { is_expected.to validate_presence_of(:project) }
end
describe '#order_by_name_asc' do
let!(:second_board) { create(:board, name: 'Secondary board', project: project) }
let!(:first_board) { create(:board, name: 'First board', project: project) }
it 'returns in alphabetical order' do
expect(project.boards.order_by_name_asc).to eq [first_board, second_board]
end
end
describe '#first_board' do
let!(:other_board) { create(:board, name: 'Other board', project: other_project) }
let!(:second_board) { create(:board, name: 'Secondary board', project: project) }
let!(:first_board) { create(:board, name: 'First board', project: project) }
it 'return the first alphabetical board as a relation' do
expect(project.boards.first_board).to eq [first_board]
end
# BoardsActions#board expects this behavior
it 'raises an error when find is done on a non-existent record' do
expect { project.boards.first_board.find(second_board.id) }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
......@@ -32,7 +32,9 @@ end
shared_examples 'multiple boards list service' do
let(:service) { described_class.new(parent, double) }
let!(:boards) { create_list(:board, 3, resource_parent: parent) }
let!(:board_B) { create(:board, resource_parent: parent, name: 'B-board') }
let!(:board_c) { create(:board, resource_parent: parent, name: 'c-board') }
let!(:board_a) { create(:board, resource_parent: parent, name: 'a-board') }
describe '#execute' do
it 'returns all issue boards' do
......@@ -40,10 +42,7 @@ shared_examples 'multiple boards list service' do
end
it 'returns boards ordered by name' do
board_names = %w[B-board c-board a-board]
boards.each_with_index { |board, i| board.update_column(:name, board_names[i]) }
expect(service.execute.pluck(:name)).to eq(%w[a-board B-board c-board])
expect(service.execute).to eq [board_a, board_B, board_c]
end
end
end
Markdown is supported
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