Commit eaaf4f2f517b1c6af3082d221eaee86d2b4ac888
Committed by
Rafael Manzo
1 parent
6f4bab0e
Exists in
colab
and in
4 other branches
Add proper Reading Group fetching in Metric Configurations controllers
Also improve error reporting functions
Showing
2 changed files
with
41 additions
and
16 deletions
Show diff stats
app/controllers/base_metric_configurations_controller.rb
| ... | ... | @@ -2,16 +2,16 @@ class BaseMetricConfigurationsController < ApplicationController |
| 2 | 2 | include OwnershipAuthentication |
| 3 | 3 | include MetricConfigurationsConcern |
| 4 | 4 | |
| 5 | - before_action :authenticate_user!, except: [:show, :index] | |
| 5 | + before_action :authenticate_user!, except: [:show] | |
| 6 | 6 | before_action :metric_configuration_owner?, only: [:edit, :update, :destroy] |
| 7 | 7 | before_action :kalibro_configuration_owner?, only: [:new, :create, :choose_metric] |
| 8 | 8 | before_action :set_kalibro_configuration! |
| 9 | 9 | before_action :find_metric_configuration!, only: [:show, :edit, :update, :destroy] |
| 10 | 10 | before_action :new_metric_configuration!, only: [:create] |
| 11 | 11 | before_action :set_metric!, only: [:create, :update] |
| 12 | + before_action :set_reading_group!, only: [:show, :edit, :create, :update] | |
| 12 | 13 | |
| 13 | 14 | def show |
| 14 | - @reading_group = ReadingGroup.find(@metric_configuration.reading_group_id) | |
| 15 | 15 | @kalibro_ranges = @metric_configuration.kalibro_ranges |
| 16 | 16 | end |
| 17 | 17 | |
| ... | ... | @@ -19,14 +19,16 @@ class BaseMetricConfigurationsController < ApplicationController |
| 19 | 19 | @metric_configuration = MetricConfiguration.new |
| 20 | 20 | end |
| 21 | 21 | |
| 22 | + def edit | |
| 23 | + end | |
| 24 | + | |
| 22 | 25 | def create |
| 23 | - respond_to { |format| save_and_redir format } | |
| 26 | + respond_to { |format| save_and_redir(format) } | |
| 24 | 27 | end |
| 25 | 28 | |
| 26 | 29 | def update |
| 27 | 30 | respond_to do |format| |
| 28 | 31 | save_and_redir(format) do |metric_configuration| |
| 29 | - metric_configuration.reading_group_id = params[:reading_group_id].to_i | |
| 30 | 32 | metric_configuration.update(metric_configuration_params) |
| 31 | 33 | end |
| 32 | 34 | end |
| ... | ... | @@ -58,14 +60,28 @@ class BaseMetricConfigurationsController < ApplicationController |
| 58 | 60 | end |
| 59 | 61 | format.json { render json: @metric_configuration, status: new_record ? :created : :ok } |
| 60 | 62 | else |
| 61 | - failed_action(format, new_record ? :new : :edit) | |
| 63 | + failed_action(format) | |
| 62 | 64 | end |
| 63 | 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 | 85 | end |
| 70 | 86 | |
| 71 | 87 | def clear_caches |
| ... | ... | @@ -84,7 +100,6 @@ class BaseMetricConfigurationsController < ApplicationController |
| 84 | 100 | def new_metric_configuration! |
| 85 | 101 | @metric_configuration = MetricConfiguration.new metric_configuration_params |
| 86 | 102 | @metric_configuration.kalibro_configuration_id = @kalibro_configuration.id |
| 87 | - @metric_configuration.reading_group_id = params[:reading_group_id].to_i | |
| 88 | 103 | end |
| 89 | 104 | |
| 90 | 105 | def find_metric_configuration! |
| ... | ... | @@ -111,9 +126,19 @@ class BaseMetricConfigurationsController < ApplicationController |
| 111 | 126 | return |
| 112 | 127 | end |
| 113 | 128 | end |
| 129 | + | |
| 114 | 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 | 142 | end |
| 118 | 143 | end |
| 119 | 144 | end | ... | ... |
app/controllers/hotspot_metric_configurations_controller.rb
| 1 | 1 | class HotspotMetricConfigurationsController < BaseMetricConfigurationsController |
| 2 | 2 | METRIC_TYPE = 'HotspotMetricSnapshot' |
| 3 | 3 | |
| 4 | + skip_before_action :set_reading_group! | |
| 5 | + | |
| 4 | 6 | def metric_configuration_params |
| 5 | 7 | # Hotspot metric configurations don't accept any parameters on creation |
| 6 | 8 | # But we must make that explicit as the method isn't implemented in the parent class |
| 7 | 9 | params.permit |
| 8 | 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 | 14 | end |
| 15 | 15 | end | ... | ... |