2010-05-02 23:33:17 +00:00
|
|
|
class Layout < LiquidTemplate
|
|
|
|
|
|
|
|
## associations ##
|
2010-05-09 12:44:53 +00:00
|
|
|
has_many_related :pages
|
2010-05-02 23:33:17 +00:00
|
|
|
embeds_many :parts, :class_name => 'PagePart'
|
|
|
|
|
|
|
|
## callbacks ##
|
|
|
|
before_save :build_parts_from_value
|
2010-05-09 12:44:53 +00:00
|
|
|
after_save :update_parts_in_pages
|
2010-05-02 23:33:17 +00:00
|
|
|
|
|
|
|
## validations ##
|
|
|
|
validates_format_of :value, :with => Locomotive::Regexps::CONTENT_FOR_LAYOUT, :message => :missing_content_for_layout
|
|
|
|
|
|
|
|
## methods ##
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def build_parts_from_value
|
|
|
|
if self.value_changed? || self.new_record?
|
2010-05-09 12:44:53 +00:00
|
|
|
self.parts.clear
|
2010-05-02 23:33:17 +00:00
|
|
|
|
2010-05-09 12:44:53 +00:00
|
|
|
body = nil
|
|
|
|
|
|
|
|
self.value.scan(Locomotive::Regexps::CONTENT_FOR).each do |part|
|
2010-05-02 23:33:17 +00:00
|
|
|
part[1].strip!
|
|
|
|
part[1] = nil if part[1].empty?
|
|
|
|
|
|
|
|
slug = part[0].strip.downcase
|
|
|
|
|
2010-05-09 12:44:53 +00:00
|
|
|
if slug == 'layout'
|
2010-05-10 22:39:52 +00:00
|
|
|
body = PagePart.new :slug => slug, :name => I18n.t('attributes.defaults.page_parts.name')
|
2010-05-02 23:33:17 +00:00
|
|
|
else
|
2010-05-09 12:44:53 +00:00
|
|
|
self.parts.build :slug => slug, :name => (part[1] || slug).gsub("\"", '')
|
|
|
|
end
|
2010-05-02 23:33:17 +00:00
|
|
|
end
|
2010-05-09 12:44:53 +00:00
|
|
|
|
|
|
|
self.parts.insert(0, body) if body
|
|
|
|
|
|
|
|
@_update_pages = true if self.value_changed?
|
2010-05-02 23:33:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
end
|