image class with attributes

This commit is contained in:
Torsten 2022-11-28 00:12:20 +02:00
parent 4814f53d1e
commit 76b29123a6
3 changed files with 56 additions and 27 deletions

View File

@ -2,32 +2,18 @@ module Cms
class ImagesController < CmsController class ImagesController < CmsController
@@root = "app/assets/images/cms/"
@@files = Set.new Dir[Rails.root + @@root + "*.*"].collect{|f|f.split("/").last}
def index def index
@files = files @images = Image.all
end end
def new def new
end end
def create def create
io = params['image_file'] Image.save_image(params['filename'] , params['image_file'])
ending = io.original_filename.split("/").last.split(".").last
filename = params['filename'] + "." + ending
File.open(Rails.root.join('app/assets/images/cms', filename), "wb") do |f|
f.write io.read
end
@@files << filename
redirect_to cms_image_index_path redirect_to cms_image_index_path
end end
private
def files
@@files
end
end end
end end

48
app/models/cms/image.rb Normal file
View File

@ -0,0 +1,48 @@
module Cms
class Image
include ActiveModel::API
include ActiveModel::Conversion
extend ActiveModel::Naming
@@images = {}
attr_reader :name , :type , :created_at , :updated_at
def initialize(filename)
puts filename
@name , @type = filename.split(".")
file = File.new(Rails.root.join(Image.root,filename))
@created_at = file.birthtime
@updated_at = file.ctime
end
def self.all
return @@images unless @@images.empty?
Dir[Rails.root.join Image.root + "*.*"].each do |f|
file = f.split("/").last
self.add( file )
end
@@images
end
#save an io with given name (without ending, that is taken from io)
def self.create_new(filename , io)
ending = io.original_filename.split("/").last.split(".").last
full_filename = filename + "." + ending
File.open(Rails.root.join(Image.root, full_filename), "wb") do |f|
f.write( io.read )
end
self.add( full_filename )
end
def self.root
"app/assets/images/cms/"
end
private
def self.add(filename)
key = filename.split(".").first
@@images[key] = Image.new(filename)
end
end
end

View File

@ -3,17 +3,12 @@
Add Image Add Image
.grid.grid-cols-6.gap-4.m-8 .grid.grid-cols-6.gap-4.m-8
-@files.each do |file| -@images.each do |name , image|
.relative.block.border.border-gray-100 .relative.block.border.border-gray-100
%button.absolute.right-4.top-4.rounded-full.bg-black.p-2.text-white{:type => "button"} =image_tag("cms/#{name}" , class: "h-56 w-full object-contain lg:h-72")
%span.sr-only Wishlist
%svg.h-4.w-4{:fill => "none", :stroke => "currentColor", :viewbox => "0 0 24 24", :xmlns => "http://www.w3.org/2000/svg"}
%path{:d => "M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z", "stroke-linecap" => "round", "stroke-linejoin" => "round", "stroke-width" => "2"}
=image_tag("cms/#{file}" , class: "h-56 w-full object-contain lg:h-72")
.p-6 .p-6
%strong.inline-block.bg-yellow-400.px-3.py-1.text-xs.font-medium %strong.inline-block.bg-yellow-400.px-3.py-1.text-xs.font-medium
New =image.type
%h3.mt-4.text-lg.font-bold= file.split(".").first %h3.mt-4.text-lg.font-bold= image.name
%p.mt-2.text-sm.text-gray-700 $14.99 %p.mt-2.text-sm.text-gray-700
%button.mt-4.block.w-full.rounded-sm.bg-yellow-500.p-4.text-sm.font-medium{:type => "button"} = "#{image.created_at.to_date} ---- #{image.updated_at.to_date}"
Add to Cart