Sha256: f640268c1f7933f125797785eddb3e56d750dd48ab5d1bf2eb8be46a43d7e54c

Contents?: true

Size: 1.92 KB

Versions: 23

Compression:

Stored size: 1.92 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

      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

23 entries across 23 versions & 1 rubygems

Version Path
effective_cpd-1.3.3 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.3.2 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.3.1 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.3.0 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.2.4 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.2.3 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.2.2 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.2.1 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.2.0 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.1.3 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.1.2 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.1.1 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.1.0 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-1.0.0 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-0.6.8 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-0.6.7 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-0.6.6 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-0.6.5 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-0.6.4 app/controllers/effective/cpd_statements_controller.rb
effective_cpd-0.6.3 app/controllers/effective/cpd_statements_controller.rb