fix indexing and ordering for section and card
This commit is contained in:
@ -28,10 +28,19 @@ module Merged
|
||||
end
|
||||
|
||||
def move_up
|
||||
@section.move_card_up(self)
|
||||
swap_index_with(next_card)
|
||||
end
|
||||
|
||||
def move_down
|
||||
@section.move_card_down(self)
|
||||
swap_index_with(previous_card)
|
||||
end
|
||||
|
||||
def previous_card
|
||||
section.cards.where(index: index - 1).first
|
||||
end
|
||||
|
||||
def next_card
|
||||
section.cards.where(index: index + 1).first
|
||||
end
|
||||
|
||||
def save
|
||||
|
@ -24,5 +24,13 @@ module Merged
|
||||
options[option] = value
|
||||
end
|
||||
|
||||
#other may be nil
|
||||
def swap_index_with(other)
|
||||
return unless other
|
||||
old_index = self.index
|
||||
self.index = other.index
|
||||
other.index = old_index
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
@ -1,8 +1,7 @@
|
||||
module Merged
|
||||
class Page < ActiveYaml::Base
|
||||
set_root_path Rails.root #ouside engines not necessary
|
||||
include ActiveHash::Associations
|
||||
has_many :sections , class_name: "Merged::Section" , scope: -> { order(index: :desc) }
|
||||
|
||||
# could be config options
|
||||
def self.cms_root
|
||||
"cms"
|
||||
@ -12,6 +11,10 @@ module Merged
|
||||
|
||||
alias :id :name
|
||||
|
||||
def sections
|
||||
Section.where(page_id: name).order(index: :asc)
|
||||
end
|
||||
|
||||
def self.check_name(name)
|
||||
return "only alphanumeric, not #{name}" if name.match(/\A[a-zA-Z0-9]*\z/).nil?
|
||||
nil
|
||||
@ -53,32 +56,6 @@ module Merged
|
||||
@content[0]["template"]
|
||||
end
|
||||
|
||||
def move_section_up(section)
|
||||
return if sections.length == 1
|
||||
return if section.index == 0
|
||||
swap_sections( section , sections[section.index - 1])
|
||||
end
|
||||
|
||||
def move_section_down(section)
|
||||
return if sections.length == 1
|
||||
return if section.index == sections.last.index
|
||||
swap_sections( section , sections[section.index + 1])
|
||||
end
|
||||
|
||||
def swap_sections( this_section , that_section)
|
||||
# swap in the actual objects, index is cached in the objects
|
||||
this_old_index = this_section.index
|
||||
this_section.set_index( that_section.index )
|
||||
that_section.set_index( this_old_index )
|
||||
|
||||
# swap in the sections cache
|
||||
sections[ this_section.index ] = this_section
|
||||
sections[ that_section.index ] = that_section
|
||||
# swap in the yaml
|
||||
content[this_section.index] = this_section.content
|
||||
content[that_section.index] = that_section.content
|
||||
end
|
||||
|
||||
def save
|
||||
super
|
||||
data = Page.all.collect {|obj| obj.attributes}
|
||||
|
@ -4,14 +4,16 @@ module Merged
|
||||
|
||||
include ActiveHash::Associations
|
||||
belongs_to :page , class_name: "Merged::Page"
|
||||
has_many :cards , class_name: "Merged::Card" , scope: -> { order(index: :desc) }
|
||||
|
||||
|
||||
include Optioned
|
||||
|
||||
fields :name , :page_id , :index , :cards , :options
|
||||
fields :template , :card_template , :id , :text , :header, :image
|
||||
|
||||
def cards
|
||||
Card.where(section_id: id).order(index: :asc)
|
||||
end
|
||||
|
||||
def set_template(new_template)
|
||||
self.template = new_template
|
||||
new_style = template_style
|
||||
@ -54,16 +56,13 @@ module Merged
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@page.remove_section(self)
|
||||
end
|
||||
|
||||
def move_up
|
||||
@page.move_section_up(self)
|
||||
swap_index_with(next_section)
|
||||
end
|
||||
|
||||
def move_down
|
||||
@page.move_section_down(self)
|
||||
swap_index_with(previous_section)
|
||||
end
|
||||
|
||||
def previous_section
|
||||
@ -74,33 +73,6 @@ module Merged
|
||||
page.sections.where(index: index + 1).first
|
||||
end
|
||||
|
||||
def move_card_up(card)
|
||||
return if cards.length == 1
|
||||
return if card.index == 0
|
||||
swap( card , cards[card.index - 1])
|
||||
end
|
||||
|
||||
def move_card_down(card)
|
||||
return if cards.length == 1
|
||||
return if card.index == cards.last.index
|
||||
swap( card , cards[card.index + 1])
|
||||
end
|
||||
|
||||
def swap( this_card , that_card)
|
||||
# swap in the actual objects, index is cached in the objects
|
||||
this_old_index = this_card.index
|
||||
this_card.set_index( that_card.index )
|
||||
that_card.set_index( this_old_index )
|
||||
|
||||
# swap in the cards cache
|
||||
cards[ this_card.index ] = this_card
|
||||
cards[ that_card.index ] = that_card
|
||||
# swap in the yaml
|
||||
card_content = content["cards"]
|
||||
card_content[this_card.index] = this_card.content
|
||||
card_content[that_card.index] = that_card.content
|
||||
end
|
||||
|
||||
def update(key , value)
|
||||
raise "unsuported field #{key} for #{template}" unless allowed_fields.include?(key)
|
||||
if(! @content[key].nil? ) # first setting ok, types not (yet?) specified
|
||||
|
Reference in New Issue
Block a user