Sha256: 4f73fc8e90b98dbb44481fc2487076f3dcea7684ffdd2d4b1fea932f63a2ecb9

Contents?: true

Size: 1.13 KB

Versions: 8

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module Katalyst
  module Content
    class Table < Item
      has_rich_text :content

      attribute :heading_rows, :integer
      attribute :heading_columns, :integer

      validates :content, presence: true

      default_scope { with_rich_text_content }

      after_initialize :set_defaults

      def initialize_copy(source)
        super

        content.body = source.content&.body if source.content.is_a?(ActionText::RichText)
      end

      def self.permitted_params
        super + %i[content heading_rows heading_columns]
      end

      def valid?(context = nil)
        super
      end

      def to_plain_text
        content.to_plain_text if visible?
      end

      def content=(value)
        Tables::Importer.call(self, value)

        set_defaults

        content
      end

      private

      def set_defaults
        super

        if content.present? && (fragment = content.body.fragment)
          self.heading_rows    ||= fragment.find_all("thead > tr").count
          self.heading_columns ||= fragment.find_all("tbody > tr:first-child > th").count
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
katalyst-content-2.7.0 app/models/katalyst/content/table.rb
katalyst-content-2.6.2 app/models/katalyst/content/table.rb
katalyst-content-2.6.1 app/models/katalyst/content/table.rb
katalyst-content-2.6.0 app/models/katalyst/content/table.rb
katalyst-content-2.5.1 app/models/katalyst/content/table.rb
katalyst-content-2.5.0 app/models/katalyst/content/table.rb
katalyst-content-2.4.2 app/models/katalyst/content/table.rb
katalyst-content-2.4.1 app/models/katalyst/content/table.rb