app/models/landable/page.rb in landable-1.13.1 vs app/models/landable/page.rb in landable-1.13.2

- old
+ new

@@ -11,14 +11,14 @@ include Landable::HasTemplates include Landable::Engine.routes.url_helpers include Landable::TableName include Landable::Librarian - validates_presence_of :path, :status_code - validates_presence_of :redirect_url, if: -> page { page.redirect? } + validates_presence_of :path, :status_code + validates_presence_of :redirect_url, if: -> page { page.redirect? } - validates_inclusion_of :status_code, in: [200, 301, 302, 410] + validates_inclusion_of :status_code, in: [200, 301, 302, 410] validates_with PathValidator, fields: [:path] validates_uniqueness_of :path validates :path, presence: true @@ -33,25 +33,27 @@ belongs_to :theme, class_name: 'Landable::Theme', inverse_of: :pages, counter_cache: true belongs_to :published_revision, class_name: 'Landable::PageRevision' belongs_to :category, class_name: 'Landable::Category' belongs_to :updated_by_author, class_name: 'Landable::Author' belongs_to :hero_asset, class_name: 'Landable::Asset' - has_many :revisions, class_name: 'Landable::PageRevision' - has_many :screenshots, class_name: 'Landable::Screenshot', as: :screenshotable - has_many :audits, class_name: 'Landable::Audit', as: :auditable + has_many :revisions, class_name: 'Landable::PageRevision' + has_many :screenshots, class_name: 'Landable::Screenshot', as: :screenshotable + has_many :audits, class_name: 'Landable::Audit', as: :auditable delegate :republish!, to: :published_revision - scope :imported, -> { where("imported_at IS NOT NULL") } - scope :sitemappable, -> { where("COALESCE(meta_tags -> 'robots' NOT LIKE '%noindex%', TRUE)") - .where("published_revision_id is NOT NULL") - .where(status_code: 200)} - scope :published, -> { where("published_revision_id is NOT NULL") } + scope :imported, -> { where('imported_at IS NOT NULL') } + scope :sitemappable, lambda { + where("COALESCE(meta_tags -> 'robots' NOT LIKE '%noindex%', TRUE)") + .where('published_revision_id is NOT NULL') + .where(status_code: 200) + } + scope :published, -> { where('published_revision_id is NOT NULL') } before_validation :downcase_path! - before_save -> page { + before_save lambda { |page| page.lock_version ||= 0 page.is_publishable = true unless page.published_revision_id_changed? } class << self @@ -66,13 +68,13 @@ def by_path!(path) where(path: path).first! end def with_fuzzy_path(path) - select("*, similarity(path, #{Page.sanitize path}) _sml"). - where('path LIKE ?', "%#{path}%"). - order('_sml DESC, path ASC') + select("*, similarity(path, #{Page.sanitize path}) _sml") + .where('path LIKE ?', "%#{path}%") + .order('_sml DESC, path ASC') end def example(attrs) defaults = { title: 'Example page', @@ -80,29 +82,28 @@ } new defaults.merge(attrs) end - def generate_sitemap(options = {}) pages = Landable::Page.sitemappable - xml = Builder::XmlMarkup.new( :indent => 2 ) - xml.instruct! :xml, encoding: "UTF-8" - xml.urlset(xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9") do |xml| + xml = Builder::XmlMarkup.new(indent: 2) + xml.instruct! :xml, encoding: 'UTF-8' + xml.urlset(xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9') do |markup| pages.each do |page| next if options[:exclude_categories].to_a.include? page.category.try(:name) - xml.url do |p| + markup.url do |p| p.loc "#{options[:protocol]}://#{options[:host]}#{page.path}" p.lastmod page.updated_at.to_time.iso8601 p.changefreq 'weekly' p.priority '1' end end if options[:sitemap_additional_paths].present? options[:sitemap_additional_paths].each do |page| - xml.url do |p| + markup.url do |p| p.loc "#{options[:protocol]}://#{options[:host]}#{page}" p.changefreq 'weekly' p.priority '1' end end @@ -131,23 +132,23 @@ 'text/plain' end end def deactivate - self.update_attribute(:status_code, 410) - - publish!(author_id: updated_by_author.id, notes: "This page has been trashed") + update_attribute(:status_code, 410) + publish!(author_id: updated_by_author.id, notes: 'This page has been trashed') + super end def html? content_type == 'text/html' end def directory_after(prefix) - remainder = path.gsub(/^#{prefix}\/?/, '') + remainder = path.gsub(%r{^#{prefix}\/?}, '') segments = remainder.split('/', 2) if segments.length == 1 nil else segments.first @@ -158,29 +159,27 @@ status_code == 301 || status_code == 302 end def path=(name) # if not present, add a leading slash for a non-empty path - if name and not name.empty? - name = name.gsub(/^\/?(.*)/, '/\1') - end + name = name.gsub(%r{^\/?(.*)}, '/\1') if name && !name.empty? self[:path] = name end def hero_asset_name - self.hero_asset.try(:name) + hero_asset.try(:name) end def hero_asset_name=(name) @hero_asset_name = name asset = Asset.find_by_name(name) self.hero_asset_id = asset.try(:asset_id) end def hero_asset_url - self.hero_asset.try(:public_url) + hero_asset.try(:public_url) end def publish!(options) transaction do published_revision.unpublish! if published_revision @@ -214,43 +213,39 @@ def preview_url public_preview_page_url(self) end def forbid_changing_path - errors[:path] = "can not be changed!" if self.path_changed? + errors[:path] = 'can not be changed!' if self.path_changed? end def body_strip_search - begin - RenderService.call(self) - rescue ::Liquid::Error => error - errors[:body] = 'contains a Liquid syntax error' - rescue StandardError => error - errors[:body] = 'had a problem: ' + error.message - end + RenderService.call(self) + rescue ::Liquid::Error + errors[:body] = 'contains a Liquid syntax error' + rescue StandardError => error + errors[:body] = 'had a problem: ' + error.message end def page_name_byte_size - if page_name.present? && page_name.bytesize > 100 - errors[:page_name] = 'Invalid PageName, bytesize is too big!' - end + return unless page_name.present? && page_name.bytesize > 100 + errors[:page_name] = 'Invalid PageName, bytesize is too big!' end def hero_asset_existence return true if @hero_asset_name.blank? - unless Asset.find_by_name(@hero_asset_name) - errors[:hero_asset_name] = "System can't find an asset with this name" - end + return if Asset.find_by_name(@hero_asset_name) + errors[:hero_asset_name] = "System can't find an asset with this name" end def to_liquid { - "title" => title, - "url" => path, - "hero_asset" => hero_asset ? true : false, - "hero_asset_url" => hero_asset_url, - "abstract" => abstract + 'title' => title, + 'url' => path, + 'hero_asset' => hero_asset ? true : false, + 'hero_asset_url' => hero_asset_url, + 'abstract' => abstract } end module Errors extend ActiveSupport::Concern @@ -274,8 +269,7 @@ end end end include Errors - end end