new cards

This commit is contained in:
Torsten 2022-12-02 23:22:43 +02:00
parent 6287373dae
commit 1e452ca62a
8 changed files with 62 additions and 22 deletions

View File

@ -1,6 +1,6 @@
module Merged module Merged
class CardsController < MergedController class CardsController < MergedController
before_action :set_card , except: :index before_action :set_card , except: [:index , :new]
def index def index
@section = Section.find(params[:section_id]) @section = Section.find(params[:section_id])
@ -26,6 +26,12 @@ module Merged
redirect_to section_cards_url(@card.section.id) redirect_to section_cards_url(@card.section.id)
end end
def new
@section = Section.find(params[:section_id])
new_card = @section.new_card
redirect_to section_cards_url(@section.id)
end
def remove def remove
section = @card.section section = @card.section
section.remove_card( @card ) section.remove_card( @card )
@ -34,9 +40,9 @@ module Merged
end end
def update def update
@card.content.each do |key , value| @card.allowed_fields.each do |key|
next if key == "id" puts "Update Card #{key}"
if(!params[key].nil?) if( params.has_key?(key) )
@card.update(key, params[key]) @card.update(key, params[key])
puts "updating:#{key}=#{params[key]}" puts "updating:#{key}=#{params[key]}"
end end

View File

@ -64,7 +64,7 @@ module Merged
def update def update
@section.allowed_fields.each do |key| @section.allowed_fields.each do |key|
puts "Update #{key}" puts "Update Section #{key}"
if( params.has_key?(key) ) if( params.has_key?(key) )
@section.update(key, params[key]) @section.update(key, params[key])
puts "updating:#{key}=#{params[key]}" puts "updating:#{key}=#{params[key]}"

View File

@ -6,17 +6,19 @@ module Merged
"merged/view/" + section.template "merged/view/" + section.template
end end
# background image as inline style
def bg(section) def bg(section)
return "" if section.image.blank? return "" if section.image.blank?
puts "--#{Image.image_root}/#{section.image}--" puts "--#{Image.image_root}/#{section.image}--"
img = asset_url( "#{Image.image_root}/#{section.image}" ) img = asset_url( "#{Image.image_root}/#{section.image}" )
"background-image: url('#{img}');" "background-image: url('#{img}');"
end end
def image_for(section , classes)
image_tag("#{Image.image_root}/#{section.image}" , class: classes) # works for with sections and cards that respond to .image
end def image_for(element , classes)
def has_button(section) return "" if element.image.blank?
section.content['button'] image_tag("#{Image.image_root}/#{element.image}" , class: classes)
end end
end end
end end

View File

@ -46,15 +46,30 @@ module Merged
section.save section.save
end 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) def self.find_card(id)
raise "nil given" if id.blank? raise "nil given" if id.blank?
card = @@all[id] card = @@all[id]
raise "Section not found #{id}" unless card raise "Section not found #{id}" unless card
return card return card
end end
def set_index(index)
@index = index
end
end end
end end

View File

@ -35,7 +35,8 @@ module Merged
end end
def new_section def new_section
section_data = Section.build_data section_template = params[:template] || "section_spacer"
section_data = Section.build_data(section_template)
index = sections.length index = sections.length
section = Section.new(self , index, section_data) section = Section.new(self , index, section_data)
@sections << section @sections << section

View File

@ -25,12 +25,6 @@ module Merged
end end
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| [:template , :card_template , :id , :text , :header, :image].each do |meth|
define_method(meth) do define_method(meth) do
@content[meth.to_s] @content[meth.to_s]
@ -67,6 +61,15 @@ module Merged
! cards.empty? ! cards.empty?
end 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) def remove_card(card)
from_index = card.index from_index = card.index
@cards.delete_at(from_index) @cards.delete_at(from_index)
@ -128,6 +131,15 @@ module Merged
@index = index @index = index
end 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) def self.find(section_id)
raise "nil given" if section_id.blank? raise "nil given" if section_id.blank?
section = @@all[section_id] section = @@all[section_id]

View File

@ -13,6 +13,7 @@
Cards for Cards for
= link_to "Section #{@section.index + 1}", section_url( @section.id) , class: "underline" = link_to "Section #{@section.index + 1}", section_url( @section.id) , class: "underline"
.grid.grid-cols-4.gap-2.m-8 .grid.grid-cols-4.gap-2.m-8
- @section.cards.each_with_index do |card , index| - @section.cards.each_with_index do |card , index|
.relative.block.border.border-gray-100 .relative.block.border.border-gray-100
@ -20,7 +21,6 @@
%h3.mt-4.text-lg.font-bold Card #{index + 1} %h3.mt-4.text-lg.font-bold Card #{index + 1}
= blue_button( "Up" , card_move_url(card.id , dir: :up) ) = blue_button( "Up" , card_move_url(card.id , dir: :up) )
= blue_button( "Down" , card_move_url(card.id , dir: :down) ) = blue_button( "Down" , card_move_url(card.id , dir: :down) )
= green_button( "New" , "/index" )
= red_button( "Delete" , card_remove_path(card.id) ) = red_button( "Delete" , card_remove_path(card.id) )
.p-4 .p-4
%h3.mt-4.text-lg.font-bold Image %h3.mt-4.text-lg.font-bold Image
@ -47,3 +47,5 @@
.relative.block.border.border-gray-100 .relative.block.border.border-gray-100
%h3.mt-4.text-lg.font-bold Options %h3.mt-4.text-lg.font-bold Options
To be done To be done
%p
= green_button( "New Card" , new_section_card_url(@section.id) )

View File

@ -52,6 +52,8 @@
.text-sm "#{card.text[0..70]} ....." .text-sm "#{card.text[0..70]} ....."
%p.p-3 %p.p-3
= yellow_button( "View and Edit Cards" , section_cards_url(@section.id)) = 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 .relative.block.border.border-gray-100
%h3.mt-4.text-lg.font-bold Options %h3.mt-4.text-lg.font-bold Options
To be done To be done