layouts must have cms_block tags present

Oleg committed Oct 29, 2010
commit 1651e5224f3d2b759aebe197802dc07b1e8e367d
Showing 6 changed files with 58 additions and 11 deletions
app/models/cms_layout.rb +13 -1
@@ @@ -18,6 +18,8 @@ class CmsLayout < ActiveRecord::Base
validates :content,
:presence => true
+ validate :content_tag_presence
+
# -- Class Methods --------------------------------------------------------
# Tree-like structure for layouts
def self.options_for_select(cms_site, cms_layout = nil, current_layout = nil, depth = 0, spacer = '. . ')
@@ @@ -37,7 +39,7 @@ class CmsLayout < ActiveRecord::Base
Dir.glob(File.expand_path('app/views/layouts/*.html.*', Rails.root)).collect do |filename|
match = filename.match(/\w*.html.\w*$/)
app_layout = match && match[0]
- app_layout[0...1] == '_' ? nil : app_layout
+ app_layout.to_s[0...1] == '_' ? nil : app_layout
end.compact
end
@@ @@ -72,4 +74,14 @@ class CmsLayout < ActiveRecord::Base
def merged_js
self.parent ? [self.parent.merged_js, self.js].join("\n") : self.js.to_s
end
+
+ protected
+
+ def content_tag_presence
+ CmsTag.process_content((test_page = CmsPage.new), content)
+ if test_page.cms_tags.select{|t| t.class.superclass == CmsBlock}.blank?
+ self.errors.add(:content, 'No cms page tags defined')
+ end
+ end
+
end
app/models/cms_page.rb +2 -1
@@ @@ -38,7 +38,8 @@ class CmsPage < ActiveRecord::Base
# Tree-like structure for pages
def self.options_for_select(cms_site, cms_page = nil, current_page = nil, depth = 0, exclude_self = true, spacer = '. . ')
return [] if (current_page ||= cms_site.cms_pages.root) == cms_page && exclude_self || !current_page
- out = [[ "#{spacer*depth}#{current_page.label}", current_page.id ]]
+ out = []
+ out << [ "#{spacer*depth}#{current_page.label}", current_page.id ] unless current_page == cms_page
current_page.children.each do |child|
out += options_for_select(cms_site, cms_page, child, depth + 1, exclude_self, spacer)
end
app/views/cms_admin/layouts/_form.html.erb +6 -2
@@ @@ -1,7 +1,11 @@
<%= form.text_field :label, :label => 'Layout Name', :id => (@cms_layout.new_record?? 'slugify' : nil)%>
<%= form.text_field :slug, :id => 'slug' %>
- <%= form.select :parent_id, CmsLayout.options_for_select(@cms_site, @cms_layout), :include_blank => true %>
- <%= form.select :app_layout, CmsLayout.app_layouts_for_select, :include_blank => true %>
+ <% if (options = CmsLayout.options_for_select(@cms_site, @cms_layout)).present? %>
+ <%= form.select :parent_id, [['---- Select Parent Layout ----', nil]] + options %>
+ <% end %>
+ <% if (options = CmsLayout.app_layouts_for_select).present? %>
+ <%= form.select :app_layout, [['---- Select Application Layout ----', nil]] + options %>
+ <% end %>
<%= form.text_area :content %>
<%= form.text_area :css %>
<%= form.text_area :js %>
app/views/cms_admin/pages/_form.html.erb +12 -4
@@ @@ -1,10 +1,18 @@
<%= form.text_field :label, :id => (@cms_page.new_record?? 'slugify' : nil) %>
<div class='page_form_extras'>
- <%= form.text_field :slug, :id => 'slug' %>
- <%= form.select :cms_layout_id, CmsLayout.options_for_select(@cms_site), {}, 'data-page-id' => @cms_page.id.to_i, :label => 'Layout' %>
- <%= form.select :parent_id, CmsPage.options_for_select(@cms_site, @cms_page) %>
- <%= form.select :target_page_id, CmsPage.options_for_select(@cms_site, @cms_page, nil, 0, false), :include_blank => true, :label => 'Redirect To Page' %>
+ <% unless @cms_site.cms_pages.count == 0 || @cms_site.cms_pages.root == @cms_page%>
+ <%= form.text_field :slug, :id => 'slug' %>
+ <% end %>
+ <% if (options = CmsLayout.options_for_select(@cms_site)).present? %>
+ <%= form.select :cms_layout_id, options, {}, 'data-page-id' => @cms_page.id.to_i, :label => 'Layout' %>
+ <% end %>
+ <% if (options = CmsPage.options_for_select(@cms_site, @cms_page)).present? %>
+ <%= form.select :parent_id, options %>
+ <% end %>
+ <% if (options = CmsPage.options_for_select(@cms_site, @cms_page, nil, 0, false)).present? %>
+ <%= form.select :target_page_id, options, :include_blank => true, :label => 'Redirect To Page' %>
+ <% end %>
</div>
<%= render :partial => 'form_blocks' %>
test/functional/cms_admin/layouts_controller_test.rb +3 -3
@@ @@ -45,7 +45,7 @@ class CmsAdmin::LayoutsControllerTest < ActionController::TestCase
post :create, :cms_layout => {
:label => 'Test Layout',
:slug => 'test',
- :content => 'Test Content'
+ :content => 'Test {cms:page:content}'
}
assert_response :redirect
layout = CmsLayout.last
@@ @@ -68,14 +68,14 @@ class CmsAdmin::LayoutsControllerTest < ActionController::TestCase
layout = cms_layouts(:default)
put :update, :id => layout, :cms_layout => {
:label => 'New Label',
- :content => 'New Content'
+ :content => 'New {cms:page:content}'
}
assert_response :redirect
assert_redirected_to :action => :edit, :id => layout
assert_equal 'Layout updated', flash[:notice]
layout.reload
assert_equal 'New Label', layout.label
- assert_equal 'New Content', layout.content
+ assert_equal 'New {cms:page:content}', layout.content
end
def test_update_failure
test/unit/cms_layout_test.rb +22 -0
@@ @@ -14,6 +14,28 @@ class CmsLayoutTest < ActiveSupport::TestCase
assert_has_errors_on layout, [:label, :slug, :content]
end
+ def test_validation_of_tag_presence
+ layout = CmsLayout.create(:content => 'some text')
+ assert_has_errors_on layout, :content
+
+ layout = CmsLayout.create(:content => '{cms:snippet:blah}')
+ assert_has_errors_on layout, :content
+
+ layout = cms_sites(:default).cms_layouts.new(
+ :label => 'test',
+ :slug => 'test',
+ :content => '{cms:page:blah}'
+ )
+ assert layout.valid?
+
+ layout = cms_sites(:default).cms_layouts.new(
+ :label => 'test',
+ :slug => 'test',
+ :content => '{cms:field:blah}'
+ )
+ assert layout.valid?
+ end
+
def test_options_for_select
assert_equal ['Default Layout', 'Nested Layout', '. . Child Layout'],
CmsLayout.options_for_select(cms_sites(:default)).collect{|t| t.first}