2010-05-02 23:33:17 +00:00
|
|
|
class Layout < LiquidTemplate
|
|
|
|
|
|
|
|
## associations ##
|
2010-07-31 02:15:24 +00:00
|
|
|
references_many :pages
|
2010-05-02 23:33:17 +00:00
|
|
|
embeds_many :parts, :class_name => 'PagePart'
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
## callbacks ##
|
|
|
|
before_save :build_parts_from_value
|
2010-05-09 12:44:53 +00:00
|
|
|
after_save :update_parts_in_pages
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
## validations ##
|
|
|
|
validates_format_of :value, :with => Locomotive::Regexps::CONTENT_FOR_LAYOUT, :message => :missing_content_for_layout
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
## methods ##
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
protected
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
def build_parts_from_value
|
|
|
|
if self.value_changed? || self.new_record?
|
2010-05-30 23:57:33 +00:00
|
|
|
self.parts.each { |p| p.disabled = true }
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-24 00:18:23 +00:00
|
|
|
self.value.scan(Locomotive::Regexps::CONTENT_FOR).each do |attributes|
|
|
|
|
slug = attributes[0].strip.downcase
|
2010-07-06 15:00:02 +00:00
|
|
|
name = slug.humanize
|
|
|
|
name = I18n.t('attributes.defaults.page_parts.name') if slug == 'layout'
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-24 00:18:23 +00:00
|
|
|
if part = self.parts.detect { |p| p.slug == slug }
|
|
|
|
part.name = name if name.present?
|
2010-05-30 23:57:33 +00:00
|
|
|
part.disabled = false
|
2010-05-02 23:33:17 +00:00
|
|
|
else
|
2010-05-24 00:18:23 +00:00
|
|
|
self.parts.build :slug => slug, :name => name || slug
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|
2010-05-02 23:33:17 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-24 00:18:23 +00:00
|
|
|
# body always first
|
|
|
|
body = self.parts.detect { |p| p.slug == 'layout' }
|
|
|
|
self.parts.delete(body)
|
|
|
|
self.parts.insert(0, body)
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-09 12:44:53 +00:00
|
|
|
@_update_pages = true if self.value_changed?
|
2010-05-02 23:33:17 +00:00
|
|
|
end
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-09 12:44:53 +00:00
|
|
|
def update_parts_in_pages
|
|
|
|
self.pages.each { |p| p.send(:update_parts!, self.parts) } if @_update_pages
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|