Sha256: 687de3c6888c79262eb3878137796de89c01ee63087b0680c65050a0e7a0b03f

Contents?: true

Size: 1.54 KB

Versions: 12

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module Maglev
  class PersistPage
    include Injectable

    dependency :fetch_theme

    argument :page
    argument :page_attributes
    argument :site
    argument :site_attributes, default: nil
    argument :theme, default: nil

    def call
      ActiveRecord::Base.transaction do
        persist_page!
        persist_site!
        persist_style!
        page
      end
    end

    private

    def persist_page!
      page.attributes = page_attributes
      page.prepare_sections
      page.save!
    end

    def persist_site!
      return unless can_persist_site?

      site.attributes = site_attributes_with_consistent_sections
      site.prepare_sections
      site.save!
    end

    def persist_style!
      return if site_attributes&.dig(:style).blank?

      site.style = site_attributes[:style]
      site.save!
    end

    def theme
      @theme ||= fetch_theme.call
    end

    # unlike the pages, we don't want to erase
    # the other global sections of a site if they're missing
    # from the site_attributes
    def site_attributes_with_consistent_sections
      attributes = site_attributes.dup

      site.sections&.each do |section|
        next if site_section_types.include?(section['type'])

        attributes[:sections].push(section)
      end

      attributes
    end

    def site_section_types
      @site_section_types ||= site_attributes[:sections].map { |section| section[:type] }
    end

    def can_persist_site?
      site_attributes.present? && site_attributes[:sections].present?
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
maglevcms-1.1.7 app/services/maglev/persist_page.rb
maglevcms-1.1.6 app/services/maglev/persist_page.rb
maglevcms-1.1.5 app/services/maglev/persist_page.rb
maglevcms-1.1.4 app/services/maglev/persist_page.rb
maglevcms-1.1.3 app/services/maglev/persist_page.rb
maglevcms-1.1.2 app/services/maglev/persist_page.rb
maglevcms-1.1.1 app/services/maglev/persist_page.rb
maglevcms-1.1.0 app/services/maglev/persist_page.rb
maglevcms-1.0.0 app/services/maglev/persist_page.rb
maglevcms-1.0.0.rc3 app/services/maglev/persist_page.rb
maglevcms-1.0.0.rc2 app/services/maglev/persist_page.rb
maglevcms-1.0.0.rc1 app/services/maglev/persist_page.rb