Class: Cms::Page

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

#blocks_attributes_changedObject

Returns the value of attribute blocks_attributes_changed.



23
24
25
# File 'app/models/cms/page.rb', line 23

def blocks_attributes_changed
  @blocks_attributes_changed
end

#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



133
134
135
# File 'app/models/cms/page.rb', line 133

def tags
  @tags
end

Class Method Details

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

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



69
70
71
72
73
74
75
76
77
# File 'app/models/cms/page.rb', line 69

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

#blocks_attributes(was = false) ⇒ Object

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



88
89
90
91
92
93
94
95
# File 'app/models/cms/page.rb', line 88

def blocks_attributes(was = false)
  self.blocks.collect do |block|
    block_attr = {}
    block_attr[:identifier] = block.identifier
    block_attr[:content]    = was ? block.content_was : block.content
    block_attr
  end
end

#blocks_attributes=(block_hashes = []) ⇒ Object

Array of block hashes in the following format:

[
  { :identifier => 'block_1', :content => 'block content' },
  { :identifier => 'block_2', :content => 'block content' }
]


102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/cms/page.rb', line 102

def blocks_attributes=(block_hashes = [])
  block_hashes = block_hashes.values if block_hashes.is_a?(Hash)
  block_hashes.each do |block_hash|
    block_hash.symbolize_keys! unless block_hash.is_a?(HashWithIndifferentAccess)
    block = 
      self.blocks.detect{|b| b.identifier == block_hash[:identifier]} || 
      self.blocks.build(:identifier => block_hash[:identifier])
    block.content = block_hash[:content]
    self.blocks_attributes_changed = self.blocks_attributes_changed || block.content_changed?
  end
end

#blocks_attributes_wasObject

Method to collect prevous state of blocks for revisions



144
145
146
# File 'app/models/cms/page.rb', line 144

def blocks_attributes_was
  blocks_attributes(true)
end

#content(force_reload = false) ⇒ Object

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/cms/page.rb', line 116

def content(force_reload = false)
  @content = force_reload ? nil : read_attribute(:content)
  @content ||= begin
    self.tags = [] # resetting
    if layout
      ComfyPress::Tag.process_content(
        self,
        ComfyPress::Tag.sanitize_irb(layout.merged_content)
      )
    else
      ''
    end
  end
end

#full_pathObject

– Instance Methods —————————————————– For previewing purposes sometimes we need to have full_path set. This full path take care of the pages and its childs but not of the site path



82
83
84
# File 'app/models/cms/page.rb', line 82

def full_path
  self.read_attribute(:full_path) || self.assign_full_path
end

#urlObject

Full url for a page



139
140
141
# File 'app/models/cms/page.rb', line 139

def url
  "http://" + "#{self.site.hostname}/#{self.site.path}/#{self.full_path}".squeeze("/")
end