Sha256: 9c39731bf28d426ac98dc7cfde1018bf38dd7594bd7aa3f34cc604ec0396abf3

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module Decidim
  module Plans
    # Exposes Plan versions so users can see how a Plan
    # has been updated through time.
    class VersionsController < Decidim::Plans::ApplicationController
      helper Decidim::Plans::TraceabilityHelper
      helper_method :current_version, :item_versions, :associated_versions,
                    :content_versions, :item

      private

      def item
        @item ||= Plan.where(component: current_component).find(params[:plan_id])
      end

      def current_version
        return nil if params[:id].to_i < 1
        @current_version ||= item.versions[params[:id].to_i - 1]
      end

      def item_versions
        # There may be multiple updates on the item during the same transaction
        Decidim::Plans::PaperTrail::Version.where(
          transaction_id: current_version.transaction_id,
          item_type: "Decidim::Plans::Plan"
        ).order(:created_at)
      end

      def associated_versions
        @associated_versions ||= Decidim::Plans::PaperTrail::Version.where(
          transaction_id: current_version.transaction_id
        ).where.not(
          item_type: ["Decidim::Plans::Plan", "Decidim::Plans::Content"]
        )
      end

      def content_versions
        @content_versions ||= item.sections.map do |section|
          content = item.contents.find_by(section: section)
          next unless content

          content.versions.find_by(
            transaction_id: current_version.transaction_id
          )
        end.compact
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
decidim-plans-0.16.0 app/controllers/decidim/plans/versions_controller.rb
decidim-plans-0.15.1 app/controllers/decidim/plans/versions_controller.rb
decidim-plans-0.15.0 app/controllers/decidim/plans/versions_controller.rb