moving AR models to Cms:: namespace. Unit tests are ok, functional stuff broken
Oleg
committed Apr 15, 2011
commit 35c551c4a42f3ec6962b9be34eb8812d65a68eb6
Showing 53
changed files with
1009 additions
and 975 deletions
app/models/cms/block.rb
+30
-0
| @@ | @@ -0,0 +1,30 @@ |
| + | class Cms::Block < ActiveRecord::Base |
| + | |
| + | set_table_name :cms_blocks |
| + | |
| + | # -- Relationships -------------------------------------------------------- |
| + | belongs_to :page |
| + | |
| + | # -- Validations ---------------------------------------------------------- |
| + | validates :label, |
| + | :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
+90
-0
| @@ | @@ -0,0 +1,90 @@ |
| + | class Cms::Layout < ActiveRecord::Base |
| + | |
| + | set_table_name :cms_layouts |
| + | |
| + | acts_as_tree |
| + | |
| + | # -- Relationships -------------------------------------------------------- |
| + | belongs_to :site |
| + | has_many :pages, :dependent => :nullify |
| + | |
| + | # -- Callbacks ------------------------------------------------------------ |
| + | after_save :clear_cache, :clear_cached_page_content |
| + | after_destroy :clear_cache, :clear_cached_page_content |
| + | |
| + | # -- Validations ---------------------------------------------------------- |
| + | validates :site_id, |
| + | :presence => true |
| + | validates :label, |
| + | :presence => true |
| + | validates :slug, |
| + | :presence => true, |
| + | :uniqueness => { :scope => :site_id }, |
| + | :format => { :with => /^\w[a-z0-9_-]*$/i } |
| + | validates :content, |
| + | :presence => true |
| + | validate :check_content_tag_presence |
| + | |
| + | # -- Class Methods -------------------------------------------------------- |
| + | # Tree-like structure for layouts |
| + | def self.options_for_select(site, layout = nil, current_layout = nil, depth = 0, spacer = '. . ') |
| + | out = [] |
| + | [current_layout || site.layouts.roots].flatten.each do |l| |
| + | next if layout == l |
| + | out << [ "#{spacer*depth}#{l.label}", l.id ] |
| + | l.children.each do |child| |
| + | out += options_for_select(site, layout, child, depth + 1, spacer) |
| + | end |
| + | end |
| + | return out.compact |
| + | end |
| + | |
| + | # List of available application layouts |
| + | def self.app_layouts_for_select |
| + | 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.to_s[0...1] == '_' ? nil : app_layout |
| + | end.compact |
| + | end |
| + | |
| + | # -- Instance Methods ----------------------------------------------------- |
| + | # magical merging tag is {cms:page:content} If parent layout has this tag |
| + | # defined its content will be merged. If no such tag found, parent content |
| + | # is ignored. |
| + | def merged_content |
| + | if parent |
| + | regex = /\{\{\s*cms:page:content:?(?:(?::text)|(?::rich_text))?\s*\}\}/ |
| + | if parent.merged_content.match(regex) |
| + | parent.merged_content.gsub(regex, content) |
| + | else |
| + | content |
| + | end |
| + | else |
| + | content |
| + | end |
| + | end |
| + | |
| + | protected |
| + | |
| + | def check_content_tag_presence |
| + | CmsTag.process_content((test_page = site.pages.new), content) |
| + | if test_page.tags.select{|t| t.class.superclass == Cms::Block}.blank? |
| + | self.errors.add(:content, 'No cms page tags defined') |
| + | end |
| + | end |
| + | |
| + | # After saving need to make sure that cached pages for css and js for this |
| + | # layout and its children are gone. Good enough to avoid using cache sweepers. |
| + | def clear_cache |
| + | FileUtils.rm File.expand_path("cms-css/#{self.slug}.css", Rails.public_path), :force => true |
| + | FileUtils.rm File.expand_path("cms-js/#{self.slug}.js", Rails.public_path), :force => true |
| + | end |
| + | |
| + | # Forcing page content reload |
| + | def clear_cached_page_content |
| + | self.pages.each{ |page| page.save! } |
| + | self.children.each{ |child_layout| child_layout.save! } |
| + | end |
| + | |
| + | end |
app/models/cms/page.rb
+134
-0
| @@ | @@ -0,0 +1,134 @@ |
| + | class Cms::Page < ActiveRecord::Base |
| + | |
| + | set_table_name :cms_pages |
| + | |
| + | # -- AR Extensions -------------------------------------------------------- |
| + | acts_as_tree :counter_cache => :children_count |
| + | |
| + | attr_accessor :tags |
| + | |
| + | # -- Relationships -------------------------------------------------------- |
| + | belongs_to :site |
| + | belongs_to :layout |
| + | belongs_to :target_page, |
| + | :class_name => 'Cms::Page' |
| + | has_many :blocks, |
| + | :dependent => :destroy |
| + | accepts_nested_attributes_for :blocks |
| + | |
| + | # -- Callbacks ------------------------------------------------------------ |
| + | before_validation :assign_parent, |
| + | :assign_full_path |
| + | before_validation :assign_position, |
| + | :on => :create |
| + | before_save :set_cached_content |
| + | after_save :sync_child_pages |
| + | |
| + | # -- Validations ---------------------------------------------------------- |
| + | validates :site_id, |
| + | :presence => true |
| + | validates :label, |
| + | :presence => true |
| + | validates :slug, |
| + | :presence => true, |
| + | :format => /^\w[a-z0-9_-]*$/i, |
| + | :unless => lambda{ |p| p == Cms::Page.root || p.site && p.site.pages.count == 0 } |
| + | validates :layout, |
| + | :presence => true |
| + | validates :full_path, |
| + | :presence => true, |
| + | :uniqueness => { :scope => :site_id } |
| + | validate :validate_target_page |
| + | |
| + | # -- Scopes --------------------------------------------------------------- |
| + | default_scope order(:position) |
| + | scope :published, where(:is_published => true) |
| + | |
| + | # -- Class Methods -------------------------------------------------------- |
| + | # Tree-like structure for pages |
| + | def self.options_for_select(site, page = nil, current_page = nil, depth = 0, exclude_self = true, spacer = '. . ') |
| + | return [] if (current_page ||= site.pages.root) == page && exclude_self || !current_page |
| + | out = [] |
| + | out << [ "#{spacer*depth}#{current_page.label}", current_page.id ] unless current_page == page |
| + | current_page.children.each do |child| |
| + | out += options_for_select(site, page, child, depth + 1, exclude_self, spacer) |
| + | end |
| + | return out.compact |
| + | end |
| + | |
| + | # -- Instance Methods ----------------------------------------------------- |
| + | # For previewing purposes sometimes we need to have full_path set |
| + | def full_path |
| + | self.read_attribute(:full_path) || self.assign_full_path |
| + | end |
| + | |
| + | # Transforms existing cms_block information into a hash that can be used |
| + | # during form processing. That's the only way to modify cms_blocks. |
| + | def blocks_attributes |
| + | self.blocks.inject([]) do |arr, block| |
| + | block_attr = {} |
| + | block_attr[:label] = block.label |
| + | block_attr[:content] = block.content |
| + | block_attr[:id] = block.id |
| + | arr << block_attr |
| + | end |
| + | end |
| + | |
| + | # Processing content will return rendered content and will populate |
| + | # self.cms_tags with instances of CmsTag |
| + | def content(force_reload = false) |
| + | @content = read_attribute(:content) |
| + | @content = nil if force_reload |
| + | @content ||= begin |
| + | self.tags = [] # resetting |
| + | layout ? CmsTag.process_content(self, layout.merged_content) : '' |
| + | end |
| + | end |
| + | |
| + | # Array of cms_tags for a page. Content generation is called if forced. |
| + | # These also include initialized cms_blocks if present |
| + | def tags(force_reload = false) |
| + | self.content(true) if force_reload |
| + | @tags ||= [] |
| + | end |
| + | |
| + | # Full url for a page |
| + | def url |
| + | "http://#{self.site.hostname}#{self.full_path}" |
| + | end |
| + | |
| + | protected |
| + | |
| + | def assign_parent |
| + | return unless site |
| + | self.parent ||= site.pages.root unless self == site.pages.root || site.pages.count == 0 |
| + | end |
| + | |
| + | def assign_full_path |
| + | self.full_path = self.parent ? "#{self.parent.full_path}/#{self.slug}".squeeze('/') : '/' |
| + | end |
| + | |
| + | def assign_position |
| + | return unless self.parent |
| + | max = self.parent.children.maximum(:position) |
| + | self.position = max ? max + 1 : 0 |
| + | end |
| + | |
| + | def validate_target_page |
| + | return unless self.target_page |
| + | p = self |
| + | while p.target_page do |
| + | return self.errors.add(:target_page_id, 'Invalid Redirect') if (p = p.target_page) == self |
| + | end |
| + | end |
| + | |
| + | def set_cached_content |
| + | write_attribute(:content, self.content(true)) |
| + | end |
| + | |
| + | # Forcing re-saves for child pages so they can update full_paths |
| + | def sync_child_pages |
| + | children.each{ |p| p.save! } if full_path_changed? |
| + | end |
| + | |
| + | end |
app/models/cms/site.rb
+25
-0
| @@ | @@ -0,0 +1,25 @@ |
| + | class Cms::Site < ActiveRecord::Base |
| + | |
| + | set_table_name :cms_sites |
| + | |
| + | # -- Relationships -------------------------------------------------------- |
| + | has_many :layouts, :dependent => :destroy |
| + | has_many :pages, :dependent => :destroy |
| + | has_many :snippets, :dependent => :destroy |
| + | has_many :uploads, :dependent => :destroy |
| + | |
| + | # -- Validations ---------------------------------------------------------- |
| + | validates :label, |
| + | :presence => true, |
| + | :uniqueness => true |
| + | validates :hostname, |
| + | :presence => true, |
| + | :uniqueness => true, |
| + | :format => { :with => /^[\w\.\-]+$/ } |
| + | |
| + | # -- Class Methods -------------------------------------------------------- |
| + | def self.options_for_select |
| + | Cms::Site.all.collect{|s| ["#{s.label} (#{s.hostname})", s.id]} |
| + | end |
| + | |
| + | end |
| \ No newline at end of file | |
app/models/cms/snippet.rb
+41
-0
| @@ | @@ -0,0 +1,41 @@ |
| + | class Cms::Snippet < ActiveRecord::Base |
| + | |
| + | set_table_name :cms_snippets |
| + | |
| + | # -- Relationships -------------------------------------------------------- |
| + | belongs_to :site |
| + | |
| + | # -- Callbacks ------------------------------------------------------------ |
| + | after_save :clear_cached_page_content |
| + | after_destroy :clear_cached_page_content |
| + | |
| + | # -- Validations ---------------------------------------------------------- |
| + | validates :site_id, |
| + | :presence => true |
| + | validates :label, |
| + | :presence => true |
| + | validates :slug, |
| + | :presence => true, |
| + | :uniqueness => { :scope => :site_id }, |
| + | :format => { :with => /^\w[a-z0-9_-]*$/i } |
| + | |
| + | # -- Class Methods -------------------------------------------------------- |
| + | def self.content_for(slug) |
| + | (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 |
| + | # gotta reload every single page. Kinda sucks, but might be ok unless there |
| + | # are hundreds of pages. |
| + | def clear_cached_page_content |
| + | site.pages.all.each{ |page| page.save! } |
| + | end |
| + | |
| + | end |
app/models/cms/upload.rb
+15
-0
| @@ | @@ -0,0 +1,15 @@ |
| + | class Cms::Upload < ActiveRecord::Base |
| + | |
| + | set_table_name :cms_uploads |
| + | |
| + | # -- AR Extensions -------------------------------------------------------- |
| + | has_attached_file :file, ComfortableMexicanSofa.config.upload_file_options |
| + | |
| + | # -- Relationships -------------------------------------------------------- |
| + | belongs_to :site |
| + | |
| + | # -- Validations ---------------------------------------------------------- |
| + | validates :site_id, :presence => true |
| + | validates_attachment_presence :file |
| + | |
| + | end |
app/models/cms_block.rb
+0
-28
| @@ | @@ -1,28 +0,0 @@ |
| - | class CmsBlock < ActiveRecord::Base |
| - | |
| - | # -- Relationships -------------------------------------------------------- |
| - | belongs_to :cms_page |
| - | |
| - | # -- Validations ---------------------------------------------------------- |
| - | validates :label, |
| - | :presence => true, |
| - | :uniqueness => { :scope => :cms_page_id } |
| - | |
| - | # -- Class Methods -------------------------------------------------------- |
| - | def self.initialize_or_find(cms_page, label) |
| - | if block = cms_page.cms_blocks.detect{ |b| b.label == label.to_s } |
| - | self.new( |
| - | :record_id => block.id, |
| - | :cms_page => cms_page, |
| - | :label => block.label, |
| - | :content => block.content |
| - | ) |
| - | else |
| - | self.new( |
| - | :label => label.to_s, |
| - | :cms_page => cms_page |
| - | ) |
| - | end |
| - | end |
| - | |
| - | end |
app/models/cms_layout.rb
+0
-88
| @@ | @@ -1,88 +0,0 @@ |
| - | class CmsLayout < ActiveRecord::Base |
| - | |
| - | acts_as_tree |
| - | |
| - | # -- Relationships -------------------------------------------------------- |
| - | belongs_to :cms_site |
| - | has_many :cms_pages, :dependent => :nullify |
| - | |
| - | # -- Callbacks ------------------------------------------------------------ |
| - | after_save :clear_cache, :clear_cached_page_content |
| - | after_destroy :clear_cache, :clear_cached_page_content |
| - | |
| - | # -- Validations ---------------------------------------------------------- |
| - | validates :cms_site_id, |
| - | :presence => true |
| - | validates :label, |
| - | :presence => true |
| - | validates :slug, |
| - | :presence => true, |
| - | :uniqueness => { :scope => :cms_site_id }, |
| - | :format => { :with => /^\w[a-z0-9_-]*$/i } |
| - | validates :content, |
| - | :presence => true |
| - | validate :check_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 = '. . ') |
| - | out = [] |
| - | [current_layout || cms_site.cms_layouts.roots].flatten.each do |layout| |
| - | next if cms_layout == layout |
| - | out << [ "#{spacer*depth}#{layout.label}", layout.id ] |
| - | layout.children.each do |child| |
| - | out += options_for_select(cms_site, cms_layout, child, depth + 1, spacer) |
| - | end |
| - | end |
| - | return out.compact |
| - | end |
| - | |
| - | # List of available application layouts |
| - | def self.app_layouts_for_select |
| - | 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.to_s[0...1] == '_' ? nil : app_layout |
| - | end.compact |
| - | end |
| - | |
| - | # -- Instance Methods ----------------------------------------------------- |
| - | # magical merging tag is {cms:page:content} If parent layout has this tag |
| - | # defined its content will be merged. If no such tag found, parent content |
| - | # is ignored. |
| - | def merged_content |
| - | if parent |
| - | regex = /\{\{\s*cms:page:content:?(?:(?::text)|(?::rich_text))?\s*\}\}/ |
| - | if parent.merged_content.match(regex) |
| - | parent.merged_content.gsub(regex, content) |
| - | else |
| - | content |
| - | end |
| - | else |
| - | content |
| - | end |
| - | end |
| - | |
| - | protected |
| - | |
| - | def check_content_tag_presence |
| - | CmsTag.process_content((test_page = cms_site.cms_pages.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 |
| - | |
| - | # After saving need to make sure that cached pages for css and js for this |
| - | # layout and its children are gone. Good enough to avoid using cache sweepers. |
| - | def clear_cache |
| - | FileUtils.rm File.expand_path("cms-css/#{self.slug}.css", Rails.public_path), :force => true |
| - | FileUtils.rm File.expand_path("cms-js/#{self.slug}.js", Rails.public_path), :force => true |
| - | end |
| - | |
| - | # Forcing page content reload |
| - | def clear_cached_page_content |
| - | self.cms_pages.each{ |page| page.save! } |
| - | self.children.each{ |child_layout| child_layout.save! } |
| - | end |
| - | |
| - | end |
app/models/cms_page.rb
+0
-131
| @@ | @@ -1,131 +0,0 @@ |
| - | class CmsPage < ActiveRecord::Base |
| - | |
| - | # -- AR Extensions -------------------------------------------------------- |
| - | acts_as_tree :counter_cache => :children_count |
| - | |
| - | attr_accessor :cms_tags |
| - | |
| - | # -- Relationships -------------------------------------------------------- |
| - | belongs_to :cms_site |
| - | belongs_to :cms_layout |
| - | belongs_to :target_page, |
| - | :class_name => 'CmsPage' |
| - | has_many :cms_blocks, |
| - | :dependent => :destroy |
| - | accepts_nested_attributes_for :cms_blocks |
| - | |
| - | # -- Callbacks ------------------------------------------------------------ |
| - | before_validation :assign_parent, |
| - | :assign_full_path |
| - | before_validation :assign_position, |
| - | :on => :create |
| - | before_save :set_cached_content |
| - | after_save :sync_child_pages |
| - | |
| - | # -- Validations ---------------------------------------------------------- |
| - | validates :cms_site_id, |
| - | :presence => true |
| - | validates :label, |
| - | :presence => true |
| - | validates :slug, |
| - | :presence => true, |
| - | :format => /^\w[a-z0-9_-]*$/i, |
| - | :unless => lambda{ |p| p == CmsPage.root || CmsPage.count == 0 } |
| - | validates :cms_layout, |
| - | :presence => true |
| - | validates :full_path, |
| - | :presence => true, |
| - | :uniqueness => { :scope => :cms_site_id } |
| - | validate :validate_target_page |
| - | |
| - | # -- Scopes --------------------------------------------------------------- |
| - | default_scope order(:position) |
| - | scope :published, where(:is_published => true) |
| - | |
| - | # -- Class Methods -------------------------------------------------------- |
| - | # 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 = [] |
| - | 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 |
| - | return out.compact |
| - | end |
| - | |
| - | # -- Instance Methods ----------------------------------------------------- |
| - | # For previewing purposes sometimes we need to have full_path set |
| - | def full_path |
| - | self.read_attribute(:full_path) || self.assign_full_path |
| - | end |
| - | |
| - | # Transforms existing cms_block information into a hash that can be used |
| - | # during form processing. That's the only way to modify cms_blocks. |
| - | def cms_blocks_attributes |
| - | self.cms_blocks.inject([]) do |arr, block| |
| - | block_attr = {} |
| - | block_attr[:label] = block.label |
| - | block_attr[:content] = block.content |
| - | block_attr[:id] = block.id |
| - | arr << block_attr |
| - | end |
| - | end |
| - | |
| - | # Processing content will return rendered content and will populate |
| - | # self.cms_tags with instances of CmsTag |
| - | def content(force_reload = false) |
| - | @content = read_attribute(:content) |
| - | @content = nil if force_reload |
| - | @content ||= begin |
| - | self.cms_tags = [] # resetting |
| - | cms_layout ? CmsTag.process_content(self, cms_layout.merged_content) : '' |
| - | end |
| - | end |
| - | |
| - | # Array of cms_tags for a page. Content generation is called if forced. |
| - | # These also include initialized cms_blocks if present |
| - | def cms_tags(force_reload = false) |
| - | self.content(true) if force_reload |
| - | @cms_tags ||= [] |
| - | end |
| - | |
| - | # Full url for a page |
| - | def url |
| - | "http://#{self.cms_site.hostname}#{self.full_path}" |
| - | end |
| - | |
| - | protected |
| - | |
| - | def assign_parent |
| - | self.parent ||= CmsPage.root unless self == CmsPage.root || CmsPage.count == 0 |
| - | end |
| - | |
| - | def assign_full_path |
| - | self.full_path = self.parent ? "#{self.parent.full_path}/#{self.slug}".squeeze('/') : '/' |
| - | end |
| - | |
| - | def assign_position |
| - | return unless self.parent |
| - | max = self.parent.children.maximum(:position) |
| - | self.position = max ? max + 1 : 0 |
| - | end |
| - | |
| - | def validate_target_page |
| - | return unless self.target_page |
| - | p = self |
| - | while p.target_page do |
| - | return self.errors.add(:target_page_id, 'Invalid Redirect') if (p = p.target_page) == self |
| - | end |
| - | end |
| - | |
| - | def set_cached_content |
| - | write_attribute(:content, self.content(true)) |
| - | end |
| - | |
| - | # Forcing re-saves for child pages so they can update full_paths |
| - | def sync_child_pages |
| - | children.each{ |p| p.save! } if full_path_changed? |
| - | end |
| - | |
| - | end |
app/models/cms_site.rb
+0
-23
| @@ | @@ -1,23 +0,0 @@ |
| - | class CmsSite < ActiveRecord::Base |
| - | |
| - | # -- Relationships -------------------------------------------------------- |
| - | has_many :cms_layouts, :dependent => :destroy |
| - | has_many :cms_pages, :dependent => :destroy |
| - | has_many :cms_snippets, :dependent => :destroy |
| - | has_many :cms_uploads, :dependent => :destroy |
| - | |
| - | # -- Validations ---------------------------------------------------------- |
| - | validates :label, |
| - | :presence => true, |
| - | :uniqueness => true |
| - | validates :hostname, |
| - | :presence => true, |
| - | :uniqueness => true, |
| - | :format => { :with => /^[\w\.\-]+$/ } |
| - | |
| - | # -- Class Methods -------------------------------------------------------- |
| - | def self.options_for_select |
| - | CmsSite.all.collect{|s| ["#{s.label} (#{s.hostname})", s.id]} |
| - | end |
| - | |
| - | end |
| \ No newline at end of file | |
app/models/cms_snippet.rb
+0
-39
| @@ | @@ -1,39 +0,0 @@ |
| - | class CmsSnippet < ActiveRecord::Base |
| - | |
| - | # -- Relationships -------------------------------------------------------- |
| - | belongs_to :cms_site |
| - | |
| - | # -- Callbacks ------------------------------------------------------------ |
| - | after_save :clear_cached_page_content |
| - | after_destroy :clear_cached_page_content |
| - | |
| - | # -- Validations ---------------------------------------------------------- |
| - | validates :cms_site_id, |
| - | :presence => true |
| - | validates :label, |
| - | :presence => true |
| - | validates :slug, |
| - | :presence => true, |
| - | :uniqueness => { :scope => :cms_site_id }, |
| - | :format => { :with => /^\w[a-z0-9_-]*$/i } |
| - | |
| - | # -- Class Methods -------------------------------------------------------- |
| - | def self.content_for(slug) |
| - | (s = find_by_slug(slug)) ? s.content : '' |
| - | end |
| - | |
| - | def self.initialize_or_find(cms_page, slug) |
| - | find_by_slug(slug, :conditions => {:cms_site_id => cms_page.cms_site.id}) || |
| - | new(:slug => slug, :cms_site => cms_page.cms_site) |
| - | end |
| - | |
| - | protected |
| - | |
| - | # Note: This might be slow. We have no idea where the snippet is used, so |
| - | # gotta reload every single page. Kinda sucks, but might be ok unless there |
| - | # are hundreds of pages. |
| - | def clear_cached_page_content |
| - | CmsPage.all.each{ |page| page.save! } |
| - | end |
| - | |
| - | end |
app/models/cms_upload.rb
+0
-13
| @@ | @@ -1,13 +0,0 @@ |
| - | class CmsUpload < ActiveRecord::Base |
| - | |
| - | # -- AR Extensions -------------------------------------------------------- |
| - | has_attached_file :file, ComfortableMexicanSofa.config.upload_file_options |
| - | |
| - | # -- Relationships -------------------------------------------------------- |
| - | belongs_to :cms_site |
| - | |
| - | # -- Validations ---------------------------------------------------------- |
| - | validates :cms_site_id, :presence => true |
| - | validates_attachment_presence :file |
| - | |
| - | end |
migrate/01_create_cms.rb b/db/migrate/01_create_cms.rb
+11
-11
| @@ | @@ -10,7 +10,7 @@ class CreateCms < ActiveRecord::Migration |
| # -- Layouts ------------------------------------------------------------ | |
| create_table :cms_layouts do |t| | |
| - | t.integer :cms_site_id |
| + | t.integer :site_id |
| t.integer :parent_id | |
| t.string :app_layout | |
| t.string :label | |
| @@ | @@ -22,12 +22,12 @@ class CreateCms < ActiveRecord::Migration |
| t.timestamps | |
| end | |
| add_index :cms_layouts, [:parent_id, :position] | |
| - | add_index :cms_layouts, [:cms_site_id, :slug], :unique => true |
| + | add_index :cms_layouts, [:site_id, :slug], :unique => true |
| # -- Pages -------------------------------------------------------------- | |
| create_table :cms_pages do |t| | |
| - | t.integer :cms_site_id |
| - | t.integer :cms_layout_id |
| + | t.integer :site_id |
| + | t.integer :layout_id |
| t.integer :parent_id | |
| t.integer :target_page_id | |
| t.string :label | |
| @@ | @@ -39,37 +39,37 @@ class CreateCms < ActiveRecord::Migration |
| t.boolean :is_published, :null => false, :default => true | |
| t.timestamps | |
| end | |
| - | add_index :cms_pages, [:cms_site_id, :full_path] |
| + | add_index :cms_pages, [:site_id, :full_path] |
| add_index :cms_pages, [:parent_id, :position] | |
| # -- Page Blocks -------------------------------------------------------- | |
| create_table :cms_blocks do |t| | |
| - | t.integer :cms_page_id |
| + | t.integer :page_id |
| t.string :label | |
| t.text :content | |
| t.timestamps | |
| end | |
| - | add_index :cms_blocks, [:cms_page_id, :label] |
| + | add_index :cms_blocks, [:page_id, :label] |
| # -- Snippets ----------------------------------------------------------- | |
| create_table :cms_snippets do |t| | |
| - | t.integer :cms_site_id |
| + | t.integer :site_id |
| t.string :label | |
| t.string :slug | |
| t.text :content | |
| t.timestamps | |
| end | |
| - | add_index :cms_snippets, [:cms_site_id, :slug], :unique => true |
| + | add_index :cms_snippets, [:site_id, :slug], :unique => true |
| # -- Assets ------------------------------------------------------------- | |
| create_table :cms_uploads do |t| | |
| - | t.integer :cms_site_id |
| + | t.integer :site_id |
| t.string :file_file_name | |
| t.string :file_content_type | |
| t.integer :file_file_size | |
| t.timestamps | |
| end | |
| - | add_index :cms_uploads, [:cms_site_id, :file_file_name] |
| + | add_index :cms_uploads, [:site_id, :file_file_name] |
| end | |
| def self.down | |
migrate/02_upgrade_to_1_1_0.rb b/db/migrate/02_upgrade_to_1_1_0.rb
+19
-0
| @@ | @@ -0,0 +1,19 @@ |
| + | class UpgradeTo110 < ActiveRecord::Migration |
| + | def self.up |
| + | rename_column :cms_layouts, :cms_site_id, :site_id |
| + | rename_column :cms_pages, :cms_site_id, :site_id |
| + | rename_column :cms_pages, :cms_layout_id, :layout_id |
| + | rename_column :cms_blocks, :cms_page_id, :page_id |
| + | rename_column :cms_snippets, :cms_site_id, :site_id |
| + | rename_column :cms_uploads, :cms_site_id, :site_id |
| + | end |
| + | |
| + | def self.down |
| + | rename_column :cms_uploads, :site_id, :cms_site_id |
| + | rename_column :cms_snippets, :site_id, :cms_site_id |
| + | rename_column :cms_blocks, :page_id, :cms_page_id |
| + | rename_column :cms_layouts, :site_id, :cms_site_id |
| + | rename_column :cms_pages, :layout_id, :cms_layout_id |
| + | rename_column :cms_pages, :site_id, :cms_site_id |
| + | end |
| + | end |
| \ No newline at end of file | |
comfortable_mexican_sofa.rb b/lib/comfortable_mexican_sofa.rb
+2
-12
| @@ | @@ -1,9 +1,5 @@ |
| - | # Loading engine only if this is not a standalone installation |
| - | unless defined? ComfortableMexicanSofa::Application |
| - | require File.expand_path('comfortable_mexican_sofa/engine', File.dirname(__FILE__)) |
| - | end |
| - | |
| - | [ 'comfortable_mexican_sofa/configuration', |
| + | [ 'comfortable_mexican_sofa/engine', |
| + | 'comfortable_mexican_sofa/configuration', |
| 'comfortable_mexican_sofa/http_auth', | |
| 'comfortable_mexican_sofa/rails_extensions', | |
| 'comfortable_mexican_sofa/controller_methods', | |
| @@ | @@ -11,17 +7,11 @@ end |
| 'comfortable_mexican_sofa/view_methods', | |
| 'comfortable_mexican_sofa/form_builder', | |
| 'comfortable_mexican_sofa/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__)) | |
| end | |
| - | Dir.glob(File.expand_path('comfortable_mexican_sofa/cms_tag/*.rb', File.dirname(__FILE__))).each do |tag_path| |
| - | require tag_path |
| - | end |
| - | |
| module ComfortableMexicanSofa | |
| class << self | |
comfortable_mexican_sofa/cms_tag.rb b/lib/comfortable_mexican_sofa/cms_tag.rb
+6
-6
| @@ | @@ -79,24 +79,24 @@ module CmsTag |
| private | |
| # Initializes a tag. It's handled by one of the tag classes | |
| - | def self.initialize_tag(cms_page, tag_signature) |
| + | def self.initialize_tag(page, tag_signature) |
| tag_instance = nil | |
| - | tag_classes.find{ |c| tag_instance = c.initialize_tag(cms_page, tag_signature) } |
| + | tag_classes.find{ |c| tag_instance = c.initialize_tag(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. | |
| # Tags are defined in the parent tags are ignored and not rendered. | |
| - | def self.process_content(cms_page, content = '', parent_tag = nil) |
| + | def self.process_content(page, content = '', parent_tag = nil) |
| tokens = content.to_s.scan(TOKENIZER_REGEX) | |
| tokens.collect do |tag_signature, text| | |
| if tag_signature | |
| - | if tag = self.initialize_tag(cms_page, tag_signature) |
| + | if tag = self.initialize_tag(page, tag_signature) |
| tag.parent = parent_tag if parent_tag | |
| if tag.ancestors.select{|a| a.identifier == tag.identifier}.blank? | |
| - | cms_page.cms_tags << tag |
| - | self.process_content(cms_page, tag.render, tag) |
| + | page.tags << tag |
| + | self.process_content(page, tag.render, tag) |
| end | |
| end | |
| else | |
comfortable_mexican_sofa/cms_tag/field_datetime.rb b/lib/comfortable_mexican_sofa/cms_tag/field_datetime.rb
+1
-1
| @@ | @@ -1,4 +1,4 @@ |
| - | class CmsTag::FieldDateTime < CmsBlock |
| + | class CmsTag::FieldDateTime < Cms::Block |
| include CmsTag | |
comfortable_mexican_sofa/cms_tag/field_integer.rb b/lib/comfortable_mexican_sofa/cms_tag/field_integer.rb
+1
-1
| @@ | @@ -1,4 +1,4 @@ |
| - | class CmsTag::FieldInteger < CmsBlock |
| + | class CmsTag::FieldInteger < Cms::Block |
| include CmsTag | |
comfortable_mexican_sofa/cms_tag/field_string.rb b/lib/comfortable_mexican_sofa/cms_tag/field_string.rb
+1
-1
| @@ | @@ -1,4 +1,4 @@ |
| - | class CmsTag::FieldString < CmsBlock |
| + | class CmsTag::FieldString < Cms::Block |
| include CmsTag | |
comfortable_mexican_sofa/cms_tag/field_text.rb b/lib/comfortable_mexican_sofa/cms_tag/field_text.rb
+1
-1
| @@ | @@ -1,4 +1,4 @@ |
| - | class CmsTag::FieldText < CmsBlock |
| + | class CmsTag::FieldText < Cms::Block |
| include CmsTag | |
comfortable_mexican_sofa/cms_tag/page_datetime.rb b/lib/comfortable_mexican_sofa/cms_tag/page_datetime.rb
+1
-1
| @@ | @@ -1,4 +1,4 @@ |
| - | class CmsTag::PageDateTime < CmsBlock |
| + | class CmsTag::PageDateTime < Cms::Block |
| include CmsTag | |
comfortable_mexican_sofa/cms_tag/page_integer.rb b/lib/comfortable_mexican_sofa/cms_tag/page_integer.rb
+1
-1
| @@ | @@ -1,4 +1,4 @@ |
| - | class CmsTag::PageInteger < CmsBlock |
| + | class CmsTag::PageInteger < Cms::Block |
| include CmsTag | |
comfortable_mexican_sofa/cms_tag/page_rich_text.rb b/lib/comfortable_mexican_sofa/cms_tag/page_rich_text.rb
+1
-1
| @@ | @@ -1,4 +1,4 @@ |
| - | class CmsTag::PageRichText < CmsBlock |
| + | class CmsTag::PageRichText < Cms::Block |
| include CmsTag | |
comfortable_mexican_sofa/cms_tag/page_string.rb b/lib/comfortable_mexican_sofa/cms_tag/page_string.rb
+1
-1
| @@ | @@ -1,4 +1,4 @@ |
| - | class CmsTag::PageString < CmsBlock |
| + | class CmsTag::PageString < Cms::Block |
| include CmsTag | |
comfortable_mexican_sofa/cms_tag/page_text.rb b/lib/comfortable_mexican_sofa/cms_tag/page_text.rb
+1
-1
| @@ | @@ -1,4 +1,4 @@ |
| - | class CmsTag::PageText < CmsBlock |
| + | class CmsTag::PageText < Cms::Block |
| include CmsTag | |
comfortable_mexican_sofa/cms_tag/snippet.rb b/lib/comfortable_mexican_sofa/cms_tag/snippet.rb
+1
-1
| @@ | @@ -1,4 +1,4 @@ |
| - | class CmsTag::Snippet < CmsSnippet |
| + | class CmsTag::Snippet < Cms::Snippet |
| include CmsTag | |
comfortable_mexican_sofa/engine.rb b/lib/comfortable_mexican_sofa/engine.rb
+6
-0
| @@ | @@ -7,6 +7,12 @@ require 'mime/types' |
| module ComfortableMexicanSofa | |
| class Engine < ::Rails::Engine | |
| + | config.after_initialize do |
| + | Dir.glob(File.expand_path('cms_tag/*.rb', File.dirname(__FILE__))).each do |tag_path| |
| + | require tag_path |
| + | end |
| + | end |
| + | |
| end | |
| end | |
comfortable_mexican_sofa/view_methods.rb b/lib/comfortable_mexican_sofa/view_methods.rb
+3
-3
| @@ | @@ -23,8 +23,8 @@ module ComfortableMexicanSofa::ViewMethods |
| # Content of a snippet. Example: | |
| # cms_snippet_content(:my_snippet) | |
| def cms_snippet_content(snippet_slug, cms_site = nil) | |
| - | return '' unless cms_site ||= (@cms_site || CmsSite.find_by_hostname!(request.host.downcase)) |
| - | return '' unless snippet = cms_site.cms_snippets.find_by_slug(snippet_slug) |
| + | return '' unless cms_site ||= (@cms_site || Cms::Site.find_by_hostname!(request.host.downcase)) |
| + | return '' unless snippet = cms_site.snippets.find_by_slug(snippet_slug) |
| snippet.content.to_s.html_safe | |
| end | |
| @@ | @@ -34,7 +34,7 @@ module ComfortableMexicanSofa::ViewMethods |
| # cms_page_content(:left_column) # if @cms_page is present | |
| def cms_page_content(block_label, page = nil) | |
| return '' unless page ||= @cms_page | |
| - | return '' unless block = page.cms_blocks.find_by_label(block_label) |
| + | return '' unless block = page.blocks.find_by_label(block_label) |
| block.content.to_s.html_safe | |
| end | |
| end | |
test/fixtures/cms/blocks.yml
+12
-0
| @@ | @@ -0,0 +1,12 @@ |
| + | default_field_text: |
| + | page: default |
| + | label: default_field_text |
| + | content: default_field_text_content |
| + | |
| + | default_page_text: |
| + | page: default |
| + | label: default_page_text |
| + | content: |- |
| + | default_page_text_content_a |
| + | {{cms:snippet:default}} |
| + | default_page_text_content_b |
test/fixtures/cms/layouts.yml
+36
-0
| @@ | @@ -0,0 +1,36 @@ |
| + | default: |
| + | site: default |
| + | label: Default Layout |
| + | slug: default |
| + | parent: |
| + | content: |- |
| + | {{cms:field:default_field_text:text}} |
| + | layout_content_a |
| + | {{cms:page:default_page_text:text}} |
| + | layout_content_b |
| + | {{cms:snippet:default}} |
| + | layout_content_c |
| + | css: default_css |
| + | js: default_js |
| + | |
| + | nested: |
| + | site: default |
| + | label: Nested Layout |
| + | slug: nested |
| + | parent: |
| + | content: |- |
| + | {{cms:page:header}} |
| + | {{cms:page:content}} |
| + | css: nested_css |
| + | js: nested_js |
| + | |
| + | child: |
| + | site: default |
| + | label: Child Layout |
| + | slug: child |
| + | parent: nested |
| + | content: |- |
| + | {{cms:page:left_column}} |
| + | {{cms:page:right_column}} |
| + | css: child_css |
| + | js: child_js |
| \ No newline at end of file | |
test/fixtures/cms/pages.yml
+39
-0
| @@ | @@ -0,0 +1,39 @@ |
| + | default: |
| + | site: default |
| + | parent: |
| + | target_page: |
| + | layout: default |
| + | label: Default Page |
| + | slug: |
| + | full_path: '/' |
| + | children_count: 1 |
| + | position: 0 |
| + | is_published: true |
| + | content: |- |
| + | |
| + | layout_content_a |
| + | default_page_text_content_a |
| + | default_snippet_content |
| + | default_page_text_content_b |
| + | layout_content_b |
| + | default_snippet_content |
| + | layout_content_c |
| + | |
| + | child: |
| + | site: default |
| + | parent: default |
| + | target_page: |
| + | layout: default |
| + | label: Child Page |
| + | slug: 'child-page' |
| + | full_path: '/child-page' |
| + | children_count: 0 |
| + | position: 0 |
| + | is_published: true |
| + | content: |- |
| + | |
| + | layout_content_a |
| + | |
| + | layout_content_b |
| + | default_snippet_content |
| + | layout_content_c |
test/fixtures/cms/sites.yml
+3
-0
| @@ | @@ -0,0 +1,3 @@ |
| + | default: |
| + | label: Default Site |
| + | hostname: test.host |
| \ No newline at end of file | |
test/fixtures/cms/snippets.yml
+5
-0
| @@ | @@ -0,0 +1,5 @@ |
| + | default: |
| + | site: default |
| + | label: Default Snippet |
| + | slug: default |
| + | content: default_snippet_content |
test/fixtures/cms/uploads.yml
+5
-0
| @@ | @@ -0,0 +1,5 @@ |
| + | default: |
| + | site: default |
| + | file_file_name: sample.jpg |
| + | file_content_type: image/jpeg |
| + | file_file_size: 20099 |
| \ No newline at end of file | |
test/fixtures/cms_blocks.yml
+0
-12
| @@ | @@ -1,12 +0,0 @@ |
| - | default_field_text: |
| - | cms_page: default |
| - | label: default_field_text |
| - | content: default_field_text_content |
| - | |
| - | default_page_text: |
| - | cms_page: default |
| - | label: default_page_text |
| - | content: |- |
| - | default_page_text_content_a |
| - | {{cms:snippet:default}} |
| - | default_page_text_content_b |
test/fixtures/cms_layouts.yml
+0
-36
| @@ | @@ -1,36 +0,0 @@ |
| - | default: |
| - | cms_site: default |
| - | label: Default Layout |
| - | slug: default |
| - | parent: |
| - | content: |- |
| - | {{cms:field:default_field_text:text}} |
| - | layout_content_a |
| - | {{cms:page:default_page_text:text}} |
| - | layout_content_b |
| - | {{cms:snippet:default}} |
| - | layout_content_c |
| - | css: default_css |
| - | js: default_js |
| - | |
| - | nested: |
| - | cms_site: default |
| - | label: Nested Layout |
| - | slug: nested |
| - | parent: |
| - | content: |- |
| - | {{cms:page:header}} |
| - | {{cms:page:content}} |
| - | css: nested_css |
| - | js: nested_js |
| - | |
| - | child: |
| - | cms_site: default |
| - | label: Child Layout |
| - | slug: child |
| - | parent: nested |
| - | content: |- |
| - | {{cms:page:left_column}} |
| - | {{cms:page:right_column}} |
| - | css: child_css |
| - | js: child_js |
| \ No newline at end of file | |
test/fixtures/cms_pages.yml
+0
-39
| @@ | @@ -1,39 +0,0 @@ |
| - | default: |
| - | cms_site: default |
| - | parent: |
| - | target_page: |
| - | cms_layout: default |
| - | label: Default Page |
| - | slug: |
| - | full_path: '/' |
| - | children_count: 1 |
| - | position: 0 |
| - | is_published: true |
| - | content: |- |
| - | |
| - | layout_content_a |
| - | default_page_text_content_a |
| - | default_snippet_content |
| - | default_page_text_content_b |
| - | layout_content_b |
| - | default_snippet_content |
| - | layout_content_c |
| - | |
| - | child: |
| - | cms_site: default |
| - | parent: default |
| - | target_page: |
| - | cms_layout: default |
| - | label: Child Page |
| - | slug: 'child-page' |
| - | full_path: '/child-page' |
| - | children_count: 0 |
| - | position: 0 |
| - | is_published: true |
| - | content: |- |
| - | |
| - | layout_content_a |
| - | |
| - | layout_content_b |
| - | default_snippet_content |
| - | layout_content_c |
test/fixtures/cms_sites.yml
+0
-3
| @@ | @@ -1,3 +0,0 @@ |
| - | default: |
| - | label: Default Site |
| - | hostname: test.host |
| \ No newline at end of file | |
test/fixtures/cms_snippets.yml
+0
-5
| @@ | @@ -1,5 +0,0 @@ |
| - | default: |
| - | cms_site: default |
| - | label: Default Snippet |
| - | slug: default |
| - | content: default_snippet_content |
test/fixtures/cms_uploads.yml
+0
-5
| @@ | @@ -1,5 +0,0 @@ |
| - | default: |
| - | cms_site: default |
| - | file_file_name: sample.jpg |
| - | file_content_type: image/jpeg |
| - | file_file_size: 20099 |
| \ No newline at end of file | |
test/unit/cms_block_test.rb
+0
-43
| @@ | @@ -1,43 +0,0 @@ |
| - | require File.expand_path('../test_helper', File.dirname(__FILE__)) |
| - | |
| - | class CmsBlockTest < ActiveSupport::TestCase |
| - | |
| - | def test_fixtures_validity |
| - | CmsBlock.all.each do |block| |
| - | assert block.valid?, block.errors.full_messages.to_s |
| - | end |
| - | end |
| - | |
| - | def test_new_via_page_nested_attributes |
| - | assert_difference ['CmsPage.count', 'CmsBlock.count'] do |
| - | page = CmsPage.create!( |
| - | :cms_site => cms_sites(:default), |
| - | :cms_layout => cms_layouts(:default), |
| - | :label => 'test page', |
| - | :slug => 'test_page', |
| - | :parent_id => cms_pages(:default).id, |
| - | :cms_blocks_attributes => [ |
| - | { |
| - | :label => 'test_block', |
| - | :content => 'test_content' |
| - | } |
| - | ] |
| - | ) |
| - | assert_equal 1, page.cms_blocks.count |
| - | block = page.cms_blocks.first |
| - | assert_equal 'test_block', block.label |
| - | assert_equal 'test_content', block.content |
| - | end |
| - | end |
| - | |
| - | def test_initialize_or_find |
| - | tag = CmsTag::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 = CmsTag::PageText.initialize_or_find(cms_pages(:default), :new_block) |
| - | assert_equal 'new_block', tag.label |
| - | assert tag.content.blank? |
| - | end |
| - | |
| - | end |
test/unit/cms_layout_test.rb
+0
-129
| @@ | @@ -1,129 +0,0 @@ |
| - | require File.expand_path('../test_helper', File.dirname(__FILE__)) |
| - | |
| - | class CmsLayoutTest < ActiveSupport::TestCase |
| - | |
| - | def test_fixtures_validity |
| - | CmsLayout.all.each do |layout| |
| - | assert layout.valid?, layout.errors.full_messages.to_s |
| - | end |
| - | end |
| - | |
| - | def test_validations |
| - | layout = cms_sites(:default).cms_layouts.create |
| - | assert layout.errors.present? |
| - | assert_has_errors_on layout, [:label, :slug, :content] |
| - | end |
| - | |
| - | def test_validation_of_tag_presence |
| - | layout = cms_sites(:default).cms_layouts.create(:content => 'some text') |
| - | assert_has_errors_on layout, :content |
| - | |
| - | layout = cms_sites(:default).cms_layouts.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_creation |
| - | assert_difference 'CmsLayout.count' do |
| - | layout = cms_sites(:default).cms_layouts.create( |
| - | :label => 'New Layout', |
| - | :slug => 'new-layout', |
| - | :content => '{{cms:page:content}}', |
| - | :css => 'css', |
| - | :js => 'js' |
| - | ) |
| - | assert_equal 'New Layout', layout.label |
| - | assert_equal 'new-layout', layout.slug |
| - | assert_equal '{{cms:page:content}}', layout.content |
| - | assert_equal 'css', layout.css |
| - | assert_equal 'js', layout.js |
| - | end |
| - | 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} |
| - | assert_equal ['Default Layout', 'Nested Layout'], |
| - | CmsLayout.options_for_select(cms_sites(:default), cms_layouts(:child)).collect{|t| t.first} |
| - | assert_equal ['Default Layout'], |
| - | CmsLayout.options_for_select(cms_sites(:default), cms_layouts(:nested)).collect{|t| t.first} |
| - | end |
| - | |
| - | def test_app_layouts_for_select |
| - | assert_equal ['cms_admin.html.erb'], CmsLayout.app_layouts_for_select |
| - | end |
| - | |
| - | def test_merged_content_with_same_child_content |
| - | parent_layout = cms_layouts(:nested) |
| - | assert_equal "{{cms:page:header}}\n{{cms:page:content}}", parent_layout.content |
| - | assert_equal "{{cms:page:header}}\n{{cms:page:content}}", parent_layout.merged_content |
| - | |
| - | child_layout = cms_layouts(:child) |
| - | assert_equal parent_layout, child_layout.parent |
| - | assert_equal "{{cms:page:left_column}}\n{{cms:page:right_column}}", child_layout.content |
| - | assert_equal "{{cms:page:header}}\n{{cms:page:left_column}}\n{{cms:page:right_column}}", child_layout.merged_content |
| - | |
| - | child_layout.update_attribute(:content, '{{cms:page:content}}') |
| - | assert_equal "{{cms:page:header}}\n{{cms:page:content}}", child_layout.merged_content |
| - | |
| - | parent_layout.update_attribute(:content, '{{cms:page:whatever}}') |
| - | child_layout.reload |
| - | assert_equal '{{cms:page:content}}', child_layout.merged_content |
| - | end |
| - | |
| - | def test_update_forces_page_content_reload |
| - | layout_1 = cms_layouts(:nested) |
| - | layout_2 = cms_layouts(:child) |
| - | page_1 = cms_sites(:default).cms_pages.create!( |
| - | :label => 'page_1', |
| - | :slug => 'page-1', |
| - | :parent_id => cms_pages(:default).id, |
| - | :cms_layout_id => layout_1.id, |
| - | :is_published => '1', |
| - | :cms_blocks_attributes => [ |
| - | { :label => 'header', |
| - | :content => 'header_content' }, |
| - | { :label => 'content', |
| - | :content => 'content_content' } |
| - | ] |
| - | ) |
| - | page_2 = cms_sites(:default).cms_pages.create!( |
| - | :label => 'page_2', |
| - | :slug => 'page-2', |
| - | :parent_id => cms_pages(:default).id, |
| - | :cms_layout_id => layout_2.id, |
| - | :is_published => '1', |
| - | :cms_blocks_attributes => [ |
| - | { :label => 'header', |
| - | :content => 'header_content' }, |
| - | { :label => 'left_column', |
| - | :content => 'left_column_content' }, |
| - | { :label => 'right_column', |
| - | :content => 'left_column_content' } |
| - | ] |
| - | ) |
| - | assert_equal "header_content\ncontent_content", page_1.content |
| - | assert_equal "header_content\nleft_column_content\nleft_column_content", page_2.content |
| - | |
| - | layout_1.update_attribute(:content, "Updated {{cms:page:content}}") |
| - | page_1.reload |
| - | page_2.reload |
| - | |
| - | assert_equal "Updated content_content", page_1.content |
| - | assert_equal "Updated left_column_content\nleft_column_content", page_2.content |
| - | end |
| - | |
| - | end |
test/unit/cms_page_test.rb
+0
-199
| @@ | @@ -1,199 +0,0 @@ |
| - | require File.expand_path('../test_helper', File.dirname(__FILE__)) |
| - | |
| - | class CmsPageTest < ActiveSupport::TestCase |
| - | |
| - | def test_fixtures_validity |
| - | CmsPage.all.each do |page| |
| - | assert page.valid?, page.errors.full_messages.to_s |
| - | assert_equal page.read_attribute(:content), page.content(true) |
| - | end |
| - | end |
| - | |
| - | def test_validations |
| - | page = CmsPage.new |
| - | page.save |
| - | assert page.invalid? |
| - | assert_has_errors_on page, [:cms_layout, :slug, :label] |
| - | end |
| - | |
| - | def test_validation_of_parent_presence |
| - | page = cms_sites(:default).cms_pages.new(new_params) |
| - | assert !page.parent |
| - | assert page.valid?, page.errors.full_messages.to_s |
| - | assert_equal cms_pages(:default), page.parent |
| - | end |
| - | |
| - | def test_validation_of_parent_relationship |
| - | page = cms_pages(:default) |
| - | assert !page.parent |
| - | page.parent = page |
| - | assert page.invalid? |
| - | assert_has_errors_on page, :parent_id |
| - | page.parent = cms_pages(:child) |
| - | assert page.invalid? |
| - | assert_has_errors_on page, :parent_id |
| - | end |
| - | |
| - | def test_validation_of_target_page |
| - | page = cms_pages(:child) |
| - | page.target_page = cms_pages(:default) |
| - | page.save! |
| - | assert_equal cms_pages(:default), page.target_page |
| - | page.target_page = page |
| - | assert page.invalid? |
| - | assert_has_errors_on page, :target_page_id |
| - | end |
| - | |
| - | def test_creation |
| - | assert_difference ['CmsPage.count', 'CmsBlock.count'] do |
| - | page = cms_sites(:default).cms_pages.create!( |
| - | :label => 'test', |
| - | :slug => 'test', |
| - | :parent_id => cms_pages(:default).id, |
| - | :cms_layout_id => cms_layouts(:default).id, |
| - | :cms_blocks_attributes => [ |
| - | { :label => 'test', |
| - | :content => 'test' } |
| - | ] |
| - | ) |
| - | assert page.is_published? |
| - | assert_equal 1, page.position |
| - | end |
| - | end |
| - | |
| - | def test_initialization_of_full_path |
| - | page = CmsPage.new |
| - | assert_equal '/', page.full_path |
| - | |
| - | page = CmsPage.new(new_params) |
| - | assert page.invalid? |
| - | assert_has_errors_on page, :cms_site_id |
| - | |
| - | page = cms_sites(:default).cms_pages.new(new_params(:parent => cms_pages(:default))) |
| - | assert page.valid? |
| - | assert_equal '/test-page', page.full_path |
| - | |
| - | page = cms_sites(:default).cms_pages.new(new_params(:parent => cms_pages(:child))) |
| - | assert page.valid? |
| - | assert_equal '/child-page/test-page', page.full_path |
| - | |
| - | CmsPage.destroy_all |
| - | page = cms_sites(:default).cms_pages.new(new_params) |
| - | assert page.valid? |
| - | assert_equal '/', page.full_path |
| - | end |
| - | |
| - | def test_sync_child_pages |
| - | page = cms_pages(:child) |
| - | page_1 = cms_sites(:default).cms_pages.create!(new_params(:parent => page, :slug => 'test-page-1')) |
| - | page_2 = cms_sites(:default).cms_pages.create!(new_params(:parent => page, :slug => 'test-page-2')) |
| - | page_3 = cms_sites(:default).cms_pages.create!(new_params(:parent => page_2, :slug => 'test-page-3')) |
| - | page_4 = cms_sites(:default).cms_pages.create!(new_params(:parent => page_1, :slug => 'test-page-4')) |
| - | assert_equal '/child-page/test-page-1', page_1.full_path |
| - | assert_equal '/child-page/test-page-2', page_2.full_path |
| - | assert_equal '/child-page/test-page-2/test-page-3', page_3.full_path |
| - | assert_equal '/child-page/test-page-1/test-page-4', page_4.full_path |
| - | |
| - | page.update_attributes!(:slug => 'updated-page') |
| - | assert_equal '/updated-page', page.full_path |
| - | page_1.reload; page_2.reload; page_3.reload; page_4.reload |
| - | assert_equal '/updated-page/test-page-1', page_1.full_path |
| - | assert_equal '/updated-page/test-page-2', page_2.full_path |
| - | assert_equal '/updated-page/test-page-2/test-page-3', page_3.full_path |
| - | assert_equal '/updated-page/test-page-1/test-page-4', page_4.full_path |
| - | |
| - | page_2.update_attributes!(:parent => page_1) |
| - | page_1.reload; page_2.reload; page_3.reload; page_4.reload |
| - | assert_equal '/updated-page/test-page-1', page_1.full_path |
| - | assert_equal '/updated-page/test-page-1/test-page-2', page_2.full_path |
| - | assert_equal '/updated-page/test-page-1/test-page-2/test-page-3', page_3.full_path |
| - | assert_equal '/updated-page/test-page-1/test-page-4', page_4.full_path |
| - | end |
| - | |
| - | def test_children_count_updating |
| - | page_1 = cms_pages(:default) |
| - | page_2 = cms_pages(:child) |
| - | assert_equal 1, page_1.children_count |
| - | assert_equal 0, page_2.children_count |
| - | |
| - | page_3 = cms_sites(:default).cms_pages.create!(new_params(:parent => page_2)) |
| - | page_1.reload; page_2.reload |
| - | assert_equal 1, page_1.children_count |
| - | assert_equal 1, page_2.children_count |
| - | assert_equal 0, page_3.children_count |
| - | |
| - | page_3.update_attributes!(:parent => page_1) |
| - | page_1.reload; page_2.reload |
| - | assert_equal 2, page_1.children_count |
| - | assert_equal 0, page_2.children_count |
| - | |
| - | page_3.destroy |
| - | page_1.reload; page_2.reload |
| - | assert_equal 1, page_1.children_count |
| - | assert_equal 0, page_2.children_count |
| - | end |
| - | |
| - | def test_cascading_destroy |
| - | assert_difference 'CmsPage.count', -2 do |
| - | cms_pages(:default).destroy |
| - | end |
| - | end |
| - | |
| - | def test_options_for_select |
| - | assert_equal ['Default Page', '. . Child Page'], |
| - | CmsPage.options_for_select(cms_sites(:default)).collect{|t| t.first } |
| - | assert_equal ['Default Page'], |
| - | CmsPage.options_for_select(cms_sites(:default), cms_pages(:child)).collect{|t| t.first } |
| - | assert_equal [], |
| - | CmsPage.options_for_select(cms_sites(:default), cms_pages(:default)) |
| - | |
| - | page = CmsPage.new(new_params(:parent => cms_pages(:default))) |
| - | assert_equal ['Default Page', '. . Child Page'], |
| - | CmsPage.options_for_select(cms_sites(:default), page).collect{|t| t.first } |
| - | end |
| - | |
| - | def test_cms_blocks_attributes_accessor |
| - | page = cms_pages(:default) |
| - | assert_equal page.cms_blocks.count, page.cms_blocks_attributes.size |
| - | assert_equal 'default_field_text', page.cms_blocks_attributes.first[:label] |
| - | assert_equal 'default_field_text_content', page.cms_blocks_attributes.first[:content] |
| - | assert page.cms_blocks_attributes.first[:id] |
| - | end |
| - | |
| - | def test_content_caching |
| - | page = cms_pages(:default) |
| - | assert_equal page.read_attribute(:content), page.content |
| - | assert_equal page.read_attribute(:content), page.content(true) |
| - | |
| - | page.update_attribute(:content, 'changed') |
| - | assert_equal page.read_attribute(:content), page.content |
| - | assert_equal page.read_attribute(:content), page.content(true) |
| - | assert_not_equal 'changed', page.read_attribute(:content) |
| - | end |
| - | |
| - | def test_scope_published |
| - | assert_equal 2, CmsPage.published.count |
| - | cms_pages(:child).update_attribute(:is_published, false) |
| - | assert_equal 1, CmsPage.published.count |
| - | end |
| - | |
| - | def test_root? |
| - | assert cms_pages(:default).root? |
| - | assert !cms_pages(:child).root? |
| - | end |
| - | |
| - | def test_url |
| - | assert_equal 'http://test.host/', cms_pages(:default).url |
| - | assert_equal 'http://test.host/child-page', cms_pages(:child).url |
| - | end |
| - | |
| - | protected |
| - | |
| - | def new_params(options = {}) |
| - | { |
| - | :label => 'Test Page', |
| - | :slug => 'test-page', |
| - | :cms_layout => cms_layouts(:default) |
| - | }.merge(options) |
| - | end |
| - | end |
test/unit/cms_site_test.rb
+0
-41
| @@ | @@ -1,41 +0,0 @@ |
| - | require File.expand_path('../test_helper', File.dirname(__FILE__)) |
| - | |
| - | class CmsSiteTest < ActiveSupport::TestCase |
| - | |
| - | def test_fixtures_validity |
| - | CmsSite.all.each do |site| |
| - | assert site.valid?, site.errors.full_messages.to_s |
| - | end |
| - | end |
| - | |
| - | def test_validation |
| - | site = CmsSite.new |
| - | assert site.invalid? |
| - | assert_has_errors_on site, [:label, :hostname] |
| - | |
| - | site = CmsSite.new(:label => 'My Site', :hostname => 'http://mysite.com') |
| - | assert site.invalid? |
| - | assert_has_errors_on site, :hostname |
| - | |
| - | site = CmsSite.new(:label => 'My Site', :hostname => 'mysite.com') |
| - | assert site.valid? |
| - | end |
| - | |
| - | def test_cascading_destroy |
| - | assert_difference 'CmsSite.count', -1 do |
| - | assert_difference 'CmsLayout.count', -3 do |
| - | assert_difference 'CmsPage.count', -2 do |
| - | assert_difference 'CmsSnippet.count', -1 do |
| - | cms_sites(:default).destroy |
| - | end |
| - | end |
| - | end |
| - | end |
| - | end |
| - | |
| - | def test_options_for_select |
| - | assert_equal 1, CmsSite.options_for_select.size |
| - | assert_equal 'Default Site (test.host)', CmsSite.options_for_select[0][0] |
| - | end |
| - | |
| - | end |
| \ No newline at end of file | |
test/unit/cms_snippet_test.rb
+0
-32
| @@ | @@ -1,32 +0,0 @@ |
| - | require File.expand_path('../test_helper', File.dirname(__FILE__)) |
| - | |
| - | class CmsSnippetTest < ActiveSupport::TestCase |
| - | |
| - | def test_fixtures_validity |
| - | CmsSnippet.all.each do |snippet| |
| - | assert snippet.valid?, snippet.errors.full_messages.to_s |
| - | end |
| - | end |
| - | |
| - | def test_validations |
| - | snippet = CmsSnippet.new |
| - | snippet.save |
| - | assert snippet.invalid? |
| - | assert_has_errors_on snippet, [:label, :slug] |
| - | end |
| - | |
| - | def test_method_content |
| - | assert_equal cms_snippets(:default).content, CmsSnippet.content_for('default') |
| - | assert_equal '', CmsSnippet.content_for('nonexistent_snippet') |
| - | end |
| - | |
| - | def test_update_forces_page_content_reload |
| - | snippet = cms_snippets(:default) |
| - | page = cms_pages(:default) |
| - | assert_match snippet.content, page.content |
| - | snippet.update_attribute(:content, 'new_snippet_content') |
| - | page.reload |
| - | assert_match /new_snippet_content/, page.content |
| - | end |
| - | |
| - | end |
test/unit/cms_tag_test.rb
+41
-41
| @@ | @@ -68,7 +68,7 @@ class CmsTagTest < ActiveSupport::TestCase |
| def test_content_for_existing_page | |
| page = cms_pages(:default) | |
| - | assert page.cms_tags.blank? |
| + | assert page.tags.blank? |
| assert_equal rendered_content_formatter( | |
| ' | |
| layout_content_a | |
| @@ | @@ -80,26 +80,26 @@ class CmsTagTest < ActiveSupport::TestCase |
| layout_content_c' | |
| ), page.content(true) | |
| - | assert_equal 4, page.cms_tags.size |
| - | assert_equal 'cms_tag/field_text_default_field_text', page.cms_tags[0].identifier |
| - | assert_equal 'cms_tag/page_text_default_page_text', page.cms_tags[1].identifier |
| - | assert_equal 'cms_tag/snippet_default', page.cms_tags[2].identifier |
| - | assert_equal page.cms_tags[1], page.cms_tags[2].parent |
| - | assert_equal 'cms_tag/snippet_default', page.cms_tags[3].identifier |
| + | assert_equal 4, page.tags.size |
| + | assert_equal 'cms_tag/field_text_default_field_text', page.tags[0].identifier |
| + | assert_equal 'cms_tag/page_text_default_page_text', page.tags[1].identifier |
| + | assert_equal 'cms_tag/snippet_default', page.tags[2].identifier |
| + | assert_equal page.tags[1], page.tags[2].parent |
| + | assert_equal 'cms_tag/snippet_default', page.tags[3].identifier |
| end | |
| def test_content_for_new_page | |
| - | page = CmsPage.new |
| - | assert page.cms_blocks.blank? |
| - | assert page.cms_tags.blank? |
| + | page = Cms::Page.new |
| + | assert page.blocks.blank? |
| + | assert page.tags.blank? |
| assert_equal '', page.content | |
| - | assert page.cms_tags.blank? |
| + | assert page.tags.blank? |
| end | |
| def test_content_for_new_page_with_layout | |
| - | page = cms_sites(:default).cms_pages.new(:cms_layout => cms_layouts(:default)) |
| - | assert page.cms_blocks.blank? |
| - | assert page.cms_tags.blank? |
| + | page = cms_sites(:default).pages.new(:layout => cms_layouts(:default)) |
| + | assert page.blocks.blank? |
| + | assert page.tags.blank? |
| assert_equal rendered_content_formatter( | |
| ' | |
| layout_content_a | |
| @@ | @@ -109,17 +109,17 @@ class CmsTagTest < ActiveSupport::TestCase |
| layout_content_c' | |
| ), page.content | |
| - | assert_equal 3, page.cms_tags.size |
| - | assert_equal 'cms_tag/field_text_default_field_text', page.cms_tags[0].identifier |
| - | assert_equal 'cms_tag/page_text_default_page_text', page.cms_tags[1].identifier |
| - | assert_equal 'cms_tag/snippet_default', page.cms_tags[2].identifier |
| + | assert_equal 3, page.tags.size |
| + | assert_equal 'cms_tag/field_text_default_field_text', page.tags[0].identifier |
| + | assert_equal 'cms_tag/page_text_default_page_text', page.tags[1].identifier |
| + | assert_equal 'cms_tag/snippet_default', page.tags[2].identifier |
| end | |
| def test_content_for_new_page_with_initilized_cms_blocks | |
| - | page = cms_sites(:default).cms_pages.new(:cms_layout => cms_layouts(:default)) |
| - | assert page.cms_blocks.blank? |
| - | assert page.cms_tags.blank? |
| - | page.cms_blocks_attributes = [ |
| + | page = cms_sites(:default).pages.new(:layout => cms_layouts(:default)) |
| + | assert page.blocks.blank? |
| + | assert page.tags.blank? |
| + | page.blocks_attributes = [ |
| { | |
| :label => 'default_field_text', | |
| :content => 'new_default_field_content', | |
| @@ | @@ -136,7 +136,7 @@ class CmsTagTest < ActiveSupport::TestCase |
| :type => 'CmsTag::PageText' | |
| } | |
| ] | |
| - | assert_equal 3, page.cms_blocks.size |
| + | assert_equal 3, page.blocks.size |
| assert_equal rendered_content_formatter( | |
| ' | |
| @@ | @@ -148,18 +148,18 @@ class CmsTagTest < ActiveSupport::TestCase |
| layout_content_c' | |
| ), page.content | |
| - | assert_equal 4, page.cms_tags.size |
| - | assert_equal 'cms_tag/field_text_default_field_text', page.cms_tags[0].identifier |
| - | assert_equal 'cms_tag/page_text_default_page_text', page.cms_tags[1].identifier |
| - | assert_equal 'cms_tag/snippet_default', page.cms_tags[2].identifier |
| - | assert_equal page.cms_tags[1], page.cms_tags[2].parent |
| - | assert_equal 'cms_tag/snippet_default', page.cms_tags[3].identifier |
| + | assert_equal 4, page.tags.size |
| + | assert_equal 'cms_tag/field_text_default_field_text', page.tags[0].identifier |
| + | assert_equal 'cms_tag/page_text_default_page_text', page.tags[1].identifier |
| + | assert_equal 'cms_tag/snippet_default', page.tags[2].identifier |
| + | assert_equal page.tags[1], page.tags[2].parent |
| + | assert_equal 'cms_tag/snippet_default', page.tags[3].identifier |
| end | |
| def test_content_with_repeated_tags | |
| page = cms_pages(:default) | |
| - | page.cms_layout.content << "\n{{cms:page:default_page_text:text}}" |
| - | page.cms_layout.save! |
| + | page.layout.content << "\n{{cms:page:default_page_text:text}}" |
| + | page.layout.save! |
| assert_equal rendered_content_formatter( | |
| ' | |
| @@ | @@ -175,15 +175,15 @@ class CmsTagTest < ActiveSupport::TestCase |
| default_page_text_content_b' | |
| ), page.content(true) | |
| - | assert_equal 6, page.cms_tags.size |
| - | assert_equal 'cms_tag/field_text_default_field_text', page.cms_tags[0].identifier |
| - | assert_equal 'cms_tag/page_text_default_page_text', page.cms_tags[1].identifier |
| - | assert_equal 'cms_tag/snippet_default', page.cms_tags[2].identifier |
| - | assert_equal page.cms_tags[1], page.cms_tags[2].parent |
| - | assert_equal 'cms_tag/snippet_default', page.cms_tags[3].identifier |
| - | assert_equal 'cms_tag/page_text_default_page_text', page.cms_tags[4].identifier |
| - | assert_equal 'cms_tag/snippet_default', page.cms_tags[5].identifier |
| - | assert_equal page.cms_tags[4], page.cms_tags[5].parent |
| + | assert_equal 6, page.tags.size |
| + | assert_equal 'cms_tag/field_text_default_field_text', page.tags[0].identifier |
| + | assert_equal 'cms_tag/page_text_default_page_text', page.tags[1].identifier |
| + | assert_equal 'cms_tag/snippet_default', page.tags[2].identifier |
| + | assert_equal page.tags[1], page.tags[2].parent |
| + | assert_equal 'cms_tag/snippet_default', page.tags[3].identifier |
| + | assert_equal 'cms_tag/page_text_default_page_text', page.tags[4].identifier |
| + | assert_equal 'cms_tag/snippet_default', page.tags[5].identifier |
| + | assert_equal page.tags[4], page.tags[5].parent |
| end | |
| def test_content_with_cyclical_tags | |
| @@ | @@ -200,7 +200,7 @@ class CmsTagTest < ActiveSupport::TestCase |
| infinite loop | |
| layout_content_c' | |
| ), page.content(true) | |
| - | assert_equal 6, page.cms_tags.size |
| + | assert_equal 6, page.tags.size |
| end | |
| end | |
test/unit/cms_upload_test.rb
+0
-26
| @@ | @@ -1,26 +0,0 @@ |
| - | require File.expand_path('../test_helper', File.dirname(__FILE__)) |
| - | |
| - | class CmsUploadTest < ActiveSupport::TestCase |
| - | |
| - | def test_validations |
| - | assert_no_difference 'CmsUpload.count' do |
| - | upload = CmsUpload.create |
| - | assert upload.errors.present? |
| - | assert_has_errors_on upload, [:file_file_name] |
| - | end |
| - | end |
| - | |
| - | def test_create |
| - | assert_difference 'CmsUpload.count' do |
| - | cms_sites(:default).cms_uploads.create( |
| - | :file => fixture_file_upload('files/valid_image.jpg') |
| - | ) |
| - | end |
| - | end |
| - | |
| - | def test_create_failure |
| - | assert_no_difference 'CmsUpload.count' do |
| - | cms_sites(:default).cms_uploads.create(:file => '') |
| - | end |
| - | end |
| - | end |
test/unit/models/block_test.rb
+43
-0
| @@ | @@ -0,0 +1,43 @@ |
| + | require File.expand_path('../../test_helper', File.dirname(__FILE__)) |
| + | |
| + | class BlockTest < ActiveSupport::TestCase |
| + | |
| + | def test_fixtures_validity |
| + | Cms::Block.all.each do |block| |
| + | assert block.valid?, block.errors.full_messages.to_s |
| + | end |
| + | end |
| + | |
| + | def test_new_via_page_nested_attributes |
| + | assert_difference ['Cms::Page.count', 'Cms::Block.count'] do |
| + | page = Cms::Page.create!( |
| + | :site => cms_sites(:default), |
| + | :layout => cms_layouts(:default), |
| + | :label => 'test page', |
| + | :slug => 'test_page', |
| + | :parent_id => cms_pages(:default).id, |
| + | :blocks_attributes => [ |
| + | { |
| + | :label => 'test_block', |
| + | :content => 'test_content' |
| + | } |
| + | ] |
| + | ) |
| + | assert_equal 1, page.blocks.count |
| + | block = page.blocks.first |
| + | assert_equal 'test_block', block.label |
| + | assert_equal 'test_content', block.content |
| + | end |
| + | end |
| + | |
| + | def test_initialize_or_find |
| + | tag = CmsTag::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 = CmsTag::PageText.initialize_or_find(cms_pages(:default), :new_block) |
| + | assert_equal 'new_block', tag.label |
| + | assert tag.content.blank? |
| + | end |
| + | |
| + | end |
test/unit/models/layout_test.rb
+129
-0
| @@ | @@ -0,0 +1,129 @@ |
| + | require File.expand_path('../../test_helper', File.dirname(__FILE__)) |
| + | |
| + | class CmsLayoutTest < ActiveSupport::TestCase |
| + | |
| + | def test_fixtures_validity |
| + | Cms::Layout.all.each do |layout| |
| + | assert layout.valid?, layout.errors.full_messages.to_s |
| + | end |
| + | end |
| + | |
| + | def test_validations |
| + | layout = cms_sites(:default).layouts.create |
| + | assert layout.errors.present? |
| + | assert_has_errors_on layout, [:label, :slug, :content] |
| + | end |
| + | |
| + | def test_validation_of_tag_presence |
| + | layout = cms_sites(:default).layouts.create(:content => 'some text') |
| + | assert_has_errors_on layout, :content |
| + | |
| + | layout = cms_sites(:default).layouts.create(:content => '{cms:snippet:blah}') |
| + | assert_has_errors_on layout, :content |
| + | |
| + | layout = cms_sites(:default).layouts.new( |
| + | :label => 'test', |
| + | :slug => 'test', |
| + | :content => '{{cms:page:blah}}' |
| + | ) |
| + | assert layout.valid? |
| + | |
| + | layout = cms_sites(:default).layouts.new( |
| + | :label => 'test', |
| + | :slug => 'test', |
| + | :content => '{{cms:field:blah}}' |
| + | ) |
| + | assert layout.valid? |
| + | end |
| + | |
| + | def test_creation |
| + | assert_difference 'Cms::Layout.count' do |
| + | layout = cms_sites(:default).layouts.create( |
| + | :label => 'New Layout', |
| + | :slug => 'new-layout', |
| + | :content => '{{cms:page:content}}', |
| + | :css => 'css', |
| + | :js => 'js' |
| + | ) |
| + | assert_equal 'New Layout', layout.label |
| + | assert_equal 'new-layout', layout.slug |
| + | assert_equal '{{cms:page:content}}', layout.content |
| + | assert_equal 'css', layout.css |
| + | assert_equal 'js', layout.js |
| + | end |
| + | end |
| + | |
| + | def test_options_for_select |
| + | assert_equal ['Default Layout', 'Nested Layout', '. . Child Layout'], |
| + | Cms::Layout.options_for_select(cms_sites(:default)).collect{|t| t.first} |
| + | assert_equal ['Default Layout', 'Nested Layout'], |
| + | Cms::Layout.options_for_select(cms_sites(:default), cms_layouts(:child)).collect{|t| t.first} |
| + | assert_equal ['Default Layout'], |
| + | Cms::Layout.options_for_select(cms_sites(:default), cms_layouts(:nested)).collect{|t| t.first} |
| + | end |
| + | |
| + | def test_app_layouts_for_select |
| + | assert_equal ['cms_admin.html.erb'], Cms::Layout.app_layouts_for_select |
| + | end |
| + | |
| + | def test_merged_content_with_same_child_content |
| + | parent_layout = cms_layouts(:nested) |
| + | assert_equal "{{cms:page:header}}\n{{cms:page:content}}", parent_layout.content |
| + | assert_equal "{{cms:page:header}}\n{{cms:page:content}}", parent_layout.merged_content |
| + | |
| + | child_layout = cms_layouts(:child) |
| + | assert_equal parent_layout, child_layout.parent |
| + | assert_equal "{{cms:page:left_column}}\n{{cms:page:right_column}}", child_layout.content |
| + | assert_equal "{{cms:page:header}}\n{{cms:page:left_column}}\n{{cms:page:right_column}}", child_layout.merged_content |
| + | |
| + | child_layout.update_attribute(:content, '{{cms:page:content}}') |
| + | assert_equal "{{cms:page:header}}\n{{cms:page:content}}", child_layout.merged_content |
| + | |
| + | parent_layout.update_attribute(:content, '{{cms:page:whatever}}') |
| + | child_layout.reload |
| + | assert_equal '{{cms:page:content}}', child_layout.merged_content |
| + | end |
| + | |
| + | def test_update_forces_page_content_reload |
| + | layout_1 = cms_layouts(:nested) |
| + | layout_2 = cms_layouts(:child) |
| + | page_1 = cms_sites(:default).pages.create!( |
| + | :label => 'page_1', |
| + | :slug => 'page-1', |
| + | :parent_id => cms_pages(:default).id, |
| + | :layout_id => layout_1.id, |
| + | :is_published => '1', |
| + | :blocks_attributes => [ |
| + | { :label => 'header', |
| + | :content => 'header_content' }, |
| + | { :label => 'content', |
| + | :content => 'content_content' } |
| + | ] |
| + | ) |
| + | page_2 = cms_sites(:default).pages.create!( |
| + | :label => 'page_2', |
| + | :slug => 'page-2', |
| + | :parent_id => cms_pages(:default).id, |
| + | :layout_id => layout_2.id, |
| + | :is_published => '1', |
| + | :blocks_attributes => [ |
| + | { :label => 'header', |
| + | :content => 'header_content' }, |
| + | { :label => 'left_column', |
| + | :content => 'left_column_content' }, |
| + | { :label => 'right_column', |
| + | :content => 'left_column_content' } |
| + | ] |
| + | ) |
| + | assert_equal "header_content\ncontent_content", page_1.content |
| + | assert_equal "header_content\nleft_column_content\nleft_column_content", page_2.content |
| + | |
| + | layout_1.update_attribute(:content, "Updated {{cms:page:content}}") |
| + | page_1.reload |
| + | page_2.reload |
| + | |
| + | assert_equal "Updated content_content", page_1.content |
| + | assert_equal "Updated left_column_content\nleft_column_content", page_2.content |
| + | end |
| + | |
| + | end |
test/unit/models/page_test.rb
+199
-0
| @@ | @@ -0,0 +1,199 @@ |
| + | require File.expand_path('../../test_helper', File.dirname(__FILE__)) |
| + | |
| + | class CmsPageTest < ActiveSupport::TestCase |
| + | |
| + | def test_fixtures_validity |
| + | Cms::Page.all.each do |page| |
| + | assert page.valid?, page.errors.full_messages.to_s |
| + | assert_equal page.read_attribute(:content), page.content(true) |
| + | end |
| + | end |
| + | |
| + | def test_validations |
| + | page = Cms::Page.new |
| + | page.save |
| + | assert page.invalid? |
| + | assert_has_errors_on page, [:layout, :slug, :label] |
| + | end |
| + | |
| + | def test_validation_of_parent_presence |
| + | page = cms_sites(:default).pages.new(new_params) |
| + | assert !page.parent |
| + | assert page.valid?, page.errors.full_messages.to_s |
| + | assert_equal cms_pages(:default), page.parent |
| + | end |
| + | |
| + | def test_validation_of_parent_relationship |
| + | page = cms_pages(:default) |
| + | assert !page.parent |
| + | page.parent = page |
| + | assert page.invalid? |
| + | assert_has_errors_on page, :parent_id |
| + | page.parent = cms_pages(:child) |
| + | assert page.invalid? |
| + | assert_has_errors_on page, :parent_id |
| + | end |
| + | |
| + | def test_validation_of_target_page |
| + | page = cms_pages(:child) |
| + | page.target_page = cms_pages(:default) |
| + | page.save! |
| + | assert_equal cms_pages(:default), page.target_page |
| + | page.target_page = page |
| + | assert page.invalid? |
| + | assert_has_errors_on page, :target_page_id |
| + | end |
| + | |
| + | def test_creation |
| + | assert_difference ['Cms::Page.count', 'Cms::Block.count'] do |
| + | page = cms_sites(:default).pages.create!( |
| + | :label => 'test', |
| + | :slug => 'test', |
| + | :parent_id => cms_pages(:default).id, |
| + | :layout_id => cms_layouts(:default).id, |
| + | :blocks_attributes => [ |
| + | { :label => 'test', |
| + | :content => 'test' } |
| + | ] |
| + | ) |
| + | assert page.is_published? |
| + | assert_equal 1, page.position |
| + | end |
| + | end |
| + | |
| + | def test_initialization_of_full_path |
| + | page = Cms::Page.new |
| + | assert_equal '/', page.full_path |
| + | |
| + | page = Cms::Page.new(new_params) |
| + | assert page.invalid? |
| + | assert_has_errors_on page, :site_id |
| + | |
| + | page = cms_sites(:default).pages.new(new_params(:parent => cms_pages(:default))) |
| + | assert page.valid? |
| + | assert_equal '/test-page', page.full_path |
| + | |
| + | page = cms_sites(:default).pages.new(new_params(:parent => cms_pages(:child))) |
| + | assert page.valid? |
| + | assert_equal '/child-page/test-page', page.full_path |
| + | |
| + | Cms::Page.destroy_all |
| + | page = cms_sites(:default).pages.new(new_params) |
| + | assert page.valid? |
| + | assert_equal '/', page.full_path |
| + | end |
| + | |
| + | def test_sync_child_pages |
| + | page = cms_pages(:child) |
| + | page_1 = cms_sites(:default).pages.create!(new_params(:parent => page, :slug => 'test-page-1')) |
| + | page_2 = cms_sites(:default).pages.create!(new_params(:parent => page, :slug => 'test-page-2')) |
| + | page_3 = cms_sites(:default).pages.create!(new_params(:parent => page_2, :slug => 'test-page-3')) |
| + | page_4 = cms_sites(:default).pages.create!(new_params(:parent => page_1, :slug => 'test-page-4')) |
| + | assert_equal '/child-page/test-page-1', page_1.full_path |
| + | assert_equal '/child-page/test-page-2', page_2.full_path |
| + | assert_equal '/child-page/test-page-2/test-page-3', page_3.full_path |
| + | assert_equal '/child-page/test-page-1/test-page-4', page_4.full_path |
| + | |
| + | page.update_attributes!(:slug => 'updated-page') |
| + | assert_equal '/updated-page', page.full_path |
| + | page_1.reload; page_2.reload; page_3.reload; page_4.reload |
| + | assert_equal '/updated-page/test-page-1', page_1.full_path |
| + | assert_equal '/updated-page/test-page-2', page_2.full_path |
| + | assert_equal '/updated-page/test-page-2/test-page-3', page_3.full_path |
| + | assert_equal '/updated-page/test-page-1/test-page-4', page_4.full_path |
| + | |
| + | page_2.update_attributes!(:parent => page_1) |
| + | page_1.reload; page_2.reload; page_3.reload; page_4.reload |
| + | assert_equal '/updated-page/test-page-1', page_1.full_path |
| + | assert_equal '/updated-page/test-page-1/test-page-2', page_2.full_path |
| + | assert_equal '/updated-page/test-page-1/test-page-2/test-page-3', page_3.full_path |
| + | assert_equal '/updated-page/test-page-1/test-page-4', page_4.full_path |
| + | end |
| + | |
| + | def test_children_count_updating |
| + | page_1 = cms_pages(:default) |
| + | page_2 = cms_pages(:child) |
| + | assert_equal 1, page_1.children_count |
| + | assert_equal 0, page_2.children_count |
| + | |
| + | page_3 = cms_sites(:default).pages.create!(new_params(:parent => page_2)) |
| + | page_1.reload; page_2.reload |
| + | assert_equal 1, page_1.children_count |
| + | assert_equal 1, page_2.children_count |
| + | assert_equal 0, page_3.children_count |
| + | |
| + | page_3.update_attributes!(:parent => page_1) |
| + | page_1.reload; page_2.reload |
| + | assert_equal 2, page_1.children_count |
| + | assert_equal 0, page_2.children_count |
| + | |
| + | page_3.destroy |
| + | page_1.reload; page_2.reload |
| + | assert_equal 1, page_1.children_count |
| + | assert_equal 0, page_2.children_count |
| + | end |
| + | |
| + | def test_cascading_destroy |
| + | assert_difference 'Cms::Page.count', -2 do |
| + | cms_pages(:default).destroy |
| + | end |
| + | end |
| + | |
| + | def test_options_for_select |
| + | assert_equal ['Default Page', '. . Child Page'], |
| + | Cms::Page.options_for_select(cms_sites(:default)).collect{|t| t.first } |
| + | assert_equal ['Default Page'], |
| + | Cms::Page.options_for_select(cms_sites(:default), cms_pages(:child)).collect{|t| t.first } |
| + | assert_equal [], |
| + | Cms::Page.options_for_select(cms_sites(:default), cms_pages(:default)) |
| + | |
| + | page = Cms::Page.new(new_params(:parent => cms_pages(:default))) |
| + | assert_equal ['Default Page', '. . Child Page'], |
| + | Cms::Page.options_for_select(cms_sites(:default), page).collect{|t| t.first } |
| + | end |
| + | |
| + | def test_cms_blocks_attributes_accessor |
| + | page = cms_pages(:default) |
| + | assert_equal page.blocks.count, page.blocks_attributes.size |
| + | assert_equal 'default_field_text', page.blocks_attributes.first[:label] |
| + | assert_equal 'default_field_text_content', page.blocks_attributes.first[:content] |
| + | assert page.blocks_attributes.first[:id] |
| + | end |
| + | |
| + | def test_content_caching |
| + | page = cms_pages(:default) |
| + | assert_equal page.read_attribute(:content), page.content |
| + | assert_equal page.read_attribute(:content), page.content(true) |
| + | |
| + | page.update_attribute(:content, 'changed') |
| + | assert_equal page.read_attribute(:content), page.content |
| + | assert_equal page.read_attribute(:content), page.content(true) |
| + | assert_not_equal 'changed', page.read_attribute(:content) |
| + | end |
| + | |
| + | def test_scope_published |
| + | assert_equal 2, Cms::Page.published.count |
| + | cms_pages(:child).update_attribute(:is_published, false) |
| + | assert_equal 1, Cms::Page.published.count |
| + | end |
| + | |
| + | def test_root? |
| + | assert cms_pages(:default).root? |
| + | assert !cms_pages(:child).root? |
| + | end |
| + | |
| + | def test_url |
| + | assert_equal 'http://test.host/', cms_pages(:default).url |
| + | assert_equal 'http://test.host/child-page', cms_pages(:child).url |
| + | end |
| + | |
| + | protected |
| + | |
| + | def new_params(options = {}) |
| + | { |
| + | :label => 'Test Page', |
| + | :slug => 'test-page', |
| + | :layout => cms_layouts(:default) |
| + | }.merge(options) |
| + | end |
| + | end |
test/unit/models/site_test.rb
+41
-0
| @@ | @@ -0,0 +1,41 @@ |
| + | require File.expand_path('../../test_helper', File.dirname(__FILE__)) |
| + | |
| + | class CmsSiteTest < ActiveSupport::TestCase |
| + | |
| + | def test_fixtures_validity |
| + | Cms::Site.all.each do |site| |
| + | assert site.valid?, site.errors.full_messages.to_s |
| + | end |
| + | end |
| + | |
| + | def test_validation |
| + | site = Cms::Site.new |
| + | assert site.invalid? |
| + | assert_has_errors_on site, [:label, :hostname] |
| + | |
| + | site = Cms::Site.new(:label => 'My Site', :hostname => 'http://mysite.com') |
| + | assert site.invalid? |
| + | assert_has_errors_on site, :hostname |
| + | |
| + | site = Cms::Site.new(:label => 'My Site', :hostname => 'mysite.com') |
| + | assert site.valid? |
| + | end |
| + | |
| + | def test_cascading_destroy |
| + | assert_difference 'Cms::Site.count', -1 do |
| + | assert_difference 'Cms::Layout.count', -3 do |
| + | assert_difference 'Cms::Page.count', -2 do |
| + | assert_difference 'Cms::Snippet.count', -1 do |
| + | cms_sites(:default).destroy |
| + | end |
| + | end |
| + | end |
| + | end |
| + | end |
| + | |
| + | def test_options_for_select |
| + | assert_equal 1, Cms::Site.options_for_select.size |
| + | assert_equal 'Default Site (test.host)', Cms::Site.options_for_select[0][0] |
| + | end |
| + | |
| + | end |
| \ No newline at end of file | |
test/unit/models/snippet_test.rb
+32
-0
| @@ | @@ -0,0 +1,32 @@ |
| + | require File.expand_path('../../test_helper', File.dirname(__FILE__)) |
| + | |
| + | class CmsSnippetTest < ActiveSupport::TestCase |
| + | |
| + | def test_fixtures_validity |
| + | Cms::Snippet.all.each do |snippet| |
| + | assert snippet.valid?, snippet.errors.full_messages.to_s |
| + | end |
| + | end |
| + | |
| + | def test_validations |
| + | snippet = Cms::Snippet.new |
| + | snippet.save |
| + | assert snippet.invalid? |
| + | assert_has_errors_on snippet, [:label, :slug] |
| + | end |
| + | |
| + | def test_method_content |
| + | assert_equal cms_snippets(:default).content, Cms::Snippet.content_for('default') |
| + | assert_equal '', Cms::Snippet.content_for('nonexistent_snippet') |
| + | end |
| + | |
| + | def test_update_forces_page_content_reload |
| + | snippet = cms_snippets(:default) |
| + | page = cms_pages(:default) |
| + | assert_match snippet.content, page.content |
| + | snippet.update_attribute(:content, 'new_snippet_content') |
| + | page.reload |
| + | assert_match /new_snippet_content/, page.content |
| + | end |
| + | |
| + | end |
test/unit/models/upload_test.rb
+32
-0
| @@ | @@ -0,0 +1,32 @@ |
| + | require File.expand_path('../../test_helper', File.dirname(__FILE__)) |
| + | |
| + | class CmsUploadTest < ActiveSupport::TestCase |
| + | |
| + | def test_fixtures_validity |
| + | Cms::Upload.all.each do |upload| |
| + | assert upload.valid?, upload.errors.full_messages.to_s |
| + | end |
| + | end |
| + | |
| + | def test_validations |
| + | assert_no_difference 'Cms::Upload.count' do |
| + | upload = Cms::Upload.create |
| + | assert upload.errors.present? |
| + | assert_has_errors_on upload, [:file_file_name] |
| + | end |
| + | end |
| + | |
| + | def test_create |
| + | assert_difference 'Cms::Upload.count' do |
| + | cms_sites(:default).uploads.create( |
| + | :file => fixture_file_upload('files/valid_image.jpg') |
| + | ) |
| + | end |
| + | end |
| + | |
| + | def test_create_failure |
| + | assert_no_difference 'Cms::Upload.count' do |
| + | cms_sites(:default).uploads.create(:file => '') |
| + | end |
| + | end |
| + | end |