working on image feature

This commit is contained in:
2026-10-01 09:56:32 -06:00
parent 7d4e446d4a
commit 23d8154f17
26 changed files with 1259 additions and 57 deletions
@@ -0,0 +1,39 @@
module EntryImageValidation
extend ActiveSupport::Concern
MAX_IMAGE_SIZE = 10.megabytes
ALLOWED_IMAGE_CONTENT_TYPES = %w[image/jpeg image/png].freeze
included do
validate :images_are_valid
validate :images_are_not_duplicated
end
private
def images_are_valid
images.each do |image|
validate_image_size(image)
validate_image_content_type(image)
end
end
def images_are_not_duplicated
duplicate_checksum = images.map { |image| image.blob.checksum }.compact.tally.any? { |_checksum, count| count > 1 }
return unless duplicate_checksum
errors.add(:base, "This image is already attached to this entry")
end
def validate_image_size(image)
return if image.blob.byte_size <= MAX_IMAGE_SIZE
errors.add(:images, "must be 10 MB or smaller")
end
def validate_image_content_type(image)
return if ALLOWED_IMAGE_CONTENT_TYPES.include?(image.blob.content_type)
errors.add(:images, "must be JPEG or PNG")
end
end
+3
View File
@@ -1,6 +1,9 @@
class FuelEntry < ApplicationRecord
include EntryImageValidation
belongs_to :vehicle
belongs_to :updated_by_user, class_name: "User"
has_many_attached :images
validates :odometer, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :gallons_pumped, presence: true, numericality: { greater_than: 0 }
+3
View File
@@ -1,6 +1,9 @@
class MaintenanceEntry < ApplicationRecord
include EntryImageValidation
belongs_to :vehicle
belongs_to :updated_by_user, class_name: "User"
has_many_attached :images
validates :name, :notes, :date, presence: true
validates :odometer, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }