stuff broken, but renderer works!
Oleg
committed Sep 17, 2010
commit 93d02f03e1bbf9570fc77766bb44a2f077bd11f6
Showing 21
changed files with
189 additions
and 124 deletions
app/models/cms_block.rb
+6
-0
| @@ | @@ -19,5 +19,11 @@ class CmsBlock < ActiveRecord::Base |
| new_without_cast(*args, &block) | |
| end | |
| alias_method_chain :new, :cast | |
| + | |
| + | def initialize_or_find(cms_page, label) |
| + | cms_page.cms_blocks.detect{ |b| b.label == label } || |
| + | self.new(:label => label, :type => self.name, :cms_page => cms_page) |
| + | end |
| end | |
| + | |
| end | |
app/models/cms_page.rb
+17
-21
| @@ | @@ -47,13 +47,9 @@ class CmsPage < ActiveRecord::Base |
| end | |
| # -- Instance Methods ----------------------------------------------------- | |
| - | # Scans through the content defined in the layout and replaces tag signatures |
| - | # with content defined in cms_blocks, or whatever tag's render method does |
| - | # TODO: This is incomplete, need to implement tag tree rendering |
| - | def initialize_content |
| - | layout_content = cms_layout.content.dup |
| - | raise CmsTag.tag_classes.inspect |
| - | |
| + | # Processing content will return rendered content and all tag that were used. |
| + | def process_content |
| + | CmsTag.process_content(self, cms_layout.content.dup) |
| end | |
| # Initilize tags the moment layout gets assigned. This way there's no need to | |
| @@ | @@ -72,23 +68,23 @@ 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(self) |
| - | end |
| + | # def initialize_tags |
| + | # CmsTag.initialize_tags(self) |
| + | # end |
| # Converting object from cms_tags collection into cms_blocks | |
| # TODO: Explicitely calling it. Can't really put it into a callback | |
| - | def assign_cms_blocks! |
| - | self.cms_tags.each do |tag| |
| - | if block = self.cms_blocks.find_by_label(tag.label) |
| - | block.type = tag.type |
| - | block.content = tag.content |
| - | block.save! |
| - | else |
| - | self.cms_blocks.create!(:label => tag.label, :type => tag.type, :content => tag.content) |
| - | end |
| - | end |
| - | end |
| + | # def assign_cms_blocks! |
| + | # self.cms_tags.each do |tag| |
| + | # if block = self.cms_blocks.find_by_label(tag.label) |
| + | # block.type = tag.type |
| + | # block.content = tag.content |
| + | # block.save! |
| + | # else |
| + | # self.cms_blocks.create!(:label => tag.label, :type => tag.type, :content => tag.content) |
| + | # end |
| + | # end |
| + | # end |
| protected | |
app/models/cms_snippet.rb
+4
-0
| @@ | @@ -12,5 +12,9 @@ class CmsSnippet < ActiveRecord::Base |
| def self.content_for(label) | |
| (s = find_by_label(label)) ? s.content : '' | |
| end | |
| + | |
| + | def self.initialize_or_find(cms_page, label) |
| + | find_by_label(label) || new(:label => label) |
| + | end |
| end | |
comfortable_mexican_sofa.rb b/lib/comfortable_mexican_sofa.rb
+1
-0
| @@ | @@ -2,6 +2,7 @@ |
| comfortable_mexican_sofa/cms_form_builder | |
| comfortable_mexican_sofa/cms_acts_as_tree | |
| ../app/models/cms_block | |
| + | ../app/models/cms_snippet |
| comfortable_mexican_sofa/cms_tag | |
| ).each do |path| | |
| require File.expand_path(path, File.dirname(__FILE__)) | |
comfortable_mexican_sofa/cms_tag.rb b/lib/comfortable_mexican_sofa/cms_tag.rb
+12
-12
| @@ | @@ -20,19 +20,17 @@ module CmsTag |
| # First capture group in the regex is the tag label | |
| def initialize_tag(cms_page, tag_signature) | |
| if match = tag_signature.match(regex_tag_signature) | |
| - | self.new(:label => match[1]) |
| + | if self.respond_to?(:initialize_or_find) |
| + | self.initialize_or_find(cms_page, match[1]) |
| + | else |
| + | self.new(:label => match[1]) |
| + | end |
| end | |
| end | |
| end | |
| module InstanceMethods | |
| - | attr_accessor :parent |
| - | |
| - | def identifier |
| - | "#{self.class.name.underscore}_#{self.label}" |
| - | end |
| - | |
| # Regex that is used to identify instance of the tag | |
| # Example: | |
| # /<\s*?cms:page:tag_label\/?>/ | |
| @@ | @@ -58,22 +56,24 @@ private |
| # Initializes a tag. It's handled by one of the tag classes | |
| def self.initialize_tag(cms_page, tag_signature) | |
| - | tag_classes.find{ |c| c.initialize_tag(cms_page, tag_signature) } |
| + | tag_instance = nil |
| + | tag_classes.find{ |c| tag_instance = c.initialize_tag(cms_page, tag_signature) } |
| + | tag_instance |
| end | |
| # Scanning provided content and splitting it into [tag, text] tuples. | |
| # Tags are processed further and their content is expanded in the same way | |
| def self.process_content(cms_page, content = '') | |
| - | tokens = content.to_s.scan(/(<\s*cms:\w+:\w+\s*\/?>)|((?:[^<]|\<(?!\s*cms:\w+:\w+\s*\/?>))+)/) |
| + | tokens = content.to_s.scan(/(<\s*cms:\w+:\w+(?::\w+)?\s*\/?>)|((?:[^<]|\<(?!\s*cms:\w+:\w+(?::\w+)?\s*\/?>))+)/) |
| tokens.collect do |tag_signature, text| | |
| if tag_signature | |
| - | if tag = self.initialize_tag(tag_signature) |
| - | self.process_content(tag.content) |
| + | if tag = self.initialize_tag(cms_page, tag_signature) |
| + | self.process_content(cms_page, tag.render) |
| end | |
| else | |
| text | |
| end | |
| - | end |
| + | end.join('') |
| end | |
| def self.included(tag) | |
comfortable_mexican_sofa/cms_tag/field_text.rb b/lib/comfortable_mexican_sofa/cms_tag/field_text.rb
+26
-0
| @@ | @@ -0,0 +1,26 @@ |
| + | class CmsTag::FieldText < CmsBlock |
| + | |
| + | include CmsTag |
| + | |
| + | def self.regex_tag_signature(label = nil) |
| + | label ||= /\w+/ |
| + | /<\s*?cms:field:(#{label}):?(?:text)?\s*?\/?>/ |
| + | end |
| + | |
| + | def regex_tag_signature |
| + | self.class.regex_tag_signature(label) |
| + | end |
| + | |
| + | def content=(value) |
| + | write_attribute(:content_text, value) |
| + | end |
| + | |
| + | def content |
| + | read_attribute(:content_text) |
| + | end |
| + | |
| + | def render |
| + | '' |
| + | end |
| + | |
| + | end |
| \ No newline at end of file | |
comfortable_mexican_sofa/cms_tag/field_text.rb.todo b/lib/comfortable_mexican_sofa/cms_tag/field_text.rb.todo
+0
-3
| @@ | @@ -1,3 +0,0 @@ |
| - | class CmsTag::FieldText |
| - | |
| - | end |
| \ No newline at end of file | |
comfortable_mexican_sofa/cms_tag/page_integer.rb b/lib/comfortable_mexican_sofa/cms_tag/page_integer.rb
+0
-22
| @@ | @@ -1,22 +0,0 @@ |
| - | 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=(value) |
| - | write_attribute(:content_integer, value) |
| - | end |
| - | |
| - | def content |
| - | read_attribute(:content_integer) |
| - | end |
| - | |
| - | end |
| \ No newline at end of file | |
comfortable_mexican_sofa/cms_tag/page_integer.rb.todo b/lib/comfortable_mexican_sofa/cms_tag/page_integer.rb.todo
+22
-0
| @@ | @@ -0,0 +1,22 @@ |
| + | 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=(value) |
| + | write_attribute(:content_integer, value) |
| + | end |
| + | |
| + | def content |
| + | read_attribute(:content_integer) |
| + | end |
| + | |
| + | end |
| \ No newline at end of file | |
comfortable_mexican_sofa/cms_tag/page_string.rb b/lib/comfortable_mexican_sofa/cms_tag/page_string.rb
+0
-22
| @@ | @@ -1,22 +0,0 @@ |
| - | 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=(value) |
| - | write_attribute(:content_string, value) |
| - | end |
| - | |
| - | def content |
| - | read_attribute(:content_string) |
| - | end |
| - | |
| - | end |
| \ No newline at end of file | |
comfortable_mexican_sofa/cms_tag/page_string.rb.todo b/lib/comfortable_mexican_sofa/cms_tag/page_string.rb.todo
+22
-0
| @@ | @@ -0,0 +1,22 @@ |
| + | 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=(value) |
| + | write_attribute(:content_string, value) |
| + | end |
| + | |
| + | def content |
| + | read_attribute(:content_string) |
| + | end |
| + | |
| + | end |
| \ No newline at end of file | |
comfortable_mexican_sofa/cms_tag/partial.rb b/lib/comfortable_mexican_sofa/cms_tag/partial.rb
+0
-18
| @@ | @@ -1,18 +0,0 @@ |
| - | class CmsTag::Partial |
| - | |
| - | include CmsTag |
| - | |
| - | def self.regex_tag_signature(label = nil) |
| - | label ||= /\w+/ |
| - | /<\s*?#{TAG_PREFIX}:partial:(#{label})\s*?\/?>/ |
| - | end |
| - | |
| - | def regex_tag_signature |
| - | self.class.regex_tag_signature(label) |
| - | end |
| - | |
| - | def content |
| - | "partial #{label}" |
| - | end |
| - | |
| - | end |
| \ No newline at end of file | |
comfortable_mexican_sofa/cms_tag/partial.rb.todo b/lib/comfortable_mexican_sofa/cms_tag/partial.rb.todo
+18
-0
| @@ | @@ -0,0 +1,18 @@ |
| + | class CmsTag::Partial |
| + | |
| + | include CmsTag |
| + | |
| + | def self.regex_tag_signature(label = nil) |
| + | label ||= /\w+/ |
| + | /<\s*?#{TAG_PREFIX}:partial:(#{label})\s*?\/?>/ |
| + | end |
| + | |
| + | def regex_tag_signature |
| + | self.class.regex_tag_signature(label) |
| + | end |
| + | |
| + | def content |
| + | "partial #{label}" |
| + | end |
| + | |
| + | end |
| \ No newline at end of file | |
comfortable_mexican_sofa/cms_tag/snippet.rb b/lib/comfortable_mexican_sofa/cms_tag/snippet.rb
+18
-0
| @@ | @@ -0,0 +1,18 @@ |
| + | class CmsTag::Snippet < CmsSnippet |
| + | |
| + | include CmsTag |
| + | |
| + | def self.regex_tag_signature(label = nil) |
| + | label ||= /\w+/ |
| + | /<\s*?cms:snippet:(#{label})\s*?\/?>/ |
| + | end |
| + | |
| + | def regex_tag_signature |
| + | self.class.regex_tag_signature(label) |
| + | end |
| + | |
| + | def content |
| + | self.read_attribute(:content) |
| + | end |
| + | |
| + | end |
| \ No newline at end of file | |
comfortable_mexican_sofa/cms_tag/snippet.rb.todo b/lib/comfortable_mexican_sofa/cms_tag/snippet.rb.todo
+0
-3
| @@ | @@ -1,3 +0,0 @@ |
| - | class CmsTag::Snippet |
| - | |
| - | end |
| \ No newline at end of file | |
test/fixtures/README.md
+22
-0
| @@ | @@ -0,0 +1,22 @@ |
| + | Default Test Application Data Structure |
| + | ======================================= |
| + | Layout |
| + | ------ |
| + | <cms:field:default_field:text> |
| + | content_a |
| + | <cms:page:default_page:text> |
| + | content_b |
| + | <cms:snippet:default_snippet> |
| + | content_c |
| + | Page |
| + | ---- |
| + | ### CmsBlock::FieldText |
| + | default_field_content |
| + | ### CmsBlock::PageText |
| + | default_page_content_a |
| + | <cms:snippet:default_snippet> |
| + | default_page_content_a |
| + | Snippets |
| + | ------- |
| + | ### Snippet |
| + | default_snippet_content |
| \ No newline at end of file | |
test/fixtures/cms_blocks.yml
+11
-14
| @@ | @@ -1,17 +1,14 @@ |
| + | default_field_text: |
| + | cms_page: default |
| + | type: CmsTag::FieldText |
| + | label: default_field_text |
| + | content_text: default_field_text_content |
| + | |
| default_page_text: | |
| cms_page: default | |
| type: CmsTag::PageText | |
| - | label: content |
| - | content_text: default_page_text_content |
| - | |
| - | default_page_string: |
| - | cms_page: default |
| - | type: CmsTag::PageString |
| - | label: title |
| - | content_string: default_page_string_content |
| - | |
| - | default_page_integer: |
| - | cms_page: default |
| - | type: CmsTag::PageInteger |
| - | label: number |
| - | content_integer: 1 |
| + | label: default_page_text |
| + | content_text: |- |
| + | default_page_text_content_a |
| + | <cms:snippet:default_snippet> |
| + | default_page_text_content_b |
test/fixtures/cms_layouts.yml
+6
-3
| @@ | @@ -2,9 +2,12 @@ default: |
| label: Default Layout | |
| parent: | |
| content: |- | |
| - | <cms:page:content/> |
| - | <cms:page:title:string/> |
| - | <cms:page:number:integer/> |
| + | <cms:field:default_field_text:text> |
| + | content_a |
| + | <cms:page:default_page_text:text> |
| + | content_b |
| + | <cms:snippet:default_snippet> |
| + | content_c |
| nested: | |
| label: Nested Layout | |
test/fixtures/cms_pages.yml
+1
-1
| @@ | @@ -1,4 +1,4 @@ |
| - | default: &page |
| + | default: |
| parent: | |
| cms_layout: default | |
| label: Default Page | |
test/fixtures/cms_snippets.yml
+1
-1
| @@ | @@ -1,3 +1,3 @@ |
| default: | |
| label: default_snippet | |
| - | content: snippet content |
| + | content: default_snippet_content |
test/unit/cms_tag_test.rb
+2
-4
| @@ | @@ -3,10 +3,8 @@ require File.dirname(__FILE__) + '/../test_helper' |
| class CmsTagTest < ActiveSupport::TestCase | |
| def test_initialization_and_rendering | |
| - | content = 'a <cms:page:tag_1> b <cms:page:tag_2> c' |
| - | raise CmsTag.process_content(content).to_yaml |
| - | |
| - | |
| + | page = cms_pages(:default) |
| + | raise page.process_content.to_yaml |
| end | |
| def test_method_find_cms_tags | |