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
+88 -3
View File
@@ -1,4 +1,28 @@
require "digest/md5"
class FuelEntriesController < ApplicationController
def images
@vehicle = Vehicle.find(params[:vehicle_id])
@fuel_entry = @vehicle.fuel_entries.with_attached_images.find(params[:id])
if request.post?
attach_images(@fuel_entry)
return
end
render_images(@fuel_entry)
end
def new_images
@vehicle = Vehicle.find(params[:vehicle_id])
@fuel_entry = @vehicle.fuel_entries.find(params[:id])
render partial: "entry_images/form", locals: {
entry: @fuel_entry,
upload_path: images_vehicle_fuel_entry_path(@vehicle, @fuel_entry)
}
end
def create
@vehicle = Vehicle.find(params[:vehicle_id])
@fuel_entry = @vehicle.fuel_entries.build(fuel_entry_params)
@@ -15,7 +39,68 @@ class FuelEntriesController < ApplicationController
private
def fuel_entry_params
params.require(:fuel_entry).permit(:odometer, :gallons_pumped, :price_paid, :date)
params.require(:fuel_entry).permit(:odometer, :gallons_pumped, :price_paid, :date, images: [])
end
def image_params
params.require(:fuel_entry).permit(images: [])
end
def attach_images(entry)
uploads = Array(image_params[:images]).reject(&:blank?)
if uploads.empty?
entry.errors.add(:images, "must be selected")
render_image_form(entry, status: :unprocessable_entity)
return
end
validate_image_uploads(entry, uploads)
if entry.errors[:images].any? || entry.errors[:base].any?
render_image_form(entry, status: :unprocessable_entity)
else
entry.images.attach(uploads)
entry.save!(validate: false)
entry.images.reload
render_images(entry)
end
end
def validate_image_uploads(entry, uploads)
checksums = []
uploads.each do |upload|
entry.errors.add(:images, "must be 10 MB or smaller") if upload.size > EntryImageValidation::MAX_IMAGE_SIZE
entry.errors.add(:images, "must be JPEG or PNG") unless EntryImageValidation::ALLOWED_IMAGE_CONTENT_TYPES.include?(upload.content_type)
checksums << upload_checksum(upload)
end
existing_checksums = entry.images.map(&:checksum)
duplicate_existing = (checksums & existing_checksums).any?
duplicate_new = checksums.tally.any? { |_checksum, count| count > 1 }
return unless duplicate_existing || duplicate_new
entry.errors.add(:base, "This image is already attached to this entry")
end
def upload_checksum(upload)
Digest::MD5.base64digest(upload.read).tap do
upload.rewind
end
end
def render_image_form(entry, status: :ok)
render partial: "entry_images/form", formats: :html, locals: {
entry: entry,
upload_path: images_vehicle_fuel_entry_path(@vehicle, entry)
}, status: status
end
def render_images(entry)
render partial: "entry_images/images", formats: :html, locals: {
entry: entry,
delete_path_for: ->(image) { vehicle_fuel_entry_image_path(@vehicle, entry, image) },
add_path: new_images_vehicle_fuel_entry_path(@vehicle, entry)
}
end
def save_fuel_entry
@@ -40,8 +125,8 @@ class FuelEntriesController < ApplicationController
def prepare_vehicle_show
@maintenance_schedules = @vehicle.maintenance_schedules.active.order(:maintenance_type)
@maintenance_schedule_statuses = maintenance_schedule_statuses(@maintenance_schedules)
@fuel_entries = @vehicle.fuel_entries.includes(:updated_by_user).order(date: :desc, created_at: :desc)
@maintenance_entries = @vehicle.maintenance_entries.includes(:updated_by_user).order(date: :desc, created_at: :desc)
@fuel_entries = @vehicle.fuel_entries.with_attached_images.includes(:updated_by_user).order(date: :desc, created_at: :desc)
@maintenance_entries = @vehicle.maintenance_entries.with_attached_images.includes(:updated_by_user).order(date: :desc, created_at: :desc)
@maintenance_entry = @vehicle.maintenance_entries.build(date: Date.current)
end