app/models/alchemy/page.rb in alchemy_cms-3.3.3 vs app/models/alchemy/page.rb in alchemy_cms-3.4.0.rc1
- old
+ new
@@ -1,25 +1,25 @@
# == Schema Information
#
# Table name: alchemy_pages
#
# id :integer not null, primary key
-# name :string(255)
-# urlname :string(255)
-# title :string(255)
-# language_code :string(255)
+# name :string
+# urlname :string
+# title :string
+# language_code :string
# language_root :boolean
-# page_layout :string(255)
+# page_layout :string
# meta_keywords :text
# meta_description :text
# lft :integer
# rgt :integer
# parent_id :integer
# depth :integer
# visible :boolean default(FALSE)
# public :boolean default(FALSE)
-# locked :boolean default(FALSE)
+# locked_at :datetime
# locked_by :integer
# restricted :boolean default(FALSE)
# robot_index :boolean default(TRUE)
# robot_follow :boolean default(TRUE)
# sitemap :boolean default(TRUE)
@@ -29,10 +29,12 @@
# creator_id :integer
# updater_id :integer
# language_id :integer
# cached_tag_list :text
# published_at :datetime
+# public_on :datetime
+# public_until :datetime
#
module Alchemy
class Page < ActiveRecord::Base
include Alchemy::Hints
@@ -41,21 +43,36 @@
DEFAULT_ATTRIBUTES_FOR_COPY = {
do_not_autogenerate: true,
do_not_sweep: true,
visible: false,
- public: false,
- locked: 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)
+
+ 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,
+ :public_on,
+ :public_until,
:restricted,
:robot_index,
:robot_follow,
:sitemap,
:tag_list,
@@ -71,10 +88,11 @@
stampable stamper_class_name: Alchemy.user_class_name
belongs_to :language
has_one :site, through: :language
+ has_many :site_languages, through: :site, source: :languages
has_many :folded_pages
has_many :legacy_urls, class_name: 'Alchemy::LegacyPageUrl'
validates_presence_of :language, on: :create, unless: :root
validates_presence_of :page_layout, unless: :systempage?
@@ -82,25 +100,45 @@
validates_presence_of :parent_id, if: proc { Page.count > 1 }
attr_accessor :do_not_sweep
attr_accessor :do_not_validate_language
- before_save :set_language_code, if: -> { language.present? }, unless: :systempage?
- before_save :set_restrictions_to_child_pages, if: :restricted_changed?, unless: :systempage?
- before_save :inherit_restricted_status, if: -> { parent && parent.restricted? }, unless: :systempage?
- before_save :update_published_at, if: -> { public && read_attribute(:published_at).nil? }, unless: :systempage?
- before_create :set_language_from_parent_or_default, if: -> { language_id.blank? }, unless: :systempage?
- after_update :create_legacy_url, if: :urlname_changed?, unless: :redirects_to_external?
+ before_save :set_language_code,
+ if: -> { language.present? },
+ unless: :systempage?
+ before_save :set_restrictions_to_child_pages,
+ if: :restricted_changed?,
+ unless: :systempage?
+
+ before_save :inherit_restricted_status,
+ if: -> { parent && parent.restricted? },
+ unless: :systempage?
+
+ before_save :set_published_at,
+ if: -> { public? && published_at.nil? },
+ unless: :systempage?
+
+ before_create :set_language_from_parent_or_default,
+ if: -> { language_id.blank? },
+ unless: :systempage?
+
+ after_update :create_legacy_url,
+ if: :urlname_changed?,
+ unless: :redirects_to_external?
+
# Concerns
include Alchemy::Page::PageScopes
include Alchemy::Page::PageNatures
include Alchemy::Page::PageNaming
include Alchemy::Page::PageUsers
include Alchemy::Page::PageCells
include Alchemy::Page::PageElements
+ # site_name accessor
+ delegate :name, to: :site, prefix: true, allow_nil: true
+
# Class methods
#
class << self
alias_method :rootpage, :root
@@ -279,20 +317,20 @@
def next(options = {})
next_or_previous('>', options)
end
alias_method :next_page, :next
- # Locks the page to given user without updating the timestamps
+ # Locks the page to given user
#
def lock_to!(user)
- update_columns(locked: true, locked_by: user.id)
+ update_columns(locked_at: Time.current, locked_by: user.id)
end
# Unlocks the page without updating the timestamps
#
def unlock!
- if update_columns(locked: false, locked_by: nil)
+ if update_columns(locked_at: nil, locked_by: nil)
Page.current_preview = nil
end
end
def fold!(user_id, status)
@@ -333,16 +371,22 @@
end
end
# Publishes the page.
#
- # Sets +public+ to true and the +published_at+ value to current time.
+ # 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+.
#
def publish!
- update_columns(published_at: Time.current, public: true)
+ current_time = Time.current
+ update_columns(
+ published_at: current_time,
+ public_on: current_time,
+ public_until: nil
+ )
end
# 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
@@ -378,14 +422,13 @@
options = {
restricted: false,
public: true
}.update(options)
- self_and_siblings
- .where(["#{self.class.table_name}.lft #{dir} ?", lft])
- .where(public: options[:public])
- .where(restricted: options[:restricted])
+ pages = self_and_siblings.where(["#{Page.table_name}.lft #{dir} ?", lft])
+ pages = options[:public] ? pages.published : pages.not_public
+ pages.where(restricted: options[:restricted])
.reorder(dir == '>' ? 'lft' : 'lft DESC')
.limit(1).first
end
def set_language_from_parent_or_default
@@ -400,10 +443,10 @@
# Stores the old urlname in a LegacyPageUrl
def create_legacy_url
legacy_urls.find_or_create_by(urlname: urlname_was)
end
- def update_published_at
+ def set_published_at
self.published_at = Time.current
end
end
end