changed how things get loaded. reworked tags a bit

Oleg committed May 02, 2011
commit ed4869756b6eb0259909fb9162db366d65a241d0
Showing 11 changed files with 58 additions and 57 deletions
app/models/cms/block.rb +0 -17
@@ @@ -10,21 +10,4 @@ class Cms::Block < ActiveRecord::Base
:presence => true,
:uniqueness => { :scope => :page_id }
- # -- Class Methods --------------------------------------------------------
- def self.initialize_or_find(page, label)
- if block = page.blocks.detect{ |b| b.label == label.to_s }
- self.new(
- :record_id => block.id,
- :page => page,
- :label => block.label,
- :content => block.content
- )
- else
- self.new(
- :label => label.to_s,
- :page => page
- )
- end
- end
-
end
app/models/cms/layout.rb +1 -1
@@ @@ -69,7 +69,7 @@ protected
def check_content_tag_presence
ComfortableMexicanSofa::Tag.process_content((test_page = site.pages.new), content)
- if test_page.tags.select{|t| t.class.superclass == Cms::Block}.blank?
+ if test_page.tags.select{|t| t.is_cms_block?}.blank?
self.errors.add(:content, 'No cms page tags defined')
end
end
app/models/cms/snippet.rb +0 -5
@@ @@ -24,11 +24,6 @@ class Cms::Snippet < ActiveRecord::Base
(s = find_by_slug(slug)) ? s.content : ''
end
- def self.initialize_or_find(page, slug)
- find_by_slug(slug, :conditions => {:site_id => page.site.id}) ||
- new(:slug => slug, :site => page.site)
- end
-
protected
# Note: This might be slow. We have no idea where the snippet is used, so
app/views/cms_admin/pages/_form_blocks.html.erb +1 -1
@@ @@ -1,6 +1,6 @@
<div id='form_blocks'>
<%= fields_for :blocks, :builder => ComfortableMexicanSofa::FormBuilder do |cms_blocks| %>
- <% @cms_page.tags(true).select{|t| t.class < Cms::Block}.each do |tag| %>
+ <% @cms_page.tags(true).select{|t| t.is_cms_block?}.each do |tag| %>
<%= cms_blocks.send(tag.class.to_s.demodulize.underscore, tag)%>
<% end %>
<% end %>
config/environments/test.rb +1 -1
@@ @@ -5,7 +5,7 @@ ComfortableMexicanSofa::Application.configure do
# test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!
- config.cache_classes = false
+ config.cache_classes = true
# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true
comfortable_mexican_sofa.rb b/lib/comfortable_mexican_sofa.rb +4 -0
@@ @@ -13,6 +13,10 @@
require File.expand_path(path, File.dirname(__FILE__))
end
+ Dir.glob(File.expand_path('comfortable_mexican_sofa/tags/*.rb', File.dirname(__FILE__))).each do |path|
+ require path
+ end
+
module ComfortableMexicanSofa
class << self
comfortable_mexican_sofa/engine.rb b/lib/comfortable_mexican_sofa/engine.rb +1 -7
@@ @@ -6,13 +6,7 @@ require 'mime/types'
module ComfortableMexicanSofa
class Engine < ::Rails::Engine
-
- config.to_prepare do
- Dir.glob(File.expand_path('tags/*.rb', File.dirname(__FILE__))).each do |tag_path|
- require tag_path
- end
- end
-
+ # ...
end
end
comfortable_mexican_sofa/tag.rb b/lib/comfortable_mexican_sofa/tag.rb +30 -12
@@ @@ -8,9 +8,10 @@ module ComfortableMexicanSofa::Tag
TOKENIZER_REGEX = /(\{\{\s*cms:[^{}]*\}\})|((?:\{?[^{])+|\{+)/
- attr_accessor :params,
- :parent,
- :record_id
+ attr_accessor :page,
+ :label,
+ :params,
+ :parent
module ClassMethods
# Regex that is used to match tags in the content
@@ @@ -24,16 +25,13 @@ module ComfortableMexicanSofa::Tag
# Initializing tag object for a particular Tag type
# First capture group in the regex is the tag label
- def initialize_tag(cms_page, tag_signature)
+ def initialize_tag(page, tag_signature)
if match = tag_signature.match(regex_tag_signature)
- if self.respond_to?(:initialize_or_find)
- self.initialize_or_find(cms_page, match[1])
- else
- tag = self.new
- tag.label = match[1]
- tag.params = match[2]
- tag
- end
+ tag = self.new
+ tag.page = page
+ tag.label = match[1]
+ tag.params = match[2]
+ tag
end
end
end
@@ @@ -75,6 +73,26 @@ module ComfortableMexicanSofa::Tag
content.to_s
end
end
+
+ # Find or initialize Cms::Block object
+ def block
+ page.blocks.detect{|b| b.label == self.label.to_s} || page.blocks.build(:label => self.label.to_s)
+ end
+
+ # Find or initialize Cms::Snippet object
+ def snippet
+ page.site.snippets.detect{|s| s.slug == self.label.to_s} || page.site.snippets.build(:slug => self.label.to_s)
+ end
+
+ # Checks if this tag is using Cms::Block
+ def is_cms_block?
+ %w(page field).member?(self.class.to_s.demodulize.underscore.split(/_/).first)
+ end
+
+ # Used in displaying form elements for Cms::Block
+ def record_id
+ block.id
+ end
end
private
test/unit/models/block_test.rb +2 -12
@@ @@ -18,26 +18,16 @@ class BlockTest < ActiveSupport::TestCase
:parent_id => cms_pages(:default).id,
:blocks_attributes => [
{
- :label => 'test_block',
+ :label => 'default_page_text',
:content => 'test_content'
}
]
)
assert_equal 1, page.blocks.count
block = page.blocks.first
- assert_equal 'test_block', block.label
+ assert_equal 'default_page_text', block.label
assert_equal 'test_content', block.content
end
end
- def test_initialize_or_find
- tag = ComfortableMexicanSofa::Tag::PageText.initialize_or_find(cms_pages(:default), :default_field_text)
- assert_equal 'default_field_text', tag.label
- assert_equal 'default_field_text_content', tag.content
-
- tag = ComfortableMexicanSofa::Tag::PageText.initialize_or_find(cms_pages(:default), :new_block)
- assert_equal 'new_block', tag.label
- assert tag.content.blank?
- end
-
end
test/unit/models/page_test.rb +1 -1
@@ @@ -52,7 +52,7 @@ class CmsPageTest < ActiveSupport::TestCase
:parent_id => cms_pages(:default).id,
:layout_id => cms_layouts(:default).id,
:blocks_attributes => [
- { :label => 'test',
+ { :label => 'default_page_text',
:content => 'test' }
]
)
test/unit/tag_test.rb +17 -0
@@ @@ -203,4 +203,21 @@ class TagTest < ActiveSupport::TestCase
assert_equal 6, page.tags.size
end
+ def test_is_cms_block?
+ tag = ComfortableMexicanSofa::Tag::PageText.initialize_tag(
+ cms_pages(:default), '{{ cms:page:content:text }}'
+ )
+ assert tag.is_cms_block?
+
+ tag = ComfortableMexicanSofa::Tag::FieldText.initialize_tag(
+ cms_pages(:default), '{{ cms:field:content:text }}'
+ )
+ assert tag.is_cms_block?
+
+ tag = ComfortableMexicanSofa::Tag::Snippet.initialize_tag(
+ cms_pages(:default), '{{ cms:snippet:label }}'
+ )
+ assert !tag.is_cms_block?
+ end
+
end