Sha256: 482ac1a2cc74a4332ab7c1cdf117486c787c214069e98d34c694f80f07c83af0

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

module CmPageBuilder::Rails
  class Page < ApplicationRecord
    belongs_to :container, polymorphic: true
    has_many :page_components, dependent: :destroy

    accepts_nested_attributes_for :page_components, allow_destroy: true

    def get_components
      self.page_components.with_attached_component_attachment.select(:id, :uuid, :component_type, :position, :content).map do |component|
        json_component = component.as_json.transform_keys! {|key| key.camelize(:lower)}
        attachment = component.component_attachment.attachment
        pp component.component_attachment
        pp attachment
        pp "======"
        json_component["id"] = component[:uuid]
        json_component["component_attachment"] = if attachment
           {
            filename: attachment.filename.to_s,
            url: attachment.service_url
          }
        end
        json_component
      end
    end

    def save_content(component_json)
      components = JSON.parse component_json
      deleted_components = self.page_components.where.not(
        uuid: components.map {|c| c["id"] } )
      deleted_components.delete_all
      components.each do |component|
        page_component = self.page_components.find_or_initialize_by(uuid: component["id"])
        signed_id = component.dig("component_attachment", "signed_id")
        component_data = {
          content: component["content"],
          position: component["position"],
          component_type: component["componentType"]
        }
        component_data[:component_attachment] = signed_id if signed_id
        page_component.update!(component_data)
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cm_page_builder-rails-0.1.3 app/models/cm_page_builder/rails/page.rb