Class: CmsPage

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/cms_page.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cms_tags(force_reload = false) ⇒ Object

Array of cms_tags for a page. Content generation is called if forced. These also include initialized cms_blocks if present



114
115
116
# File 'app/models/cms_page.rb', line 114

def cms_tags
  @cms_tags
end

Class Method Details

.load_for_full_path(site, path) ⇒ Object

Non-blowing-up version of the method above



82
83
84
85
86
# File 'app/models/cms_page.rb', line 82

def self.load_for_full_path(site, path)
  load_for_full_path!(site, path) 
rescue ActiveRecord::RecordNotFound
  nil
end

.load_for_full_path!(site, path) ⇒ Object

Wrapper around load_from_file and find_by_full_path returns page object if loaded / found



73
74
75
76
77
78
79
# File 'app/models/cms_page.rb', line 73

def self.load_for_full_path!(site, path)
  if ComfortableMexicanSofa.configuration.seed_data_path
    load_from_file(site, path)
  else
    site.cms_pages.find_by_full_path(path)
  end || raise(ActiveRecord::RecordNotFound, "CmsPage with path: #{path} cannot be found")
end

.load_from_file(site, path) ⇒ Object

Attempting to initialize page object from yaml file that is found in config.seed_data_path This file defines all attributes of the page plus all the block information



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/cms_page.rb', line 57

def self.load_from_file(site, path)
  return nil if ComfortableMexicanSofa.config.seed_data_path.blank?
  path = (path == '/')? '/index' : path.to_s.chomp('/')
  file_path = "#{ComfortableMexicanSofa.config.seed_data_path}/#{site.hostname}/pages#{path}.yml"
  return nil unless File.exists?(file_path)
  attributes              = YAML.load_file(file_path).symbolize_keys!
  attributes[:cms_layout] = CmsLayout.load_from_file(site, attributes[:cms_layout])
  attributes[:parent]     = CmsPage.load_from_file(site, attributes[:parent])
  attributes[:cms_site]   = site
  new(attributes)
rescue
  raise "Failed to load from #{file_path}"
end

.options_for_select(cms_site, cms_page = nil, current_page = nil, depth = 0, exclude_self = true, spacer = '. . ') ⇒ Object

– Class Methods ——————————————————– Tree-like structure for pages



45
46
47
48
49
50
51
52
53
# File 'app/models/cms_page.rb', line 45

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 Method Details

#cms_blocks_attributesObject

– Instance Methods —————————————————– Transforms existing cms_block information into a hash that can be used during form processing. That’s the only way to modify cms_blocks.



91
92
93
94
95
96
97
98
99
# File 'app/models/cms_page.rb', line 91

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

#content(force_reload = false) ⇒ Object

Processing content will return rendered content and will populate self.cms_tags with instances of CmsTag



103
104
105
106
107
108
109
110
# File 'app/models/cms_page.rb', line 103

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

#urlObject

Full url for a page



120
121
122
# File 'app/models/cms_page.rb', line 120

def url
  "http://#{self.cms_site.hostname}#{self.full_path}"
end