From 4814f53d1e080b09b5c4bd7e16c0b931c40521fa Mon Sep 17 00:00:00 2001 From: Torsten Date: Sun, 27 Nov 2022 21:03:49 +0200 Subject: [PATCH] helper for forms, done fighting rails on namespacing --- app/controllers/cms/images_controller.rb | 2 +- app/helpers/cms/section_helper.rb | 8 ++++++++ app/models/cms/page.rb | 6 ++++-- app/models/cms/section.rb | 4 ++-- app/views/cms/sections/editors/_header.haml | 2 +- app/views/cms/sections/editors/_image.haml | 3 +++ app/views/cms/sections/editors/_text.haml | 5 ++++- 7 files changed, 23 insertions(+), 7 deletions(-) diff --git a/app/controllers/cms/images_controller.rb b/app/controllers/cms/images_controller.rb index 778ab07..feec65b 100644 --- a/app/controllers/cms/images_controller.rb +++ b/app/controllers/cms/images_controller.rb @@ -3,7 +3,7 @@ module Cms class ImagesController < CmsController @@root = "app/assets/images/cms/" - @@files = Set.new Dir.new(Rails.root + @@root).children + @@files = Set.new Dir[Rails.root + @@root + "*.*"].collect{|f|f.split("/").last} def index @files = files diff --git a/app/helpers/cms/section_helper.rb b/app/helpers/cms/section_helper.rb index fe19bfe..454285b 100644 --- a/app/helpers/cms/section_helper.rb +++ b/app/helpers/cms/section_helper.rb @@ -1,2 +1,10 @@ module Cms::SectionHelper + + def section_form(options) + url = cms_page_section_url( @page.id , @section.id) + puts "URL #{url}" + form_tag( url , {method: :patch}) do + yield + end + end end diff --git a/app/models/cms/page.rb b/app/models/cms/page.rb index 860095f..e246733 100644 --- a/app/models/cms/page.rb +++ b/app/models/cms/page.rb @@ -1,13 +1,15 @@ module Cms class Page - include ActiveModel::Model + include ActiveModel::API include ActiveModel::Conversion - include ActiveModel::Dirty + extend ActiveModel::Naming @@files = Set.new Dir.new(Rails.root.join("cms")).children attr_reader :name , :content + alias :id :name + def persisted? false end diff --git a/app/models/cms/section.rb b/app/models/cms/section.rb index 4d1bcd3..acfaa2f 100644 --- a/app/models/cms/section.rb +++ b/app/models/cms/section.rb @@ -1,8 +1,8 @@ module Cms class Section - include ActiveModel::Model + include ActiveModel::API include ActiveModel::Conversion - include ActiveModel::Dirty + extend ActiveModel::Naming attr_reader :name , :content , :page , :index diff --git a/app/views/cms/sections/editors/_header.haml b/app/views/cms/sections/editors/_header.haml index 28ba370..2e6c0b2 100644 --- a/app/views/cms/sections/editors/_header.haml +++ b/app/views/cms/sections/editors/_header.haml @@ -1,6 +1,6 @@ .relative.block.border.border-gray-100 %h3.mt-4.text-lg.font-bold= key.upcase -= form_tag( {url: cms_page_section_path( @page.name, @section.id), class: "mx-auto.mt-8.mb-0.max-w.space-y-4"} , {method: :patch }) do += 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 diff --git a/app/views/cms/sections/editors/_image.haml b/app/views/cms/sections/editors/_image.haml index c6ce686..7fab388 100644 --- a/app/views/cms/sections/editors/_image.haml +++ b/app/views/cms/sections/editors/_image.haml @@ -1,5 +1,8 @@ .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-sm.font-medium.text-white + Update + .relative.block.border.border-gray-100 -if value = image_tag "cms/" + value diff --git a/app/views/cms/sections/editors/_text.haml b/app/views/cms/sections/editors/_text.haml index 54f381e..72e3700 100644 --- a/app/views/cms/sections/editors/_text.haml +++ b/app/views/cms/sections/editors/_text.haml @@ -1,4 +1,7 @@ .relative.block.border.border-gray-100 %h3.mt-4.text-lg.font-bold= key.upcase .relative.block.border.border-gray-100 - %p= value + = 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") + %button.ml-3.inline-block.rounded-lg.bg-blue-500.px-5.py-3.text-sm.font-medium.text-white{:type => "submit"} + Update