Class: Alchemy::Page

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

Defined Under Namespace

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

Constant Summary collapse

DEFAULT_ATTRIBUTES_FOR_COPY =
{
  autogenerate_elements: false,
  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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PageElements

#available_element_definitions, #available_element_names, #available_elements_within_current_scope, #descendent_element_definitions, #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, #editor_roles, #expiration_time, #folded?, #has_controller?, #has_limited_editors?, #layout_display_name, #layout_partial_name, #locked?, #public?, #published_at, #redirects_to_external?, #rootpage?, #status, #status_title, #systempage?, #taggable?

Methods included from Taggable

included, #tag_list=

Methods included from Logger

#log_warning, warn

Methods included from Hints

#has_hint?, #hint

Methods inherited from BaseRecord

#active_record_5_1?

Class Method Details

.all_from_clipboard(clipboard) ⇒ Object


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

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


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

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.


252
253
254
255
# File 'app/models/alchemy/page.rb', line 252

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:


187
188
189
190
191
192
193
194
195
# File 'app/models/alchemy/page.rb', line 187

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


214
215
216
217
218
219
220
221
222
223
224
225
# File 'app/models/alchemy/page.rb', line 214

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.


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

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.


155
156
157
# File 'app/models/alchemy/page.rb', line 155

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

.find_or_create_layout_root_for(language_id) ⇒ Object


201
202
203
204
205
206
207
208
209
210
211
212
# File 'app/models/alchemy/page.rb', line 201

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,
    autogenerate_elements: false,
    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.


168
169
170
# File 'app/models/alchemy/page.rb', line 168

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

.layout_root_for(language_id) ⇒ Object


197
198
199
# File 'app/models/alchemy/page.rb', line 197

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

240
241
242
243
244
245
246
247
248
# File 'app/models/alchemy/page.rb', line 240

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

.rootObject Also known as: rootpage

The root page of the page tree

Internal use only. You wouldn't use this page ever.

Automatically created when accessed the first time.


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

def root
  super || create!(name: 'Root')
end

Instance Method Details

#attribute_fixed?(name) ⇒ Boolean

True if given attribute name is defined as fixed

Returns:

  • (Boolean)

431
432
433
# File 'app/models/alchemy/page.rb', line 431

def attribute_fixed?(name)
  fixed_attributes.fixed?(name)
end

#copy_children_to(new_parent) ⇒ Object


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

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

#editable_by?(user) ⇒ Boolean

Checks the current page's list of editors, if defined.

This allows us to pass in a user and see if any of their roles are enable them to make edits

Returns:

  • (Boolean)

440
441
442
443
# File 'app/models/alchemy/page.rb', line 440

def editable_by?(user)
  return true unless has_limited_editors?
  (editor_roles & user.alchemy_roles).any?
end

#first_public_childObject

Returns the first published child


369
370
371
# File 'app/models/alchemy/page.rb', line 369

def first_public_child
  children.published.first
end

#fixed_attributesObject

Holds an instance of FixedAttributes


426
427
428
# File 'app/models/alchemy/page.rb', line 426

def fixed_attributes
  @_fixed_attributes ||= Alchemy::Page::FixedAttributes.new(self)
end

#fold!(user_id, status) ⇒ Object


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

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


374
375
376
# File 'app/models/alchemy/page.rb', line 374

def get_language_root
  self_and_ancestors.find_by(language_root: true)
end

#inherit_restricted_statusObject


364
365
366
# File 'app/models/alchemy/page.rb', line 364

def inherit_restricted_status
  self.restricted = parent.restricted?
end

#lock_to!(user) ⇒ Object

Locks the page to given user


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

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.

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :restricted (Boolean) — default: false

    only restricted pages (true), skip restricted pages (false)

  • :public (Boolean) — default: true

    only public pages (true), skip public pages (false)


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

def next(options = {})
  pages = self_and_siblings.where('lft > ?', lft)
  select_page(pages, options.merge(order: :asc))
end

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

Returns the previous page on the same level or nil.

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :restricted (Boolean) — default: false

    only restricted pages (true), skip restricted pages (false)

  • :public (Boolean) — default: true

    only public pages (true), skip public pages (false)


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

def previous(options = {})
  pages = self_and_siblings.where('lft < ?', lft)
  select_page(pages, options.merge(order: :desc))
end

#public_onObject

Returns the value of public_on attribute

If it's a fixed attribute then the fixed value is returned instead


449
450
451
# File 'app/models/alchemy/page.rb', line 449

def public_on
  attribute_fixed?(:public_on) ? fixed_attributes[:public_on] : self[:public_on]
end

#public_untilObject

Returns the value of public_until attribute

If it's a fixed attribute then the fixed value is returned instead


457
458
459
# File 'app/models/alchemy/page.rb', line 457

def public_until
  attribute_fixed?(:public_until) ? fixed_attributes[:public_until] : self[:public_until]
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.


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

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

#set_restrictions_to_child_pagesObject


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

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


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

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

#unlock!Object

Unlocks the page without updating the timestamps


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

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


414
415
416
417
418
419
420
421
422
423
# File 'app/models/alchemy/page.rb', line 414

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