new image workflow, also for sections and cards
This commit is contained in:
parent
5d4da2c032
commit
c7c1ab369f
@ -21,16 +21,23 @@ module Merged
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
new_image = Image.create_new(params['filename'] , params['image_file'])
|
image = Image.create_new!(params['filename'] ,params['tags'], params['image_file'])
|
||||||
redirect = :images
|
where_to = determine_redirect(image)
|
||||||
if(params[:redirect])
|
redirect_to where_to , notice: "New image created: #{image.name}"
|
||||||
redirect = params[:redirect].gsub("NEW" ,new_image.name)
|
|
||||||
puts "image redirect #{redirect}"
|
|
||||||
end
|
|
||||||
redirect_to redirect
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
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)
|
def build_link_for(image)
|
||||||
if(params[:section_id])
|
if(params[:section_id])
|
||||||
return view_context.section_set_image_url(params[:section_id] , image_id: image.id)
|
return view_context.section_set_image_url(params[:section_id] , image_id: image.id)
|
||||||
|
@ -3,11 +3,38 @@ module Merged
|
|||||||
module ImagesHelper
|
module ImagesHelper
|
||||||
|
|
||||||
def text_for_index
|
def text_for_index
|
||||||
return section_text if(section_id)
|
if(section_id)
|
||||||
return card_text if(card_id)
|
section = Section.find(section_id)
|
||||||
"All Images"
|
"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
|
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
|
private
|
||||||
def section_id
|
def section_id
|
||||||
@ -16,15 +43,6 @@ module Merged
|
|||||||
def card_id
|
def card_id
|
||||||
params[:card_id]
|
params[:card_id]
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
@ -7,29 +7,6 @@ module Merged
|
|||||||
|
|
||||||
fields :name , :tags , :type , :size , :created_at , :height , :width
|
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
|
def aspect_ratio
|
||||||
ratio = self.ratio
|
ratio = self.ratio
|
||||||
ratios = (1..9).collect{ |i| ((ratio * i) - (ratio * i).round(0)).abs }
|
ratios = (1..9).collect{ |i| ((ratio * i) - (ratio * i).round(0)).abs }
|
||||||
@ -40,21 +17,34 @@ module Merged
|
|||||||
def ratio
|
def ratio
|
||||||
self.width.to_f / self.height
|
self.width.to_f / self.height
|
||||||
end
|
end
|
||||||
#save an io with given name (without ending, that is taken from io)
|
|
||||||
#Should save to tmp first
|
#save an io as new image. The filename is the id, type taken from io
|
||||||
def self.create_new(filename , io)
|
def self.create_new!(name , tags, io)
|
||||||
original , ending = io.original_filename.split("/").last.split(".")
|
original , ending = io.original_filename.split("/").last.split(".")
|
||||||
filename = original if( filename.blank? )
|
name = original if( name.blank? )
|
||||||
full_filename = filename + "." + ending
|
image = Image.new name: name , type: ending , tags: (tags || "")
|
||||||
File.open(Rails.root.join("app/assets/images/cms/", full_filename), "wb") do |f|
|
self.insert(image) # assigns next id
|
||||||
f.write( io.read )
|
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
|
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
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
delete
|
delete
|
||||||
File.delete self.filename_old
|
File.delete self.full_filename
|
||||||
Image.save_all
|
Image.save_all
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -10,6 +10,9 @@
|
|||||||
|
|
||||||
.images
|
.images
|
||||||
.flex.justify-center.gap-4
|
.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
|
%label.block
|
||||||
.mt-1.text-lg.font-bold Search:
|
.mt-1.text-lg.font-bold Search:
|
||||||
%input.border.rounded{:category => "query", placeholder:"by name", "v-model" => "search_name" }
|
%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"}
|
%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"}
|
%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.flex-col.border.border-gray-100.rounded.image_box{"v-for": "image in filter_and_sort"}
|
||||||
.flex.align-center.justify-between.mb-4
|
.flex.align-center.justify-between.mb-4
|
||||||
.text-lg.font-bold.mt-2.mx-2
|
.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
|
%strong.rounded.h-10.bg-orange-50.px-5.py-2.font-medium
|
||||||
{{image.tags}}
|
{{image.tags}}
|
||||||
|
|
||||||
%script{:src => "https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"}
|
|
||||||
|
|
||||||
:ruby2js
|
:ruby2js
|
||||||
class Images < Vue
|
class Images < Vue
|
||||||
options el: '.images'
|
options el: '.images'
|
||||||
@ -55,6 +70,7 @@
|
|||||||
@search_tag = ""
|
@search_tag = ""
|
||||||
@sort_by = "created"
|
@sort_by = "created"
|
||||||
@sort_dir = -1 # 1 up
|
@sort_dir = -1 # 1 up
|
||||||
|
@show_new = true
|
||||||
end
|
end
|
||||||
def filter_and_sort
|
def filter_and_sort
|
||||||
dat = @image_data
|
dat = @image_data
|
||||||
@ -75,4 +91,11 @@
|
|||||||
end
|
end
|
||||||
dat
|
dat
|
||||||
end
|
end
|
||||||
|
def toggle_new()
|
||||||
|
if @show_new == true
|
||||||
|
@show_new = false
|
||||||
|
else
|
||||||
|
@show_new = true
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user