aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/compliance_controls_controller.rb
blob: ac6c54a3c5fe111fb9e660eeb18aad7eb46b2020 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class ComplianceControlsController < ChouetteController
  include PolicyChecker
  defaults resource_class: ComplianceControl
  belongs_to :compliance_control_set
  actions :all, :except => [:index]

  def select_type
    @sti_subclasses ||= ComplianceControl.subclasses_to_hash 
  end

  def show
    show! do
      @compliance_control = @compliance_control.decorate
    end
  end

  def new
    if params[:sti_class].blank?
      flash[:notice] = I18n.t("compliance_controls.errors.mandatory_control_type")
      redirect_to(action: :select_type)
    end
    new!
  end

  def create
    create! do |success, failure|
      success.html { redirect_to compliance_control_set_path(parent) }
      failure.html { render( :action => 'new' ) }
    end
  end

  def update
    update! do |success, failure|
      success.html { redirect_to compliance_control_set_path(parent) }
      failure.html { render( :action => 'edit' ) }
    end
  end

  protected

  alias_method :compliance_control_set, :parent
  alias_method :compliance_control, :resource

  def build_resource
    get_resource_ivar || set_resource_ivar(compliance_control_class.send(:new, *resource_params))
  end

  private

  def compliance_control_class
    (params[:sti_class] || params[:compliance_control][:type]).constantize
  end

  def dynamic_attributes_params
    compliance_control_class.dynamic_attributes
  end

  def compliance_control_params
    base = [:name, :code, :origin_code, :criticity, :comment, :control_attributes, :type, :compliance_control_block_id, :compliance_control_set_id]
    permitted = base + dynamic_attributes_params
    params.require(:compliance_control).permit(permitted)
  end
end