app/helpers/panda/cms/admin/posts_helper.rb in panda-cms-0.7.0 vs app/helpers/panda/cms/admin/posts_helper.rb in panda-cms-0.7.2

- old
+ new

@@ -1,47 +1,38 @@ +# frozen_string_literal: true + module Panda module CMS module Admin module PostsHelper def editor_content_for(post, preserved_content = nil) - Rails.logger.debug "Editor content for post: #{post.inspect}" - Rails.logger.debug "Preserved content: #{preserved_content.inspect}" - Rails.logger.debug "Post content: #{post.content.inspect}" + content = preserved_content || post.content - content = if preserved_content.present? - # If preserved_content is a string (JSON), parse it - begin - preserved_content.is_a?(String) ? JSON.parse(preserved_content) : preserved_content - rescue JSON::ParserError => e - Rails.logger.error "Failed to parse preserved content: #{e.message}" - post.content - end + # 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 - post.content + {blocks: []}.to_json end - Rails.logger.debug "Using content: #{content.inspect}" + # Base64 encode the JSON content + Base64.strict_encode64(json_content) + end - # Ensure we have a valid editor content structure - content = if content.blank? || !content.is_a?(Hash) || !content.key?("blocks") - Rails.logger.debug "Creating new editor content structure" - { - time: Time.now.to_i * 1000, - blocks: [], - version: "2.28.2" - } - else - # Ensure we have all required fields - Rails.logger.debug "Ensuring required fields are present" - content["time"] ||= Time.now.to_i * 1000 - content["version"] ||= "2.28.2" - content["blocks"] ||= [] - content - end + private - json = content.to_json - Rails.logger.debug "Returning JSON: #{json}" - json + def valid_json?(string) + JSON.parse(string) + true + rescue JSON::ParserError + false end end end end end