Sha256: 07d8751f4f27cf10831b52aca0bd878a5f412acd7e3a19bed0f6ef44cc605dec

Contents?: true

Size: 972 Bytes

Versions: 2

Compression:

Stored size: 972 Bytes

Contents

# frozen_string_literal: true

module Panda
  module CMS
    module Admin
      module PostsHelper
        def editor_content_for(post, preserved_content = nil)
          content = preserved_content || post.content

          # Return empty structure if no content
          json_content = if content.blank?
            {blocks: []}.to_json
          # If content is already JSON string, use it
          elsif content.is_a?(String) && valid_json?(content)
            content
          # If it's a hash, convert to JSON
          elsif content.is_a?(Hash)
            content.to_json
          # Default to empty structure
          else
            {blocks: []}.to_json
          end

          # Base64 encode the JSON content
          Base64.strict_encode64(json_content)
        end

        private

        def valid_json?(string)
          JSON.parse(string)
          true
        rescue JSON::ParserError
          false
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
panda-cms-0.7.3 app/helpers/panda/cms/admin/posts_helper.rb
panda-cms-0.7.2 app/helpers/panda/cms/admin/posts_helper.rb