-
Notifications
You must be signed in to change notification settings - Fork 69
Calculated Fields Mockup
Pavel Nabutovsky edited this page Dec 22, 2021
·
4 revisions
To implement:
-
Modify the Field object:
- Add new JSONB column
calculationto thefieldstable - The values will look like this
calculation: { type: "boolean|integer|string|string|array|date|date-time", expression: <EXPRESSION> } - Add new field type
'calculated' - A calculated field will be configured like this:
Field.new( name: 'felt_stigma_score_calc', type: 'calculated', calculation: { type: 'number', expression: { sum: ['feelings_worthlessness', 'feeling_detached', 'feeling_badly_treated', ...} }, display_name_en: 'Score' )
- Add new JSONB column
-
Update the lookup values of the source fields to be numeric strings:
Lookup.create_or_update!( unique_id: 'lookup-felt-stigma-scale', name_en: 'Felt Stigma Scale', locked: true, lookup_values: [ { id: '0', display_text: 'Not at all (0 pts)' }, { id: '1', display_text: 'A little bit (1 pt)' }, { id: '2', display_text: 'A moderate amount (2 pts)' }, { id: '3', display_text: 'A lot (3 pts)' } ] -
Update the
RecordJsonValidatorService:- Use the
calculation.typefield property - Always permit null values
- Make sure that existing constraints apply: eg. integers should range from -2_147_483_648 to 2_147_483_647
- Use the
-
Calculations will be performed on the front end.
- See expression evaluator library
app/javascript/libs/expressions/parse-expression.js - We implemented skip logic and conditional options using this expression library. See:
app/javascript/components/record-form/form/subforms/subform-dialog-fields/component.jsxapp/javascript/components/record-form/components/render-form-sections.js
- Note that the lookup values are actually strings. The new
sumandaveragefunctions can try to cast strings to int.
- See expression evaluator library