working on image feature
This commit is contained in:
@@ -140,6 +140,12 @@ body > header .button_to,
|
||||
color: var(--color-danger);
|
||||
}
|
||||
|
||||
.button--small {
|
||||
min-block-size: 2rem;
|
||||
padding: 0.35rem 0.65rem;
|
||||
font-size: 0.88rem;
|
||||
}
|
||||
|
||||
.back-link {
|
||||
font-weight: 700;
|
||||
text-decoration: none;
|
||||
@@ -437,6 +443,54 @@ textarea {
|
||||
color: var(--color-danger);
|
||||
}
|
||||
|
||||
.form-errors--compact {
|
||||
padding: 0.5rem 0.75rem;
|
||||
}
|
||||
|
||||
.form-errors--compact ul {
|
||||
margin-block: 0;
|
||||
padding-inline-start: 1.1rem;
|
||||
}
|
||||
|
||||
.entry-image-form {
|
||||
display: grid;
|
||||
gap: 0.75rem;
|
||||
min-inline-size: min(18rem, 100%);
|
||||
}
|
||||
|
||||
.entry-images {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 0.75rem;
|
||||
min-inline-size: 14rem;
|
||||
}
|
||||
|
||||
.entry-image {
|
||||
display: grid;
|
||||
gap: 0.35rem;
|
||||
inline-size: 8.5rem;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.entry-image__thumbnail {
|
||||
display: block;
|
||||
inline-size: 7.5rem;
|
||||
block-size: 7.5rem;
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: var(--radius-sm);
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
.entry-image figcaption {
|
||||
overflow-wrap: anywhere;
|
||||
color: var(--color-muted);
|
||||
font-size: 0.78rem;
|
||||
}
|
||||
|
||||
.entry-images__actions {
|
||||
margin-block: 0.75rem 0;
|
||||
}
|
||||
|
||||
@media (max-width: 720px) {
|
||||
body > header {
|
||||
font-size: 0.9rem;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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 }
|
||||
|
||||
@@ -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 }
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
require "base64"
|
||||
|
||||
class BackupExport
|
||||
FORMAT = "car_tracker_backup".freeze
|
||||
VERSION = 1
|
||||
VERSION = 2
|
||||
|
||||
def self.call
|
||||
new.call
|
||||
def self.call(include_images: false)
|
||||
new(include_images: include_images).call
|
||||
end
|
||||
|
||||
def initialize(include_images: false)
|
||||
@include_images = include_images
|
||||
end
|
||||
|
||||
def call
|
||||
@@ -17,6 +23,8 @@ class BackupExport
|
||||
|
||||
private
|
||||
|
||||
attr_reader :include_images
|
||||
|
||||
def vehicles
|
||||
Vehicle.includes(
|
||||
:maintenance_schedules,
|
||||
@@ -67,7 +75,7 @@ class BackupExport
|
||||
"gallons_pumped" => decimal_string(entry.gallons_pumped),
|
||||
"price_paid" => decimal_string(entry.price_paid),
|
||||
"updated_by_user" => user_data(entry.updated_by_user)
|
||||
}
|
||||
}.tap { |data| data["images"] = images(entry) if include_images }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -80,6 +88,18 @@ class BackupExport
|
||||
"odometer" => entry.odometer,
|
||||
"cost" => decimal_string(entry.cost),
|
||||
"updated_by_user" => user_data(entry.updated_by_user)
|
||||
}.tap { |data| data["images"] = images(entry) if include_images }
|
||||
end
|
||||
end
|
||||
|
||||
def images(entry)
|
||||
entry.images.map do |image|
|
||||
{
|
||||
"filename" => image.filename.to_s,
|
||||
"content_type" => image.content_type,
|
||||
"checksum" => image.checksum,
|
||||
"byte_size" => image.byte_size,
|
||||
"data_base64" => Base64.strict_encode64(image.download)
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
require "base64"
|
||||
require "digest/md5"
|
||||
require "stringio"
|
||||
|
||||
class BackupImport
|
||||
SUPPORTED_VERSION = 1
|
||||
SUPPORTED_VERSIONS = [ 1, 2 ].freeze
|
||||
MODES = %w[merge replace].freeze
|
||||
|
||||
class Error < StandardError; end
|
||||
@@ -46,9 +50,9 @@ class BackupImport
|
||||
|
||||
def migrate_backup(backup)
|
||||
version = backup["version"].to_i
|
||||
return backup if version == SUPPORTED_VERSION
|
||||
return backup if SUPPORTED_VERSIONS.include?(version)
|
||||
|
||||
if version > SUPPORTED_VERSION
|
||||
if version > SUPPORTED_VERSIONS.max
|
||||
raise Error, "Backup version #{version} is newer than this application supports."
|
||||
end
|
||||
|
||||
@@ -57,6 +61,8 @@ class BackupImport
|
||||
|
||||
def replace_app_data!
|
||||
UserSession.delete_all
|
||||
FuelEntry.find_each { |entry| entry.images.purge }
|
||||
MaintenanceEntry.find_each { |entry| entry.images.purge }
|
||||
FuelEntry.delete_all
|
||||
MaintenanceEntry.delete_all
|
||||
MaintenanceSchedule.delete_all
|
||||
@@ -132,18 +138,23 @@ class BackupImport
|
||||
def import_fuel_entries(vehicle, entries)
|
||||
entries.each do |entry_data|
|
||||
user = import_user(entry_data.fetch("updated_by_user"))
|
||||
next if fuel_entry_exists?(vehicle, entry_data, user)
|
||||
entry = matching_fuel_entry(vehicle, entry_data, user)
|
||||
|
||||
FuelEntry.insert!({
|
||||
vehicle_id: vehicle.id,
|
||||
updated_by_user_id: user.id,
|
||||
odometer: integer_value(entry_data.fetch("odometer")),
|
||||
gallons_pumped: decimal_value(entry_data.fetch("gallons_pumped")),
|
||||
price_paid: decimal_value(entry_data.fetch("price_paid")),
|
||||
date: date_value(entry_data.fetch("date")),
|
||||
created_at: Time.current,
|
||||
updated_at: Time.current
|
||||
})
|
||||
unless entry
|
||||
FuelEntry.insert!({
|
||||
vehicle_id: vehicle.id,
|
||||
updated_by_user_id: user.id,
|
||||
odometer: integer_value(entry_data.fetch("odometer")),
|
||||
gallons_pumped: decimal_value(entry_data.fetch("gallons_pumped")),
|
||||
price_paid: decimal_value(entry_data.fetch("price_paid")),
|
||||
date: date_value(entry_data.fetch("date")),
|
||||
created_at: Time.current,
|
||||
updated_at: Time.current
|
||||
})
|
||||
entry = matching_fuel_entry(vehicle, entry_data, user)
|
||||
end
|
||||
|
||||
import_images(entry, array_value(entry_data, "images"))
|
||||
end
|
||||
rescue KeyError => error
|
||||
raise Error, "Fuel entry is missing required field #{error.key}."
|
||||
@@ -152,19 +163,24 @@ class BackupImport
|
||||
def import_maintenance_entries(vehicle, entries)
|
||||
entries.each do |entry_data|
|
||||
user = import_user(entry_data.fetch("updated_by_user"))
|
||||
next if maintenance_entry_exists?(vehicle, entry_data, user)
|
||||
entry = matching_maintenance_entry(vehicle, entry_data, user)
|
||||
|
||||
MaintenanceEntry.insert!({
|
||||
vehicle_id: vehicle.id,
|
||||
updated_by_user_id: user.id,
|
||||
name: string_value(entry_data.fetch("name")),
|
||||
notes: string_value(entry_data.fetch("notes")),
|
||||
odometer: integer_value(entry_data.fetch("odometer")),
|
||||
cost: decimal_value(entry_data.fetch("cost")),
|
||||
date: date_value(entry_data.fetch("date")),
|
||||
created_at: Time.current,
|
||||
updated_at: Time.current
|
||||
})
|
||||
unless entry
|
||||
MaintenanceEntry.insert!({
|
||||
vehicle_id: vehicle.id,
|
||||
updated_by_user_id: user.id,
|
||||
name: string_value(entry_data.fetch("name")),
|
||||
notes: string_value(entry_data.fetch("notes")),
|
||||
odometer: integer_value(entry_data.fetch("odometer")),
|
||||
cost: decimal_value(entry_data.fetch("cost")),
|
||||
date: date_value(entry_data.fetch("date")),
|
||||
created_at: Time.current,
|
||||
updated_at: Time.current
|
||||
})
|
||||
entry = matching_maintenance_entry(vehicle, entry_data, user)
|
||||
end
|
||||
|
||||
import_images(entry, array_value(entry_data, "images"))
|
||||
end
|
||||
rescue KeyError => error
|
||||
raise Error, "Maintenance entry is missing required field #{error.key}."
|
||||
@@ -182,17 +198,17 @@ class BackupImport
|
||||
raise Error, "User is missing required field #{error.key}."
|
||||
end
|
||||
|
||||
def fuel_entry_exists?(vehicle, entry_data, user)
|
||||
def matching_fuel_entry(vehicle, entry_data, user)
|
||||
vehicle.fuel_entries.where(
|
||||
updated_by_user: user,
|
||||
odometer: integer_value(entry_data.fetch("odometer")),
|
||||
gallons_pumped: decimal_value(entry_data.fetch("gallons_pumped")),
|
||||
price_paid: decimal_value(entry_data.fetch("price_paid")),
|
||||
date: date_value(entry_data.fetch("date"))
|
||||
).exists?
|
||||
).first
|
||||
end
|
||||
|
||||
def maintenance_entry_exists?(vehicle, entry_data, user)
|
||||
def matching_maintenance_entry(vehicle, entry_data, user)
|
||||
vehicle.maintenance_entries.where(
|
||||
updated_by_user: user,
|
||||
name: string_value(entry_data.fetch("name")),
|
||||
@@ -200,7 +216,49 @@ class BackupImport
|
||||
odometer: integer_value(entry_data.fetch("odometer")),
|
||||
cost: decimal_value(entry_data.fetch("cost")),
|
||||
date: date_value(entry_data.fetch("date"))
|
||||
).exists?
|
||||
).first
|
||||
end
|
||||
|
||||
def import_images(entry, images)
|
||||
images.each do |image_data|
|
||||
filename = string_value(image_data.fetch("filename"))
|
||||
checksum = string_value(image_data.fetch("checksum"))
|
||||
next if image_attached?(entry, filename, checksum)
|
||||
|
||||
content_type = string_value(image_data.fetch("content_type"))
|
||||
byte_size = integer_value(image_data.fetch("byte_size"))
|
||||
validate_image_metadata!(content_type, byte_size)
|
||||
|
||||
content = Base64.strict_decode64(string_value(image_data.fetch("data_base64")))
|
||||
raise Error, "Image byte size does not match backup metadata." unless content.bytesize == byte_size
|
||||
raise Error, "Image checksum does not match backup metadata." unless Digest::MD5.base64digest(content) == checksum
|
||||
|
||||
entry.images.attach(
|
||||
io: StringIO.new(content),
|
||||
filename: filename,
|
||||
content_type: content_type
|
||||
)
|
||||
end
|
||||
rescue ArgumentError
|
||||
raise Error, "Image data is not valid Base64."
|
||||
rescue KeyError => error
|
||||
raise Error, "Image is missing required field #{error.key}."
|
||||
end
|
||||
|
||||
def image_attached?(entry, filename, checksum)
|
||||
entry.images.any? do |image|
|
||||
image.filename.to_s == filename && image.checksum == checksum
|
||||
end
|
||||
end
|
||||
|
||||
def validate_image_metadata!(content_type, byte_size)
|
||||
unless EntryImageValidation::ALLOWED_IMAGE_CONTENT_TYPES.include?(content_type)
|
||||
raise Error, "Images must be JPEG or PNG."
|
||||
end
|
||||
|
||||
return if byte_size <= EntryImageValidation::MAX_IMAGE_SIZE
|
||||
|
||||
raise Error, "Images must be 10 MB or smaller."
|
||||
end
|
||||
|
||||
def recalculate_odometer(vehicle, incoming_odometer)
|
||||
|
||||
@@ -20,7 +20,10 @@
|
||||
<h2>Download Backup</h2>
|
||||
<p>Download a gzip-compressed YAML backup of vehicles, users, maintenance schedules, fuel entries, and maintenance entries.</p>
|
||||
<p>Authentication data, access keys, and browser sessions are not included.</p>
|
||||
<p><%= link_to "Download backup", download_backup_path, class: "button button--primary" %></p>
|
||||
<div class="action-row">
|
||||
<%= link_to "Download data backup", download_backup_path, class: "button button--primary" %>
|
||||
<%= link_to "Download backup with images", download_with_images_backup_path, class: "button button--secondary" %>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="dashboard-section">
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
<%= turbo_frame_tag dom_id(entry, :images) do %>
|
||||
<%= form_with url: upload_path, scope: entry.model_name.param_key, multipart: true, class: "entry-image-form" do |form| %>
|
||||
<% if entry.errors[:images].any? || entry.errors[:base].any? %>
|
||||
<div class="form-errors form-errors--compact">
|
||||
<ul>
|
||||
<% entry.errors[:base].each do |message| %>
|
||||
<li><%= message %></li>
|
||||
<% end %>
|
||||
|
||||
<% entry.errors[:images].each do |message| %>
|
||||
<li>Images <%= message %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div class="field">
|
||||
<%= form.label :images, "Images" %>
|
||||
<%= form.file_field :images, multiple: true, accept: "image/jpeg,image/png", required: true %>
|
||||
</div>
|
||||
|
||||
<div class="action-row">
|
||||
<%= form.submit "Upload images", class: "button button--primary button--small" %>
|
||||
</div>
|
||||
<% end %>
|
||||
<% end %>
|
||||
@@ -0,0 +1,23 @@
|
||||
<%= turbo_frame_tag dom_id(entry, :images) do %>
|
||||
<% if entry.images.attached? %>
|
||||
<div class="entry-images">
|
||||
<% entry.images.each do |image| %>
|
||||
<figure class="entry-image">
|
||||
<%= link_to rails_blob_path(image, disposition: "inline"), target: "_blank", rel: "noopener" do %>
|
||||
<%= image_tag rails_blob_path(image, disposition: "inline"), alt: image.filename.to_s, class: "entry-image__thumbnail" %>
|
||||
<% end %>
|
||||
|
||||
<figcaption><%= image.filename %></figcaption>
|
||||
|
||||
<%= button_to "Delete", delete_path_for.call(image), method: :delete, class: "button button--danger button--small" %>
|
||||
</figure>
|
||||
<% end %>
|
||||
</div>
|
||||
<% else %>
|
||||
<span>No images attached.</span>
|
||||
<% end %>
|
||||
|
||||
<p class="entry-images__actions">
|
||||
<%= link_to "Add images", add_path, class: "button button--secondary button--small" %>
|
||||
</p>
|
||||
<% end %>
|
||||
@@ -103,7 +103,7 @@
|
||||
<details class="dashboard-section dashboard-section--primary" open>
|
||||
<summary>Record Fuel</summary>
|
||||
|
||||
<%= form_with model: [@vehicle, @fuel_entry], class: "stacked-form" do |form| %>
|
||||
<%= form_with model: [@vehicle, @fuel_entry], class: "stacked-form", multipart: true do |form| %>
|
||||
<% if @fuel_entry.errors.any? %>
|
||||
<section class="form-errors">
|
||||
<h3><%= pluralize(@fuel_entry.errors.count, "error") %> prevented this fuel entry from being saved</h3>
|
||||
@@ -136,6 +136,11 @@
|
||||
<%= form.label :date %>
|
||||
<%= form.date_field :date, max: Date.current, required: true %>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<%= form.label :images, "Receipt images" %>
|
||||
<%= form.file_field :images, multiple: true, accept: "image/jpeg,image/png" %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
@@ -148,7 +153,7 @@
|
||||
<summary>Record Maintenance</summary>
|
||||
|
||||
<% if @maintenance_schedules.any? %>
|
||||
<%= form_with model: [@vehicle, @maintenance_entry], class: "stacked-form", data: {
|
||||
<%= form_with model: [@vehicle, @maintenance_entry], class: "stacked-form", multipart: true, data: {
|
||||
controller: "maintenance-odometer-warning",
|
||||
maintenance_odometer_warning_current_odometer_value: @vehicle.current_odometer,
|
||||
maintenance_odometer_warning_threshold_value: 500
|
||||
@@ -188,6 +193,11 @@
|
||||
<%= form.label :date %>
|
||||
<%= form.date_field :date, max: Date.current, required: true %>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<%= form.label :images, "Service images" %>
|
||||
<%= form.file_field :images, multiple: true, accept: "image/jpeg,image/png" %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p class="field-warning" hidden data-maintenance-odometer-warning-target="warning">
|
||||
@@ -305,6 +315,7 @@
|
||||
<th>Odometer</th>
|
||||
<th>Cost</th>
|
||||
<th>Notes</th>
|
||||
<th>Images</th>
|
||||
<th>Updated by</th>
|
||||
</tr>
|
||||
</thead>
|
||||
@@ -316,6 +327,18 @@
|
||||
<td><%= number_with_delimiter(entry.odometer) %></td>
|
||||
<td><%= number_to_currency(entry.cost) %></td>
|
||||
<td><%= entry.notes %></td>
|
||||
<td>
|
||||
<%= turbo_frame_tag dom_id(entry, :images) do %>
|
||||
<% if entry.images.attached? %>
|
||||
<div class="action-row">
|
||||
<%= link_to "Show images (#{entry.images.size})", images_vehicle_maintenance_entry_path(@vehicle, entry), class: "button button--secondary button--small" %>
|
||||
<%= link_to "Add images", new_images_vehicle_maintenance_entry_path(@vehicle, entry), class: "button button--secondary button--small" %>
|
||||
</div>
|
||||
<% else %>
|
||||
<%= link_to "Add images", new_images_vehicle_maintenance_entry_path(@vehicle, entry), class: "button button--secondary button--small" %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</td>
|
||||
<td><%= entry.updated_by_user.name %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
@@ -339,6 +362,7 @@
|
||||
<th>Odometer</th>
|
||||
<th>Gallons</th>
|
||||
<th>Price paid</th>
|
||||
<th>Images</th>
|
||||
<th>Updated by</th>
|
||||
</tr>
|
||||
</thead>
|
||||
@@ -349,6 +373,18 @@
|
||||
<td><%= number_with_delimiter(entry.odometer) %></td>
|
||||
<td><%= entry.gallons_pumped %></td>
|
||||
<td><%= number_to_currency(entry.price_paid) %></td>
|
||||
<td>
|
||||
<%= turbo_frame_tag dom_id(entry, :images) do %>
|
||||
<% if entry.images.attached? %>
|
||||
<div class="action-row">
|
||||
<%= link_to "Show images (#{entry.images.size})", images_vehicle_fuel_entry_path(@vehicle, entry), class: "button button--secondary button--small" %>
|
||||
<%= link_to "Add images", new_images_vehicle_fuel_entry_path(@vehicle, entry), class: "button button--secondary button--small" %>
|
||||
</div>
|
||||
<% else %>
|
||||
<%= link_to "Add images", new_images_vehicle_fuel_entry_path(@vehicle, entry), class: "button button--secondary button--small" %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</td>
|
||||
<td><%= entry.updated_by_user.name %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
||||
Reference in New Issue
Block a user