2010-08-24 22:59:22 +00:00
|
|
|
module Models
|
|
|
|
module Extensions
|
|
|
|
module Page
|
|
|
|
module EditableElements
|
|
|
|
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
embeds_many :editable_elements
|
2010-08-27 15:40:03 +00:00
|
|
|
|
|
|
|
accepts_nested_attributes_for :editable_elements
|
2010-08-24 22:59:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
module InstanceMethods
|
|
|
|
|
2010-08-27 15:40:03 +00:00
|
|
|
def disable_parent_editable_elements(block)
|
|
|
|
self.editable_elements.each { |el| el.disabled = true if el.from_parent? && el.block == block }
|
|
|
|
end
|
|
|
|
|
2010-08-28 00:00:05 +00:00
|
|
|
def disable_all_editable_elements
|
|
|
|
self.editable_elements.each { |el| el.disabled = true }
|
|
|
|
end
|
|
|
|
|
2010-08-27 15:40:03 +00:00
|
|
|
def editable_element_blocks
|
|
|
|
self.editable_elements.collect(&:block)
|
|
|
|
end
|
|
|
|
|
2010-08-26 11:06:44 +00:00
|
|
|
def editable_elements_grouped_by_blocks
|
2010-08-27 15:40:03 +00:00
|
|
|
groups = self.editable_elements.group_by(&:block)
|
|
|
|
groups.delete_if { |block, elements| elements.all? { |el| el.disabled? } }
|
2010-08-26 11:06:44 +00:00
|
|
|
end
|
|
|
|
|
2010-08-24 22:59:22 +00:00
|
|
|
def find_editable_element(block, slug)
|
|
|
|
self.editable_elements.detect { |el| el.block == block && el.slug == slug }
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_or_update_editable_element(attributes)
|
|
|
|
element = self.find_editable_element(attributes[:block], attributes[:slug])
|
|
|
|
|
|
|
|
if element
|
|
|
|
element.attributes = attributes
|
|
|
|
else
|
|
|
|
self.editable_elements.build(attributes)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def enable_editable_elements(block)
|
|
|
|
self.editable_elements.each { |el| el.disabled = false if el.block == block }
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_editable_elements_from_page(source)
|
|
|
|
source.editable_elements.each do |el|
|
2010-08-27 15:40:03 +00:00
|
|
|
next if el.disabled?
|
|
|
|
|
2010-08-24 22:59:22 +00:00
|
|
|
existing_el = self.find_editable_element(el.block, el.slug)
|
|
|
|
|
|
|
|
if existing_el.nil? # new one from parents
|
2010-08-27 15:40:03 +00:00
|
|
|
self.editable_elements.build(el.attributes.merge(:from_parent => true))
|
|
|
|
else
|
|
|
|
existing_el.disabled = false
|
2010-08-24 22:59:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|