figured out proper tag initialization
Oleg
committed Aug 25, 2010
commit 41d391d721734c6e483c0c5e935712f860fb232b
Showing 25
changed files with
139 additions
and 86 deletions
app/models/cms_page.rb
+7
-4
| @@ | @@ -6,11 +6,14 @@ class CmsPage < ActiveRecord::Base |
| # -- Instance Methods ----------------------------------------------------- | |
| def content | |
| + | # |
| + | end |
| + | |
| + | # Initilizing tags based on layout content. If not found in the database, |
| + | # blank ones will be initilized. |
| + | def initialize_tags |
| content = cms_layout.content.dup | |
| - | cms_blocks.each do |block| |
| - | content.gsub!(block.regex_tag_signature, block.render.to_s) |
| - | end |
| - | content |
| + | raise CmsTag.tag_classes.inspect |
| end | |
| end | |
app/models/cms_tag.rb
+31
-7
| @@ | @@ -1,5 +1,15 @@ |
| + | # This module provides all Tag classes with neccessary methods. |
| + | # Example class that will behave as a Tag: |
| + | # class MySpecialTag |
| + | # include CmsTag |
| + | # ... |
| + | # end |
| module CmsTag | |
| + | # All tags must follow this format: |
| + | # <cms:*> |
| + | TAG_PREFIX = 'cms' |
| + | |
| module ClassMethods | |
| # Regex that is used to match tags in the content | |
| # Example: | |
| @@ | @@ -39,19 +49,33 @@ module CmsTag |
| private | |
| + | # scans for cms tags inside given content |
| + | def self.find_cms_tags(content = '') |
| + | content.scan(/<\s*cms:.+\s*\/?>/).flatten |
| + | end |
| + | |
| + | def self.initialize_tags(content = '') |
| + | find_cms_tags(content).collect do |tag_signature| |
| + | tag_classes.collect do |tag_class| |
| + | tag_class.initialize_tag_objects(tag_signature) |
| + | end |
| + | end.flatten.compact |
| + | end |
| + | |
| def self.included(tag) | |
| tag.send(:include, CmsTag::InstanceMethods) | |
| tag.send(:extend, CmsTag::ClassMethods) | |
| - | @@tag_instances ||= [] |
| - | @@tag_instances << tag |
| + | @@tag_classes ||= [] |
| + | @@tag_classes << tag |
| end | |
| - | def self.tag_instances |
| - | @@tag_instances |
| + | def self.tag_classes |
| + | @@tag_classes ||= [] |
| end | |
| end | |
| - | # Loading all cms_tags |
| - | Dir.glob(File.join(File.dirname(__FILE__), 'cms_tags', '*.rb')).each do |tag| |
| + | # Loading all cms_tags. Need to do this manually so CmsTag module is aware |
| + | # about all defined tags. |
| + | Dir.glob(File.join(File.dirname(__FILE__), 'cms_tag', '*.rb')).each do |tag| |
| require tag | |
| - | end |
| \ No newline at end of file | |
| + | end |
app/models/cms_tag/field_datetime.rb.todo
+3
-0
| @@ | @@ -0,0 +1,3 @@ |
| + | class CmsTag::FieldDateTime |
| + | |
| + | end |
| \ No newline at end of file | |
app/models/cms_tag/field_integer.rb.todo
+3
-0
| @@ | @@ -0,0 +1,3 @@ |
| + | class CmsTag::FieldInteger |
| + | |
| + | end |
| \ No newline at end of file | |
app/models/cms_tag/field_string.rb.todo
+3
-0
| @@ | @@ -0,0 +1,3 @@ |
| + | class CmsTag::FieldString |
| + | |
| + | end |
| \ No newline at end of file | |
app/models/cms_tag/field_text.rb.todo
+3
-0
| @@ | @@ -0,0 +1,3 @@ |
| + | class CmsTag::FieldText |
| + | |
| + | end |
| \ No newline at end of file | |
app/models/cms_tag/page_datetime.rb.todo
+3
-0
| @@ | @@ -0,0 +1,3 @@ |
| + | class CmsTag::PageDateTime |
| + | |
| + | end |
| \ No newline at end of file | |
app/models/cms_tag/page_integer.rb
+18
-0
| @@ | @@ -0,0 +1,18 @@ |
| + | class CmsTag::PageInteger < CmsBlock |
| + | |
| + | include CmsTag |
| + | |
| + | def self.regex_tag_signature(label = nil) |
| + | label ||= /\w+/ |
| + | /<\s*?#{TAG_PREFIX}:page:(#{label}):integer\s*?\/?>/ |
| + | end |
| + | |
| + | def regex_tag_signature |
| + | self.class.regex_tag_signature(label) |
| + | end |
| + | |
| + | def content |
| + | read_attribute(:content_integer) |
| + | end |
| + | |
| + | end |
| \ No newline at end of file | |
app/models/cms_tag/page_string.rb
+18
-0
| @@ | @@ -0,0 +1,18 @@ |
| + | class CmsTag::PageString < CmsBlock |
| + | |
| + | include CmsTag |
| + | |
| + | def self.regex_tag_signature(label = nil) |
| + | label ||= /\w+/ |
| + | /<\s*?#{TAG_PREFIX}:page:(#{label}):string\s*?\/?>/ |
| + | end |
| + | |
| + | def regex_tag_signature |
| + | self.class.regex_tag_signature(label) |
| + | end |
| + | |
| + | def content |
| + | read_attribute(:content_string) |
| + | end |
| + | |
| + | end |
| \ No newline at end of file | |
app/models/cms_tag/page_text.rb
+18
-0
| @@ | @@ -0,0 +1,18 @@ |
| + | class CmsTag::PageText < CmsBlock |
| + | |
| + | include CmsTag |
| + | |
| + | def self.regex_tag_signature(label = nil) |
| + | label ||= /\w+/ |
| + | /<\s*?#{TAG_PREFIX}:page:(#{label}):?(?:text)?\s*?\/?>/ |
| + | end |
| + | |
| + | def regex_tag_signature |
| + | self.class.regex_tag_signature(label) |
| + | end |
| + | |
| + | def content |
| + | read_attribute(:content_text) |
| + | end |
| + | |
| + | end |
| \ No newline at end of file | |
app/models/cms_tag/partial.rb.todo
+3
-0
| @@ | @@ -0,0 +1,3 @@ |
| + | class CmsTag::Partial |
| + | |
| + | end |
| \ No newline at end of file | |
app/models/cms_tag/snippet.rb.todo
+3
-0
| @@ | @@ -0,0 +1,3 @@ |
| + | class CmsTag::Snippet |
| + | |
| + | end |
| \ No newline at end of file | |
app/models/cms_tags/field_datetime.rb.todo
+0
-3
| @@ | @@ -1,3 +0,0 @@ |
| - | class CmsTag::FieldDateTime |
| - | |
| - | end |
| \ No newline at end of file | |
app/models/cms_tags/field_integer.rb.todo
+0
-3
| @@ | @@ -1,3 +0,0 @@ |
| - | class CmsTag::FieldInteger |
| - | |
| - | end |
| \ No newline at end of file | |
app/models/cms_tags/field_string.rb.todo
+0
-3
| @@ | @@ -1,3 +0,0 @@ |
| - | class CmsTag::FieldString |
| - | |
| - | end |
| \ No newline at end of file | |
app/models/cms_tags/field_text.rb.todo
+0
-3
| @@ | @@ -1,3 +0,0 @@ |
| - | class CmsTag::FieldText |
| - | |
| - | end |
| \ No newline at end of file | |
app/models/cms_tags/page_datetime.rb.todo
+0
-3
| @@ | @@ -1,3 +0,0 @@ |
| - | class CmsTag::PageDateTime |
| - | |
| - | end |
| \ No newline at end of file | |
app/models/cms_tags/page_integer.rb
+0
-18
| @@ | @@ -1,18 +0,0 @@ |
| - | class CmsTag::PageInteger < CmsBlock |
| - | |
| - | include CmsTag |
| - | |
| - | def self.regex_tag_signature(label = nil) |
| - | label ||= /\w+/ |
| - | /<\s*?cms:page:(#{label}):integer\s*?\/?>/ |
| - | end |
| - | |
| - | def regex_tag_signature |
| - | self.class.regex_tag_signature(label) |
| - | end |
| - | |
| - | def content |
| - | read_attribute(:content_integer) |
| - | end |
| - | |
| - | end |
| \ No newline at end of file | |
app/models/cms_tags/page_string.rb
+0
-18
| @@ | @@ -1,18 +0,0 @@ |
| - | class CmsTag::PageString < CmsBlock |
| - | |
| - | include CmsTag |
| - | |
| - | def self.regex_tag_signature(label = nil) |
| - | label ||= /\w+/ |
| - | /<\s*?cms:page:(#{label}):string\s*?\/?>/ |
| - | end |
| - | |
| - | def regex_tag_signature |
| - | self.class.regex_tag_signature(label) |
| - | end |
| - | |
| - | def content |
| - | read_attribute(:content_string) |
| - | end |
| - | |
| - | end |
| \ No newline at end of file | |
app/models/cms_tags/page_text.rb
+0
-18
| @@ | @@ -1,18 +0,0 @@ |
| - | class CmsTag::PageText < CmsBlock |
| - | |
| - | include CmsTag |
| - | |
| - | def self.regex_tag_signature(label = nil) |
| - | label ||= /\w+/ |
| - | /<\s*?cms:page:(#{label}):?(?:text)?\s*?\/?>/ |
| - | end |
| - | |
| - | def regex_tag_signature |
| - | self.class.regex_tag_signature(label) |
| - | end |
| - | |
| - | def content |
| - | read_attribute(:content_text) |
| - | end |
| - | |
| - | end |
| \ No newline at end of file | |
app/models/cms_tags/partial.rb.todo
+0
-3
| @@ | @@ -1,3 +0,0 @@ |
| - | class CmsTag::Partial |
| - | |
| - | end |
| \ No newline at end of file | |
app/models/cms_tags/snippet.rb.todo
+0
-3
| @@ | @@ -1,3 +0,0 @@ |
| - | class CmsTag::Snippet |
| - | |
| - | end |
| \ No newline at end of file | |
config/application.rb
+3
-0
| @@ | @@ -6,8 +6,11 @@ require 'rails/all' |
| # you've limited to :test, :development, or :production. | |
| Bundler.require(:default, Rails.env) if defined?(Bundler) | |
| + | |
| + | |
| module ComfortableMexicanSofa | |
| class Application < Rails::Application | |
| + | |
| # Settings in config/environments/* take precedence over those specified here. | |
| # Application configuration should go into files in config/initializers | |
| # -- all .rb files in that directory are automatically loaded. | |
test/unit/cms_page_test.rb
+5
-0
| @@ | @@ -11,4 +11,9 @@ class CmsPageTest < ActiveSupport::TestCase |
| ].join("\n"), page.content | |
| end | |
| + | def test_blocks_initialization |
| + | page = cms_pages(:default) |
| + | page.initialize_tags |
| + | end |
| + | |
| end | |
test/unit/cms_tag_test.rb
+18
-0
| @@ | @@ -0,0 +1,18 @@ |
| + | require File.dirname(__FILE__) + '/../test_helper' |
| + | |
| + | class CmsTagTest < ActiveSupport::TestCase |
| + | |
| + | def test_method_find_cms_tags |
| + | content = cms_layouts(:default).content |
| + | assert_equal [ |
| + | '<cms:page:content/>', |
| + | '<cms:page:title:string/>', |
| + | '<cms:page:number:integer/>' |
| + | ], CmsTag.find_cms_tags(content) |
| + | end |
| + | |
| + | def test_method_initialize_tags |
| + | raise CmsTag.initialize_tags(cms_layouts(:default).content).inspect |
| + | end |
| + | |
| + | end |