add sorted_column
This commit is contained in:
parent
693629afc8
commit
305b31e53b
2 changed files with 44 additions and 0 deletions
16
app/models/sorted_column.rb
Normal file
16
app/models/sorted_column.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class SortedColumn
|
||||
attr_reader :column
|
||||
|
||||
def initialize(column:, order:)
|
||||
@column = column
|
||||
@order = order
|
||||
end
|
||||
|
||||
def ascending? = @order == 'asc'
|
||||
|
||||
def ==(other)
|
||||
other&.column == column && other.order == order
|
||||
end
|
||||
end
|
28
spec/models/sorted_column_spec.rb
Normal file
28
spec/models/sorted_column_spec.rb
Normal file
|
@ -0,0 +1,28 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
describe SortedColumn do
|
||||
let(:column) { Column.new(procedure_id: 1, table: 'table', column: 'column') }
|
||||
let(:sorted_column) { SortedColumn.new(column: column, order: 'asc') }
|
||||
|
||||
describe '==' do
|
||||
it 'returns true for the same sorted column' do
|
||||
other = SortedColumn.new(column: column, order: 'asc')
|
||||
expect(sorted_column == other).to eq(true)
|
||||
end
|
||||
|
||||
it 'returns false if the order is different' do
|
||||
other = SortedColumn.new(column: column, order: 'desc')
|
||||
expect(sorted_column == other).to eq(false)
|
||||
end
|
||||
|
||||
it 'returns false if the column is different' do
|
||||
other_column = Column.new(procedure_id: 1, table: 'table', column: 'other')
|
||||
other = SortedColumn.new(column: other_column, order: 'asc')
|
||||
expect(sorted_column == other).to eq(false)
|
||||
end
|
||||
|
||||
it 'returns false if the other is nil' do
|
||||
expect(sorted_column == nil).to eq(false)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue