Sha256: 78c3083408b3f205d0415d59fdeb059b3da7cbcd5aeed45d743dcfeb15ac5bf3

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

module Georgia
  class CloneRevision

    def initialize revision, status: nil, revised_by_id: nil
      raise 'Revision must be persisted to be copied' if revision.new_record?
      @revision = revision
      @status = status || @revision.status
      @revised_by_id = revised_by_id || @revision.revised_by_id
    end

    # Returns a copy of the revision in 'revision' status
    def call
      clone_revision
    end

    def self.create revision, attributes={}
      clone = new(revision, attributes).call
      clone.save!
      clone
    end

    private

    def clone_revision
      @duplicate_revision = Georgia::Revision.new(template: @revision.template, status: @status, revisionable: @revision.revisionable, revised_by_id: @revised_by_id) do |r|
        r.contents = clone_contents
        r.ui_associations = clone_ui_associations
        r.slides = clone_slides
      end
      @duplicate_revision
    end

    def clone_contents
      @revision.contents.map{|c| clone_content(c)}
    end

    def clone_ui_associations
      @revision.ui_associations.map{|ui_assoc| clone_ui_association(ui_assoc)}
    end

    def clone_slides
      @revision.slides.map do |slide|
        new_slide = Georgia::Slide.new(page_id: slide.page_id)
        slide.contents.each do |content|
          new_slide.contents << clone_content(content)
        end
        new_slide
      end
    end

    def clone_content(content)
      Georgia::Content.new(
        locale: content.locale,
        text: content.text,
        title: content.title,
        excerpt: content.excerpt,
        image_id: content.image_id,
        keyword_list: content.keyword_list
      )
    end

    def clone_ui_association(ui_assoc)
      Georgia::UiAssociation.new(
        page_id: ui_assoc.page_id,
        widget_id: ui_assoc.widget_id,
        ui_section_id: ui_assoc.ui_section_id
      )
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
georgia-0.8.0 app/services/georgia/clone_revision.rb