diff --git a/app/controllers/metric_configurations_controller.rb b/app/controllers/metric_configurations_controller.rb index 8999a8f..5457077 100644 --- a/app/controllers/metric_configurations_controller.rb +++ b/app/controllers/metric_configurations_controller.rb @@ -7,18 +7,29 @@ def create metric_snapshot = build_metric_snapshot @metric_configuration = MetricConfiguration.new(all_params.reject { |key, _| key == 'metric' }) if valid_metric_snapshot_for_configuration(metric_snapshot, @metric_configuration) - if save_metric_configuration_with_snapshot(@metric_configuration, metric_snapshot) - data = { metric_configuration: @metric_configuration } - status = :created - else - data = { errors: @metric_configuration.errors.full_messages + metric_snapshot.errors.full_messages } - status = :unprocessable_entity - end + result = try_to_save_metric_configuration(metric_snapshot) + else + result = get_result_from_invalid_metric_configuration(metric_snapshot) + end + respond_with_json result[:data], result[:status] + end + + def try_to_save_metric_configuration(metric_snapshot) + result = {} + if save_metric_configuration_with_snapshot(@metric_configuration, metric_snapshot) + result[:data] = { metric_configuration: @metric_configuration } + result[:status] = :created else - data = { errors: @metric_configuration.errors.full_messages + metric_snapshot.errors.full_messages } - status = :unprocessable_entity + result = get_result_from_invalid_metric_configuration(metric_snapshot) end - respond_with_json data, status + result + end + + def get_result_from_invalid_metric_configuration(metric_snapshot) + result = {} + result[:data] = { errors: @metric_configuration.errors.full_messages + metric_snapshot.errors.full_messages } + result[:status] = :unprocessable_entity + result end def update