Files
simple-aircraft-manager-rails/app/models/application_record.rb
T
rickyandClaude Opus 5.5 908c943e21 Fix crashes found by fuzzing malformed input
- Components can't be made into a parent loop, and the parts tree walks
  stop safely if one exists (logbook entry and squawk resolution pages
  crashed).
- A rejected aircraft photo no longer breaks the edit page; empty photos
  are refused; images that can't be read aren't resized, and a failed
  resize sends the original instead of an error.
- Unsupported formats (/report.pdf…) answer 406; tampered upload
  references, out-of-range numbers and constraint violations get a form
  message instead of a 500; requests with null bytes are refused (400).
- Blank values in required columns with defaults take the default;
  optional links to records that don't exist are validation errors.
- Imports (oil report, logbook scans, .sam.zip) accept only uploaded files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 13:17:43 -05:00

29 lines
1.1 KiB
Ruby

class ApplicationRecord < ActiveRecord::Base
primary_abstract_class
before_validation :default_blank_required_columns
validate :optional_references_exist
private
# A blank form field arrives as nil; for a column that can't be empty and has
# a default (a checkbox, a count, a position), use the default rather than
# failing in the database.
def default_blank_required_columns
self.class.columns.each do |column|
next if column.null || column.default.nil? || !self[column.name].nil?
self[column.name] = self.class.type_for_attribute(column.name).cast(column.default)
end
end
# An optional link (a parent part, a collection…) given an id that doesn't
# exist is a mistake in the form, not something to store.
def optional_references_exist
self.class.reflect_on_all_associations(:belongs_to).each do |assoc|
next if assoc.polymorphic? || !assoc.options[:optional]
next if self[assoc.foreign_key].blank? || !attribute_changed?(assoc.foreign_key)
errors.add(assoc.name, :invalid) unless public_send(assoc.name)
end
end
end