working on image feature
This commit is contained in:
@@ -1,6 +1,30 @@
|
||||
require "digest/md5"
|
||||
|
||||
class MaintenanceEntriesController < ApplicationController
|
||||
ODOMETER_WARNING_THRESHOLD = 500
|
||||
|
||||
def images
|
||||
@vehicle = Vehicle.find(params[:vehicle_id])
|
||||
@maintenance_entry = @vehicle.maintenance_entries.with_attached_images.find(params[:id])
|
||||
|
||||
if request.post?
|
||||
attach_images(@maintenance_entry)
|
||||
return
|
||||
end
|
||||
|
||||
render_images(@maintenance_entry)
|
||||
end
|
||||
|
||||
def new_images
|
||||
@vehicle = Vehicle.find(params[:vehicle_id])
|
||||
@maintenance_entry = @vehicle.maintenance_entries.find(params[:id])
|
||||
|
||||
render partial: "entry_images/form", locals: {
|
||||
entry: @maintenance_entry,
|
||||
upload_path: images_vehicle_maintenance_entry_path(@vehicle, @maintenance_entry)
|
||||
}
|
||||
end
|
||||
|
||||
def create
|
||||
@vehicle = Vehicle.find(params[:vehicle_id])
|
||||
original_odometer = @vehicle.current_odometer
|
||||
@@ -19,7 +43,68 @@ class MaintenanceEntriesController < ApplicationController
|
||||
private
|
||||
|
||||
def maintenance_entry_params
|
||||
params.require(:maintenance_entry).permit(:name, :notes, :odometer, :cost, :date)
|
||||
params.require(:maintenance_entry).permit(:name, :notes, :odometer, :cost, :date, images: [])
|
||||
end
|
||||
|
||||
def image_params
|
||||
params.require(:maintenance_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_maintenance_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_maintenance_entry_image_path(@vehicle, entry, image) },
|
||||
add_path: new_images_vehicle_maintenance_entry_path(@vehicle, entry)
|
||||
}
|
||||
end
|
||||
|
||||
def save_maintenance_entry
|
||||
@@ -55,8 +140,8 @@ class MaintenanceEntriesController < 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)
|
||||
@fuel_entry = @vehicle.fuel_entries.build(date: Date.current)
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user