make base to image connectio and use image instances instead of names
This commit is contained in:
parent
4d4de51c8b
commit
27bf4f4269
@ -19,10 +19,6 @@ module Merged
|
|||||||
"#{x} / #{y}"
|
"#{x} / #{y}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def image_root
|
|
||||||
Image.image_root
|
|
||||||
end
|
|
||||||
|
|
||||||
def markdown(text)
|
def markdown(text)
|
||||||
text = text.text unless text.is_a?(String)
|
text = text.text unless text.is_a?(String)
|
||||||
return "" if text.blank?
|
return "" if text.blank?
|
||||||
|
@ -7,8 +7,7 @@ module Merged
|
|||||||
def bg(section , clazz = "")
|
def bg(section , clazz = "")
|
||||||
attributes = {class: clazz}
|
attributes = {class: clazz}
|
||||||
return attributes if section.image.blank?
|
return attributes if section.image.blank?
|
||||||
#puts "--#{Image.image_root}/#{section.image}--"
|
img = asset_url( section.image.assert_name )
|
||||||
img = asset_url( "#{Image.image_root}/#{section.image}" )
|
|
||||||
attributes["style"] = "background-image: url('#{img}');"
|
attributes["style"] = "background-image: url('#{img}');"
|
||||||
if(section.option("fixed") == "on")
|
if(section.option("fixed") == "on")
|
||||||
attributes[:class] = attributes[:class] + " bg-fixed"
|
attributes[:class] = attributes[:class] + " bg-fixed"
|
||||||
@ -19,7 +18,7 @@ module Merged
|
|||||||
# works for with sections and cards that respond to .image
|
# works for with sections and cards that respond to .image
|
||||||
def image_for(element , classes = "")
|
def image_for(element , classes = "")
|
||||||
return "" if element.image.blank?
|
return "" if element.image.blank?
|
||||||
image_tag("#{Image.image_root}/#{element.image}" , class: classes)
|
image_tag(element.image.assert_name , class: classes)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -21,7 +21,7 @@ module Merged
|
|||||||
name , type = filename.split(".")
|
name , type = filename.split(".")
|
||||||
self.name = name
|
self.name = name
|
||||||
self.type = type
|
self.type = type
|
||||||
fullname = Rails.root.join(Image.asset_root,filename)
|
fullname = Rails.root.join(asset_root,filename)
|
||||||
file = File.new(fullname)
|
file = File.new(fullname)
|
||||||
image = MiniMagick::Image.new(fullname)
|
image = MiniMagick::Image.new(fullname)
|
||||||
self.width = image.width
|
self.width = image.width
|
||||||
@ -31,18 +31,19 @@ module Merged
|
|||||||
end
|
end
|
||||||
|
|
||||||
def aspect_ratio
|
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 }
|
ratios = (1..9).collect{ |i| ((ratio * i) - (ratio * i).round(0)).abs }
|
||||||
min , min_index = ratios.each_with_index.min
|
min , min_index = ratios.each_with_index.min
|
||||||
[(ratio * (min_index + 1) ).to_i , (min_index + 1) ]
|
[(ratio * (min_index + 1) ).to_i , (min_index + 1) ]
|
||||||
end
|
end
|
||||||
|
|
||||||
#save an io with given name (without ending, that is taken from io)
|
#save an io with given name (without ending, that is taken from io)
|
||||||
|
#Should save to tmp first
|
||||||
def self.create_new(filename , io)
|
def self.create_new(filename , io)
|
||||||
original , ending = io.original_filename.split("/").last.split(".")
|
original , ending = io.original_filename.split("/").last.split(".")
|
||||||
filename = original if( filename.blank? )
|
filename = original if( filename.blank? )
|
||||||
full_filename = filename + "." + ending
|
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 )
|
f.write( io.read )
|
||||||
end
|
end
|
||||||
Image.new( full_filename )
|
Image.new( full_filename )
|
||||||
@ -69,11 +70,17 @@ module Merged
|
|||||||
Image.reload
|
Image.reload
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.asset_root
|
def assert_name
|
||||||
|
image_root + "/" + self.name
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def asset_root
|
||||||
"app/assets/images/" + image_root
|
"app/assets/images/" + image_root
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.image_root
|
def image_root
|
||||||
"cms"
|
"cms"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -61,5 +61,8 @@ module Merged
|
|||||||
template_style.fields.collect{|f| f.to_sym}
|
template_style.fields.collect{|f| f.to_sym}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def image
|
||||||
|
Image.find_by_name(@attributes[:image])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -31,7 +31,7 @@
|
|||||||
-if( card.image.blank? )
|
-if( card.image.blank? )
|
||||||
%p No image
|
%p No image
|
||||||
-else
|
-else
|
||||||
= image_tag( "cms/" + card.image , class: "p-3")
|
= image_tag( card.image.assert_name , class: "p-3")
|
||||||
.basis-72.grow
|
.basis-72.grow
|
||||||
%h3.mt-4.text-lg.font-bold Fields
|
%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
|
= form_tag( card_url(card.id) , {method: :patch , class: "mx-auto mt-8 mb-0 max-w space-y-4" } ) do
|
||||||
|
@ -6,12 +6,12 @@
|
|||||||
= form_tag(merged.images_path, multipart: true) do
|
= form_tag(merged.images_path, multipart: true) do
|
||||||
= render "merged/images/new_image"
|
= 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.flex-col.justify-start.border.rounded.border-gray-100.drop-shadow-lg
|
||||||
.flex.justify-between
|
.flex.justify-between
|
||||||
.text-xl.m-2= image.name
|
.text-xl.m-2= image.name
|
||||||
%strong.inline-block.rounded.bg-yellow-50.px-2.py-3.text-md.font-medium
|
%strong.inline-block.rounded.bg-yellow-50.px-2.py-3.text-md.font-medium
|
||||||
= aspect_ratio(image)
|
= aspect_ratio(image)
|
||||||
.p-2.object-contain
|
.p-2.object-contain
|
||||||
= link_to( card_set_image_path( image: name)) do
|
= link_to( card_set_image_path( image: image.name)) do
|
||||||
=image_tag("#{image_root}/#{name}" )
|
=image_tag( image.asset_name )
|
||||||
|
@ -7,15 +7,15 @@
|
|||||||
.grid.grid-cols-6.gap-4.m-8
|
.grid.grid-cols-6.gap-4.m-8
|
||||||
= form_tag({action: :create}, multipart: true) do
|
= form_tag({action: :create}, multipart: true) do
|
||||||
= render "new_image"
|
= render "new_image"
|
||||||
-@images.each do |name , image|
|
-@images.each do |image|
|
||||||
.flex.flex-col.border.border-gray-100.rounded
|
.flex.flex-col.border.border-gray-100.rounded.image_box
|
||||||
.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
|
||||||
= image.name
|
= image.name
|
||||||
%strong.inline-block.rounded.bg-yellow-200.px-3.py-1.text-md.font-medium
|
%strong.inline-block.rounded.bg-yellow-200.px-3.py-1.text-md.font-medium
|
||||||
= aspect_ratio(image)
|
= aspect_ratio(image)
|
||||||
.w-full.object-contain.h-72
|
.w-full.object-contain.h-72
|
||||||
=image_tag("cms/#{name}")
|
=image_tag(image.assert_name)
|
||||||
.flex.justify-between.mb-8
|
.flex.justify-between.mb-8
|
||||||
%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"
|
||||||
|
@ -44,7 +44,7 @@
|
|||||||
= link_to(section_select_image_url(section.id)) do
|
= link_to(section_select_image_url(section.id)) do
|
||||||
%h3.mt-4.text-lg.font-bold Image
|
%h3.mt-4.text-lg.font-bold Image
|
||||||
-if section.image
|
-if section.image
|
||||||
= image_tag( "cms/" + section.image , class: "h-40")
|
= image_tag( section.image.assert_name , class: "h-40")
|
||||||
-else
|
-else
|
||||||
%p No image
|
%p No image
|
||||||
.basis-52.grow
|
.basis-52.grow
|
||||||
|
@ -6,12 +6,12 @@
|
|||||||
= form_tag(merged.images_path, multipart: true) do
|
= form_tag(merged.images_path, multipart: true) do
|
||||||
= render "merged/images/new_image"
|
= 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.flex-col.justify-start.border.rounded.border-gray-100.drop-shadow-lg
|
||||||
.flex.justify-between
|
.flex.justify-between
|
||||||
.text-xl.m-2= image.name
|
.text-xl.m-2= image.name
|
||||||
%strong.inline-block.rounded.bg-yellow-50.px-2.py-3.text-md.font-medium
|
%strong.inline-block.rounded.bg-yellow-50.px-2.py-3.text-md.font-medium
|
||||||
= aspect_ratio(image)
|
= aspect_ratio(image)
|
||||||
.p-2.object-contain
|
.p-2.object-contain
|
||||||
= link_to( section_set_image_path( image: name)) do
|
= link_to( section_set_image_path( image: image.name)) do
|
||||||
=image_tag("#{image_root}/#{name}" )
|
=image_tag(image.asset_name )
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
-if( @section.image.blank? )
|
-if( @section.image.blank? )
|
||||||
%p No image
|
%p No image
|
||||||
-else
|
-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))
|
= yellow_button("Change Image", section_select_image_url(@section.id))
|
||||||
= red_button( "Remove image", section_set_image_path( @section.id , image: ""))
|
= red_button( "Remove image", section_set_image_path( @section.id , image: ""))
|
||||||
|
|
||||||
|
@ -7,5 +7,11 @@ RSpec.feature "Images", type: :feature do
|
|||||||
expect(page).to have_title("Dummy")
|
expect(page).to have_title("Dummy")
|
||||||
expect(page).to have_text("Pages")
|
expect(page).to have_text("Pages")
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user