Sha256: 26c5e978d50293bffa16218120ef46abf9d53ade73037889d55c60b3c3da3e80

Contents?: true

Size: 1022 Bytes

Versions: 5

Compression:

Stored size: 1022 Bytes

Contents

# frozen_string_literal: true

module Katalyst
  module Content
    # STI base class for content items
    class Item < ApplicationRecord
      def self.config
        Katalyst::Content.config
      end

      belongs_to :container, polymorphic: true

      validates :heading, presence: true
      validates :background, presence: true, inclusion: { in: config.backgrounds }, if: :validate_background?

      after_initialize :initialize_tree

      attr_accessor :parent, :children, :index, :depth

      def self.permitted_params
        %i[
          container_type
          container_id
          type
          heading
          show_heading
          background
          visible
        ]
      end

      def to_plain_text
        heading if show_heading? && visible?
      end

      def layout?
        is_a? Layout
      end

      private

      def initialize_tree
        self.parent   ||= nil
        self.children ||= []
      end

      def validate_background?
        true
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
katalyst-content-1.0.2 app/models/katalyst/content/item.rb
katalyst-content-1.0.1 app/models/katalyst/content/item.rb
katalyst-content-1.0.0 app/models/katalyst/content/item.rb
katalyst-content-0.2.2 app/models/katalyst/content/item.rb
katalyst-content-0.2.1 app/models/katalyst/content/item.rb