From df67ed16b51eabb562a22d25778078491d815ac6 Mon Sep 17 00:00:00 2001 From: Torsten Date: Fri, 27 Jan 2023 19:08:57 +0200 Subject: [PATCH] multi source style loading --- app/controllers/merged/changes_controller.rb | 6 +++--- app/helpers/merged/changes_helper.rb | 4 ++-- app/models/merged/image.rb | 10 +--------- app/models/merged/shared_base.rb | 6 ++++-- lib/generators/merged/install/templates/empty.yml | 2 +- .../merged/install/templates/initializer.rb | 6 ++++-- lib/merged.rb | 13 ------------- lib/merged/engine.rb | 14 +++++++++++++- .../app/assets/images/{cms => merged}/12.webp | Bin .../app/assets/images/{cms => merged}/16.jpg | Bin .../app/assets/images/{cms => merged}/17.webp | Bin .../app/assets/images/{cms => merged}/20.webp | Bin .../app/assets/images/{cms => merged}/25.webp | Bin .../app/assets/images/{cms => merged}/26.webp | Bin .../app/assets/images/{cms => merged}/27.jpg | Bin .../app/assets/images/{cms => merged}/28.webp | Bin .../app/assets/images/{cms => merged}/29.webp | Bin .../app/assets/images/{cms => merged}/30.webp | Bin .../app/assets/images/{cms => merged}/35.webp | Bin .../app/assets/images/{cms => merged}/36.webp | Bin .../app/assets/images/{cms => merged}/37.jpg | Bin .../app/assets/images/{cms => merged}/4.webp | Bin .../app/assets/images/{cms => merged}/40.webp | Bin .../app/assets/images/{cms => merged}/7.webp | Bin .../app/assets/images/{cms => merged}/8.webp | Bin test/dummy/config/initializers/merged.rb | 9 +++++++++ 26 files changed, 37 insertions(+), 33 deletions(-) rename test/dummy/app/assets/images/{cms => merged}/12.webp (100%) rename test/dummy/app/assets/images/{cms => merged}/16.jpg (100%) rename test/dummy/app/assets/images/{cms => merged}/17.webp (100%) rename test/dummy/app/assets/images/{cms => merged}/20.webp (100%) rename test/dummy/app/assets/images/{cms => merged}/25.webp (100%) rename test/dummy/app/assets/images/{cms => merged}/26.webp (100%) rename test/dummy/app/assets/images/{cms => merged}/27.jpg (100%) rename test/dummy/app/assets/images/{cms => merged}/28.webp (100%) rename test/dummy/app/assets/images/{cms => merged}/29.webp (100%) rename test/dummy/app/assets/images/{cms => merged}/30.webp (100%) rename test/dummy/app/assets/images/{cms => merged}/35.webp (100%) rename test/dummy/app/assets/images/{cms => merged}/36.webp (100%) rename test/dummy/app/assets/images/{cms => merged}/37.jpg (100%) rename test/dummy/app/assets/images/{cms => merged}/4.webp (100%) rename test/dummy/app/assets/images/{cms => merged}/40.webp (100%) rename test/dummy/app/assets/images/{cms => merged}/7.webp (100%) rename test/dummy/app/assets/images/{cms => merged}/8.webp (100%) create mode 100644 test/dummy/config/initializers/merged.rb diff --git a/app/controllers/merged/changes_controller.rb b/app/controllers/merged/changes_controller.rb index a0f2b5d..a27313b 100644 --- a/app/controllers/merged/changes_controller.rb +++ b/app/controllers/merged/changes_controller.rb @@ -15,7 +15,7 @@ module Merged else git = Git.open(Rails.root) git.add("merged") - git.add( Image.root ) + git.add( Image.asset_root ) begin git.config('user.email', current_member.email) git.commit(params[:message]) @@ -30,8 +30,8 @@ module Merged def reset git = Git.open(Rails.root) begin - git.checkout_file("HEAD" , "merged") - git.checkout_file("HEAD" , "app/assets/images/cms") + git.checkout_file("HEAD" , Merged.data_dir) + git.checkout_file("HEAD" , Merged::Image.asset_root) ChangeSet.current.zero message = "Changes reset" rescue diff --git a/app/helpers/merged/changes_helper.rb b/app/helpers/merged/changes_helper.rb index caacb2e..dbb4961 100644 --- a/app/helpers/merged/changes_helper.rb +++ b/app/helpers/merged/changes_helper.rb @@ -2,8 +2,8 @@ module Merged module ChangesHelper def cms_part? name - return true if name.include?( Image.root ) - return true if name.include?("merged/") + return true if name.include?( Merged.images_dir ) + return true if name.include?(Merged.data_dir) false end diff --git a/app/models/merged/image.rb b/app/models/merged/image.rb index f055e68..964b6a7 100644 --- a/app/models/merged/image.rb +++ b/app/models/merged/image.rb @@ -65,7 +65,7 @@ module Merged end def asset_name - image_root + "/" + self.id.to_s + "." + self.type + Merged.images_dir + "/" + self.id.to_s + "." + self.type end def full_filename @@ -87,10 +87,6 @@ module Merged end end - def self.root - "app/assets/images/cms" - end - def init_file_size magick_image = MiniMagick::Image.open(full_filename) self.width = magick_image.width @@ -102,9 +98,5 @@ module Merged "app/assets/images/" + Merged.images_dir end - def image_root - Merged.images_dir - end - end end diff --git a/app/models/merged/shared_base.rb b/app/models/merged/shared_base.rb index 8841b23..6927a0d 100644 --- a/app/models/merged/shared_base.rb +++ b/app/models/merged/shared_base.rb @@ -3,11 +3,13 @@ module Merged # takes a little footwork, as seen below class SharedBase < ActiveYaml::Base def self.raw_data - paths = [ Merged::Engine.root.join("config")] + paths = [ Merged::Engine.root.join("config") , Rails.root.join(Merged.data_dir) ] loaded_files = paths.collect do |path| actual_filename = filename || name.tableize full = File.join(path, "#{actual_filename}.#{extension}") - load_path(full) + got = load_path(full) + raise "#{full} contained #{got.class}, not Array" unless got.class == Array + got end loaded_files.sum end diff --git a/lib/generators/merged/install/templates/empty.yml b/lib/generators/merged/install/templates/empty.yml index 73ee34c..dcd024e 100644 --- a/lib/generators/merged/install/templates/empty.yml +++ b/lib/generators/merged/install/templates/empty.yml @@ -1 +1 @@ ----[] +--- [] diff --git a/lib/generators/merged/install/templates/initializer.rb b/lib/generators/merged/install/templates/initializer.rb index 013f49d..88fe621 100644 --- a/lib/generators/merged/install/templates/initializer.rb +++ b/lib/generators/merged/install/templates/initializer.rb @@ -1,7 +1,9 @@ require "merged" -# directory inside /app/assets/images where the images are kept -Merged.images_dir = "cms" +# directory inside /app/assets/images where YOUR images are kept +# if you change this and add own styles, you will still need a merged directory +# for the previews (card_preview and section_preview) +Merged.images_dir = "merged" # directory where data and styles are kept # Notice that the data is ALWAYS inside a merged directory, diff --git a/lib/merged.rb b/lib/merged.rb index 83d50ed..4c75997 100644 --- a/lib/merged.rb +++ b/lib/merged.rb @@ -7,21 +7,8 @@ module Merged # Directory inside the app/assets/images mattr_accessor :images_dir - @@images_dir = "cms" # directory in root to store data mattr_accessor :data_dir - @@data_dir = "." - def self.load_data - # pre-load data - [OptionDefinition, CardStyle, SectionStyle , PageStyle, - Card , Section , Page , Image].each {|clazz| clazz.all } - end - - def self.load_from( kind , path ) - return unless File.exists?(path) - clazz = "Merged::#{kind.camelcase}".constantize - clazz.load(YAML.load_file( path )) - end end diff --git a/lib/merged/engine.rb b/lib/merged/engine.rb index 1424061..f35bd97 100644 --- a/lib/merged/engine.rb +++ b/lib/merged/engine.rb @@ -20,7 +20,7 @@ module Merged initializer "after_initialize" do |app| ActiveSupport::Reloader.to_prepare do - Merged.load_data + Merged::Engine.load_data end end @@ -32,5 +32,17 @@ module Merged config.assets.precompile << kid end end + def self.load_data + # pre-load data + [OptionDefinition, CardStyle, SectionStyle , PageStyle, + Card , Section , Page , Image].each {|clazz| clazz.all } + end + + def self.load_from( kind , path ) + return unless File.exists?(path) + clazz = "Merged::#{kind.camelcase}".constantize + clazz.load(YAML.load_file( path )) + end + end end diff --git a/test/dummy/app/assets/images/cms/12.webp b/test/dummy/app/assets/images/merged/12.webp similarity index 100% rename from test/dummy/app/assets/images/cms/12.webp rename to test/dummy/app/assets/images/merged/12.webp diff --git a/test/dummy/app/assets/images/cms/16.jpg b/test/dummy/app/assets/images/merged/16.jpg similarity index 100% rename from test/dummy/app/assets/images/cms/16.jpg rename to test/dummy/app/assets/images/merged/16.jpg diff --git a/test/dummy/app/assets/images/cms/17.webp b/test/dummy/app/assets/images/merged/17.webp similarity index 100% rename from test/dummy/app/assets/images/cms/17.webp rename to test/dummy/app/assets/images/merged/17.webp diff --git a/test/dummy/app/assets/images/cms/20.webp b/test/dummy/app/assets/images/merged/20.webp similarity index 100% rename from test/dummy/app/assets/images/cms/20.webp rename to test/dummy/app/assets/images/merged/20.webp diff --git a/test/dummy/app/assets/images/cms/25.webp b/test/dummy/app/assets/images/merged/25.webp similarity index 100% rename from test/dummy/app/assets/images/cms/25.webp rename to test/dummy/app/assets/images/merged/25.webp diff --git a/test/dummy/app/assets/images/cms/26.webp b/test/dummy/app/assets/images/merged/26.webp similarity index 100% rename from test/dummy/app/assets/images/cms/26.webp rename to test/dummy/app/assets/images/merged/26.webp diff --git a/test/dummy/app/assets/images/cms/27.jpg b/test/dummy/app/assets/images/merged/27.jpg similarity index 100% rename from test/dummy/app/assets/images/cms/27.jpg rename to test/dummy/app/assets/images/merged/27.jpg diff --git a/test/dummy/app/assets/images/cms/28.webp b/test/dummy/app/assets/images/merged/28.webp similarity index 100% rename from test/dummy/app/assets/images/cms/28.webp rename to test/dummy/app/assets/images/merged/28.webp diff --git a/test/dummy/app/assets/images/cms/29.webp b/test/dummy/app/assets/images/merged/29.webp similarity index 100% rename from test/dummy/app/assets/images/cms/29.webp rename to test/dummy/app/assets/images/merged/29.webp diff --git a/test/dummy/app/assets/images/cms/30.webp b/test/dummy/app/assets/images/merged/30.webp similarity index 100% rename from test/dummy/app/assets/images/cms/30.webp rename to test/dummy/app/assets/images/merged/30.webp diff --git a/test/dummy/app/assets/images/cms/35.webp b/test/dummy/app/assets/images/merged/35.webp similarity index 100% rename from test/dummy/app/assets/images/cms/35.webp rename to test/dummy/app/assets/images/merged/35.webp diff --git a/test/dummy/app/assets/images/cms/36.webp b/test/dummy/app/assets/images/merged/36.webp similarity index 100% rename from test/dummy/app/assets/images/cms/36.webp rename to test/dummy/app/assets/images/merged/36.webp diff --git a/test/dummy/app/assets/images/cms/37.jpg b/test/dummy/app/assets/images/merged/37.jpg similarity index 100% rename from test/dummy/app/assets/images/cms/37.jpg rename to test/dummy/app/assets/images/merged/37.jpg diff --git a/test/dummy/app/assets/images/cms/4.webp b/test/dummy/app/assets/images/merged/4.webp similarity index 100% rename from test/dummy/app/assets/images/cms/4.webp rename to test/dummy/app/assets/images/merged/4.webp diff --git a/test/dummy/app/assets/images/cms/40.webp b/test/dummy/app/assets/images/merged/40.webp similarity index 100% rename from test/dummy/app/assets/images/cms/40.webp rename to test/dummy/app/assets/images/merged/40.webp diff --git a/test/dummy/app/assets/images/cms/7.webp b/test/dummy/app/assets/images/merged/7.webp similarity index 100% rename from test/dummy/app/assets/images/cms/7.webp rename to test/dummy/app/assets/images/merged/7.webp diff --git a/test/dummy/app/assets/images/cms/8.webp b/test/dummy/app/assets/images/merged/8.webp similarity index 100% rename from test/dummy/app/assets/images/cms/8.webp rename to test/dummy/app/assets/images/merged/8.webp diff --git a/test/dummy/config/initializers/merged.rb b/test/dummy/config/initializers/merged.rb new file mode 100644 index 0000000..b335466 --- /dev/null +++ b/test/dummy/config/initializers/merged.rb @@ -0,0 +1,9 @@ +require "merged" + +# directory inside /app/assets/images where the images are kept +Merged.images_dir = "merged" + +# directory where data and styles are kept +# Notice that the data is ALWAYS inside a merged directory, +# so in the default case Rails.root/merged/*.yml +Merged.data_dir = "."