2011-04-01 00:34:19 +00:00
|
|
|
module Extensions
|
|
|
|
module Page
|
|
|
|
module EditableElements
|
2010-08-24 22:59:22 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
extend ActiveSupport::Concern
|
2010-08-24 22:59:22 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
included do
|
|
|
|
embeds_many :editable_elements
|
2010-08-27 15:40:03 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
after_save :remove_disabled_editable_elements
|
2010-08-31 21:53:30 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
# editable file callbacks
|
|
|
|
after_save :store_file_sources!
|
|
|
|
before_save :write_file_source_identifiers
|
|
|
|
after_destroy :remove_file_sources!
|
2010-08-31 21:53:30 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
accepts_nested_attributes_for :editable_elements
|
|
|
|
end
|
|
|
|
|
|
|
|
module InstanceMethods
|
|
|
|
|
|
|
|
def disable_parent_editable_elements(block)
|
|
|
|
self.editable_elements.each { |el| el.disabled = true if el.from_parent? && el.block == block }
|
2010-08-24 22:59:22 +00:00
|
|
|
end
|
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
def disable_all_editable_elements
|
|
|
|
self.editable_elements.each { |el| el.disabled = true }
|
|
|
|
end
|
2010-08-24 22:59:22 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
def editable_element_blocks
|
|
|
|
self.editable_elements.collect(&:block)
|
|
|
|
end
|
2010-08-27 15:40:03 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
def editable_elements_grouped_by_blocks
|
|
|
|
all_enabled = self.editable_elements.reject { |el| el.disabled? }
|
|
|
|
groups = all_enabled.group_by(&:block)
|
|
|
|
groups.delete_if { |block, elements| elements.empty? }
|
|
|
|
end
|
2010-08-28 00:00:05 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
def find_editable_element(block, slug)
|
|
|
|
self.editable_elements.detect { |el| el.block == block && el.slug == slug }
|
|
|
|
end
|
2010-08-27 15:40:03 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
def find_editable_files
|
|
|
|
self.editable_elements.find_all { |el| el.respond_to?(:source) }
|
|
|
|
end
|
2010-08-26 11:06:44 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
def add_or_update_editable_element(attributes, type)
|
|
|
|
element = self.find_editable_element(attributes[:block], attributes[:slug])
|
2010-08-24 22:59:22 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
if element
|
|
|
|
element.attributes = attributes
|
|
|
|
else
|
|
|
|
self.editable_elements.build(attributes, type)
|
2010-08-31 21:53:30 +00:00
|
|
|
end
|
2011-04-01 00:34:19 +00:00
|
|
|
end
|
2010-08-31 21:53:30 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
def enable_editable_elements(block)
|
|
|
|
self.editable_elements.each { |el| el.disabled = false if el.block == block }
|
|
|
|
end
|
2010-08-24 22:59:22 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
def merge_editable_elements_from_page(source)
|
|
|
|
source.editable_elements.each do |el|
|
|
|
|
next if el.disabled? or !el.assignable?
|
2010-08-24 22:59:22 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
existing_el = self.find_editable_element(el.block, el.slug)
|
2010-08-24 22:59:22 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
if existing_el.nil? # new one from parents
|
|
|
|
new_attributes = el.attributes.merge(:from_parent => true)
|
|
|
|
if new_attributes['default_attribute'].present?
|
|
|
|
new_attributes['default_content'] = self.send(new_attributes['default_attribute']) || el.content
|
2011-03-03 07:35:37 +00:00
|
|
|
else
|
2011-04-01 00:34:19 +00:00
|
|
|
new_attributes['default_content'] = el.content
|
2010-08-24 22:59:22 +00:00
|
|
|
end
|
2011-04-01 00:34:19 +00:00
|
|
|
|
|
|
|
self.editable_elements.build(new_attributes, el.class)
|
|
|
|
elsif existing_el.default_attribute.nil?
|
|
|
|
existing_el.attributes = { :disabled => false, :default_content => el.content }
|
|
|
|
else
|
|
|
|
existing_el.attributes = { :disabled => false }
|
2010-08-24 22:59:22 +00:00
|
|
|
end
|
|
|
|
end
|
2011-04-01 00:34:19 +00:00
|
|
|
end
|
2010-08-24 22:59:22 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
def remove_disabled_editable_elements
|
|
|
|
return unless self.editable_elements.any? { |el| el.disabled? }
|
2010-08-31 21:53:30 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
# super fast way to remove useless elements all in once (TODO callbacks)
|
|
|
|
self.collection.update(self._selector, '$pull' => { 'editable_elements' => { 'disabled' => true } })
|
|
|
|
end
|
2010-08-31 21:53:30 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
protected
|
2010-08-31 21:53:30 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
## callbacks for editable files
|
2010-08-31 21:53:30 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
# equivalent to "after_save :store_source!" in EditableFile
|
|
|
|
def store_file_sources!
|
|
|
|
self.find_editable_files.collect(&:store_source!)
|
|
|
|
end
|
2010-08-31 21:53:30 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
# equivalent to "before_save :write_source_identifier" in EditableFile
|
|
|
|
def write_file_source_identifiers
|
|
|
|
self.find_editable_files.collect(&:write_source_identifier)
|
|
|
|
end
|
2010-08-31 21:53:30 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
# equivalent to "after_destroy :remove_source!" in EditableFile
|
|
|
|
def remove_file_sources!
|
|
|
|
self.find_editable_files.collect(&:remove_source!)
|
2010-08-24 22:59:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2011-04-01 00:34:19 +00:00
|
|
|
|
2010-08-24 22:59:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|