make base to image connectio and use image instances instead of names

This commit is contained in:
Torsten 2022-12-17 13:11:20 +02:00
parent 4d4de51c8b
commit 27bf4f4269
11 changed files with 35 additions and 24 deletions

View File

@ -19,10 +19,6 @@ module Merged
"#{x} / #{y}"
end
def image_root
Image.image_root
end
def markdown(text)
text = text.text unless text.is_a?(String)
return "" if text.blank?

View File

@ -7,8 +7,7 @@ module Merged
def bg(section , clazz = "")
attributes = {class: clazz}
return attributes if section.image.blank?
#puts "--#{Image.image_root}/#{section.image}--"
img = asset_url( "#{Image.image_root}/#{section.image}" )
img = asset_url( section.image.assert_name )
attributes["style"] = "background-image: url('#{img}');"
if(section.option("fixed") == "on")
attributes[:class] = attributes[:class] + " bg-fixed"
@ -19,7 +18,7 @@ module Merged
# works for with sections and cards that respond to .image
def image_for(element , classes = "")
return "" if element.image.blank?
image_tag("#{Image.image_root}/#{element.image}" , class: classes)
image_tag(element.image.assert_name , class: classes)
end
end

View File

@ -21,7 +21,7 @@ module Merged
name , type = filename.split(".")
self.name = name
self.type = type
fullname = Rails.root.join(Image.asset_root,filename)
fullname = Rails.root.join(asset_root,filename)
file = File.new(fullname)
image = MiniMagick::Image.new(fullname)
self.width = image.width
@ -31,18 +31,19 @@ module Merged
end
def aspect_ratio
ratio = @width.to_f / @height
ratio = self.width.to_f / self.height
ratios = (1..9).collect{ |i| ((ratio * i) - (ratio * i).round(0)).abs }
min , min_index = ratios.each_with_index.min
[(ratio * (min_index + 1) ).to_i , (min_index + 1) ]
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)
original , ending = io.original_filename.split("/").last.split(".")
filename = original if( filename.blank? )
full_filename = filename + "." + ending
File.open(Rails.root.join(Image.asset_root, full_filename), "wb") do |f|
File.open(Rails.root.join("app/assets/images/cms/", full_filename), "wb") do |f|
f.write( io.read )
end
Image.new( full_filename )
@ -69,11 +70,17 @@ module Merged
Image.reload
end
def self.asset_root
def assert_name
image_root + "/" + self.name
end
private
def asset_root
"app/assets/images/" + image_root
end
def self.image_root
def image_root
"cms"
end

View File

@ -61,5 +61,8 @@ module Merged
template_style.fields.collect{|f| f.to_sym}
end
def image
Image.find_by_name(@attributes[:image])
end
end
end

View File

@ -31,7 +31,7 @@
-if( card.image.blank? )
%p No image
-else
= image_tag( "cms/" + card.image , class: "p-3")
= image_tag( card.image.assert_name , class: "p-3")
.basis-72.grow
%h3.mt-4.text-lg.font-bold Fields
= form_tag( card_url(card.id) , {method: :patch , class: "mx-auto mt-8 mb-0 max-w space-y-4" } ) do

View File

@ -6,12 +6,12 @@
= form_tag(merged.images_path, multipart: true) do
= render "merged/images/new_image"
-@images.each do |name , image|
-@images.each do |image|
.flex.flex-col.justify-start.border.rounded.border-gray-100.drop-shadow-lg
.flex.justify-between
.text-xl.m-2= image.name
%strong.inline-block.rounded.bg-yellow-50.px-2.py-3.text-md.font-medium
= aspect_ratio(image)
.p-2.object-contain
= link_to( card_set_image_path( image: name)) do
=image_tag("#{image_root}/#{name}" )
= link_to( card_set_image_path( image: image.name)) do
=image_tag( image.asset_name )

View File

@ -7,15 +7,15 @@
.grid.grid-cols-6.gap-4.m-8
= form_tag({action: :create}, multipart: true) do
= render "new_image"
-@images.each do |name , image|
.flex.flex-col.border.border-gray-100.rounded
-@images.each do |image|
.flex.flex-col.border.border-gray-100.rounded.image_box
.flex.align-center.justify-between.mb-4
.text-lg.font-bold.mt-2.mx-2
= image.name
%strong.inline-block.rounded.bg-yellow-200.px-3.py-1.text-md.font-medium
= aspect_ratio(image)
.w-full.object-contain.h-72
=image_tag("cms/#{name}")
=image_tag(image.assert_name)
.flex.justify-between.mb-8
%strong.inline-block.rounded.bg-orange-50.px-3.py-1.text-md.font-medium
= "#{image.size}k"

View File

@ -44,7 +44,7 @@
= link_to(section_select_image_url(section.id)) do
%h3.mt-4.text-lg.font-bold Image
-if section.image
= image_tag( "cms/" + section.image , class: "h-40")
= image_tag( section.image.assert_name , class: "h-40")
-else
%p No image
.basis-52.grow

View File

@ -6,12 +6,12 @@
= form_tag(merged.images_path, multipart: true) do
= render "merged/images/new_image"
-@images.each do |name , image|
-@images.each do |image|
.flex.flex-col.justify-start.border.rounded.border-gray-100.drop-shadow-lg
.flex.justify-between
.text-xl.m-2= image.name
%strong.inline-block.rounded.bg-yellow-50.px-2.py-3.text-md.font-medium
= aspect_ratio(image)
.p-2.object-contain
= link_to( section_set_image_path( image: name)) do
=image_tag("#{image_root}/#{name}" )
= link_to( section_set_image_path( image: image.name)) do
=image_tag(image.asset_name )

View File

@ -18,7 +18,7 @@
-if( @section.image.blank? )
%p No image
-else
= image_tag( "cms/" + @section.image , class: "my-3")
= image_tag( @section.image.assert_name , class: "my-3")
= yellow_button("Change Image", section_select_image_url(@section.id))
= red_button( "Remove image", section_set_image_path( @section.id , image: ""))

View File

@ -7,5 +7,11 @@ RSpec.feature "Images", type: :feature do
expect(page).to have_title("Dummy")
expect(page).to have_text("Pages")
end
it "has picures" do
expexted_num = Merged::Image.count
visit "/merged/images"
found= find_all(".image_box").count
expect(found).to be expexted_num
end
end
end