1class Admin::InvalidationsController < Admin::AdminController
 
2  before_action :require_sysadmin
 
3  before_action :build_invalidation, only: [:new, :create]
 
4  before_action :find_invalidation, only: [:edit, :update, :destroy, :count, :cancel, :start]
 
5  before_action :find_invalidations, only: [:index]
 
6
 
7  def index
 
8    respond_to do |format|
 
 9      format.html
 
10    end
 
11  end
 
 
13  def new
 
14    respond_to do |format|
 
15      format.html
 
16    end
 
17  end
 
 
19  def create
 
20    if @invalidation.save
 
21      redirect_to admin_invalidations_url, notice: :invalidation_created
 
22    else
 
23      respond_to do |format|
 
24        format.html { render :new }
 
25      end
 
26    end
 
27  end
 
 
29  def edit
 
30    if @invalidation.pending?
 
31      respond_to do |format|
 
32        format.html
 
33      end
 
34    else
 
35      redirect_to_index_url notice: :invalidation_cant_be_edited
 
36    end
 
37  end
 
 
39  def update
 
40    if @invalidation.pending?
 
41      if @invalidation.update(invalidation_params)
 
42        redirect_to admin_invalidations_url, notice: :invalidation_updated
 
43      else
 
44        respond_to do |format|
 
45          format.html { render :edit }
 
46        end
 
47      end
 
48    else
 
49      redirect_to_index_url notice: :invalidation_cant_be_edited
 
50    end
 
51  end
 
 
53  def destroy
 
54    if @invalidation.started?
 
55      redirect_to_index_url notice: :invalidation_cant_be_removed
 
56    else
 
57      if @invalidation.destroy
 
58        redirect_to_index_url notice: :invalidation_removed
 
59      else
 
60        redirect_to_index_url alert: :invalidation_not_removed
 
61      end
 
62    end
 
63  end
 
 
65  def cancel
 
66    if @invalidation.completed?
 
67      redirect_to_index_url notice: :invalidation_cant_be_cancelled
 
68    else
 
69      if @invalidation.cancel!
 
70        redirect_to_index_url notice: :invalidation_cancelled
 
71      else
 
72        redirect_to_index_url alert: :invalidation_not_cancelled
 
73      end
 
74    end
 
75  end
 
 
77  def count
 
78    if @invalidation.pending?
 
79      if @invalidation.count!
 
80        redirect_to_index_url notice: [:invalidation_counted, summary: @invalidation.summary.inspect]
 
81      else
 
82        redirect_to_index_url alert: :invalidation_not_counted
 
83      end
 
84    else
 
85      redirect_to_index_url notice: :invalidation_cant_be_counted
 
86    end
 
87  end
 
 
89  def start
 
90    if @invalidation.pending?
 
91      if @invalidation.start!
 
92        redirect_to_index_url notice: [:invalidation_started, summary: @invalidation.summary.inspect]
 
93      else
 
94        redirect_to_index_url alert: :invalidation_not_started
 
95      end
 
96    else
 
97      redirect_to_index_url notice: :invalidation_cant_be_started
 
98    end
 
 99  end
 
 
101  private
 
 
103  def invalidation_params
 
104    if params.key?(:invalidation)
 
105      params.require(:invalidation).permit(*invalidation_attributes)
 
106    else
 
107      {}
 
108    end
 
109  end
 
 
111  def invalidation_attributes
 
112    %i[summary details] + Invalidation::CONDITIONS
 
113  end
 
 
115  def build_invalidation
 
116    @invalidation = Invalidation.new(invalidation_params)
 
117  end
 
 
119  def find_invalidation
 
120    @invalidation = Invalidation.find(params[:id])
 
121  end
 
 
123  def find_invalidations
 
124    @invalidations = Invalidation.search(params)
 
125  end
 
 
127  def index_url
 
128    admin_invalidations_url(params.slice(:state, :q))
 
129  end
 
 
131  def redirect_to_index_url(options = {})
 
132    redirect_to index_url, options
 
133  end
 
134end