From 1e452ca62a045714bd6349011ef966dff571f02b Mon Sep 17 00:00:00 2001 From: Torsten Date: Fri, 2 Dec 2022 23:22:43 +0200 Subject: [PATCH] new cards --- app/controllers/merged/cards_controller.rb | 14 +++++++---- app/controllers/merged/sections_controller.rb | 2 +- app/helpers/merged/view_helper.rb | 12 ++++++---- app/models/merged/card.rb | 23 ++++++++++++++---- app/models/merged/page.rb | 3 ++- app/models/merged/section.rb | 24 ++++++++++++++----- app/views/merged/cards/index.haml | 4 +++- app/views/merged/sections/show.html.haml | 2 ++ 8 files changed, 62 insertions(+), 22 deletions(-) diff --git a/app/controllers/merged/cards_controller.rb b/app/controllers/merged/cards_controller.rb index 7ee3c12..f704d86 100644 --- a/app/controllers/merged/cards_controller.rb +++ b/app/controllers/merged/cards_controller.rb @@ -1,6 +1,6 @@ module Merged class CardsController < MergedController - before_action :set_card , except: :index + before_action :set_card , except: [:index , :new] def index @section = Section.find(params[:section_id]) @@ -26,6 +26,12 @@ module Merged redirect_to section_cards_url(@card.section.id) end + def new + @section = Section.find(params[:section_id]) + new_card = @section.new_card + redirect_to section_cards_url(@section.id) + end + def remove section = @card.section section.remove_card( @card ) @@ -34,9 +40,9 @@ module Merged end def update - @card.content.each do |key , value| - next if key == "id" - if(!params[key].nil?) + @card.allowed_fields.each do |key| + puts "Update Card #{key}" + if( params.has_key?(key) ) @card.update(key, params[key]) puts "updating:#{key}=#{params[key]}" end diff --git a/app/controllers/merged/sections_controller.rb b/app/controllers/merged/sections_controller.rb index 3951a24..8ebda26 100644 --- a/app/controllers/merged/sections_controller.rb +++ b/app/controllers/merged/sections_controller.rb @@ -64,7 +64,7 @@ module Merged def update @section.allowed_fields.each do |key| - puts "Update #{key}" + puts "Update Section #{key}" if( params.has_key?(key) ) @section.update(key, params[key]) puts "updating:#{key}=#{params[key]}" diff --git a/app/helpers/merged/view_helper.rb b/app/helpers/merged/view_helper.rb index d165aae..873dd27 100644 --- a/app/helpers/merged/view_helper.rb +++ b/app/helpers/merged/view_helper.rb @@ -6,17 +6,19 @@ module Merged "merged/view/" + section.template end + # background image as inline style def bg(section) return "" if section.image.blank? 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.image_root}/#{section.image}" , class: classes) - end - def has_button(section) - section.content['button'] + + # works for with sections and cards that respond to .image + def image_for(element , classes) + return "" if element.image.blank? + image_tag("#{Image.image_root}/#{element.image}" , class: classes) end + end end diff --git a/app/models/merged/card.rb b/app/models/merged/card.rb index 05d6954..ea1ab3c 100644 --- a/app/models/merged/card.rb +++ b/app/models/merged/card.rb @@ -46,15 +46,30 @@ module Merged section.save end + def set_index(index) + @index = index + end + + def template_style + Style.cards[ section.card_template ] + end + def allowed_fields + template_style.fields + end + + def self.build_data(card_template) + data = { "id" => SecureRandom.hex(10) } + Style.cards[ card_template ].fields.each do |key| + data[key] = key.upcase + end + data + end + def self.find_card(id) raise "nil given" if id.blank? card = @@all[id] raise "Section not found #{id}" unless card return card end - - def set_index(index) - @index = index - end end end diff --git a/app/models/merged/page.rb b/app/models/merged/page.rb index d8bef8b..27c58b9 100644 --- a/app/models/merged/page.rb +++ b/app/models/merged/page.rb @@ -35,7 +35,8 @@ module Merged end def new_section - section_data = Section.build_data + section_template = params[:template] || "section_spacer" + section_data = Section.build_data(section_template) index = sections.length section = Section.new(self , index, section_data) @sections << section diff --git a/app/models/merged/section.rb b/app/models/merged/section.rb index fdc4920..79c04d6 100644 --- a/app/models/merged/section.rb +++ b/app/models/merged/section.rb @@ -25,12 +25,6 @@ module Merged end end - def self.build_data - data = { "template" => "spacer"} - data["id"] = SecureRandom.hex(10) - data - end - [:template , :card_template , :id , :text , :header, :image].each do |meth| define_method(meth) do @content[meth.to_s] @@ -67,6 +61,15 @@ module Merged ! cards.empty? end + def new_card + card_data = Card.build_data(card_template) + index = cards.length + card = Card.new(self , index, card_data) + @cards << card + @content["cards"] << card_data + card + end + def remove_card(card) from_index = card.index @cards.delete_at(from_index) @@ -128,6 +131,15 @@ module Merged @index = index end + def self.build_data(template) + data = { "template" => template , "id" => SecureRandom.hex(10) } + Style.sections[ template ].fields.each do |key| + data[key] = key.upcase + end + data + + end + def self.find(section_id) raise "nil given" if section_id.blank? section = @@all[section_id] diff --git a/app/views/merged/cards/index.haml b/app/views/merged/cards/index.haml index 01c90c1..a0080d8 100644 --- a/app/views/merged/cards/index.haml +++ b/app/views/merged/cards/index.haml @@ -13,6 +13,7 @@ Cards for = link_to "Section #{@section.index + 1}", section_url( @section.id) , class: "underline" + .grid.grid-cols-4.gap-2.m-8 - @section.cards.each_with_index do |card , index| .relative.block.border.border-gray-100 @@ -20,7 +21,6 @@ %h3.mt-4.text-lg.font-bold Card #{index + 1} = blue_button( "Up" , card_move_url(card.id , dir: :up) ) = blue_button( "Down" , card_move_url(card.id , dir: :down) ) - = green_button( "New" , "/index" ) = red_button( "Delete" , card_remove_path(card.id) ) .p-4 %h3.mt-4.text-lg.font-bold Image @@ -47,3 +47,5 @@ .relative.block.border.border-gray-100 %h3.mt-4.text-lg.font-bold Options To be done +%p + = green_button( "New Card" , new_section_card_url(@section.id) ) diff --git a/app/views/merged/sections/show.html.haml b/app/views/merged/sections/show.html.haml index b0d1562..77bb087 100644 --- a/app/views/merged/sections/show.html.haml +++ b/app/views/merged/sections/show.html.haml @@ -52,6 +52,8 @@ .text-sm "#{card.text[0..70]} ....." %p.p-3 = yellow_button( "View and Edit Cards" , section_cards_url(@section.id)) + = green_button( "New Card" , new_section_card_url(@section.id) ) + .relative.block.border.border-gray-100 %h3.mt-4.text-lg.font-bold Options To be done