Sha256: ae93687a33e4094cd85b51b4c2563384708c17550b9bf4c7ed6c8af35dedeb3e

Contents?: true

Size: 953 Bytes

Versions: 2

Compression:

Stored size: 953 Bytes

Contents

require "json"

module Panda::CMS::EditorJsContent
  extend ActiveSupport::Concern

  included do
    include ActiveModel::Validations
    include ActiveModel::Callbacks

    before_save :generate_cached_content
  end

  def generate_cached_content
    if content.is_a?(String)
      begin
        parsed_content = JSON.parse(content)
        self.cached_content = if parsed_content.is_a?(Hash) && parsed_content["blocks"].present?
          Panda::CMS::EditorJs::Renderer.new(parsed_content).render
        else
          content
        end
      rescue JSON::ParserError
        # If it's not JSON, treat it as plain text
        self.cached_content = content
      end
    elsif content.is_a?(Hash) && content["blocks"].present?
      # Process EditorJS content
      self.cached_content = Panda::CMS::EditorJs::Renderer.new(content).render
    else
      # For any other case, store as is
      self.cached_content = content.to_s
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
panda-cms-0.7.3 lib/panda/cms/editor_js_content.rb
panda-cms-0.7.2 lib/panda/cms/editor_js_content.rb