From c58b5fc4ba4ef138dcf7dd50592bdebac378cbda Mon Sep 17 00:00:00 2001 From: Torsten Date: Sun, 15 Jan 2023 21:23:36 +0200 Subject: [PATCH] shuffling code so it can be reused outside the engine --- app/helpers/merged/merged_helper.rb | 29 +----------------------- app/models/merged/image.rb | 24 ++++++++------------ lib/merged/engine.rb | 1 + lib/merged/shared_helper.rb | 34 +++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 43 deletions(-) create mode 100644 lib/merged/shared_helper.rb diff --git a/app/helpers/merged/merged_helper.rb b/app/helpers/merged/merged_helper.rb index ae6a2e1..8e6ac34 100644 --- a/app/helpers/merged/merged_helper.rb +++ b/app/helpers/merged/merged_helper.rb @@ -1,35 +1,8 @@ -require "redcarpet" module Merged module MergedHelper include OptionsHelper - - def renderer - options = {hard_wrap: true , autolink: true, no_intra_emphasis: true , - safe_links_only: true, no_styles: true , - link_attributes: { target: '_blank' }} - html = Redcarpet::Render::HTML.new(options) - Redcarpet::Markdown.new(html, options) - end - - def prose_classes - classes = "prose lg:prose-lg " - classes += "prose-headings:text-inherit " - { class: classes } - end - - def markdown_image(section) - return "" unless section.text - down = self.renderer.render(section.text) - image = image_for(section) - down.gsub("IMAGE" , image).html_safe - end - - def markdown(text) - text = text.text unless text.is_a?(String) - return "" if text.blank? - self.renderer.render(text).html_safe - end + include SharedHelper def aspect_ratio image x , y = image.aspect_ratio diff --git a/app/models/merged/image.rb b/app/models/merged/image.rb index 74401fe..10b1643 100644 --- a/app/models/merged/image.rb +++ b/app/models/merged/image.rb @@ -25,34 +25,28 @@ module Merged end def copy() - image = Image.new name: "#{name}_copy" , type: type , tags: (tags || "") + image = Image.new name: "#{name}_copy" , type: type , tags: (tags || "") , + width: width , height: height, size: size Image.insert(image) # assigns next id FileUtils.cp full_filename , image.full_filename - image.init_file_data image end #save an io as new image. The filename is the id, type taken from io def self.create_new(name , tags, io) - original , ending = io.original_filename.split("/").last.split(".") + original , end_ = io.original_filename.split("/").last.split(".") + magick_image = MiniMagick::Image.read(io) + ending = magick_image.type.downcase name = original if( name.blank? ) - image = Image.new name: name , type: ending , tags: (tags || "") + image = Image.new name: name , type: ending , tags: (tags || "") , + width: magick_image.width, height: magick_image.height , + size: (magick_image.size/1024).to_i self.insert(image) # assigns next id 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 - image.init_file_data + magick_image.write(Rails.root.join("app/assets/images/cms/", full_filename)) image end - def init_file_data - image = MiniMagick::Image.new(full_filename) - self.width = image.width - self.height = image.height - self.size = (image.size/1024).to_i - end - def destroy(editor) File.delete self.full_filename delete_save!(editor) diff --git a/lib/merged/engine.rb b/lib/merged/engine.rb index c40491a..a6d1ccd 100644 --- a/lib/merged/engine.rb +++ b/lib/merged/engine.rb @@ -4,6 +4,7 @@ require "ruby2js/es2015" require "ruby2js/filter/functions" require "ruby2js/haml" require "ruby2js/filter/vue" +require "merged/shared_helper" module Merged class Engine < ::Rails::Engine diff --git a/lib/merged/shared_helper.rb b/lib/merged/shared_helper.rb new file mode 100644 index 0000000..0990fe9 --- /dev/null +++ b/lib/merged/shared_helper.rb @@ -0,0 +1,34 @@ +require "redcarpet" + +module Merged + module SharedHelper + + def renderer + options = {hard_wrap: true , autolink: true, no_intra_emphasis: true , + safe_links_only: true, no_styles: true , + link_attributes: { target: '_blank' }} + html = Redcarpet::Render::HTML.new(options) + Redcarpet::Markdown.new(html, options) + end + + def prose_classes + classes = "prose lg:prose-lg " + classes += "prose-headings:text-inherit " + { class: classes } + end + + def markdown_image(section) + return "" unless section.text + down = self.renderer.render(section.text) + image = image_for(section) + down.gsub("IMAGE" , image).html_safe + end + + def markdown(text) + text = text.text unless text.is_a?(String) + return "" if text.blank? + self.renderer.render(text).html_safe + end + + end +end