Sha256: cfc39d1087b6a8801797302ec093cb9ea5e71f56aacf518a5abf883b709ca48e

Contents?: true

Size: 1.3 KB

Versions: 3

Compression:

Stored size: 1.3 KB

Contents

module PageflowScrolled
  # @api private
  class ContentElement < Pageflow::ApplicationRecord
    include Pageflow::SerializedConfiguration
    include Pageflow::AutoGeneratedPermaId
    include Pageflow::NestedRevisionComponent

    belongs_to :section

    def self.all_for_revision(revision)
      joins(section: {chapter: {storyline: :revision}})
        .where(pageflow_scrolled_storylines: {revision_id: revision})
    end

    # @api private
    class Batch
      def initialize(section, items)
        @section = section
        @storyline = section.chapter.storyline
        @items = items
      end

      def save!
        ContentElement.transaction do
          @items.map.with_index { |item, index|
            if item[:_delete]
              @section.content_elements.delete(item[:id])
              nil
            else
              create_or_update(item, index)
            end
          }.compact
        end
      end

      private

      def create_or_update(item, index)
        attributes = {
          section_id: @section.id,
          position: index
        }.merge(item.slice(:type_name, :configuration))

        if item[:id]
          @storyline.content_elements.update(item[:id], attributes)
        else
          @section.content_elements.create(attributes)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pageflow-15.6.1 entry_types/scrolled/app/models/pageflow_scrolled/content_element.rb
pageflow-15.6.0 entry_types/scrolled/app/models/pageflow_scrolled/content_element.rb
pageflow-15.5.0 entry_types/scrolled/app/models/pageflow_scrolled/content_element.rb