tag models are not reloaded on each request in dev mode, ugh
Oleg
committed Aug 27, 2010
commit 9abf3be85cd4e786b2db34a59bd0acbb568eb625
Showing 26
changed files with
189 additions
and 166 deletions
app/controllers/cms_admin/pages_controller.rb
+10
-0
| @@ | @@ -9,11 +9,21 @@ class CmsAdmin::PagesController < CmsAdmin::BaseController |
| def new | |
| render | |
| end | |
| + | |
| + | def edit |
| + | render |
| + | end |
| + | |
| + | def create |
| + | |
| + | end |
| protected | |
| def build_cms_page | |
| @cms_page = CmsPage.new(params[:cms_page]) | |
| + | @cms_page.cms_layout = CmsLayout.first |
| + | CmsTag.initialize_tags(@cms_page.cms_layout.content, :cms_page => @cms_page) |
| end | |
| end | |
app/models/cms_tag.rb
+0
-90
| @@ | @@ -1,90 +0,0 @@ |
| - | # 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: |
| - | # /<\s*?cms:page:(\w+)\/?>/ |
| - | # will match tags like these: |
| - | # <cms:page:my_label/> |
| - | def regex_tag_signature |
| - | nil |
| - | 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.find_by_label(label) || self.new(:label => label, :cms_page => cms_page) |
| - | else |
| - | self.new(:label => label) |
| - | end |
| - | end |
| - | end |
| - | end |
| - | |
| - | module InstanceMethods |
| - | # Regex that is used to identify instance of the tag |
| - | # Example: |
| - | # /<\s*?cms:page:tag_label\/?>/ |
| - | def regex_tag_signature |
| - | nil |
| - | end |
| - | |
| - | # Content that gets returned by the instance of the tag |
| - | def content |
| - | '' |
| - | end |
| - | |
| - | # Content that is used during page rendering |
| - | def render |
| - | content |
| - | end |
| - | end |
| - | |
| - | private |
| - | |
| - | # scans for cms tags inside given content |
| - | def self.find_cms_tags(content = '') |
| - | content.scan(/<\s*#{TAG_PREFIX}:.+\s*\/?>/).flatten |
| - | end |
| - | |
| - | # Scans provided content and initializes Tag objects based |
| - | # on their tag signature |
| - | def self.initialize_tags(content = '', options = {}) |
| - | find_cms_tags(content).collect do |tag_signature| |
| - | tag_classes.collect do |tag_class| |
| - | tag_class.initialize_tag_objects(tag_signature, options) |
| - | end |
| - | end.flatten.compact |
| - | end |
| - | |
| - | def self.included(tag) |
| - | tag.send(:include, CmsTag::InstanceMethods) |
| - | tag.send(:extend, CmsTag::ClassMethods) |
| - | @@tag_classes ||= [] |
| - | @@tag_classes << tag |
| - | end |
| - | |
| - | # A list of registered Tag classes |
| - | def self.tag_classes |
| - | @@tag_classes ||= [] |
| - | end |
| - | end |
| - | |
| - | # 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 |
app/models/cms_tag/field_datetime.rb.todo
+0
-3
| @@ | @@ -1,3 +0,0 @@ |
| - | class CmsTag::FieldDateTime |
| - | |
| - | end |
| \ No newline at end of file | |
app/models/cms_tag/field_integer.rb.todo
+0
-3
| @@ | @@ -1,3 +0,0 @@ |
| - | class CmsTag::FieldInteger |
| - | |
| - | end |
| \ No newline at end of file | |
app/models/cms_tag/field_string.rb.todo
+0
-3
| @@ | @@ -1,3 +0,0 @@ |
| - | class CmsTag::FieldString |
| - | |
| - | end |
| \ No newline at end of file | |
app/models/cms_tag/field_text.rb.todo
+0
-3
| @@ | @@ -1,3 +0,0 @@ |
| - | class CmsTag::FieldText |
| - | |
| - | end |
| \ No newline at end of file | |
app/models/cms_tag/page_datetime.rb.todo
+0
-3
| @@ | @@ -1,3 +0,0 @@ |
| - | class CmsTag::PageDateTime |
| - | |
| - | end |
| \ No newline at end of file | |
app/models/cms_tag/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*?#{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
+0
-18
| @@ | @@ -1,18 +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 |
| - | read_attribute(:content_string) |
| - | end |
| - | |
| - | end |
| \ No newline at end of file | |
app/models/cms_tag/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*?#{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
+0
-3
| @@ | @@ -1,3 +0,0 @@ |
| - | class CmsTag::Partial |
| - | |
| - | end |
| \ No newline at end of file | |
app/models/cms_tag/snippet.rb.todo
+0
-3
| @@ | @@ -1,3 +0,0 @@ |
| - | class CmsTag::Snippet |
| - | |
| - | end |
| \ No newline at end of file | |
app/views/cms_admin/pages/_form.html.erb
+6
-1
| @@ | @@ -1,2 +1,7 @@ |
| <%= form.text_field :label %> | |
| - | <%= form.text_field :slug %> |
| \ No newline at end of file | |
| + | <%= form.text_field :slug %> |
| + | |
| + | |
| + | |
| + | <%= debug @cms_page.cms_layout.content %> |
| + | <%= debug @cms_page.cms_blocks %> |
| \ No newline at end of file | |
config/application.rb
+2
-0
| @@ | @@ -9,6 +9,8 @@ Bundler.require(:default, Rails.env) if defined?(Bundler) |
| module ComfortableMexicanSofa | |
| class Application < Rails::Application | |
| + | require 'comfortable_mexican_sofa' |
| + | |
| # 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. | |
comfortable_mexican_sofa.rb b/lib/comfortable_mexican_sofa.rb
+6
-0
| @@ | @@ -1,3 +1,9 @@ |
| + | require File.join(File.dirname(__FILE__), '..', 'app', 'models', 'cms_block') |
| + | require File.join(File.dirname(__FILE__), 'comfortable_mexican_sofa', 'cms_tag') |
| + | Dir.glob(File.join(File.dirname(__FILE__), 'comfortable_mexican_sofa', 'cms_tag', '*.rb')).each do |tag| |
| + | require tag |
| + | end |
| + | |
| module ComfortableMexicanSofa | |
| # TODO | |
comfortable_mexican_sofa/cms_tag.rb b/lib/comfortable_mexican_sofa/cms_tag.rb
+90
-0
| @@ | @@ -0,0 +1,90 @@ |
| + | # 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: |
| + | # /<\s*?cms:page:(\w+)\/?>/ |
| + | # will match tags like these: |
| + | # <cms:page:my_label/> |
| + | def regex_tag_signature |
| + | nil |
| + | 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 |
| + | else |
| + | self.new(:label => label) |
| + | end |
| + | end |
| + | end |
| + | end |
| + | |
| + | module InstanceMethods |
| + | # Regex that is used to identify instance of the tag |
| + | # Example: |
| + | # /<\s*?cms:page:tag_label\/?>/ |
| + | def regex_tag_signature |
| + | nil |
| + | end |
| + | |
| + | # Content that gets returned by the instance of the tag |
| + | def content |
| + | '' |
| + | end |
| + | |
| + | # Content that is used during page rendering |
| + | def render |
| + | content |
| + | end |
| + | end |
| + | |
| + | private |
| + | |
| + | # scans for cms tags inside given content |
| + | def self.find_cms_tags(content = '') |
| + | content.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| |
| + | tag_classes.collect do |tag_class| |
| + | tag_class.initialize_tag_objects(tag_signature, options) |
| + | end |
| + | end.flatten.compact |
| + | end |
| + | |
| + | def self.included(tag) |
| + | tag.send(:include, CmsTag::InstanceMethods) |
| + | tag.send(:extend, CmsTag::ClassMethods) |
| + | @@tag_classes ||= [] |
| + | @@tag_classes << tag |
| + | end |
| + | |
| + | # A list of registered Tag classes |
| + | def self.tag_classes |
| + | @@tag_classes ||= [] |
| + | end |
| + | end |
| \ No newline at end of file | |
comfortable_mexican_sofa/cms_tag/field_datetime.rb.todo b/lib/comfortable_mexican_sofa/cms_tag/field_datetime.rb.todo
+3
-0
| @@ | @@ -0,0 +1,3 @@ |
| + | class CmsTag::FieldDateTime |
| + | |
| + | end |
| \ No newline at end of file | |
comfortable_mexican_sofa/cms_tag/field_integer.rb.todo b/lib/comfortable_mexican_sofa/cms_tag/field_integer.rb.todo
+3
-0
| @@ | @@ -0,0 +1,3 @@ |
| + | class CmsTag::FieldInteger |
| + | |
| + | end |
| \ No newline at end of file | |
comfortable_mexican_sofa/cms_tag/field_string.rb.todo b/lib/comfortable_mexican_sofa/cms_tag/field_string.rb.todo
+3
-0
| @@ | @@ -0,0 +1,3 @@ |
| + | class CmsTag::FieldString |
| + | |
| + | 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
+3
-0
| @@ | @@ -0,0 +1,3 @@ |
| + | class CmsTag::FieldText |
| + | |
| + | end |
| \ No newline at end of file | |
comfortable_mexican_sofa/cms_tag/page_datetime.rb.todo b/lib/comfortable_mexican_sofa/cms_tag/page_datetime.rb.todo
+3
-0
| @@ | @@ -0,0 +1,3 @@ |
| + | class CmsTag::PageDateTime |
| + | |
| + | 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
+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 | |
comfortable_mexican_sofa/cms_tag/page_string.rb b/lib/comfortable_mexican_sofa/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 | |
comfortable_mexican_sofa/cms_tag/page_text.rb b/lib/comfortable_mexican_sofa/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 | |
comfortable_mexican_sofa/cms_tag/partial.rb.todo b/lib/comfortable_mexican_sofa/cms_tag/partial.rb.todo
+3
-0
| @@ | @@ -0,0 +1,3 @@ |
| + | class CmsTag::Partial |
| + | |
| + | 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
+3
-0
| @@ | @@ -0,0 +1,3 @@ |
| + | class CmsTag::Snippet |
| + | |
| + | end |
| \ No newline at end of file | |