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) @fuel_entry.updated_by_user = Current.user if save_fuel_entry redirect_to @vehicle, notice: "Fuel entry recorded." else prepare_vehicle_show render "vehicles/show", status: :unprocessable_entity end end private def fuel_entry_params 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 saved = false FuelEntry.transaction do if @fuel_entry.save update_vehicle_odometer(@fuel_entry.odometer) saved = true end end saved end def update_vehicle_odometer(odometer) return unless odometer > @vehicle.current_odometer @vehicle.update!(current_odometer: odometer) end 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.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 def maintenance_schedule_statuses(schedules) schedules.index_with do |schedule| latest_entry = @vehicle.maintenance_entries .where("lower(name) = ?", schedule.maintenance_type.downcase) .order(date: :desc, created_at: :desc) .first MaintenanceScheduleStatus.new(schedule, latest_entry, current_odometer: @vehicle.current_odometer) end end end