Class: Page

Inherits:
Object
  • Object
show all
Includes:
MongoSearch::Searchable, Mongoid::Document, Mongoid::Timestamps, Slices::HasAttachments::PageInstanceMethods, Slices::HasSlices, Slices::PageAsJSON, Slices::Tree
Defined in:
app/models/page.rb

Direct Known Subclasses

SetPage

Defined Under Namespace

Classes: NotFound

Constant Summary collapse

DESCRIPTION_DEPRECATION_WARNING =
"Page#description is now meta_description. If you are upgrading, run 'rake slices:migrate:meta_description' to update."
CACHED_VIRTUAL_PAGES =
{
  'not_found' => '404',
  'error' => '500'
}

Instance Attribute Summary

Attributes included from Slices::Tree

#external_url, #path, #position, #show_in_nav

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Slices::HasAttachments::PageInstanceMethods

#attachment_assets, #slice_attachment_asset_ids

Methods included from Slices::HasSlices

#embeded_slices, #messages_from_errors, #ordered_slices_for, #slice_errors_for, #slices_for, #validate_slices

Methods included from Slices::PageAsJSON

#as_json

Methods included from Slices::Tree

#ancestors, #children, #descended_from?, #entry_children, #first_sibling?, #generate_path, #home?, #last_sibling?, minimal, #navigable_children, #navigation_path, #next_sibling, #page_children, #parent, #parent=, #peers, #permalink, #previous_sibling, #siblings, #siblings_by_position, #update_path_for_children

Class Method Details

.available_layoutsObject



32
33
34
35
36
# File 'app/models/page.rb', line 32

def self.available_layouts
  Layout.all.map do |human_name, machine_name|
    { human_name: human_name, machine_name: machine_name }
  end
end

.find_by_id(id) ⇒ Object



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

def self.find_by_id(id)
  find(id)
rescue BSON::InvalidObjectId, Mongoid::Errors::DocumentNotFound
  nil
end

.find_by_id!(id) ⇒ Object



86
87
88
# File 'app/models/page.rb', line 86

def self.find_by_id!(id)
  find_by_id(id) || (raise NotFound)
end

.find_virtual(role) ⇒ Object



90
91
92
# File 'app/models/page.rb', line 90

def self.find_virtual(role)
  first(conditions: { role: role })
end

.make(attributes = {}) ⇒ Object

Virtual pages don’t live in the and are not associated with a specific URL. Instead, they can be rendered at any path, depending on the circumstances (e.g. when a page isn’t found, or when an error occurs). Consequently they aren’t created with a :parent attribute.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/page.rb', line 64

def self.make(attributes = {})
  attributes = attributes.symbolize_keys
  parent = parent_from_attributes(attributes)
  attributes[:path] ||= path_from_attributes(attributes, parent)

  new(attributes).tap do |page|
    yield(page) if block_given?
    page.parent = parent unless attributes.include?(:role)
    if parent.present?
      page.position = parent.children.count
      page.layout = parent.layout if page.entry?
    end
    page.save
  end
end

.role_for_status(status) ⇒ Object



52
53
54
55
56
57
58
# File 'app/models/page.rb', line 52

def self.role_for_status(status)
  if CACHED_VIRTUAL_PAGES.has_value?(status)
    CACHED_VIRTUAL_PAGES.detect { |k, v| v == status }[0]
  else
    nil
  end
end

Instance Method Details

#available_layoutsObject



164
165
166
# File 'app/models/page.rb', line 164

def available_layouts
  self.class.available_layouts
end

#cache_virtual_pageObject



98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/page.rb', line 98

def cache_virtual_page
  if cacheable_virtual_page? && (! Rails.env.test?)
    fork do
      script = File.join(Slices.gem_path, 'script', 'request-local-page')
      rails = File.join(Rails.root, 'script', 'rails')
      path = "/#{CACHED_VIRTUAL_PAGES[role]}.html"
      FileUtils.rm_f(File.join(Rails.root, 'public', path))
      exec(rails, 'runner', '-e', Rails.env, script, path)
    end
  end
end

#cacheable_virtual_page?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'app/models/page.rb', line 94

def cacheable_virtual_page?
  CACHED_VIRTUAL_PAGES.has_key?(role)
end

#descriptionObject

End of added



170
171
172
173
# File 'app/models/page.rb', line 170

def description
  ActiveSupport::Deprecation::warn DESCRIPTION_DEPRECATION_WARNING
  meta_description
end

#description=(value) ⇒ Object



175
176
177
178
# File 'app/models/page.rb', line 175

def description=(value)
  ActiveSupport::Deprecation::warn DESCRIPTION_DEPRECATION_WARNING
  self.meta_description = value
end

#entry?Boolean

Returns:

  • (Boolean)


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

def entry?
  false
end

#set_keywordsObject

Added in merge or page & content



156
157
158
159
160
161
162
# File 'app/models/page.rb', line 156

def set_keywords
  super
  slices.each do |slice|
    self._keywords += MongoSearch::KeywordsExtractor.extract(slice.search_text)
  end
  self._keywords.uniq!
end

#set_page?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'app/models/page.rb', line 118

def set_page?
  kind_of?(SetPage)
end

#set_slice(kind) ⇒ Object



126
127
128
129
# File 'app/models/page.rb', line 126

def set_slice(kind)
  slice_class = Object.const_get("#{kind.to_s}SetSlice".camelize)
  slices.detect { |slice| slice.kind_of?(slice_class) }
end

#setsObject



122
123
124
# File 'app/models/page.rb', line 122

def sets
  slices.select { |slice| slice.kind_of?(SetSlice) }
end

#templateObject



131
132
133
# File 'app/models/page.rb', line 131

def template
  "pages/show"
end

#update_attributes(attributes) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/models/page.rb', line 135

def update_attributes(attributes)
  attributes = attributes.symbolize_keys

  unless home?
    if attributes.has_key?(:name) || attributes.has_key?(:permalink)
      new_path = self.class.path_from_attributes(attributes, parent)
      attributes[:path] = new_path if new_path != path
    end
    if attributes.has_key?(:path) && attributes[:path].blank?
      attributes.delete(:path)
    end
  end

  super
  if valid?
    update_path_for_children if attributes.has_key?(:path)
  end
end

#virtual?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'app/models/page.rb', line 110

def virtual?
  role.present?
end