working on image feature
This commit is contained in:
@@ -6,10 +6,11 @@ class BackupsController < ApplicationController
|
||||
end
|
||||
|
||||
def download
|
||||
send_data gzip(BackupExport.call),
|
||||
filename: "car-tracker-backup-#{Time.current.strftime('%Y%m%d%H%M%S')}.yml.gz",
|
||||
type: "application/gzip",
|
||||
disposition: "attachment"
|
||||
send_backup(BackupExport.call(include_images: false), "car-tracker-backup")
|
||||
end
|
||||
|
||||
def download_with_images
|
||||
send_backup(BackupExport.call(include_images: true), "car-tracker-backup-with-images")
|
||||
end
|
||||
|
||||
def restore
|
||||
@@ -21,6 +22,13 @@ class BackupsController < ApplicationController
|
||||
|
||||
private
|
||||
|
||||
def send_backup(content, basename)
|
||||
send_data gzip(content),
|
||||
filename: "#{basename}-#{Time.current.strftime('%Y%m%d%H%M%S')}.yml.gz",
|
||||
type: "application/gzip",
|
||||
disposition: "attachment"
|
||||
end
|
||||
|
||||
def uploaded_backup
|
||||
params.fetch(:backup_file)
|
||||
rescue KeyError
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
class FuelEntryImagesController < ApplicationController
|
||||
def destroy
|
||||
attachment = fuel_entry.images.attachments.find(params[:attachment_id])
|
||||
attachment.purge
|
||||
|
||||
redirect_to images_vehicle_fuel_entry_path(vehicle, fuel_entry), status: :see_other, notice: "Fuel image deleted."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def vehicle
|
||||
@vehicle ||= Vehicle.find(params[:vehicle_id])
|
||||
end
|
||||
|
||||
def fuel_entry
|
||||
@fuel_entry ||= vehicle.fuel_entries.find(params[:fuel_entry_id])
|
||||
end
|
||||
end
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
class MaintenanceEntryImagesController < ApplicationController
|
||||
def destroy
|
||||
attachment = maintenance_entry.images.attachments.find(params[:attachment_id])
|
||||
attachment.purge
|
||||
|
||||
redirect_to images_vehicle_maintenance_entry_path(vehicle, maintenance_entry), status: :see_other, notice: "Maintenance image deleted."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def vehicle
|
||||
@vehicle ||= Vehicle.find(params[:vehicle_id])
|
||||
end
|
||||
|
||||
def maintenance_entry
|
||||
@maintenance_entry ||= vehicle.maintenance_entries.find(params[:maintenance_entry_id])
|
||||
end
|
||||
end
|
||||
@@ -75,8 +75,8 @@ class VehiclesController < ApplicationController
|
||||
def prepare_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)
|
||||
@maintenance_entry ||= @vehicle.maintenance_entries.build(date: Date.current)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user