Overview
TheFormFill model represents a filled-out form instance based on a FormTemplate. It manages form data, photo attachments, signatures, and PDF generation for inspections.
Database Schema
Attributes
Primary key
Name of the form fill instance
Foreign key to the associated form template
Foreign key to the associated inspection (optional)
JSON string defining the form field structure (copied from template)
JSONB column storing all form field values and metadataIndexed: GIN index for efficient querying
Status of PDF generationValues:
ready- Ready to generate PDFgenerating- PDF generation in progresscompleted- PDF generated successfullyfailed- PDF generation error
Whether an individual PDF has been created for this form fillIndexed: For efficient querying
Timestamp of last PDF generation
Timestamp when the form fill was created
Timestamp when the form fill was last updated
Associations
Belongs To
The template this form fill is based on
The inspection this form fill belongs to (optional)
Active Storage Attachments
The generated PDF file for this form fill
Multiple photos attached to various form fields
Enums
pdf_generation_status
Data Column Methods
get_field_value
Retrieves the value for a specific field from the data column.Parameters:
field_name(String) - Name of the field
nilExample:set_field_value
Stores a value for a specific field in the data column.Parameters:
field_name(String) - Name of the fieldvalue(Any) - Value to store
true if successful, false otherwiseExample:bulk_update_data
Updates multiple fields efficiently in a single operation.Parameters:
field_hash(Hash) - Hash of field names and values
true if successful, false otherwiseExample:Photo Management Methods
attach_photo_for_field
Attaches a photo to a specific field. Supports multiple photos per field.Parameters:
field_name(String) - Name of the fieldphoto_file(File/IO) - Photo file to attach
:success and :attachment_id or :errorExample:get_photo_for_field
Retrieves the last photo attached to a field.Parameters:
field_name(String) - Name of the field
nilExample:get_photos_for_field
Retrieves all photos attached to a field.Parameters:
field_name(String) - Name of the field
get_photos_by_field
Retrieves all photos organized by field name.Returns: Hash with field names as keys and arrays of photo data as valuesExample:
remove_photo_for_field
Removes photo(s) from a field.Parameters:
field_name(String) - Name of the fieldphoto_id(String, optional) - Specific photo ID to remove; if nil, removes all photos
:success and :message or :errorExample:get_photos_with_context
Retrieves all photos with their field context (type, section, label).Returns: Array of hashes with photo and field metadataExample:
Signature Management Methods
attach_signature_for_field
Attaches a signature image to a signature field. Automatically handles technician vs. client signatures.Parameters:
field_name(String) - Name of the signature fieldimage_file(File/IO) - Signature image (PNG or JPEG)
:success and :attachment_id or :errorSignature Types:Signature_Field- Technician signatureSignature_Annex- Client signature
get_signature_for_field
Retrieves the signature for a specific field. Automatically filters by signature type.Parameters:
field_name(String) - Name of the signature field
nilExample:remove_all_signatures_for_field
Removes all signatures from a signature field.Parameters:
field_name(String) - Name of the signature field
PDF Management Methods
pdf_url
Returns the URL path for the generated PDF.Returns: String or
nilExample:mark_pdf_created!
Marks the PDF as successfully created and updates timestamps.Example:
has_individual_pdf?
Checks if this form fill has an individual PDF created.Returns: BooleanExample:
main_form_fill?
Checks if this is the main form fill for the inspection.Returns: BooleanExample:
should_include_in_main_merge?
Determines if this form fill should be included in the main PDF merge.Returns: BooleanLogic: Returns true if PDF is created and this is not the main form fillExample:
Data Analysis Methods
calculate_form_counts
Calculates counts of Pass, Fail, and N/A values in the form data.Returns: Hash with
:pass, :fail, and :na countsExample:get_sprinklers_data
Extracts sprinkler-specific data from the form.Returns: Hash with sprinkler informationExample:
get_deficiencies_for_processing
Retrieves all deficiency data for PDF generation.Returns: Array of deficiency hashesExample:
Legacy Data Migration
has_legacy_data?
Checks if this form fill has legacy data stored in
form_structure.Returns: BooleanExample:migrate_legacy_data!
Migrates legacy data from
form_structure to the new data column.Returns: Boolean indicating successExample:merge_structure_with_data
Merges form structure with data values for PDF generation.Returns: Array of field hashes with valuesExample:
Utility Methods
cleanup_duplicate_photos!
Removes duplicate photos, keeping only the most recent for each field.Returns: Hash with cleanup statisticsExample:
sync_photos_with_structure!
Synchronizes existing photos with the form structure.Returns: Boolean indicating successExample:
Usage Examples
Creating and Filling a Form
Generating a PDF
Querying Form Data
Related Models
- FormTemplate - The template this form is based on
- Inspection - The inspection this form belongs to