diff --git a/app/models/sorted_column.rb b/app/models/sorted_column.rb new file mode 100644 index 000000000..ad96d7ac6 --- /dev/null +++ b/app/models/sorted_column.rb @@ -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 diff --git a/spec/models/sorted_column_spec.rb b/spec/models/sorted_column_spec.rb new file mode 100644 index 000000000..0cd0a49b8 --- /dev/null +++ b/spec/models/sorted_column_spec.rb @@ -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