Sha256: b4a3315a774dafe59c79b023d53f77caf796d5d2a369b38d1dd53ff862373720

Contents?: true

Size: 1.58 KB

Versions: 41

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module Decidim
  # A page is used to add static content to the website, it can be useful so
  # organization can add their own terms and conditions, privacy policy or
  # other pages they might need from an admin panel.
  #
  # Pages with a default slug cannot be destroyed and its slug cannot be
  # modified.
  class StaticPage < ApplicationRecord
    belongs_to :organization, foreign_key: "decidim_organization_id", class_name: "Decidim::Organization", inverse_of: :static_pages

    validates :slug, presence: true, uniqueness: { scope: :organization }
    validates :slug, format: { with: /\A[a-z0-9-]+/ }

    # These pages will be created by default when registering an organization
    # and cannot be deleted.
    DEFAULT_PAGES = %w(faq terms-and-conditions accessibility).freeze

    before_destroy :can_be_destroyed?
    before_update :can_update_slug?

    # Whether this is slug of a default page or not.
    #
    # slug - The String with the value of the slug.
    #
    # Returns Boolean.
    def self.default?(slug)
      DEFAULT_PAGES.include?(slug)
    end

    # Whether this is page is a default one or not.
    #
    # Returns Boolean.
    def default?
      self.class.default?(slug)
    end

    # Customize to_param so when we want to create a link to a page we use the
    # slug instead of the id.
    #
    # Returns a String.
    def to_param
      slug
    end

    private

    def can_be_destroyed?
      throw(:abort) if default?
    end

    def can_update_slug?
      throw(:abort) if slug_changed? && self.class.default?(slug_was)
    end
  end
end

Version data entries

41 entries across 41 versions & 2 rubygems

Version Path
decidim-core-0.9.3 app/models/decidim/static_page.rb
decidim-core-0.9.2 app/models/decidim/static_page.rb
decidim-core-0.9.1 app/models/decidim/static_page.rb
decidim-core-0.9.0 app/models/decidim/static_page.rb
decidim-core-0.8.4 app/models/decidim/static_page.rb
decidim-core-0.8.3 app/models/decidim/static_page.rb
decidim-core-0.8.2 app/models/decidim/static_page.rb
decidim-core-0.8.1 app/models/decidim/static_page.rb
decidim-core-0.8.0 app/models/decidim/static_page.rb
decidim-core-0.7.4 app/models/decidim/static_page.rb
decidim-core-0.7.3 app/models/decidim/static_page.rb
decidim-core-0.7.2 app/models/decidim/static_page.rb
decidim-core-0.7.1 app/models/decidim/static_page.rb
decidim-core-0.7.0 app/models/decidim/static_page.rb
decidim-core-0.6.8 app/models/decidim/static_page.rb
decidim-0.6.8 decidim-core/app/models/decidim/static_page.rb
decidim-core-0.6.7 app/models/decidim/static_page.rb
decidim-0.6.7 decidim-core/app/models/decidim/static_page.rb
decidim-core-0.6.6 app/models/decidim/static_page.rb
decidim-0.6.6 decidim-core/app/models/decidim/static_page.rb