From 48cfcb592da24ebffc80656b927cc09cbddfc36e Mon Sep 17 00:00:00 2001 From: Torsten Date: Fri, 2 Dec 2022 11:10:38 +0200 Subject: [PATCH] change views to use objects instead of yaml --- app/controllers/merged/view_controller.rb | 5 +---- app/helpers/merged/view_helper.rb | 10 +++++----- app/models/merged/section.rb | 19 +++++++++++++++++++ app/views/merged/elements/_button.haml | 2 -- app/views/merged/view/_section_2_col.haml | 8 ++++---- .../merged/view/_section_full_left1.haml | 8 ++++---- .../merged/view/_section_full_left2.haml | 9 ++++----- app/views/merged/view/_section_full_up.haml | 4 ++-- .../merged/view/_section_half_header.haml | 6 +++--- app/views/merged/view/_section_half_left.haml | 8 ++++---- .../merged/view/_section_half_right.haml | 8 ++++---- .../merged/view/cards/_card_full_image.haml | 4 ++-- app/views/merged/view/elements/_button.haml | 4 ++-- app/views/merged/view/view.haml | 2 +- 14 files changed, 55 insertions(+), 42 deletions(-) delete mode 100644 app/views/merged/elements/_button.haml diff --git a/app/controllers/merged/view_controller.rb b/app/controllers/merged/view_controller.rb index a7c6caa..30feff6 100644 --- a/app/controllers/merged/view_controller.rb +++ b/app/controllers/merged/view_controller.rb @@ -2,10 +2,7 @@ module Merged class ViewController < ::ApplicationController def view - page = params[:id] - # assert file exists - @data = YAML.load_file(Rails.root.join('cms' , "#{page}.yaml")) - #assert data is an array (of hashes?) + @page = Page.find(params[:id]) end end diff --git a/app/helpers/merged/view_helper.rb b/app/helpers/merged/view_helper.rb index 7a5e4fc..08014a1 100644 --- a/app/helpers/merged/view_helper.rb +++ b/app/helpers/merged/view_helper.rb @@ -2,19 +2,19 @@ module Merged module ViewHelper # section should be hash with at least 'template' key def find_template(section) - "merged/view/" + section["template"] + "merged/view/" + section.template end def bg(section) - puts "--#{Image.root}/#{section['image']}--" - img = asset_url( "#{Image.root}/#{section['image']}" ) + puts "--#{Image.image_root}/#{section.image}--" + img = asset_url( "#{Image.image_root}/#{section.image}" ) "background-image: url('#{img}');" end def image_for(section , classes) - image_tag("#{Image.root}/#{section['image']}" , class: classes) + image_tag("#{Image.image_root}/#{section.image}" , class: classes) end def has_button(section) - section['button'] + section.content['button'] end end end diff --git a/app/models/merged/section.rb b/app/models/merged/section.rb index aef1b89..1bd241b 100644 --- a/app/models/merged/section.rb +++ b/app/models/merged/section.rb @@ -31,6 +31,25 @@ module Merged end end + [:button_link , :button_text ].each do |meth| + define_method(meth) do + @content["options"][meth.to_s] + end + end + + def has_option?(option) + options.has_key?(option) + end + + def options + @content["options"] || {} + end + + def set_option( option , value) + @content["options"] = {} if @content["options"].nil? + options[option] = value + end + def cards? ! cards.empty? end diff --git a/app/views/merged/elements/_button.haml b/app/views/merged/elements/_button.haml deleted file mode 100644 index a4d6c0f..0000000 --- a/app/views/merged/elements/_button.haml +++ /dev/null @@ -1,2 +0,0 @@ -%a.inline-block.rounded.bg-emerald-600.px-12.py-3.text-sm.font-medium.text-white.transition.hover:bg-emerald-700.focus:outline-none.focus:ring.focus:ring-yellow-400{:href => button["link"]} - =button['text'] diff --git a/app/views/merged/view/_section_2_col.haml b/app/views/merged/view/_section_2_col.haml index 2c35ced..72f630a 100644 --- a/app/views/merged/view/_section_2_col.haml +++ b/app/views/merged/view/_section_2_col.haml @@ -2,10 +2,10 @@ .flex.items-center.justify-center.flex-1 .max-w-prose.px-4.py-8.mx-auto.text-center %h1.text-2xl.font-bold.tracking-tight.text-gray-900.sm:text-4xl - = section["header"] + = section.header %p.mt-4.text-gray-500 - = section["text"] - - template = "merged/view/cards/" + section["card_template"] + = section.text + - template = "merged/view/cards/" + section.card_template .grid.grid-cols-1.md:grid-cols-2 - - section["cards"].each do |element| + - section.cards.each do |element| = render( template , element: element) diff --git a/app/views/merged/view/_section_full_left1.haml b/app/views/merged/view/_section_full_left1.haml index 1c3bdac..fae3682 100644 --- a/app/views/merged/view/_section_full_left1.haml +++ b/app/views/merged/view/_section_full_left1.haml @@ -2,12 +2,12 @@ .p-8.md:p-12.lg:px-16.lg:py-24 .max-w-lg.text-center.sm:text-left{:class => "bg-black/25"} %h2.text-2xl.font-bold.text-white.sm:text-3xl.md:text-5xl - = section["header"] + = section.header %p.hidden.max-w-md.md:mt-6.md:block.md:text-lg.md:leading-relaxed{:class => "text-white/90"} - = section["text"] - -if section["button_text"] + = section.text + -if section.has_option?("button_text") .mt-4.sm:mt-8 %a.inline-flex.items-center.rounded-full.bg-indigo-700.px-8.py-3.text-white.shadow-lg.transition.hover:bg-indigo-600.focus:outline-none.focus:ring{:href => section["button_link"]} - %span.text-sm.font-medium section["button_text"] + %span.text-sm.font-medium section.option("button_text") %svg.ml-3.h-5.w-5{:fill => "none", :stroke => "currentColor", :viewbox => "0 0 24 24", :xmlns => "http://www.w3.org/2000/svg"} %path{:d => "M17 8l4 4m0 0l-4 4m4-4H3", "stroke-linecap" => "round", "stroke-linejoin" => "round", "stroke-width" => "2"} diff --git a/app/views/merged/view/_section_full_left2.haml b/app/views/merged/view/_section_full_left2.haml index fef7853..2a05300 100644 --- a/app/views/merged/view/_section_full_left2.haml +++ b/app/views/merged/view/_section_full_left2.haml @@ -4,11 +4,10 @@ .p-8.md:p-12.lg:px-16.lg:py-24{class: 'bg-black/25'} .mx-auto.max-w-xl.text-center %h2.text-2xl.font-bold.text-white.md:text-5xl - = section["header"] + = section.header %p.hidden.sm:mt-4.sm:block.text-2xl.text-white - = section["text"] + = section.text .mt-4.md:mt-8 - -if section["button_text"] - %a.inline-block.rounded.border.border-white.bg-white.px-12.py-3.text-sm.font-medium.text-blue-500.transition.hover:bg-transparent.hover:text-white.focus:outline-none.focus:ring.focus:ring-yellow-400{:href => section["button_link"]} - =section["button_text"] + -if section.has_option?("button_text") + = render 'merged/view/elements/button' , section: section .grid.grid-cols-2.gap-4.md:grid-cols-1.lg:grid-cols-2 diff --git a/app/views/merged/view/_section_full_up.haml b/app/views/merged/view/_section_full_up.haml index 09dcf27..5bed1c0 100644 --- a/app/views/merged/view/_section_full_up.haml +++ b/app/views/merged/view/_section_full_up.haml @@ -2,6 +2,6 @@ .flex.items-center.justify-center.flex-1 .max-w-xl.px-4.py-8.mx-auto.text-center %h1.text-2xl.font-bold.tracking-tight.text-gray-900.sm:text-4xl - = section["header"] + = section.header %p.mt-4.text-gray-500 - = section["text"] + = section.text diff --git a/app/views/merged/view/_section_half_header.haml b/app/views/merged/view/_section_half_header.haml index a6517e3..9f3504d 100644 --- a/app/views/merged/view/_section_half_header.haml +++ b/app/views/merged/view/_section_half_header.haml @@ -2,13 +2,13 @@ .mx-auto.max-w-screen-xl.px-4.py-16.sm:px-6.sm:py-24.lg:px-8 .max-w-3xl %h2.text-3xl.font-bold.sm:text-4xl - = section["header"] + = section.header .mt-8.grid.grid-cols-1.gap-8.lg:grid-cols-2.lg:gap-16 .relative.h-64.overflow-hidden.sm:h-80.lg:h-full %img.absolute.inset-0.h-full.w-full.object-cover{:alt => "Party", :src => "https://images.unsplash.com/photo-1496843916299-590492c751f4?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1771&q=80"}/ .lg:py-16 %article.space-y-4.text-gray-600 %p - = section["text"] + = section.text %p - = section["text"] + = section.text diff --git a/app/views/merged/view/_section_half_left.haml b/app/views/merged/view/_section_half_left.haml index aff8c93..e504b77 100644 --- a/app/views/merged/view/_section_half_left.haml +++ b/app/views/merged/view/_section_half_left.haml @@ -3,9 +3,9 @@ .p-8.m-5.bg-cyan-100.md:p-12.lg:px-16.lg:py-24 .mx-auto.max-w-xl.text-center.sm:text-left %h2.text-2xl.font-bold.text-gray-900.md:text-3xl - = section["header"] + = section.header %p.hidden.text-gray-500.md:mt-4.md:block - = section["text"] + = section.text .mt-4.md:mt-8 - -if section["button"] - = render 'merged/view/elements/button' , button: section["button"] + -if section.has_option?("button_text") + = render 'merged/view/elements/button' , section: section diff --git a/app/views/merged/view/_section_half_right.haml b/app/views/merged/view/_section_half_right.haml index 7f2b699..2e726f5 100644 --- a/app/views/merged/view/_section_half_right.haml +++ b/app/views/merged/view/_section_half_right.haml @@ -2,10 +2,10 @@ .p-8.m-5.bg-cyan-100.md:p-12.lg:px-16.lg:py-24 .mx-auto.max-w-xl.text-center.sm:text-left %h2.text-2xl.font-bold.text-gray-900.md:text-3xl - = section["header"] + = section.header %p.hidden.text-gray-500.md:mt-4.md:block - = section["text"] + = section.text .mt-4.md:mt-8 - -if section["button"] - = render 'merged/view/elements/button' , button: section["button"] + -if section.has_option?("button_text") + = render 'merged/view/elements/button' , section: section = image_for( section , "p-5 h-56 w-full object-cover sm:h-full") diff --git a/app/views/merged/view/cards/_card_full_image.haml b/app/views/merged/view/cards/_card_full_image.haml index 5c0b773..fedbe06 100644 --- a/app/views/merged/view/cards/_card_full_image.haml +++ b/app/views/merged/view/cards/_card_full_image.haml @@ -2,6 +2,6 @@ = image_for( element , "absolute inset-0 h-full w-full object-cover hover:scale-110 ease-in duration-700") .relative.w-full.bg-cyan-600.m-2.p-4.text-center.tracking-widest.text-white.transition-colors.group-hover:bg-cyan-700{:class => "sm:w-1/2"} %h3.text-lg.uppercase - = element["header"] + = element.header %p.mt-1.text-xs.font-medium - = element["text"] + = element.text diff --git a/app/views/merged/view/elements/_button.haml b/app/views/merged/view/elements/_button.haml index a4d6c0f..a3d7741 100644 --- a/app/views/merged/view/elements/_button.haml +++ b/app/views/merged/view/elements/_button.haml @@ -1,2 +1,2 @@ -%a.inline-block.rounded.bg-emerald-600.px-12.py-3.text-sm.font-medium.text-white.transition.hover:bg-emerald-700.focus:outline-none.focus:ring.focus:ring-yellow-400{:href => button["link"]} - =button['text'] +%a.inline-block.rounded.bg-emerald-600.px-12.py-3.text-sm.font-medium.text-white.transition.hover:bg-emerald-700.focus:outline-none.focus:ring.focus:ring-yellow-400{:href => section.button_link} + =section.button_text diff --git a/app/views/merged/view/view.haml b/app/views/merged/view/view.haml index 3327f8f..3d47fc4 100644 --- a/app/views/merged/view/view.haml +++ b/app/views/merged/view/view.haml @@ -1,3 +1,3 @@ -- @data.each do |section| +- @page.sections.each do |section| - template = find_template(section) = render( template , section: section)