add column type
This commit is contained in:
parent
d51d3fd8ad
commit
693629afc8
4 changed files with 115 additions and 0 deletions
|
@ -27,4 +27,8 @@ class Column
|
|||
table:, column:, label:, classname:, type:, scope:, value_column:, filterable:, displayable:
|
||||
}
|
||||
end
|
||||
|
||||
def self.find(h_id)
|
||||
Procedure.with_discarded.find(h_id[:procedure_id]).find_column(h_id:)
|
||||
end
|
||||
end
|
||||
|
|
38
app/types/column_type.rb
Normal file
38
app/types/column_type.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class ColumnType < ActiveRecord::Type::Value
|
||||
# value can come from:
|
||||
# setter: column (Column),
|
||||
# form_input: column.id == { procedure_id:, column_id: }.to_json (String),
|
||||
# from db: { procedure_id:, column_id: } (Hash)
|
||||
def cast(value)
|
||||
case value
|
||||
in NilClass
|
||||
nil
|
||||
in Column
|
||||
value
|
||||
# from form
|
||||
in String => id
|
||||
h_id = JSON.parse(id, symbolize_names: true)
|
||||
Column.find(h_id)
|
||||
# from db
|
||||
in Hash => h_id
|
||||
Column.find(h_id)
|
||||
end
|
||||
end
|
||||
|
||||
# db -> ruby
|
||||
def deserialize(value) = cast(value)
|
||||
|
||||
# ruby -> db
|
||||
def serialize(value)
|
||||
case value
|
||||
in NilClass
|
||||
nil
|
||||
in Column
|
||||
JSON.generate(value.h_id)
|
||||
else
|
||||
raise ArgumentError, "Invalid value for Column serialization: #{value}"
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,7 +1,9 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require Rails.root.join("app/types/column_type")
|
||||
require Rails.root.join("app/types/export_item_type")
|
||||
|
||||
ActiveSupport.on_load(:active_record) do
|
||||
ActiveRecord::Type.register(:column, ColumnType)
|
||||
ActiveRecord::Type.register(:export_item, ExportItemType)
|
||||
end
|
||||
|
|
71
spec/types/column_type_spec.rb
Normal file
71
spec/types/column_type_spec.rb
Normal file
|
@ -0,0 +1,71 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
describe ColumnType do
|
||||
let(:type) { ColumnType.new }
|
||||
|
||||
describe 'cast' do
|
||||
it 'from Column' do
|
||||
column = Column.new(procedure_id: 1, table: 'table', column: 'column')
|
||||
expect(type.cast(column)).to eq(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')
|
||||
|
||||
expect(Column).to receive(:find).with(column.h_id).and_return(column)
|
||||
expect(type.cast(column.id)).to eq(column)
|
||||
end
|
||||
|
||||
it 'with invalid column id' do
|
||||
expect { type.cast('invalid') }.to raise_error(JSON::ParserError)
|
||||
|
||||
id = { procedure_id: 'invalid', column_id: 'nop' }.to_json
|
||||
expect { type.cast(id) }.to raise_error(ActiveRecord::RecordNotFound)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'from db' do
|
||||
it 'with valid column id' 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.cast(column.h_id)).to eq(column)
|
||||
end
|
||||
|
||||
it 'with invalid column id' do
|
||||
h_id = { procedure_id: 'invalid', column_id: 'nop' }
|
||||
expect { type.cast(h_id) }.to raise_error(ActiveRecord::RecordNotFound)
|
||||
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(column.h_id)).to eq(column)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'serialize' do
|
||||
it 'with SortedColumn' do
|
||||
column = Column.new(procedure_id: 1, table: 'table', column: 'column')
|
||||
expect(type.serialize(column)).to eq(column.h_id.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
|
Loading…
Reference in a new issue