Class: Panda::CMS::BulkEditor

Inherits:
Object
  • Object
show all
Defined in:
lib/panda/cms/bulk_editor.rb

Overview

Bulk editor for site content in JSON format

IMPORTANT: When adding new fields to Page, Post, or Menu models via migrations:

  1. Update the ‘extract_current_data` method to export the new fields

  2. Update the ‘import` method to import the new fields

  3. Run the spec at spec/lib/panda/cms/bulk_editor_spec.rb to verify completeness

  4. The spec will fail if database columns are not being exported

This ensures content can be properly exported and imported between environments.

Class Method Summary collapse

Class Method Details

.exportString

Export all site content to a JSON string



24
25
26
27
# File 'lib/panda/cms/bulk_editor.rb', line 24

def self.export
  data = extract_current_data
  JSON.pretty_generate(data)
end

.import(json_data) ⇒ Hash

Import site content from a JSON string



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/panda/cms/bulk_editor.rb', line 35

def self.import(json_data)
  # See if we can parse the JSON
  new_data = JSON.parse(json_data)
  current_data = extract_current_data

  debug = {
    success: [],
    error: [],
    warning: []
  }

  # Make sure templates are up to date
  Panda::CMS::Template.generate_missing_blocks

  # Run through the new data and compare it to the current data
  new_data["pages"].each do |path, page_data|
    if current_data["pages"][path].nil?
      begin
        page = Panda::CMS::Page.create!(
          path: path,
          title: page_data["title"],
          status: page_data["status"] || "active",
          page_type: page_data["page_type"] || "standard",
          template: Panda::CMS::Template.find_by(name: page_data["template"]),
          parent: Panda::CMS::Page.find_by(path: page_data["parent"]),
          # SEO fields
          seo_title: page_data["seo_title"],
          seo_description: page_data["seo_description"],
          seo_keywords: page_data["seo_keywords"],
          seo_index_mode: page_data["seo_index_mode"] || "visible",
          canonical_url: page_data["canonical_url"],
          # Open Graph fields
          og_type: page_data["og_type"] || "website",
          og_title: page_data["og_title"],
          og_description: page_data["og_description"]
        )
      rescue => e
        debug[:error] << "Failed to create page '#{path}': #{e.message}"
        next
      end

      if !page
        debug[:error] << "Unhandled: page '#{path}' does not exist in the current data and cannot be created"
        next
      else
        debug[:success] << "Created page '#{path}' with title '#{page_data["title"]}'"
      end
    else
      page = Panda::CMS::Page.find_by(path: path)

      # Check if template changed
      if page_data["template"] != current_data["pages"][path]["template"]
        # TODO: Handle page template changes
        debug[:error] << "Page '#{path}' template is '#{current_data["pages"][path]["template"]}' and cannot be changed to '#{page_data["template"]}' without manual intervention"
      else
        # Update page fields
        begin
          page.update!(
            title: page_data["title"],
            status: page_data["status"] || page.status,
            page_type: page_data["page_type"] || page.page_type,
            parent: Panda::CMS::Page.find_by(path: page_data["parent"]) || page.parent,
            # SEO fields
            seo_title: page_data["seo_title"],
            seo_description: page_data["seo_description"],
            seo_keywords: page_data["seo_keywords"],
            seo_index_mode: page_data["seo_index_mode"] || page.seo_index_mode,
            canonical_url: page_data["canonical_url"],
            # Open Graph fields
            og_type: page_data["og_type"] || page.og_type,
            og_title: page_data["og_title"],
            og_description: page_data["og_description"]
          )
          debug[:success] << "Updated page '#{path}'"
        rescue => e
          debug[:error] << "Failed to update page '#{path}': #{e.message}"
        end
      end
    end

    page_data["contents"]&.each do |key, block_data|
      content = block_data["content"]

      if current_data.dig("pages", path, "contents", key).nil?
        raise "Unknown page 1" if page.nil?

        block = Panda::CMS::Block.find_or_create_by(key: key, template: page.template) do |block_meta|
          block_meta.name = key.titleize
        end

        unless block
          debug[:error] << "Error creating block '#{key.titleize}' on page '#{page.title}'"
          next
        end

        block_content = Panda::CMS::BlockContent.find_or_create_by(block: block, page: page)
        # block_content.content = HTMLEntities.new.encode(content, :named)
        block_content.content = content

        begin
          block_content.save!

          if block_content.content != content
            debug[:error] << "Failed to save content for '#{block.name}' on page '#{page.title}'"
          else
            debug[:success] << "Created '#{block.name}' content on page '#{page.title}'"
          end
        rescue => e
          debug[:error] << "Failed to create '#{block.name}' content on page '#{page.title}': #{e.message}"
        end
      elsif content != current_data["pages"][path]["contents"][key]["content"]
        # Content has changed
        raise "Unknown page 2" if page.nil?

        block = Panda::CMS::Block.find_by(key: key, template: page.template)
        if Panda::CMS::BlockContent.find_by(page: page, block: block)&.update(content: content)
          debug[:success] << "Updated '#{key.titleize}' content on page '#{page.title}'"
        else
          debug[:error] << "Failed to update '#{key.titleize}' content on page '#{page.title}'"
        end
      end
    end
  end

  # Posts
  new_data["posts"]&.each do |post_data|
    slug = post_data["slug"]
    post = Panda::CMS::Post.find_by(slug: slug)

    # Find or create user by email
    user = if post_data["user_email"]
      Panda::Core::User.find_by(email: post_data["user_email"]) || Panda::Core::User.first
    else
      Panda::Core::User.first # Fallback to first user
    end

    author = if post_data["author_email"]
      Panda::Core::User.find_by(email: post_data["author_email"])
    end

    if post.nil?
      begin
        post = Panda::CMS::Post.create!(
          slug: slug,
          title: post_data["title"],
          status: post_data["status"] || "draft",
          published_at: post_data["published_at"],
          user: user,
          author: author,
          content: post_data["content"] || "",
          cached_content: post_data["cached_content"] || "",
          # SEO fields
          seo_title: post_data["seo_title"],
          seo_description: post_data["seo_description"],
          seo_keywords: post_data["seo_keywords"],
          seo_index_mode: post_data["seo_index_mode"] || "visible",
          canonical_url: post_data["canonical_url"],
          # Open Graph fields
          og_type: post_data["og_type"] || "article",
          og_title: post_data["og_title"],
          og_description: post_data["og_description"]
        )

        debug[:success] << "Created post '#{post.title}' (#{slug})"
      rescue => e
        debug[:error] << "Failed to create post '#{slug}': #{e.message}"
      end
    else
      # Update existing post
      begin
        post.update!(
          title: post_data["title"],
          status: post_data["status"] || post.status,
          published_at: post_data["published_at"] || post.published_at,
          author: author || post.author,
          content: post_data["content"] || post.content || "",
          cached_content: post_data["cached_content"] || post.cached_content || "",
          # SEO fields
          seo_title: post_data["seo_title"],
          seo_description: post_data["seo_description"],
          seo_keywords: post_data["seo_keywords"],
          seo_index_mode: post_data["seo_index_mode"] || post.seo_index_mode,
          canonical_url: post_data["canonical_url"],
          # Open Graph fields
          og_type: post_data["og_type"] || post.og_type,
          og_title: post_data["og_title"],
          og_description: post_data["og_description"]
        )

        debug[:success] << "Updated post '#{post.title}' (#{slug})"
      rescue => e
        debug[:error] << "Failed to update post '#{slug}': #{e.message}"
      end
    end
  end

  # Menus
  new_data["menus"]&.each do |menu_data|
    menu = Panda::CMS::Menu.find_by(name: menu_data["name"])

    if menu.nil?
      begin
        if menu_data["kind"] == "auto"
          start_page = Panda::CMS::Page.find_by(path: menu_data["start_page_path"])
          menu = Panda::CMS::Menu.create!(
            name: menu_data["name"],
            kind: "auto",
            start_page: start_page
          )
          debug[:success] << "Created auto menu '#{menu.name}'"
        else
          menu = Panda::CMS::Menu.create!(
            name: menu_data["name"],
            kind: "static"
          )

          # Create menu items
          menu_data["items"]&.each do |item_data|
            page = Panda::CMS::Page.find_by(path: item_data["page_path"]) if item_data["page_path"]
            menu.menu_items.create!(
              text: item_data["text"],
              page: page,
              external_url: item_data["external_url"]
            )
          end

          debug[:success] << "Created static menu '#{menu.name}' with #{menu_data["items"]&.length || 0} items"
        end
      rescue => e
        debug[:error] << "Failed to create menu '#{menu_data["name"]}': #{e.message}"
      end
    else
      # Update existing menu
      if menu.kind != menu_data["kind"]
        debug[:warning] << "Menu '#{menu.name}' kind mismatch (#{menu.kind} vs #{menu_data["kind"]}). Skipping update."
        next
      end

      if menu_data["kind"] == "auto"
        start_page = Panda::CMS::Page.find_by(path: menu_data["start_page_path"])
        if menu.start_page != start_page
          menu.update(start_page: start_page)
          debug[:success] << "Updated auto menu '#{menu.name}' start page"
        end
      elsif menu_data["kind"] == "static"
        # Update static menu items
        menu.menu_items.destroy_all
        menu_data["items"]&.each do |item_data|
          page = Panda::CMS::Page.find_by(path: item_data["page_path"]) if item_data["page_path"]
          menu.menu_items.create!(
            text: item_data["text"],
            page: page,
            external_url: item_data["external_url"]
          )
        end
        debug[:success] << "Updated static menu '#{menu.name}' with #{menu_data["items"]&.length || 0} items"
      end
    end
  end

  # Templates - skip as they are code-based

  debug
end