From 04e189f913e63b7c510537f9a3dbd80d91cca704 Mon Sep 17 00:00:00 2001 From: Torsten Date: Sat, 10 Dec 2022 20:29:31 +0200 Subject: [PATCH] changed card and section style to active_yaml (because of entanglement :-) --- app/controllers/merged/sections_controller.rb | 6 ++-- app/controllers/merged/styles_controller.rb | 4 +-- app/models/merged/card.rb | 5 +-- app/models/merged/card_style.rb | 32 ++++--------------- app/models/merged/option.rb | 4 --- app/models/merged/optioned.rb | 2 +- app/models/merged/section.rb | 6 ++-- app/models/merged/section_style.rb | 24 +++++++------- .../merged/sections/select_card_template.haml | 2 +- .../merged/sections/select_template.haml | 2 +- app/views/merged/styles/index.haml | 4 +-- .../{card_style.yaml => card_styles.yml} | 0 ...{section_style.yaml => section_styles.yml} | 0 lib/merged.rb | 9 +++--- spec/models/merged/card_style_spec.rb | 12 ++++--- spec/models/merged/section_style_spec.rb | 9 +++--- 16 files changed, 51 insertions(+), 70 deletions(-) rename config/merged/{card_style.yaml => card_styles.yml} (100%) rename config/merged/{section_style.yaml => section_styles.yml} (100%) diff --git a/app/controllers/merged/sections_controller.rb b/app/controllers/merged/sections_controller.rb index 791802d..2f52437 100644 --- a/app/controllers/merged/sections_controller.rb +++ b/app/controllers/merged/sections_controller.rb @@ -10,11 +10,13 @@ module Merged def select_image @images = Image.all end + def select_template - @sections = SectionStyle.sections + @sections = SectionStyle.all end + def select_card_template - @cards = CardStyle.cards + @cards = CardStyle.all end def new diff --git a/app/controllers/merged/styles_controller.rb b/app/controllers/merged/styles_controller.rb index 248c58f..5716d8a 100644 --- a/app/controllers/merged/styles_controller.rb +++ b/app/controllers/merged/styles_controller.rb @@ -2,8 +2,8 @@ module Merged class StylesController < MergedController def index - @sections = SectionStyle.sections - @cards = CardStyle.cards + @section_styles = SectionStyle.all + @cards_styles = CardStyle.all end end diff --git a/app/models/merged/card.rb b/app/models/merged/card.rb index 0846050..60172e3 100644 --- a/app/models/merged/card.rb +++ b/app/models/merged/card.rb @@ -65,15 +65,16 @@ module Merged end def template_style - CardStyle.cards[ section.card_template ] + CardStyle.find_by_template( section.card_template) end + def allowed_fields template_style.fields end def self.build_data(card_template) data = { "id" => SecureRandom.hex(10) } - CardStyle.cards[ card_template ].fields.each do |key| + CardStyle.find_by_template( card_template ).fields.each do |key| data[key] = key.upcase end data diff --git a/app/models/merged/card_style.rb b/app/models/merged/card_style.rb index 92f1e2d..57585f4 100644 --- a/app/models/merged/card_style.rb +++ b/app/models/merged/card_style.rb @@ -1,42 +1,22 @@ module Merged - class CardStyle - @@cards = {} + class CardStyle < ActiveYaml::Base + set_root_path Engine.root + "config" - attr_reader :content - - def initialize content - @content = content - end - - [:template , :text , :header, :fields ].each do |meth| - define_method(meth) do - @content[meth.to_s] - end - end + fields :template , :text , :header, :fields def card_preview "merged/card_preview/" + template end - def options + def options_definitions option_defs = [] - @content["options"].each do |name| + options.each do |name| option = Option.find_by_name(name) raise "no option for #{name}:#{name.class}" if option.blank? option_defs << option - end if @content["options"] + end if options option_defs end - def self.cards - @@cards - end - - def self.load( yaml ) - yaml.each do |content| - card = CardStyle.new(content) - @@cards[card.template] = card - end - end end end diff --git a/app/models/merged/option.rb b/app/models/merged/option.rb index 894aa96..c052e31 100644 --- a/app/models/merged/option.rb +++ b/app/models/merged/option.rb @@ -23,9 +23,5 @@ module Merged attributes[:values].split(" ") end - def self.load() - self.all - end - end end diff --git a/app/models/merged/optioned.rb b/app/models/merged/optioned.rb index 35c96b8..b8cb5c5 100644 --- a/app/models/merged/optioned.rb +++ b/app/models/merged/optioned.rb @@ -7,7 +7,7 @@ module Merged end def option_definitions - template_style.options + template_style.options_definitions end def option(name) diff --git a/app/models/merged/section.rb b/app/models/merged/section.rb index 405fa0d..24d5df5 100644 --- a/app/models/merged/section.rb +++ b/app/models/merged/section.rb @@ -37,7 +37,7 @@ module Merged new_style = template_style if(new_style.has_cards?) unless card_template - @content["card_template"] = CardStyle.cards.keys.first + @content["card_template"] = CardStyle.first.name @content["cards"] = [] raise "Should not have cards" unless cards.empty? end @@ -49,7 +49,7 @@ module Merged end def template_style - SectionStyle.sections[ template ] + SectionStyle.find_by_template( template ) end def allowed_fields @@ -153,7 +153,7 @@ module Merged end unless style.fields.blank? if(style.has_cards?) data["cards"] = [] - data["card_template"] = CardStyle.cards.keys.first + data["card_template"] = CardStyle.first.name end data end diff --git a/app/models/merged/section_style.rb b/app/models/merged/section_style.rb index ec0e71b..dcdb476 100644 --- a/app/models/merged/section_style.rb +++ b/app/models/merged/section_style.rb @@ -1,25 +1,25 @@ module Merged - class SectionStyle < CardStyle + class SectionStyle < ActiveYaml::Base + set_root_path Engine.root + "config" - @@sections = {} + fields :template , :text , :header, :fields , :cards def has_cards? - @content["cards"] == true + cards == true end def section_preview "merged/section_preview/" + template end - def self.sections - @@sections - end - - def self.load(yaml) - yaml.each do |content| - section = SectionStyle.new(content) - @@sections[section.template] = section - end + def options_definitions + option_defs = [] + options.each do |name| + option = Option.find_by_name(name) + raise "no option for #{name}:#{name.class}" if option.blank? + option_defs << option + end if options + option_defs end end diff --git a/app/views/merged/sections/select_card_template.haml b/app/views/merged/sections/select_card_template.haml index dfb2067..a8f8f01 100644 --- a/app/views/merged/sections/select_card_template.haml +++ b/app/views/merged/sections/select_card_template.haml @@ -1,5 +1,5 @@ .grid.grid-cols-4.gap-2.m-8 - - @cards.each do |name ,style| + - @cards.each do |style| .relative.block.border.border-gray-100 = link_to( section_set_card_template_path( card_template: style.template )) do =image_tag(style.card_preview , class: "h-56 w-full object-contain lg:h-72") diff --git a/app/views/merged/sections/select_template.haml b/app/views/merged/sections/select_template.haml index 2fac17b..ea84267 100644 --- a/app/views/merged/sections/select_template.haml +++ b/app/views/merged/sections/select_template.haml @@ -9,7 +9,7 @@ Select Template for Section #{@section.index + 1} .grid.grid-cols-4.gap-2.m-8 - - @sections.each do |name , style| + - @sections.each do |style| .relative.block.border.border-gray-100 = link_to( section_set_template_path( template: style.template )) do =image_tag(style.section_preview , class: "h-56 w-full object-contain lg:h-72") diff --git a/app/views/merged/styles/index.haml b/app/views/merged/styles/index.haml index a402380..bca6181 100644 --- a/app/views/merged/styles/index.haml +++ b/app/views/merged/styles/index.haml @@ -2,7 +2,7 @@ %h1.text-4xl.font-bold Section styles .grid.grid-cols-3.gap-4.m-8 - -@sections.each do |name , style| + -@section_styles.each do | style| %article.overflow-hidden.rounded-lg.border.border-gray-100.shadow-sm =image_tag(style.section_preview , class: "h-56 w-full object-contain lg:h-72") %p.mt-2.text-sm.text-gray-500 @@ -31,7 +31,7 @@ %h1.text-4xl.font-bold Card styles .grid.grid-cols-3.gap-4.m-8 - -@cards.each do |name , style| + -@cards_styles.each do |style| %article.overflow-hidden.rounded-lg.border.border-gray-100.shadow-sm =image_tag(style.card_preview , class: "h-56 w-full object-contain lg:h-72") %p.mt-2.text-sm.text-gray-500 diff --git a/config/merged/card_style.yaml b/config/merged/card_styles.yml similarity index 100% rename from config/merged/card_style.yaml rename to config/merged/card_styles.yml diff --git a/config/merged/section_style.yaml b/config/merged/section_styles.yml similarity index 100% rename from config/merged/section_style.yaml rename to config/merged/section_styles.yml diff --git a/lib/merged.rb b/lib/merged.rb index baba471..515f720 100644 --- a/lib/merged.rb +++ b/lib/merged.rb @@ -4,11 +4,10 @@ require "merged/engine" module Merged def self.load_data - ["card_style" , "section_style"].each do |kind| - # loading egine definitions first, can be overriden - load_from kind , Engine.root.join("config/merged/#{kind}.yaml") - end - Option.load + # pre-load data + Option.all + CardStyle.all + SectionStyle.all Page.load_pages Image.load_images end diff --git a/spec/models/merged/card_style_spec.rb b/spec/models/merged/card_style_spec.rb index 97bcd63..ba2316f 100644 --- a/spec/models/merged/card_style_spec.rb +++ b/spec/models/merged/card_style_spec.rb @@ -2,12 +2,16 @@ require 'rails_helper' module Merged RSpec.describe CardStyle, type: :model do - let(:first_card) {Style.cards.first} + let(:first_card) {CardStyle.all.first} - it "has Style.cards" do - expect(CardStyle.cards.class).to be Hash - expect(CardStyle.cards.length).to be 5 + it "has .all" do + expect(CardStyle.all.length).to be 5 end + it "has fields" do + expect(first_card.fields.class).to be Array + expect(first_card.fields.length).to be 2 + expect(first_card.fields.first).to eq "header" + end end end diff --git a/spec/models/merged/section_style_spec.rb b/spec/models/merged/section_style_spec.rb index 3d3c130..c5c8a09 100644 --- a/spec/models/merged/section_style_spec.rb +++ b/spec/models/merged/section_style_spec.rb @@ -2,18 +2,17 @@ require 'rails_helper' module Merged RSpec.describe SectionStyle, type: :model do - let(:first_section) {SectionStyle.sections.first} + let(:first_section) {SectionStyle.all.first} it "has Style.sections" do - expect(SectionStyle.sections.class).to be Hash - expect(SectionStyle.sections.length).to be 7 + expect(SectionStyle.all.length).to be 7 end it "Finds section by template" do - spacer = SectionStyle.sections["section_spacer"] + spacer = SectionStyle.find_by_template("section_spacer") expect(spacer).not_to eq nil end it "Spacer has no fields" do - spacer = SectionStyle.sections["section_spacer"] + spacer = SectionStyle.find_by_template("section_spacer") expect(spacer.fields).to be nil end