Class: Alchemy::Page

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Hints, Logger, PageCells, PageElements, PageNaming, PageNatures, PageScopes, PageUsers, Touching
Defined in:
app/models/alchemy/page.rb

Defined Under Namespace

Modules: PageCells, PageElements, PageNaming, PageNatures, PageScopes, PageUsers

Constant Summary collapse

DEFAULT_ATTRIBUTES_FOR_COPY =
{
  :do_not_autogenerate => true,
  :do_not_sweep => true,
  :visible => false,
  :public => false,
  :locked => false,
  :locked_by => nil
}
SKIPPED_ATTRIBUTES_ON_COPY =
%w(id updated_at created_at creator_id updater_id lft rgt depth urlname cached_tag_list)
PERMITTED_ATTRIBUTES =
[
  :meta_description,
  :meta_keywords,
  :name,
  :page_layout,
  :public,
  :restricted,
  :robot_index,
  :robot_follow,
  :sitemap,
  :tag_list,
  :title,
  :urlname,
  :visible,
  :layoutpage
]

Constants included from PageNaming

PageNaming::RESERVED_URLNAMES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PageElements

#available_element_definitions, #available_element_names, #element_definition_names, #element_definitions, #element_definitions_by_name, #element_names_from_cell_definition, #element_names_from_page_definition, #feed_elements, #find_elements, #richtext_contents_ids

Methods included from PageCells

#can_have_cells?, #cell_definitions, #element_names_from_cells, #element_names_not_in_cell, #elements_grouped_by_cells, #has_cells?

Methods included from PageUsers

#creator, #creator_name, #locker, #locker_name, #updater, #updater_name

Methods included from PageNaming

#external_urlname, #renamed?, #slug, #update_urlname!, #visible_ancestors

Methods included from NameConversions

#convert_to_humanized_name, #convert_to_urlname

Methods included from PageNatures

#cache_key, #contains_feed?, #controller_and_action, #definition, #folded?, #has_controller?, #layout_display_name, #layout_partial_name, #published_at, #redirects_to_external?, #rootpage?, #status, #status_title, #systempage?, #taggable?

Methods included from Touching

#touch

Methods included from Logger

#log_warning, warn

Methods included from Hints

#has_hint?, #hint

Instance Attribute Details

#do_not_sweepObject

Returns the value of attribute do_not_sweep.



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

def do_not_sweep
  @do_not_sweep
end

#do_not_validate_languageObject

Returns the value of attribute do_not_validate_language.



83
84
85
# File 'app/models/alchemy/page.rb', line 83

def do_not_validate_language
  @do_not_validate_language
end

Class Method Details

.all_from_clipboard(clipboard) ⇒ Object



180
181
182
183
# File 'app/models/alchemy/page.rb', line 180

def all_from_clipboard(clipboard)
  return [] if clipboard.blank?
  where(id: clipboard.collect { |p| p['id'] })
end

.all_from_clipboard_for_select(clipboard, language_id, layoutpage = false) ⇒ Object



185
186
187
188
189
190
191
# File 'app/models/alchemy/page.rb', line 185

def all_from_clipboard_for_select(clipboard, language_id, layoutpage = false)
  return [] if clipboard.blank?
  clipboard_pages = self.all_from_clipboard(clipboard)
  allowed_page_layouts = Alchemy::PageLayout.selectable_layouts(language_id, layoutpage)
  allowed_page_layout_names = allowed_page_layouts.collect { |p| p['name'] }
  clipboard_pages.select { |cp| allowed_page_layout_names.include?(cp.page_layout) }
end

.ancestors_for(current) ⇒ Object

Returns an array of all pages in the same branch from current. I.e. used to find the active page in navigation.



204
205
206
207
# File 'app/models/alchemy/page.rb', line 204

def ancestors_for(current)
  return [] if current.nil?
  current.self_and_ancestors.contentpages
end

.copy(source, differences = {}) ⇒ Alchemy::Page

Creates a copy of given source.

Also copies all elements included in source.

Note:

It prevents the element auto generator from running.

Parameters:

  • source (Alchemy::Page)

    The source page the copy is taken from

  • differences (Hash) (defaults to: {})

    A optional hash with attributes that take precedence over the source attributes

Returns:



140
141
142
143
144
145
146
147
148
# File 'app/models/alchemy/page.rb', line 140

def copy(source, differences = {})
  page = Alchemy::Page.new(attributes_from_source_for_copy(source, differences))
  page.tag_list = source.tag_list
  if page.save!
    copy_cells(source, page)
    copy_elements(source, page)
    page
  end
end

.copy_and_paste(source, new_parent, new_name) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
# File 'app/models/alchemy/page.rb', line 167

def copy_and_paste(source, new_parent, new_name)
  page = copy(source, {
    parent_id: new_parent.id,
    language: new_parent.language,
    name: new_name,
    title: new_name
  })
  if source.children.any?
    source.copy_children_to(page)
  end
  return page
end

.current_previewObject

Returns the current page previewed in the edit page template.



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

def current_preview
  RequestStore.store[:alchemy_current_preview]
end

.current_preview=(page) ⇒ Object

Used to store the current page previewed in the edit page template.



108
109
110
# File 'app/models/alchemy/page.rb', line 108

def current_preview=(page)
  RequestStore.store[:alchemy_current_preview] = page
end

.find_or_create_layout_root_for(language_id) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/models/alchemy/page.rb', line 154

