fix image add and delete

This commit is contained in:
Torsten 2022-12-27 17:04:53 +02:00
parent d6b238ba94
commit e671447931
5 changed files with 27 additions and 21 deletions

View File

@ -8,8 +8,8 @@ module Merged
data = i.attributes.dup data = i.attributes.dup
data[:url] = view_context.asset_path(i.asset_name) data[:url] = view_context.asset_path(i.asset_name)
data[:link] = build_link_for(i) data[:link] = build_link_for(i)
data[:created_at] = i.created_at.to_date data[:updated_at] = i.updated_at.to_date if i.updated_at
data[:created] = i.created_at.to_i data[:created] = i.updated_at.to_i
data[:aspect_ratio] = i.aspect_ratio.join("/") data[:aspect_ratio] = i.aspect_ratio.join("/")
data[:ratio] = i.aspect_ratio data[:ratio] = i.aspect_ratio
data data
@ -30,7 +30,8 @@ module Merged
end end
def create def create
image = Image.create_new!(params['filename'] ,params['tags'], params['image_file']) image = Image.create_new(params['filename'] ,params['tags'], params['image_file'])
image.add_save current_member.email
where_to = determine_redirect(image) where_to = determine_redirect(image)
redirect_to where_to , notice: "New image created: #{image.name}" redirect_to where_to , notice: "New image created: #{image.name}"
end end

View File

@ -1,5 +1,12 @@
module Merged module Merged
class ActiveBase < ActiveYaml::Base class ActiveBase < ActiveYaml::Base
fields :updated_at , :updated_by
def edit_save( editor )
self.updated_at = Time.now
self.updated_by = editor
edit_save!
end
def edit_save! def edit_save!
ChangeSet.current.edit(self.class.name , self.change_name) ChangeSet.current.edit(self.class.name , self.change_name)
@ -7,6 +14,12 @@ module Merged
self.class.save_all self.class.save_all
end end
def add_save( editor )
self.updated_at = Time.now
self.updated_by = editor
add_save!
end
def add_save! def add_save!
ChangeSet.current.add(self.class.name , self.change_name) ChangeSet.current.add(self.class.name , self.change_name)
save!() save!()

View File

@ -5,7 +5,11 @@ module Merged
set_root_path Rails.root set_root_path Rails.root
fields :name , :tags , :type , :size , :created_at , :height , :width fields :name , :tags , :type , :size , :height , :width
def change_name
name
end
def aspect_ratio def aspect_ratio
ratio = self.ratio ratio = self.ratio
@ -19,7 +23,7 @@ module Merged
end end
#save an io as new image. The filename is the id, type taken from io #save an io as new image. The filename is the id, type taken from io
def self.create_new!(name , tags, io) def self.create_new(name , tags, io)
original , ending = io.original_filename.split("/").last.split(".") original , ending = io.original_filename.split("/").last.split(".")
name = original if( name.blank? ) name = original if( name.blank? )
image = Image.new name: name , type: ending , tags: (tags || "") image = Image.new name: name , type: ending , tags: (tags || "")
@ -29,7 +33,6 @@ module Merged
file.write( io.read ) file.write( io.read )
end end
image.init_file_data image.init_file_data
image.save
image image
end end
@ -38,13 +41,13 @@ module Merged
self.width = image.width self.width = image.width
self.height = image.height self.height = image.height
file = File.open( full_filename ) file = File.open( full_filename )
self.created_at = file.birthtime self.updated_at = file.birthtime
self.size = (file.size/1024).to_i self.size = (file.size/1024).to_i
end end
def destroy def destroy
File.delete self.full_filename File.delete self.full_filename
super delete_save!
end end
def asset_name def asset_name

View File

@ -10,7 +10,7 @@ module Merged
belongs_to :image , class_name: "Merged::Image" belongs_to :image , class_name: "Merged::Image"
fields :options , :updated_at , :updated_by fields :options
def last_update_for(elements) def last_update_for(elements)
last = Time.now last = Time.now
@ -24,17 +24,6 @@ module Merged
last_section last_section
end end
def edit_save( editor )
self.updated_at = Time.now
self.updated_by = editor
edit_save!
end
def add_save( editor )
self.updated_at = Time.now
self.updated_by = editor
add_save!
end
def has_option?(option) def has_option?(option)
options.has_key?(option) and !options[option].blank? options.has_key?(option) and !options[option].blank?

View File

@ -57,7 +57,7 @@
%strong.inline-block.rounded.bg-orange-50.px-3.py-1.text-md.font-medium %strong.inline-block.rounded.bg-orange-50.px-3.py-1.text-md.font-medium
{{image.size}}k {{image.size}}k
%strong.inline-block.rounded.bg-orange-50.px-3.py-1.text-md.font-medium %strong.inline-block.rounded.bg-orange-50.px-3.py-1.text-md.font-medium
{{image.created_at}} {{image.updated_at}}
%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}}