From 204ab1abcc35fa9d6d77ec9bb83cb08a30c2f909 Mon Sep 17 00:00:00 2001 From: Torsten Date: Wed, 30 Nov 2022 23:49:23 +0200 Subject: [PATCH] card editing version one --- app/controllers/merged/cards_controller.rb | 29 ++++++++++++++++--- app/helpers/merged/sections_helper.rb | 1 - app/models/merged/card.rb | 12 +++++++- .../merged/cards/editors/_card_template.haml | 11 ------- app/views/merged/cards/editors/_cards.haml | 0 app/views/merged/cards/editors/_header.haml | 10 +++---- app/views/merged/cards/editors/_image.haml | 8 ----- app/views/merged/cards/editors/_text.haml | 7 ++--- app/views/merged/cards/index.haml | 8 ++++- app/views/merged/cards/select_image.haml | 15 ++++++++++ app/views/merged/sections/show.html.haml | 2 +- config/routes.rb | 5 +++- 12 files changed, 71 insertions(+), 37 deletions(-) delete mode 100644 app/views/merged/cards/editors/_card_template.haml delete mode 100644 app/views/merged/cards/editors/_cards.haml create mode 100644 app/views/merged/cards/select_image.haml diff --git a/app/controllers/merged/cards_controller.rb b/app/controllers/merged/cards_controller.rb index e3131fe..6f4f814 100644 --- a/app/controllers/merged/cards_controller.rb +++ b/app/controllers/merged/cards_controller.rb @@ -6,11 +6,32 @@ module Merged @section = Section.find(params[:section_id]) end + def select_image + @images = Image.all + end + + def set_image + @card.content["image"] = params[:image] + @card.save + redirect_to section_cards_url(@card.section.id) + end + + def update + @card.content.each do |key , value| + next if key == "id" + if(!params[key].nil?) + @card.update(key, params[key]) + puts "updating:#{key}=#{params[key]}" + end + end + @card.save + redirect_to section_cards_url(@card.section.id) + end + private - def set_page - @page = Page.find(params[:page_id]) - section_id = params[:id] || params[:section_id] - @section = @page.find_section( section_id ) + def set_card + card_id = params[:id] || params[:card_id] + @card = Card.find_card( card_id ) end end diff --git a/app/helpers/merged/sections_helper.rb b/app/helpers/merged/sections_helper.rb index bd946ea..3008a82 100644 --- a/app/helpers/merged/sections_helper.rb +++ b/app/helpers/merged/sections_helper.rb @@ -3,7 +3,6 @@ module Merged def section_form(options) url = section_url( @section.id) - puts "URL #{url}" form_tag( url , {method: :patch}) do yield end diff --git a/app/models/merged/card.rb b/app/models/merged/card.rb index 2805776..a0ec35e 100644 --- a/app/models/merged/card.rb +++ b/app/models/merged/card.rb @@ -22,11 +22,21 @@ module Merged @content['id'] end + def update(key , value) + return if key == "id" #not updating that + if(! @content[key].nil? ) + if( @content[key].class != value.class ) + raise "Type mismatch #{key} #{key.class}!=#{value.class}" + end + end + @content[key] = value + end + def save section.save end - def self.find(id) + def self.find_card(id) raise "nil given" if id.blank? card = @@all[id] raise "Section not found #{id}" unless card diff --git a/app/views/merged/cards/editors/_card_template.haml b/app/views/merged/cards/editors/_card_template.haml deleted file mode 100644 index a07df98..0000000 --- a/app/views/merged/cards/editors/_card_template.haml +++ /dev/null @@ -1,11 +0,0 @@ -.relative.block.border.border-gray-100 - %h3.mt-4.text-lg.font-bold= key.upcase - %p= value - %p #{section.content['cards'].length} cards - %p view cards (index) - %button.ml-3.inline-block.rounded-lg.bg-blue-500.px-5.py-3.text-md.font-medium.text-white - =link_to "View and Edit Cards" , section_card_url(@page.name,@section.id) - %button.ml-3.inline-block.rounded-lg.bg-blue-500.px-5.py-3.text-md.font-medium.text-white - =link_to "Change Card Template" , section_select_card_template_url(@page.name,@section.id) -.relative.block.border.border-gray-100 - =image_tag("merged/card_preview/#{value}" , class: "w-full object-contain") diff --git a/app/views/merged/cards/editors/_cards.haml b/app/views/merged/cards/editors/_cards.haml deleted file mode 100644 index e69de29..0000000 diff --git a/app/views/merged/cards/editors/_header.haml b/app/views/merged/cards/editors/_header.haml index d637171..6e5fee4 100644 --- a/app/views/merged/cards/editors/_header.haml +++ b/app/views/merged/cards/editors/_header.haml @@ -1,6 +1,6 @@ .relative.block.border.border-gray-100 - %h3.mt-4.text-lg.font-bold= key.upcase -= section_form( class: "mx-auto mt-8 mb-0 max-w space-y-4") do - = text_field_tag( :header , @section.content["header"], class: "w-full rounded-lg border-gray-200 p-4 pr-12 text-sm shadow-sm") - %button.ml-3.inline-block.rounded-lg.bg-blue-500.px-5.py-3.text-sm.font-medium.text-white{:type => "submit"} - Update + %h3.mt-4.text-lg.font-bold Header + = form_tag( card_url(card.id) , {method: :patch } ) do + = text_field_tag( :header , card.content["header"], class: "w-full rounded-lg border-gray-200 p-4 pr-12 text-sm shadow-sm") + %button.ml-3.inline-block.rounded-lg.bg-blue-500.px-5.py-3.text-sm.font-medium.text-white{:type => "submit"} + Update diff --git a/app/views/merged/cards/editors/_image.haml b/app/views/merged/cards/editors/_image.haml index 6db5217..6a81d15 100644 --- a/app/views/merged/cards/editors/_image.haml +++ b/app/views/merged/cards/editors/_image.haml @@ -1,11 +1,3 @@ -.relative.block.border.border-gray-100 - %h3.mt-4.text-lg.font-bold= key.upcase - %button.ml-3.inline-block.rounded-lg.bg-blue-500.px-5.py-3.text-md.font-medium.text-white - =link_to "Change Image" , section_select_image_url(@page.name,@section.id) - %button.ml-3.inline-block.rounded-lg.bg-red-500.px-5.py-3.text-md.font-medium.text-white - = link_to( "Remove image" , section_set_image_path( @page.name, @section.id , image: "")) - - .relative.block.border.border-gray-100 -if( value.blank? ) No image diff --git a/app/views/merged/cards/editors/_text.haml b/app/views/merged/cards/editors/_text.haml index 72e3700..139357b 100644 --- a/app/views/merged/cards/editors/_text.haml +++ b/app/views/merged/cards/editors/_text.haml @@ -1,7 +1,6 @@ .relative.block.border.border-gray-100 - %h3.mt-4.text-lg.font-bold= key.upcase -.relative.block.border.border-gray-100 - = section_form( class: "mx-auto mt-8 mb-0 max-w space-y-4") do - = text_area_tag( :text , @section.content["text"], class: "w-full rounded-lg border-gray-200 p-4 pr-12 text-sm shadow-sm") + %h3.mt-4.text-lg.font-bold Text + = form_tag( card_url(card.id) , {method: :patch } ) do + = text_area_tag( :text , card.content["text"], class: "w-full rounded-lg border-gray-200 p-4 pr-12 text-sm shadow-sm") %button.ml-3.inline-block.rounded-lg.bg-blue-500.px-5.py-3.text-sm.font-medium.text-white{:type => "submit"} Update diff --git a/app/views/merged/cards/index.haml b/app/views/merged/cards/index.haml index 496af79..e797aa9 100644 --- a/app/views/merged/cards/index.haml +++ b/app/views/merged/cards/index.haml @@ -24,6 +24,12 @@ =link_to "New" , "/index" %button.mt-4.rounded-lg.bg-red-400.p-4 =link_to "Delete" , "/index" + .p-4 + %h3.mt-4.text-lg.font-bold Image + %button.ml-3.inline-block.rounded-lg.bg-blue-500.px-5.py-3.text-md.font-medium.text-white + =link_to "Change Image" , card_select_image_url(card.id) + %button.ml-3.inline-block.rounded-lg.bg-red-500.px-5.py-3.text-md.font-medium.text-white + = link_to( "Remove image" , card_set_image_path( card.id , image: "")) - card.content.each do |key , value| - = render "merged/cards/view/#{key}", card: card , key: key , value: value + = render "merged/cards/editors/#{key}", card: card , key: key , value: value diff --git a/app/views/merged/cards/select_image.haml b/app/views/merged/cards/select_image.haml new file mode 100644 index 0000000..119f6ba --- /dev/null +++ b/app/views/merged/cards/select_image.haml @@ -0,0 +1,15 @@ +.grid.grid-cols-6.gap-2.m-8 + .relative.block.border.border-gray-100 + %h3.mt-4.text-lg.font-bold Add new image or select (click) + = form_tag(merged.images_path, multipart: true) do + = text_field_tag 'filename' + %h5.mt-4.text-lg.font-bold Name is optional + %p will be taken from uploaded file + = hidden_field_tag :redirect , card_set_image_url(@card.id,image: "NEW") + = file_field_tag 'image_file' + .inline-block.rounded.border.border-indigo-600.bg-indigo-600.px-12.py-3.text-sm.font-medium.text-white.hover:bg-transparent.hover:text-indigo-600.focus:outline-none.focus:ring.active:text-indigo-500{:href => merged.new_image_path} + = submit_tag 'Submit' + -@images.each do |name , image| + .relative.block.border.border-gray-100 + = link_to( card_set_image_path( image: name)) do + =image_tag("#{image_root}/#{name}" , class: "h-56 w-full object-contain lg:h-72") diff --git a/app/views/merged/sections/show.html.haml b/app/views/merged/sections/show.html.haml index e6333f0..a4ee2f4 100644 --- a/app/views/merged/sections/show.html.haml +++ b/app/views/merged/sections/show.html.haml @@ -4,7 +4,7 @@ .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 - Page #{@section.page.name} + Page #{link_to @section.page.name, page_url(@section.page.name)} .flex.items-center.justify-center.flex-1 %h3.text-xl.font-bold.tracking-tight.text-gray-900 Section #{@section.id} diff --git a/config/routes.rb b/config/routes.rb index 323f8fc..89ce911 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,7 +9,10 @@ Merged::Engine.routes.draw do get :set_template get :select_card_template get :set_card_template - resources :cards + resources :cards do + get :select_image + get :set_image + end end end resources :images