Sha256: 4dc6d36b2838609d61c94e254c4b5002f3420c9c1d3f3da3b019170b0486eef9

Contents?: true

Size: 1.99 KB

Versions: 24

Compression:

Stored size: 1.99 KB

Contents

module Effective
  class CpdStatementsController < ApplicationController
    before_action(:authenticate_user!) if defined?(Devise)

    include Effective::WizardController
    include EffectiveCpdHelper

    resource_scope do
      cycle = Effective::CpdCycle.find(params[:cpd_cycle_id])
      EffectiveCpd.CpdStatement.deep.where(cpd_cycle: cycle, user: current_user)
    end

    after_save(if: -> { step == :start }) do
      CpdScorer.new(user: resource.user).score!
    end

    # Enforce one statement per user per cycle. Redirect them to an existing statement for this cycle.
    before_action(only: [:new, :show]) do
      existing = resource_scope.where.not(id: resource).first

      if existing.present?
        redirect_to effective_cpd.cpd_cycle_cpd_statement_build_path(existing.cpd_cycle, existing, existing.next_step)
      end
    end

    # Enforce cycle availability
    before_action(only: [:show, :update]) do
      cycle = resource.cpd_cycle

      cpd_cycle_label = EffectiveResources.et(Effective::CpdCycle)

      unless cycle.available?
        flash[:danger] = begin
          if cycle.ended?
            "This #{cpd_cycle_label} has ended"
          elsif !cycle.started?
            "This #{cpd_cycle_label} has not yet started"
          else
            "This #{cpd_cycle_label} is unavailable"
          end
        end

        redirect_to(root_path)
      end
    end

    def permitted_params
      model = (params.key?(:effective_cpd_statement) ? :effective_cpd_statement : :cpd_statement)
      case step
      when :start
        params.require(model).permit(:current_step)
      when :activities
        params.require(model).permit(:current_step)
      when :agreements
        params.require(model).permit(:current_step, :confirm_read, :confirm_factual, files: [])
      when :submit
        params.require(model).permit(:current_step, :confirm_readonly)
      when :complete
        raise('unexpected post to complete')
      else
        raise('unexpected step')
      end
    end

  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
effective_cpd-1.7.2 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.7.1 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.7.0 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.6.5 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.6.4 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.6.3 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.6.2 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.6.1 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.6.0 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.5.1 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.5.0 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.4.8 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.4.7 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.4.6 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.4.5 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.4.4 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.4.3 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.4.2 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.4.1 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.4.0 app/controllers/effective/cpd_statements_controller.rb