From 305b31e53b3da4ebf60a831132d7613b8a9dbfd1 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 23 Sep 2024 10:35:00 +0200 Subject: [PATCH] add sorted_column --- app/models/sorted_column.rb | 16 ++++++++++++++++ spec/models/sorted_column_spec.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 app/models/sorted_column.rb create mode 100644 spec/models/sorted_column_spec.rb 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