def find_or_create_layout_root_for(language_id)
  layoutroot = layout_root_for(language_id)
  return layoutroot if layoutroot
  language = Language.find(language_id)
  Page.create!(
    name: "Layoutroot for #{language.name}",
    layoutpage: true,
    language: language,
    do_not_autogenerate: true,
    parent_id: Page.root.id
  )
end

.language_root_for(language_id) ⇒ Object

Returns the language root page for given language id.

Parameters:

  • language_id (Fixnum)

Returns:

  • the language root page for given language id.



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

def language_root_for(language_id)
  self.language_roots.find_by_language_id(language_id)
end

.layout_root_for(language_id) ⇒ Object



150
151
152
# File 'app/models/alchemy/page.rb', line 150

def layout_root_for(language_id)
  where({:parent_id => Page.root.id, :layoutpage => true, :language_id => language_id}).limit(1).first
end


193
194
195
196
197
198
199
200
# File 'app/models/alchemy/page.rb', line 193

def link_target_options
  options = [[I18n.t(:default, scope: 'link_target_options'), '']]
  link_target_options = Config.get(:link_target_options)
  link_target_options.each do |option|
    options << [I18n.t(option, scope: 'link_target_options', default: option.to_s.humanize), option]
  end
  options
end

Instance Method Details

#copy_children_to(new_parent) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
# File 'app/models/alchemy/page.rb', line 323

def copy_children_to(new_parent)
  self.children.each do |child|
    next if child == new_parent
    new_child = Page.copy(child, {
      :language_id => new_parent.language_id,
      :language_code => new_parent.language_code
    })
    new_child.move_to_child_of(new_parent)
    child.copy_children_to(new_child) unless child.children.blank?
  end
end

#first_public_childObject

Returns the first published child



314
315
316
# File 'app/models/alchemy/page.rb', line 314

def first_public_child
  children.published.first
end

#fold!(user_id, status) ⇒ Object



297
298
299
300
301
# File 'app/models/alchemy/page.rb', line 297

def fold!(user_id, status)
  folded_page = folded_pages.find_or_create_by(user_id: user_id)
  folded_page.folded = status
  folded_page.save
end

#get_language_rootObject

Gets the language_root page for page



319
320
321
# File 'app/models/alchemy/page.rb', line 319

def get_language_root
  self_and_ancestors.find_by(language_root: true)
end

#inherit_restricted_statusObject



309
310
311
# File 'app/models/alchemy/page.rb', line 309

def inherit_restricted_status
  self.restricted = parent.restricted?
end

#lock_to!(user) ⇒ Object

Locks the page to given user without updating the timestamps



285
286
287
# File 'app/models/alchemy/page.rb', line 285

def lock_to!(user)
  self.update_columns(locked: true, locked_by: user.id)
end

#next(options = {}) ⇒ Object Also known as: next_page

Returns the next page on the same level or nil.

For options @see #next_or_previous



278
279
280
# File 'app/models/alchemy/page.rb', line 278

def next(options = {})
  next_or_previous('>', options)
end

#previous(options = {}) ⇒ Object Also known as: previous_page

Returns the previous page on the same level or nil.

For options @see #next_or_previous



269
270
271
# File 'app/models/alchemy/page.rb', line 269

def previous(options = {})
  next_or_previous('<', options)
end

#publish!Object

Publishes the page.

Sets public to true and the published_at value to current time.

The published_at attribute is used as cache_key.



341
342
343
# File 'app/models/alchemy/page.rb', line 341

def publish!
  update_columns(published_at: Time.now, public: true)
end

#set_restrictions_to_child_pagesObject



303
304
305
306
307
# File 'app/models/alchemy/page.rb', line 303

def set_restrictions_to_child_pages
  descendants.each do |child|
    child.update_attributes(:restricted => self.restricted?)
  end
end

#to_partial_pathObject

The page’s view partial is dependent from its page layout

Define page layouts

Page layouts are defined in the config/alchemy/page_layouts.yml file

- name: contact
  elements: [contactform]
  ...

Override the view

Page layout partials live in app/views/alchemy/page_layouts



261
262
263
# File 'app/models/alchemy/page.rb', line 261

def to_partial_path
  "alchemy/page_layouts/#{layout_partial_name}"
end

#unlock!Object

Unlocks the page without updating the timestamps



291
292
293
294
295
# File 'app/models/alchemy/page.rb', line 291

def unlock!
  if self.update_columns(locked: false, locked_by: nil)
    Page.current_preview = nil
  end
end

#update_node!(node) ⇒ Object

Updates an Alchemy::Page based on a new ordering to be applied to it

Note: Page’s urls should not be updated (and a legacy URL created) if nesting is OFF or if a page is external or if the URL is the same

Parameters:

  • A (TreeNode)

    tree node with new lft, rgt, depth, url, parent_id and restricted indexes to be updated



353
354
355
356
357
358
359
360
361
362
# File 'app/models/alchemy/page.rb', line 353

def update_node!(node)
  hash = {lft: node.left, rgt: node.right, parent_id: node.parent, depth: node.depth, restricted: node.restricted}

  if Config.get(:url_nesting) && !self.redirects_to_external? && self.urlname != node.url
    LegacyPageUrl.create(page_id: self.id, urlname: self.urlname)
    hash.merge!(urlname: node.url)
  end

  update_columns(hash)
end