Class: Alchemy::Page
- Inherits:
-
BaseRecord
- Object
- ActiveRecord::Base
- BaseRecord
- Alchemy::Page
- Includes:
- Definitions, PageElements, PageNaming, PageNatures, PageScopes, Taggable
- Defined in:
- app/models/alchemy/page/page_scopes.rb,
app/models/alchemy/page.rb,
app/models/alchemy/page/url_path.rb,
app/models/alchemy/page/publisher.rb,
app/models/alchemy/page/definitions.rb,
app/models/alchemy/page/page_naming.rb,
app/models/alchemy/page/page_natures.rb,
app/models/alchemy/page/page_elements.rb,
app/models/alchemy/page/fixed_attributes.rb
Overview
ActiveRecord scopes for Alchemy::Page
Defined Under Namespace
Modules: Definitions, PageElements, PageNaming, PageNatures, PageScopes Classes: FixedAttributes, Publisher, 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 title meta_description meta_keywords ]- PERMITTED_ATTRIBUTES =
[ :name, :page_layout, :public_on, :public_until, :restricted, :robot_index, :robot_follow, :searchable, :sitemap, :tag_list, :urlname, :layoutpage, :menu_id, { draft_version_attributes: [:id] + PageVersion::METADATA_ATTRIBUTES.map(&:to_sym) } ]
Constants included from PageNaming
Constants included from SearchableResource
SearchableResource::SEARCHABLE_COLUMN_TYPES
Class Method Summary collapse
- .all_from_clipboard(clipboard) ⇒ Object
- .all_from_clipboard_for_select(clipboard, language_id, layoutpages: false) ⇒ Object
-
.copy(source, differences = {}) ⇒ Alchemy::Page
Creates a copy of given source.
- .copy_and_paste(source, new_parent, new_name) ⇒ Object
-
.language_root_for(language_id) ⇒ Object
The language root page for given language id.
- .link_target_options ⇒ Object
-
.ransackable_attributes(_auth_object = nil) ⇒ Object
Allow all string and text attributes to be searchable by Ransack.
- .searchable_alchemy_resource_attributes ⇒ Object
-
.url_path_class ⇒ Object
The url_path class.
-
.url_path_class=(klass) ⇒ Object
Set a custom url path class.
Instance Method Summary collapse
-
#attribute_fixed?(name) ⇒ Boolean
True if given attribute name is defined as fixed.
- #copy_children_to(new_parent) ⇒ Object
-
#creator_name ⇒ Object
Returns the name of the creator of this page.
-
#editable_by?(user) ⇒ Boolean
Checks the current page’s list of editors, if defined.
-
#find_elements(options = {}) ⇒ ActiveRecord::Relation
Returns elements from pages public version.
-
#first_public_child ⇒ Object
Returns the first published child.
-
#fixed_attributes ⇒ Object
Holds an instance of
FixedAttributes. - #fold!(user_id, status) ⇒ Object
-
#get_language_root ⇒ Object
Gets the language_root page for page.
-
#has_unpublished_changes? ⇒ Boolean
Returns true if the draft version has changes not yet published.
-
#hint_translation_attribute ⇒ Object
Key hint translations by page layout, rather than the default name.
- #inherit_restricted_status ⇒ Object
-
#lock_to!(user) ⇒ Object
Locks the page to given user.
-
#locker_name ⇒ Object
Returns the name of the user currently editing this page.
-
#menus ⇒ Object
Menus (aka. root nodes) this page is attached to.
-
#meta_description ⇒ Object
Returns the meta_description from the public version, falling back to draft version.
-
#meta_description=(value) ⇒ Object
deprecated
Deprecated.
Use draft_version.meta_description= instead
-
#meta_keywords ⇒ Object
Returns the meta_keywords from the public version, falling back to draft version.
-
#meta_keywords=(value) ⇒ Object
deprecated
Deprecated.
Use draft_version.meta_keywords= instead
-
#next(options = {}) ⇒ Object
(also: #next_page)
Returns the next page on the same level or nil.
-
#previous(options = {}) ⇒ Object
(also: #previous_page)
Returns the previous page on the same level or nil.
-
#public_on ⇒ Object
Returns the value of
public_onattribute from public version. -
#public_on=(time) ⇒ Object
Sets the public_on date on the published version.
-
#public_until ⇒ Object
Returns the value of
public_untilattribute. -
#publish!(current_time = Time.current) ⇒ Object
Creates a public version of the page in the background.
- #set_restrictions_to_child_pages ⇒ Object
-
#title ⇒ Object
Returns the title from the public version, falling back to draft version.
-
#title=(value) ⇒ Object
deprecated
Deprecated.
Use draft_version.title= instead
-
#to_partial_path ⇒ Object
The page’s view partial is dependent from its page layout.
-
#unlock! ⇒ Object
Unlocks the page without updating the timestamps.
-
#updater_name ⇒ Object
Returns the name of the last updater of this page.
-
#url_path ⇒ Object
The url_path for this page.
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, #richtext_ingredients_ids
Methods included from PageNaming
#renamed?, #slug, #update_urlname!
Methods included from NameConversions
#convert_to_urlname, #sanitized_filename
Methods included from PageNatures
#cache_page?, #cache_version, #definition, #editor_roles, #expiration_time, #folded?, #has_limited_editors?, #last_modified_at, #layout_display_name, #layout_partial_name, #locked?, #public?, #rootpage?, #status, #status_message, #status_title
Methods included from Taggable
Methods included from ConfigMissing
Methods included from SearchableResource
#ransackable_associations, #ransackable_attributes, #ransackable_scopes, #ransortable_attributes
Class Method Details
.all_from_clipboard(clipboard) ⇒ Object
234 235 236 237 238 |
# File 'app/models/alchemy/page.rb', line 234 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, layoutpages: false) ⇒ Object
240 241 242 243 244 245 246 247 |
# File 'app/models/alchemy/page.rb', line 240 def all_from_clipboard_for_select(clipboard, language_id, layoutpages: false) return [] if clipboard.blank? clipboard_pages = all_from_clipboard(clipboard) allowed_page_layouts = Alchemy::Page.selectable_layouts(language_id, layoutpages: layoutpages) allowed_page_layout_names = allowed_page_layouts.collect(&: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.
217 218 219 |
# File 'app/models/alchemy/page.rb', line 217 def copy(source, differences = {}) Alchemy::CopyPage.new(page: source).call(changed_attributes: differences) end |
.copy_and_paste(source, new_parent, new_name) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'app/models/alchemy/page.rb', line 221 def copy_and_paste(source, new_parent, new_name) page = Alchemy::CopyPage.new(page: source) .call(changed_attributes: { parent: new_parent, language: new_parent&.language, name: new_name }) if source.children.any? source.copy_children_to(page) end page end |
.language_root_for(language_id) ⇒ Object
Returns the language root page for given language id.
198 199 200 |
# File 'app/models/alchemy/page.rb', line 198 def language_root_for(language_id) language_roots.find_by_language_id(language_id) end |
.link_target_options ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 |
# File 'app/models/alchemy/page.rb', line 249 def = [[Alchemy.t(:default, scope: "link_target_options"), ""]] = Alchemy.config. .each do |option| # add an underscore to the options to provide the default syntax # @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#target << [Alchemy.t(option, scope: "link_target_options", default: option.to_s.humanize), "_#{option}"] end end |
.ransackable_attributes(_auth_object = nil) ⇒ Object
Allow all string and text attributes to be searchable by Ransack.
262 263 264 |
# File 'app/models/alchemy/page.rb', line 262 def ransackable_attributes(_auth_object = nil) searchable_alchemy_resource_attributes + %w[updated_at page_layout] end |
.searchable_alchemy_resource_attributes ⇒ Object
191 192 193 |
# File 'app/models/alchemy/page.rb', line 191 def searchable_alchemy_resource_attributes %w[name urlname] end |
.url_path_class ⇒ Object
The url_path class
178 179 180 |
# File 'app/models/alchemy/page.rb', line 178 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
187 188 189 |
# File 'app/models/alchemy/page.rb', line 187 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
452 453 454 |
# File 'app/models/alchemy/page.rb', line 452 def attribute_fixed?(name) fixed_attributes.fixed?(name) end |
#copy_children_to(new_parent) ⇒ Object
394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'app/models/alchemy/page.rb', line 394 def copy_children_to(new_parent) children.each do |child| next if child == new_parent new_child = Alchemy::CopyPage.new(page: child).call(changed_attributes: { parent_id: new_parent.id, 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_name ⇒ Object
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’
536 537 538 |
# File 'app/models/alchemy/page.rb', line 536 def creator_name creator.try(:alchemy_display_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
461 462 463 464 465 |
# File 'app/models/alchemy/page.rb', line 461 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 pages public version.
You can pass another page_version to load elements from in the options.
296 297 298 299 |
# File 'app/models/alchemy/page.rb', line 296 def find_elements( = {}) finder = [:finder] || Alchemy::ElementsFinder.new() finder.elements(page_version: [:page_version] || public_version) end |
#first_public_child ⇒ Object
Returns the first published child
385 386 387 |
# File 'app/models/alchemy/page.rb', line 385 def first_public_child children.published.first end |
#fixed_attributes ⇒ Object
Holds an instance of FixedAttributes
447 448 449 |
# File 'app/models/alchemy/page.rb', line 447 def fixed_attributes @_fixed_attributes ||= FixedAttributes.new(self) end |
#fold!(user_id, status) ⇒ Object
368 369 370 371 372 |
# File 'app/models/alchemy/page.rb', line 368 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_root ⇒ Object
Gets the language_root page for page
390 391 392 |
# File 'app/models/alchemy/page.rb', line 390 def get_language_root self_and_ancestors.find_by(language_root: true) end |
#has_unpublished_changes? ⇒ Boolean
Returns true if the draft version has changes not yet published.
Compares the updated_at timestamp of the draft_version against the updated_at timestamp of the public_version. If there’s no public_version, the page has never been published and always has unpublished changes.
421 422 423 424 425 |
# File 'app/models/alchemy/page.rb', line 421 def has_unpublished_changes? return true unless public_version draft_version.updated_at > public_version.updated_at end |
#hint_translation_attribute ⇒ Object
Key hint translations by page layout, rather than the default name.
560 561 562 |
# File 'app/models/alchemy/page.rb', line 560 def hint_translation_attribute page_layout end |
#inherit_restricted_status ⇒ Object
380 381 382 |
# File 'app/models/alchemy/page.rb', line 380 def inherit_restricted_status self.restricted = parent.restricted? end |
#lock_to!(user) ⇒ Object
Locks the page to given user
356 357 358 |
# File 'app/models/alchemy/page.rb', line 356 def lock_to!(user) update_columns(locked_at: Time.current, locked_by: user.id) end |
#locker_name ⇒ Object
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’
554 555 556 |
# File 'app/models/alchemy/page.rb', line 554 def locker_name locker.try(:alchemy_display_name) || Alchemy.t("unknown") end |
#menus ⇒ Object
Menus (aka. root nodes) this page is attached to
566 567 568 |
# File 'app/models/alchemy/page.rb', line 566 def ||= nodes.map(&:root) end |
#meta_description ⇒ Object
Returns the meta_description from the public version, falling back to draft version
If it’s a fixed attribute then the fixed value is returned instead
497 498 499 500 501 |
# File 'app/models/alchemy/page.rb', line 497 def return fixed_attributes[:meta_description] if attribute_fixed?(:meta_description) public_version&. || draft_version&. end |
#meta_description=(value) ⇒ Object
Use draft_version.meta_description= instead
520 521 522 |
# File 'app/models/alchemy/page.rb', line 520 def (value) draft_version&. = value end |
#meta_keywords ⇒ Object
Returns the meta_keywords from the public version, falling back to draft version
If it’s a fixed attribute then the fixed value is returned instead
507 508 509 510 511 |
# File 'app/models/alchemy/page.rb', line 507 def return fixed_attributes[:meta_keywords] if attribute_fixed?(:meta_keywords) public_version&. || draft_version&. end |
#meta_keywords=(value) ⇒ Object
Use draft_version.meta_keywords= instead
526 527 528 |
# File 'app/models/alchemy/page.rb', line 526 def (value) draft_version&. = value end |
#next(options = {}) ⇒ Object Also known as: next_page
Returns the next page on the same level or nil.
347 348 349 350 |
# File 'app/models/alchemy/page.rb', line 347 def next( = {}) pages = self_and_siblings.where("lft > ?", lft) select_page(pages, .merge(order: :asc)) end |
#previous(options = {}) ⇒ Object Also known as: previous_page
Returns the previous page on the same level or nil.
333 334 335 336 |
# File 'app/models/alchemy/page.rb', line 333 def previous( = {}) pages = self_and_siblings.where("lft < ?", lft) select_page(pages, .merge(order: :desc)) end |
#public_on ⇒ Object
Returns the value of public_on attribute from public version
If it’s a fixed attribute then the fixed value is returned instead
471 472 473 |
# File 'app/models/alchemy/page.rb', line 471 def public_on attribute_fixed?(:public_on) ? fixed_attributes[:public_on] : public_version&.public_on end |
#public_on=(time) ⇒ Object
Sets the public_on date on the published version
Builds a new version if none exists yet. Destroys public version if empty time is set
432 433 434 435 436 437 438 439 440 441 442 |
# File 'app/models/alchemy/page.rb', line 432 def public_on=(time) if public_version && time.blank? public_version.destroy! # Need to reset the public version on the instance so we do not need to reload self.public_version = nil elsif public_version public_version.public_on = time elsif time.present? versions.build(public_on: time) end end |
#public_until ⇒ Object
Returns the value of public_until attribute
If it’s a fixed attribute then the fixed value is returned instead
479 480 481 |
# File 'app/models/alchemy/page.rb', line 479 def public_until attribute_fixed?(:public_until) ? fixed_attributes[:public_until] : public_version&.public_until end |
#publish!(current_time = Time.current) ⇒ Object
Creates a public version of the page in the background.
410 411 412 |
# File 'app/models/alchemy/page.rb', line 410 def publish!(current_time = Time.current) PublishPageJob.perform_later(id, public_on: current_time) end |
#set_restrictions_to_child_pages ⇒ Object
374 375 376 377 378 |
# File 'app/models/alchemy/page.rb', line 374 def set_restrictions_to_child_pages descendants.each do |child| child.update(restricted: restricted?) end end |
#title ⇒ Object
Returns the title from the public version, falling back to draft version
If it’s a fixed attribute then the fixed value is returned instead
487 488 489 490 491 |
# File 'app/models/alchemy/page.rb', line 487 def title return fixed_attributes[:title] if attribute_fixed?(:title) public_version&.title || draft_version&.title end |
#title=(value) ⇒ Object
Use draft_version.title= instead
514 515 516 |
# File 'app/models/alchemy/page.rb', line 514 def title=(value) draft_version&.title = value end |
#to_partial_path ⇒ Object
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
322 323 324 |
# File 'app/models/alchemy/page.rb', line 322 def to_partial_path "alchemy/page_layouts/#{layout_partial_name}" end |
#unlock! ⇒ Object
Unlocks the page without updating the timestamps
362 363 364 365 366 |
# File 'app/models/alchemy/page.rb', line 362 def unlock! if update_columns(locked_at: nil, locked_by: nil) Current.preview_page = nil end end |
#updater_name ⇒ Object
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’
545 546 547 |
# File 'app/models/alchemy/page.rb', line 545 def updater_name updater.try(:alchemy_display_name) || Alchemy.t("unknown") end |
#url_path ⇒ Object
The url_path for this page
304 305 306 |
# File 'app/models/alchemy/page.rb', line 304 def url_path self.class.url_path_class.new(self).call end |