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_on: nil,
  public_until: nil,
  locked_at: nil,
  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_on,
  :public_until,
  :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_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, #cache_page?, #contains_feed?, #controller_and_action, #definition, #expiration_time, #folded?, #has_controller?, #layout_display_name, #layout_partial_name, #locked?, #public?, #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.



102
103
104
# File 'app/models/alchemy/page.rb', line 102

def do_not_sweep
  @do_not_sweep
end

#do_not_validate_languageObject

Returns the value of attribute do_not_validate_language.



103
104
105
# File 'app/models/alchemy/page.rb', line 103

def do_not_validate_language
  @do_not_validate_language
end

Class Method Details

.all_from_clipboard(clipboard) ⇒ Object



219
220
221
222
# File 'app/models/alchemy/page.rb', line 219

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



224
225
226
227
228
229
230
# File 'app/models/alchemy/page.rb', line 224

def all_from_clipboard_for_select(clipboard, language_id, layoutpage = false)
  return [] if clipboard.blank?
  clipboard_pages = 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.



244
245
246
247
# File 'app/models/alchemy/page.rb', line 244

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:



179
180
181
182
183
184
185
186
187
# File 'app/models/alchemy/page.rb', line 179

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



206
207
208
209
210
211
212
213
214
215
216
217
# File 'app/models/alchemy/page.rb', line 206

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
  page
end

.current_previewObject

Returns the current page previewed in the edit page template.



153
154
155
# File 'app/models/alchemy/page.rb', line 153

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.



147
148
149
# File 'app/models/alchemy/page.rb', line 147

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

.find_or_create_layout_root_for(language_id) ⇒ Object



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

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.



160
161
162
# File 'app/models/alchemy/page.rb', line 160

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

.layout_root_for(language_id) ⇒ Object



189
190
191
# File 'app/models/alchemy/page.rb', line 189

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


232
233
234
235
236
237
238
239
240
# File 'app/models/alchemy/page.rb', line 232

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

Instance Method Details

#copy_children_to(new_parent) ⇒ Object



362
363
364
365
366
367
368
369
370
371
372
# File 'app/models/alchemy/page.rb', line 362

def copy_children_to(new_parent)
  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



353
354
355
# File 'app/models/alchemy/page.rb', line 353

def first_public_child
  children.published.first
end

#fold!(user_id, status) ⇒ Object



336
337
338
339
340
# File 'app/models/alchemy/page.rb', line 336

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



358
359
360
# File 'app/models/alchemy/page.rb', line 358

def get_language_root
  self_and_ancestors.find_by(language_root: true)
end

#inherit_restricted_statusObject



348
349
350
# File 'app/models/alchemy/page.rb', line 348

def inherit_restricted_status
  self.restricted = parent.restricted?
end

#lock_to!(user) ⇒ Object

Locks the page to given user



324
325
326
# File 'app/models/alchemy/page.rb', line 324

def lock_to!(user)
  update_columns(locked_at: Time.current, 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



317
318
319
# File 'app/models/alchemy/page.rb', line 317

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



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

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

#publish!Object

Publishes the page.

Sets public_on and the published_at value to current time and resets public_until to nil

The published_at attribute is used as cache_key.



381
382
383
384
385
386
387
388
# File 'app/models/alchemy/page.rb', line 381

def publish!
  current_time = Time.current
  update_columns(
    published_at: current_time,
    public_on: current_time,
    public_until: nil
  )
end

#set_restrictions_to_child_pagesObject



342
343
344
345
346
# File 'app/models/alchemy/page.rb', line 342

def set_restrictions_to_child_pages
  descendants.each do |child|
    child.update_attributes(restricted: 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



300
301
302
# File 'app/models/alchemy/page.rb', line 300

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

#unlock!Object

Unlocks the page without updating the timestamps



330
331
332
333
334
# File 'app/models/alchemy/page.rb', line 330

def unlock!
  if update_columns(locked_at: nil, 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



398
399
400
401
402
403
404
405
406
407
# File 'app/models/alchemy/page.rb', line 398

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) && !redirects_to_external? && urlname != node.url
    LegacyPageUrl.create(page_id: id, urlname: urlname)
    hash[:urlname] = node.url
  end

  update_columns(hash)
end