fix all news

This commit is contained in:
Torsten 2022-12-12 18:17:48 +02:00
parent 0d1adab3f3
commit 7966437324
10 changed files with 47 additions and 33 deletions

View File

@ -29,6 +29,7 @@ module Merged
def new def new
@section = Section.find(params[:section_id]) @section = Section.find(params[:section_id])
new_card = @section.new_card new_card = @section.new_card
new_card.save
redirect_to section_cards_url(@section.id) , notice: "Card created" redirect_to section_cards_url(@section.id) , notice: "Card created"
end end

View File

@ -11,7 +11,8 @@ module Merged
message = Page.check_name(name) message = Page.check_name(name)
message = "Must enter name" if name.blank? message = "Must enter name" if name.blank?
if( message.nil?) if( message.nil?)
@page = Page.build_new(name) @page = Page.new_page(name)
@page.save
redirect_to new_page_section_url(@page.id) , notice: "Page was successfully created." redirect_to new_page_section_url(@page.id) , notice: "Page was successfully created."
else else
@pages = Page.all.values @pages = Page.all.values

View File

@ -23,7 +23,7 @@ module Merged
page = Page.find(params[:page_id]) page = Page.find(params[:page_id])
template = params[:template] template = params[:template]
new_section = page.new_section(template) new_section = page.new_section(template)
page.save new_section.save
if(template.blank?) # new if(template.blank?) # new
redirect_to section_select_template_url(new_section.id), notice: "New section created" redirect_to section_select_template_url(new_section.id), notice: "New section created"
else # copy else # copy

View File

@ -9,10 +9,6 @@ module Merged
fields :index , :section_id, :id , :text , :header, :image fields :index , :section_id, :id , :text , :header, :image
def template_style
section.card_template
end
def move_up def move_up
swap_index_with(next_card) swap_index_with(next_card)
end end
@ -60,11 +56,12 @@ module Merged
def self.new_card(card_template , section_id , index) def self.new_card(card_template , section_id , index)
data = { section_id: section_id , index: index} data = { section_id: section_id , index: index}
CardStyle.find_by_template( card_template ).fields.each do |key| template = CardStyle.find_by_template( card_template )
template.fields.each do |key|
data[key] = "NEW" data[key] = "NEW"
end end
card = Card.new(data) card = Card.new(data)
card.add_default_options card.add_default_options(template.options_definitions)
card card
end end

View File

@ -23,12 +23,12 @@ module Merged
end end
def set_option( option , value) def set_option( option , value)
puts "#{template_style} setting option #{option}=#{value.class}"
options[option] = value options[option] = value
end end
def add_default_options def add_default_options( definitions = nil )
option_definitions.each do |option| definitions = option_definitions if definitions.nil?
definitions.each do |option|
next unless option.default next unless option.default
set_option( option.name , option.default) set_option( option.name , option.default)
end end

View File

@ -15,7 +15,7 @@ module Merged
def new_section(section_template) def new_section(section_template)
section_template = "section_spacer" if section_template.blank? section_template = "section_spacer" if section_template.blank?
section = Section.new_section(section_template, self.id , sections.length) section = Section.new_section(section_template, self.id , sections.length + 1)
section section
end end

View File

@ -19,10 +19,8 @@ module Merged
new_style = template_style new_style = template_style
if(new_style.has_cards?) if(new_style.has_cards?)
unless card_template unless card_template
self.card_template = CardStyle.first.name self.card_template = CardStyle.first.template
end end
else
cards.delete_all
end end
end end
@ -43,7 +41,6 @@ module Merged
end end
end end
def move_up def move_up
swap_index_with(next_section) swap_index_with(next_section)
end end
@ -60,19 +57,9 @@ module Merged
page.sections.where(index: index + 1).first page.sections.where(index: index + 1).first
end end
def self.new_section(template , page_id , index) def new_card
data = { template: template , index: index , page_id: page_id} card = Card.new_card(card_template, self.id , cards.length + 1)
style = SectionStyle.find_by_template( template) card
style.fields.each do |key|
data[key] = key.upcase
end unless style.fields.blank?
if(style.has_cards?)
data["cards"] = []
data["card_template"] = CardStyle.first.name
end
s = Section.new(data)
s.add_default_options
s
end end
def reset_index def reset_index
@ -98,6 +85,21 @@ module Merged
Section.save_all Section.save_all
end end
def self.new_section(template , page_id , index)
data = { template: template , index: index , page_id: page_id}
style = SectionStyle.find_by_template( template)
style.fields.each do |key|
data[key] = key.upcase
end unless style.fields.blank?
if(style.has_cards?)
data["cards"] = []
data["card_template"] = CardStyle.first.name
end
s = Section.new(data)
s.add_default_options
s
end
def self.save_all def self.save_all
data = Section.the_private_records.collect {|obj| obj.attributes} data = Section.the_private_records.collect {|obj| obj.attributes}
File.write( Section.full_path , data.to_yaml) File.write( Section.full_path , data.to_yaml)

View File

@ -23,6 +23,11 @@ module Merged
expect(first.next_card.index).to be 2 expect(first.next_card.index).to be 2
end end
it "create a new" do
card = Card.new_card("card_normal_square" , 1 , 1)
expect(card.index).to eq 1
end
it "deletes " do it "deletes " do
card = Card.find(20) card = Card.find(20)
expect(card).not_to be nil expect(card).not_to be nil

View File

@ -21,32 +21,33 @@ module Merged
expect(section.index).to be index + 1 # because we have human index expect(section.index).to be index + 1 # because we have human index
end end
end end
it "deletes " do it "deletes " do
id = index.id id = index.id
index.delete index.delete
expect{Page.find(id) }.to raise_error(ActiveHash::RecordNotFound) expect{Page.find(id) }.to raise_error(ActiveHash::RecordNotFound)
end end
it "destroys " do it "destroys " do
id = index.id id = index.id
index.destroy index.destroy
Section.reload Section.reload
expect{Page.find(id) }.to raise_error(ActiveHash::RecordNotFound) expect{Page.find(id) }.to raise_error(ActiveHash::RecordNotFound)
end end
it "destroys sections" do it "destroys sections" do
id = index.sections.first.id id = index.sections.first.id
index.destroy index.destroy
Section.reload Section.reload
expect{Page.find(id) }.to raise_error(ActiveHash::RecordNotFound) expect{Page.find(id) }.to raise_error(ActiveHash::RecordNotFound)
end end
it "creates simple section" do it "creates simple section" do
s = index.new_section("section_spacer") s = index.new_section("section_spacer")
expect(s).not_to be nil expect(s).not_to be nil
expect(s.template).to eq "section_spacer" expect(s.template).to eq "section_spacer"
end end
it "creates section with right index" do
should_be = index.sections.last.index + 1
s = index.new_section("section_spacer")
expect(s.index).to eq should_be
end
it "creates page" do it "creates page" do
name = "randomname" name = "randomname"
page = Page.new_page( name) page = Page.new_page( name)

View File

@ -50,6 +50,13 @@ module Merged
expect(s.template).to eq "section_spacer" expect(s.template).to eq "section_spacer"
end end
it "creates card with right index" do
s = Section.find_by_template("section_cards")
length = s.cards.length
c = s.new_card
expect(c.index).to eq length + 1
end
it "deletes " do it "deletes " do
last_id = last.id last_id = last.id
last.delete last.delete