Class: Alchemy::Page

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

Defined Under Namespace

Modules: PageElements, PageNaming, PageNatures, PageScopes Classes: FixedAttributes, UrlPath

Constant Summary collapse

DEFAULT_ATTRIBUTES_FOR_COPY =
{
  autogenerate_elements: 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,
  :layoutpage,
  :menu_id,
]

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, #feed_elements, #richtext_contents_ids

Methods included from PageNaming

#renamed?, #slug, #update_urlname!

Methods included from NameConversions

#convert_to_humanized_name, #convert_to_urlname

Methods included from PageNatures

#cache_key, #cache_page?, #contains_feed?, #definition, #editor_roles, #expiration_time, #folded?, #has_limited_editors?, #layout_display_name, #layout_partial_name, #locked?, #public?, #published_at, #rootpage?, #status, #status_title, #taggable?

Methods included from Taggable

included, #tag_list=

Methods included from Logger

#log_warning, warn

Methods included from Hints

#has_hint?, #hint

Class Method Details

.all_from_clipboard(clipboard) ⇒ Object


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

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


229
230
231
232
233
234
235
236
# File 'app/models/alchemy/page.rb', line 229

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

.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:


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

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_elements(source, page)
    page
  end
end

.copy_and_paste(source, new_parent, new_name) ⇒ Object


210
211
212
213
214
215
216
217
218
219
220
221
# File 'app/models/alchemy/page.rb', line 210

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.


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

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.


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

def current_preview=(page)
  RequestStore.store[:alchemy_current_preview] = page
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.


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

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

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

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

.url_path_classObject

The url_path class

See Also:


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

def url_path_class
  @_url_path_class ||= Alchemy::Page::UrlPath
end

.url_path_class=(klass) ⇒ Object

Set a custom url path class

# config/initializers/alchemy.rb
Alchemy::Page.url_path_class = MyPageUrlPathClass

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

def url_path_class=(klass)
  @_url_path_class = klass
end

Instance Method Details

#attribute_fixed?(name) ⇒ Boolean

True if given attribute name is defined as fixed

Returns:

  • (Boolean)

459
460
461
# File 'app/models/alchemy/page.rb', line 459

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

#copy_children_to(new_parent) ⇒ Object


405
406
407
408
409
410
411
412
413
414
415
416
# File 'app/models/alchemy/page.rb', line 405

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

#creator_nameObject

Returns the name of the creator of this page.

If no creator could be found or associated user model does not respond to #name it returns 'unknown'


495
496
497
# File 'app/models/alchemy/page.rb', line 495

def creator_name
  creator.try(:name) || Alchemy.t("unknown")
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)

468
469
470
471
472
# File 'app/models/alchemy/page.rb', line 468

def editable_by?(user)
  return true unless has_limited_editors?

  (editor_roles & user.alchemy_roles).any?
end

#find_elements(options = {}) ⇒ ActiveRecord::Relation

Returns elements from page.

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :only (Array<String>|String)

    Returns only elements with given names

  • :except (Array<String>|String)

    Returns all elements except the ones with given names

  • :count (Integer)

    Limit the count of returned elements

  • :offset (Integer)

    Starts with an offset while returning elements

  • :include_hidden (Boolean) — default: false

    Return hidden elements as well

  • :random (Boolean) — default: false

    Return elements randomly shuffled

  • :reverse (Boolean) — default: false

    Reverse the load order

  • :finder (Class) — default: Alchemy::ElementsFinder

    A class that will return elements from page. Use this for your custom element loading logic.

Returns:

  • (ActiveRecord::Relation)

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

def find_elements(options = {})
  finder = options[:finder] || Alchemy::ElementsFinder.new(options)
  finder.elements(page: self)
end

#first_public_childObject

Returns the first published child


396
397
398
# File 'app/models/alchemy/page.rb', line 396

def first_public_child
  children.published.first
end

#fixed_attributesObject

Holds an instance of FixedAttributes


454
455
456
# File 'app/models/alchemy/page.rb', line 454

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

#fold!(user_id, status) ⇒ Object


379
380
381
382
383
# File 'app/models/alchemy/page.rb', line 379

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


401
402
403
# File 'app/models/alchemy/page.rb', line 401

def get_language_root
  self_and_ancestors.find_by(language_root: true)
end

#inherit_restricted_statusObject


391
392
393
# File 'app/models/alchemy/page.rb', line 391

def inherit_restricted_status
  self.restricted = parent.restricted?
end

#lock_to!(user) ⇒ Object

Locks the page to given user


367
368
369
# File 'app/models/alchemy/page.rb', line 367

def lock_to!(user)
  update_columns(locked_at: Time.current, locked_by: user.id)
end

#locker_nameObject

Returns the name of the user currently editing this page.

If no locker could be found or associated user model does not respond to #name it returns 'unknown'


513
514
515
# File 'app/models/alchemy/page.rb', line 513

def locker_name
  locker.try(:name) || Alchemy.t("unknown")
end

Menus (aka. root nodes) this page is attached to


519
520
521
# File 'app/models/alchemy/page.rb', line 519

def menus
  @_menus ||= nodes.map(&:root)
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)


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

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)


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

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


478
479
480
# File 'app/models/alchemy/page.rb', line 478

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


486
487
488
# File 'app/models/alchemy/page.rb', line 486

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.


425
426
427
428
429
430
431
432
# File 'app/models/alchemy/page.rb', line 425

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


385
386
387
388
389
# File 'app/models/alchemy/page.rb', line 385

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


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

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

#unlock!Object

Unlocks the page without updating the timestamps


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

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 the URL is the same

Parameters:

  • A (TreeNode)

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


442
443
444
445
446
447
448
449
450
451
# File 'app/models/alchemy/page.rb', line 442

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

  if urlname != node.url
    LegacyPageUrl.create(page_id: id, urlname: urlname)
    hash[:urlname] = node.url
  end

  update_columns(hash)
end

#updater_nameObject

Returns the name of the last updater of this page.

If no updater could be found or associated user model does not respond to #name it returns 'unknown'


504
505
506
# File 'app/models/alchemy/page.rb', line 504

def updater_name
  updater.try(:name) || Alchemy.t("unknown")
end

#url_pathObject

The url_path for this page


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

def url_path
  self.class.url_path_class.new(self).call
end