add storted_column_type

This commit is contained in:
simon lehericey 2024-09-25 17:41:58 +02:00
parent 9652cf78c3
commit 21533f91e3
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
5 changed files with 113 additions and 3 deletions

View file

@ -29,7 +29,8 @@ class ProcedurePresentation < ApplicationRecord
validate :check_filters_max_length
validate :check_filters_max_integer
attribute :sorted_column, :jsonb
attribute :sorted_column, :sorted_column
def sorted_column = super || procedure.default_sorted_column # Dummy override to set default value
attribute :a_suivre_filters, :jsonb, array: true
attribute :suivis_filters, :jsonb, array: true

View file

@ -0,0 +1,36 @@
# frozen_string_literal: true
class SortedColumnType < ActiveRecord::Type::Value
# form_input or setter -> type
def cast(value)
value = value.deep_symbolize_keys if value.respond_to?(:deep_symbolize_keys)
case value
in SortedColumn
value
in NilClass # default value
nil
# from form (id is a string) or from db (id is a hash)
in { order: 'asc'|'desc', id: String|Hash } => h
SortedColumn.new(column: ColumnType.new.cast(h[:id]), order: h[:order])
end
end
# db -> ruby
def deserialize(value) = cast(value&.then { JSON.parse(_1) })
# ruby -> db
def serialize(value)
case value
in NilClass
nil
in SortedColumn
JSON.generate({
id: value.column.h_id,
order: value.order
})
else
raise ArgumentError, "Invalid value for SortedColumn serialization: #{value}"
end
end
end

View file

@ -2,8 +2,10 @@
require Rails.root.join("app/types/column_type")
require Rails.root.join("app/types/export_item_type")
require Rails.root.join("app/types/sorted_column_type")
ActiveSupport.on_load(:active_record) do
ActiveRecord::Type.register(:column, ColumnType)
ActiveRecord::Type.register(:export_item, ExportItemType)
ActiveRecord::Type.register(:sorted_column, SortedColumnType)
end

View file

@ -45,10 +45,10 @@ describe '20240920130741_migrate_procedure_presentation_to_columns.rake' do
order, column_id = procedure_presentation
.sorted_column
.then { |sorted| [sorted['order'], sorted['id']] }
.then { |sorted| [sorted.order, sorted.column.h_id] }
expect(order).to eq('desc')
expect(column_id).to eq("procedure_id" => procedure_id, "column_id" => "self/en_construction_at")
expect(column_id).to eq(procedure_id: procedure_id, column_id: "self/en_construction_at")
expect(procedure_presentation.tous_filters).to eq([])

View file

@ -0,0 +1,71 @@
# frozen_string_literal: true
describe SortedColumnType do
let(:type) { SortedColumnType.new }
describe 'cast' do
it 'from SortedColumn' do
column = Column.new(procedure_id: 1, table: 'table', column: 'column')
sorted_column = SortedColumn.new(column:, order: 'asc')
expect(type.cast(sorted_column)).to eq(sorted_column)
end
it 'from nil' do
expect(type.cast(nil)).to eq(nil)
end
describe 'from form' do
it 'with valid column id' do
column = Column.new(procedure_id: 1, table: 'table', column: 'column')
h = { order: 'asc', id: column.id }
expect(Column).to receive(:find).with(column.h_id).and_return(column)
expect(type.cast(h)).to eq(SortedColumn.new(column: column, order: 'asc'))
end
it 'with invalid column id' do
h = { order: 'asc', id: 'invalid' }
expect { type.cast(h) }.to raise_error(JSON::ParserError)
h = { order: 'asc', id: { procedure_id: 'invalid', column_id: 'nop' }.to_json }
expect { type.cast(h) }.to raise_error(ActiveRecord::RecordNotFound)
end
it 'with invalid order' do
column = Column.new(procedure_id: 1, table: 'table', column: 'column')
h = { order: 'invalid', id: column.id }
expect { type.cast(h) }.to raise_error(NoMatchingPatternError)
end
end
end
describe 'deserialize' do
context 'with valid value' do
it 'works' do
column = Column.new(procedure_id: 1, table: 'table', column: 'column')
expect(Column).to receive(:find).with(column.h_id).and_return(column)
expect(type.deserialize({ id: column.h_id, order: 'asc' }.to_json)).to eq(SortedColumn.new(column: column, order: 'asc'))
end
end
context 'with nil' do
it { expect(type.deserialize(nil)).to eq(nil) }
end
end
describe 'serialize' do
it 'with SortedColumn' do
column = Column.new(procedure_id: 1, table: 'table', column: 'column')
sorted_column = SortedColumn.new(column: column, order: 'asc')
expect(type.serialize(sorted_column)).to eq({ id: column.h_id, order: 'asc' }.to_json)
end
it 'with nil' do
expect(type.serialize(nil)).to eq(nil)
end
it 'with invalid value' do
expect { type.serialize('invalid') }.to raise_error(ArgumentError)
end
end
end