mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
- 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>
29 lines
1.1 KiB
Ruby
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
|