106 lines
2.3 KiB
Ruby
106 lines
2.3 KiB
Ruby
module Merged
|
|
class Section < ViewBase
|
|
|
|
belongs_to :page , class_name: "Merged::Page"
|
|
|
|
fields :id , :page_id , :index
|
|
fields :template , :card_template
|
|
fields :header, :text , :image_name
|
|
|
|
def change_name
|
|
"#{page.name}:#{header}"
|
|
end
|
|
|
|
def cards
|
|
Card.where(section_id: id).order(index: :asc)
|
|
end
|
|
|
|
def cards_update
|
|
last_update_for( cards )
|
|
end
|
|
|
|
def template_style
|
|
SectionStyle.find_by_template( template )
|
|
end
|
|
|
|
def set_template(new_template)
|
|
self.template = new_template
|
|
new_style = template_style
|
|
if(new_style.has_cards?)
|
|
unless card_template
|
|
self.card_template = CardStyle.first.template
|
|
end
|
|
end
|
|
end
|
|
|
|
def allowed_fields
|
|
super + [:page_id]
|
|
end
|
|
|
|
def has_cards?
|
|
template_style.has_cards?
|
|
end
|
|
|
|
def move_up
|
|
swap_index_with(next_section)
|
|
end
|
|
|
|
def move_down
|
|
swap_index_with(previous_section)
|
|
end
|
|
|
|
def previous_section
|
|
page.sections.where(index: index - 1).first
|
|
end
|
|
|
|
def next_section
|
|
page.sections.where(index: index + 1).first
|
|
end
|
|
|
|
def new_card
|
|
card = Card.new_card(card_template, self.id , cards.length + 1)
|
|
card
|
|
end
|
|
|
|
def reset_index
|
|
cards.each_with_index{|card, index| card.index = index + 1}
|
|
end
|
|
|
|
def delete(editor)
|
|
cards.each {|card| card.delete_save!(editor) }
|
|
delete_save!(editor)
|
|
end
|
|
|
|
def delete_and_reset_index(editor)
|
|
delete(editor)
|
|
Page.find(page_id).reset_index
|
|
Section.save_all
|
|
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.fix_cards
|
|
Section.all.each do |section|
|
|
next if section.template_style.has_cards?
|
|
puts "#{section.id}: #{section.card_template}"
|
|
section.card_template = nil if section.card_template
|
|
end
|
|
Section.save_all
|
|
nil
|
|
end
|
|
end
|
|
end
|