tweaks to tag parcing

Oleg committed Aug 31, 2010
commit 6f88970ecae8e88df00a6e75c28bc9f33a7f3f16
Showing 5 changed files with 28 additions and 25 deletions
app/controllers/cms_admin/pages_controller.rb +9 -5
@@ @@ -21,14 +21,18 @@ class CmsAdmin::PagesController < CmsAdmin::BaseController
flash[:notice] = 'Page saved'
redirect_to :action => :edit, :id => @cms_page
rescue ActiveRecord::RecordInvalid
- render :action => :edit
+ render :action => :new
end
-
+
+ def update
+ # TODO
+ end
+
protected
-
+
def build_cms_page
@cms_page = CmsPage.new(params[:cms_page])
- @cms_page.cms_layout = CmsLayout.first
+ @cms_page.cms_layout ||= CmsLayout.first
@cms_page.initialize_tags
end
@@ @@ -39,5 +43,5 @@ protected
flash[:error] = 'Page not found'
redirect_to :action => :index
end
-
+
end
app/models/cms_page.rb +1 -1
@@ @@ -26,7 +26,7 @@ class CmsPage < ActiveRecord::Base
# Returns an array of tag objects, at the same time populates #cms_blocks
# of the current page
def initialize_tags
- CmsTag.initialize_tags(cms_layout.content, :cms_page => self)
+ CmsTag.initialize_tags(self)
end
end
app/views/cms_admin/layouts/_form.html.erb +1 -1
@@ @@ -1,2 +1,2 @@
- <%= form.text_field :label %>
+ <%= form.text_field :label, :label => 'Layout Name' %>
<%= form.text_area :content %>
\ No newline at end of file
app/views/cms_admin/layouts/new.html.erb +2 -2
@@ @@ -1,6 +1,6 @@
<h1> New Layout </h1>
- <%= form_for @cms_layout, :url => {:action => :create} do |form| %>
- <%= render :partial => form %>
+ <%= cms_form_for @cms_layout, :url => {:action => :create} do |form| %>
+ <%= render :partial => 'form', :object => form %>
<%= form.submit 'Create Layout' %>
<% end %>
\ No newline at end of file
comfortable_mexican_sofa/cms_tag.rb b/lib/comfortable_mexican_sofa/cms_tag.rb +15 -16
@@ @@ -21,16 +21,10 @@ module CmsTag
end
# Initializing tag objects for a particular Tag type
- # pass :cms_page => cms_page to initialize them in context of a page
- def initialize_tag_objects(content = '', options = {})
- content.scan(regex_tag_signature).flatten.collect do |label|
- # if tag extends CmsBlock, initialize it based on data in the db
- if self.superclass == CmsBlock && (cms_page = options[:cms_page]) && cms_page.is_a?(CmsPage)
- cms_page.cms_blocks << (cms_block =
- cms_page.cms_blocks.find_by_label(label) ||
- self.new(:label => label)
- )
- cms_block
+ def initialize_tag_objects(cms_page = nil, content = '')
+ content.to_s.scan(regex_tag_signature).flatten.collect do |label|
+ if self.superclass == CmsBlock && cms_page
+ cms_page.cms_blocks.detect{|b| b.label == label} || self.new(:label => label)
else
self.new(:label => label)
end
@@ @@ -64,19 +58,24 @@ private
# scans for cms tags inside given content
def self.find_cms_tags(content = '')
- content.scan(/<\s*#{TAG_PREFIX}:.+\s*\/?>/).flatten
+ content.to_s.scan(/<\s*#{TAG_PREFIX}:.+\s*\/?>/).flatten
end
# Scans provided content and initializes Tag objects based
# on their tag signature.
- # Pass :cms_page => @cms_page to initialize tag objects based on the page context.
- # Pass :cms_blocks_only => true to initialize CmsBlock subclasses
- def self.initialize_tags(content = '', options = {})
- find_cms_tags(content).collect do |tag_signature|
+ def self.initialize_tags(cms_page = nil, content = '')
+ # content is set based on the cms_page layout content
+ content = cms_page.cms_layout.try(:content) if cms_page
+
+ cms_tags = find_cms_tags(content).collect do |tag_signature|
tag_classes.collect do |tag_class|
- tag_class.initialize_tag_objects(tag_signature, options)
+ tag_class.initialize_tag_objects(cms_page, tag_signature)
end
end.flatten.compact
+
+ # Initializing cms_blocks for the passed cms_page
+ cms_page.cms_blocks = cms_tags.select{|t| t.class.superclass == CmsBlock} if cms_page
+ return cms_tags
end
def self.included(tag)