new image workflow, also for sections and cards

This commit is contained in:
Torsten 2022-12-19 18:26:46 +02:00
parent 5d4da2c032
commit c7c1ab369f
4 changed files with 92 additions and 54 deletions

View File

@ -21,16 +21,23 @@ module Merged
end
def create
new_image = Image.create_new(params['filename'] , params['image_file'])
redirect = :images
if(params[:redirect])
redirect = params[:redirect].gsub("NEW" ,new_image.name)
puts "image redirect #{redirect}"
end
redirect_to redirect
image = Image.create_new!(params['filename'] ,params['tags'], params['image_file'])
where_to = determine_redirect(image)
redirect_to where_to , notice: "New image created: #{image.name}"
end
private
def determine_redirect(image)
if(params[:section_id])
view_context.section_set_image_url(params[:section_id],image_id: image.id )
elsif(params[:card_id])
view_context.card_set_image_url(params[:card_id],image_id: image.id )
else
:images
end
end
def build_link_for(image)
if(params[:section_id])
return view_context.section_set_image_url(params[:section_id] , image_id: image.id)

View File

@ -3,11 +3,38 @@ module Merged
module ImagesHelper
def text_for_index
return section_text if(section_id)
return card_text if(card_id)
"All Images"
if(section_id)
section = Section.find(section_id)
"Select image for Section #{section.index} : #{section.header}"
elsif(card_id)
card = Card.find(card_id)
"Select image for Card #{card.index} : #{card.header}"
else
"All Images"
end
end
def text_for_new
if(section_id)
section = Section.find(section_id)
"Add image for Section"
elsif(card_id)
card = Card.find(card_id)
"Select image for Card"
else
"New Image"
end
end
def hidden_for_select
if(section_id)
hidden_field_tag :section_id , section_id
elsif(card_id)
hidden_field_tag :card_id , card_id
else
""
end
end
private
def section_id
@ -16,15 +43,6 @@ module Merged
def card_id
params[:card_id]
end
def section_text
section = Section.find(section_id)
"Select image for Section #{section.index} : #{section.header}"
end
def card_text
card = Card.find(card_id)
"Select image for Card #{card.index} : #{card.header}"
end
end
end

View File

@ -7,29 +7,6 @@ module Merged
fields :name , :tags , :type , :size , :created_at , :height , :width
def initialize(filename)
if filename.is_a? Hash
super(filename)
else
super({name: "newname"})
init_from_file(filename)
end
end
def init_from_file(filename)
puts filename
name , type = filename.split(".")
self.name = name
self.type = type
fullname = Rails.root.join(asset_root,filename)
file = File.new(fullname)
image = MiniMagick::Image.new(fullname)
self.width = image.width
self.height = image.height
self.created_at = file.birthtime
self.size = (file.size/1024).to_i
end
def aspect_ratio
ratio = self.ratio
ratios = (1..9).collect{ |i| ((ratio * i) - (ratio * i).round(0)).abs }
@ -40,21 +17,34 @@ module Merged
def ratio
self.width.to_f / self.height
end
#save an io with given name (without ending, that is taken from io)
#Should save to tmp first
def self.create_new(filename , io)
#save an io as new image. The filename is the id, type taken from io
def self.create_new!(name , tags, io)
original , ending = io.original_filename.split("/").last.split(".")
filename = original if( filename.blank? )
full_filename = filename + "." + ending
File.open(Rails.root.join("app/assets/images/cms/", full_filename), "wb") do |f|
f.write( io.read )
name = original if( name.blank? )
image = Image.new name: name , type: ending , tags: (tags || "")
self.insert(image) # assigns next id
full_filename = image.id.to_s + "." + ending
File.open(Rails.root.join("app/assets/images/cms/", full_filename), "wb") do |file|
file.write( io.read )
end
Image.new( full_filename )
image.init_file_data
image.save
image
end
def init_file_data
image = MiniMagick::Image.new(full_filename)
self.width = image.width
self.height = image.height
file = File.open( full_filename )
self.created_at = file.birthtime
self.size = (file.size/1024).to_i
end
def destroy
delete
File.delete self.filename_old
File.delete self.full_filename
Image.save_all
end

View File

@ -10,6 +10,9 @@
.images
.flex.justify-center.gap-4
.rounded.border.mr-20{"@click" => "toggle_new" , ":class" => "{'bg-orange-200': show_new }"}
.mx-4.text-lg.font-bold Add new
%label.block
.mt-1.text-lg.font-bold Search:
%input.border.rounded{:category => "query", placeholder:"by name", "v-model" => "search_name" }
@ -27,7 +30,21 @@
%svg.w-6.h-6.mt-1{:fill => "none", :stroke => "currentColor", "stroke-width" => "1.5", :viewbox => "0 0 24 24", :xmlns => "http://www.w3.org/2000/svg"}
%path{:d => "M15.75 17.25L12 21m0 0l-3.75-3.75M12 21V3", "stroke-linecap" => "round", "stroke-linejoin" => "round"}
.grid.grid-cols-6.gap-4.m-8
.grid.grid-cols-3.gap-4.m-8{":class" => "{'hidden': !show_new }"}
%div
= form_tag(merged.images_path, multipart: true) do
.flex.flex-col.border.border-gray-100.rounded.p-4
%h3.my-4.text-xl.font-bold= text_for_new
= text_field_tag 'filename' ,nil, placeholder: "Optional name", class: "rounded mt-4"
%p.my-4 Name will be taken from uploaded file if not given
= text_field_tag 'tags' ,nil, placeholder: "Optional tags", class: "rounded mt-4"
%p.my-4 Tags describe the size or format
= hidden_for_select
= file_field_tag 'image_file' , class: "mb-8 w-full px-2 text-xl bg-clip-padding border border-solid border-gray-300 rounded"
= submit_button 'Submit'
%div
.grid.grid-cols-6.gap-4.m-8{":class" => "{'hidden': show_new }"}
.flex.flex-col.border.border-gray-100.rounded.image_box{"v-for": "image in filter_and_sort"}
.flex.align-center.justify-between.mb-4
.text-lg.font-bold.mt-2.mx-2
@ -44,8 +61,6 @@
%strong.rounded.h-10.bg-orange-50.px-5.py-2.font-medium
{{image.tags}}
%script{:src => "https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"}
:ruby2js
class Images < Vue
options el: '.images'
@ -55,6 +70,7 @@
@search_tag = ""
@sort_by = "created"
@sort_dir = -1 # 1 up
@show_new = true
end
def filter_and_sort
dat = @image_data
@ -75,4 +91,11 @@
end
dat
end
def toggle_new()
if @show_new == true
@show_new = false
else
@show_new = true
end
end
end