Commit eaaf4f2f517b1c6af3082d221eaee86d2b4ac888

Authored by Daniel
Committed by Rafael Manzo
1 parent 6f4bab0e

Add proper Reading Group fetching in Metric Configurations controllers

Also improve error reporting functions
app/controllers/base_metric_configurations_controller.rb
@@ -2,16 +2,16 @@ class BaseMetricConfigurationsController < ApplicationController @@ -2,16 +2,16 @@ class BaseMetricConfigurationsController < ApplicationController
2 include OwnershipAuthentication 2 include OwnershipAuthentication
3 include MetricConfigurationsConcern 3 include MetricConfigurationsConcern
4 4
5 - before_action :authenticate_user!, except: [:show, :index] 5 + before_action :authenticate_user!, except: [:show]
6 before_action :metric_configuration_owner?, only: [:edit, :update, :destroy] 6 before_action :metric_configuration_owner?, only: [:edit, :update, :destroy]
7 before_action :kalibro_configuration_owner?, only: [:new, :create, :choose_metric] 7 before_action :kalibro_configuration_owner?, only: [:new, :create, :choose_metric]
8 before_action :set_kalibro_configuration! 8 before_action :set_kalibro_configuration!
9 before_action :find_metric_configuration!, only: [:show, :edit, :update, :destroy] 9 before_action :find_metric_configuration!, only: [:show, :edit, :update, :destroy]
10 before_action :new_metric_configuration!, only: [:create] 10 before_action :new_metric_configuration!, only: [:create]
11 before_action :set_metric!, only: [:create, :update] 11 before_action :set_metric!, only: [:create, :update]
  12 + before_action :set_reading_group!, only: [:show, :edit, :create, :update]
12 13
13 def show 14 def show
14 - @reading_group = ReadingGroup.find(@metric_configuration.reading_group_id)  
15 @kalibro_ranges = @metric_configuration.kalibro_ranges 15 @kalibro_ranges = @metric_configuration.kalibro_ranges
16 end 16 end
17 17
@@ -19,14 +19,16 @@ class BaseMetricConfigurationsController < ApplicationController @@ -19,14 +19,16 @@ class BaseMetricConfigurationsController < ApplicationController
19 @metric_configuration = MetricConfiguration.new 19 @metric_configuration = MetricConfiguration.new
20 end 20 end
21 21
  22 + def edit
  23 + end
  24 +
22 def create 25 def create
23 - respond_to { |format| save_and_redir format } 26 + respond_to { |format| save_and_redir(format) }
24 end 27 end
25 28
26 def update 29 def update
27 respond_to do |format| 30 respond_to do |format|
28 save_and_redir(format) do |metric_configuration| 31 save_and_redir(format) do |metric_configuration|
29 - metric_configuration.reading_group_id = params[:reading_group_id].to_i  
30 metric_configuration.update(metric_configuration_params) 32 metric_configuration.update(metric_configuration_params)
31 end 33 end
32 end 34 end
@@ -58,14 +60,28 @@ class BaseMetricConfigurationsController < ApplicationController @@ -58,14 +60,28 @@ class BaseMetricConfigurationsController < ApplicationController
58 end 60 end
59 format.json { render json: @metric_configuration, status: new_record ? :created : :ok } 61 format.json { render json: @metric_configuration, status: new_record ? :created : :ok }
60 else 62 else
61 - failed_action(format, new_record ? :new : :edit) 63 + failed_action(format)
62 end 64 end
63 end 65 end
64 66
65 - # FIXME: This action should render with an error message on rendering the html  
66 - def failed_action(format, action)  
67 - format.html { render action }  
68 - format.json { render json: @metric_configuration.kalibro_errors, status: :unprocessable_entity } 67 + def failed_action(format, error = nil)
  68 + errors = @metric_configuration.kalibro_errors
  69 + errors << error unless error.nil?
  70 +
  71 + flash[:notice] = errors.join(', ')
  72 +
  73 + format.json { render json: { errors: errors }, status: :unprocessable_entity }
  74 + render_failure_html(format)
  75 + end
  76 +
  77 + def render_failure_html(format)
  78 + if action_name == 'create'
  79 + format.html { render 'new' }
  80 + elsif action_name == 'update'
  81 + format.html { render 'edit' }
  82 + else
  83 + format.html { redirect_to kalibro_configuration_path(@kalibro_configuration.id) }
  84 + end
69 end 85 end
70 86
71 def clear_caches 87 def clear_caches
@@ -84,7 +100,6 @@ class BaseMetricConfigurationsController &lt; ApplicationController @@ -84,7 +100,6 @@ class BaseMetricConfigurationsController &lt; ApplicationController
84 def new_metric_configuration! 100 def new_metric_configuration!
85 @metric_configuration = MetricConfiguration.new metric_configuration_params 101 @metric_configuration = MetricConfiguration.new metric_configuration_params
86 @metric_configuration.kalibro_configuration_id = @kalibro_configuration.id 102 @metric_configuration.kalibro_configuration_id = @kalibro_configuration.id
87 - @metric_configuration.reading_group_id = params[:reading_group_id].to_i  
88 end 103 end
89 104
90 def find_metric_configuration! 105 def find_metric_configuration!
@@ -111,9 +126,19 @@ class BaseMetricConfigurationsController &lt; ApplicationController @@ -111,9 +126,19 @@ class BaseMetricConfigurationsController &lt; ApplicationController
111 return 126 return
112 end 127 end
113 end 128 end
  129 +
114 respond_to do |format| 130 respond_to do |format|
115 - format.html { redirect_to kalibro_configurations_path(@kalibro_configuration.id) }  
116 - format.json { render json: { errors: 'Invalid metric type' }, status: :unprocessable_entity } 131 + failed_action(format, 'Invalid combination of metric collector, name/code and type')
  132 + end
  133 + end
  134 +
  135 + def set_reading_group!
  136 + begin
  137 + @reading_group = ReadingGroup.find(@metric_configuration.reading_group_id)
  138 + rescue KalibroClient::Errors::RecordNotFound
  139 + respond_to do |format|
  140 + failed_action(format, 'Invalid reading group')
  141 + end
117 end 142 end
118 end 143 end
119 end 144 end
app/controllers/hotspot_metric_configurations_controller.rb
1 class HotspotMetricConfigurationsController < BaseMetricConfigurationsController 1 class HotspotMetricConfigurationsController < BaseMetricConfigurationsController
2 METRIC_TYPE = 'HotspotMetricSnapshot' 2 METRIC_TYPE = 'HotspotMetricSnapshot'
3 3
  4 + skip_before_action :set_reading_group!
  5 +
4 def metric_configuration_params 6 def metric_configuration_params
5 # Hotspot metric configurations don't accept any parameters on creation 7 # Hotspot metric configurations don't accept any parameters on creation
6 # But we must make that explicit as the method isn't implemented in the parent class 8 # But we must make that explicit as the method isn't implemented in the parent class
7 params.permit 9 params.permit
8 end 10 end
9 11
10 - # FIXME: This action should render with an error message on rendering the html  
11 - def failed_action(format, _)  
12 - format.html { redirect_to kalibro_configurations_path(@kalibro_configuration.id) }  
13 - format.json { render json: @metric_configuration.kalibro_errors, status: :unprocessable_entity } 12 + def render_failure_html(format)
  13 + format.html { redirect_to kalibro_configuration_path(@kalibro_configuration.id) }
14 end 14 end
15 end 15 end