Sha256: 7b0e73c0c20814bf421e3b6b6dc6061e419cba762ee8fddce015ae52369c4b57

Contents?: true

Size: 1.38 KB

Versions: 6

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    # A form object to create or update pages.
    class StaticPageForm < Form
      include TranslatableAttributes

      attribute :slug, String
      validates :title, translatable_presence: true
      translatable_attribute :title, String
      translatable_attribute :content, String
      attribute :changed_notably, Boolean
      attribute :allow_public_access, Boolean
      attribute :weight, Integer
      attribute :topic_id, Integer

      mimic :static_page

      validates :slug, presence: true
      validates :slug, format: { with: %r{\A[a-zA-Z]+[a-zA-Z0-9\-_/]+\z} }, allow_blank: true

      validate :slug, :slug_uniqueness

      alias organization current_organization

      def slug
        super.to_s.downcase
      end

      def topic
        @topic ||= StaticPageTopic.find_by(
          organization:,
          id: topic_id
        )
      end

      def topics
        @topics ||= StaticPageTopic.where(
          organization: current_organization
        )
      end

      def control_public_access?
        current_organization.force_users_to_authenticate_before_access_organization?
      end

      private

      def slug_uniqueness
        return unless organization
        return unless organization.static_pages.where(slug:).where.not(id:).any?

        errors.add(:slug, :taken)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-admin-0.29.1 app/forms/decidim/admin/static_page_form.rb
decidim-admin-0.29.0 app/forms/decidim/admin/static_page_form.rb
decidim-admin-0.29.0.rc4 app/forms/decidim/admin/static_page_form.rb
decidim-admin-0.29.0.rc3 app/forms/decidim/admin/static_page_form.rb
decidim-admin-0.29.0.rc2 app/forms/decidim/admin/static_page_form.rb
decidim-admin-0.29.0.rc1 app/forms/decidim/admin/static_page_form.rb