Sha256: 608ce02aeefd5243bbd731e46dab57eb5a311b2700cddb5013e264523ac75a19

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

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

    belongs_to :section

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

    def copy_to(section)
      section.content_elements << dup
    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

2 entries across 2 versions & 1 rubygems

Version Path
pageflow-15.4.0 entry_types/scrolled/app/models/pageflow_scrolled/content_element.rb
pageflow-15.3.0 entry_types/scrolled/app/models/pageflow_scrolled/content_element.rb