Commit 951244bcf74bd07a79733950a2f49e36e7df0c01
1 parent
689ce8cc
Exists in
colab
and in
4 other branches
Extract Superclass refactoring to compound and metric controllers
Showing
3 changed files
with
66 additions
and
32 deletions
Show diff stats
| ... | ... | @@ -0,0 +1,37 @@ |
| 1 | +include OwnershipAuthentication | |
| 2 | +include MetricConfigurationsConcern | |
| 3 | + | |
| 4 | +class BaseConfigurationsController < ApplicationController | |
| 5 | + | |
| 6 | + def new | |
| 7 | + update_metric_configuration(MetricConfiguration.new) | |
| 8 | + metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i | |
| 9 | + end | |
| 10 | + | |
| 11 | + def show | |
| 12 | + @reading_group = ReadingGroup.find(metric_configuration.reading_group_id) | |
| 13 | + @mezuro_ranges = metric_configuration.mezuro_ranges | |
| 14 | + metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i | |
| 15 | + end | |
| 16 | + | |
| 17 | + def create | |
| 18 | + update_metric_configuration(MetricConfiguration.new(metric_configuration_params)) | |
| 19 | + metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i | |
| 20 | + end | |
| 21 | + | |
| 22 | + protected | |
| 23 | + | |
| 24 | + def metric_configuration | |
| 25 | + raise "SubclassResponsibility"; | |
| 26 | + end | |
| 27 | + | |
| 28 | + def update_metric_configuration (new_metric_configuration) | |
| 29 | + raise "SubclassResponsibility"; | |
| 30 | + end | |
| 31 | + | |
| 32 | + # Never trust parameters from the scary internet, only allow the white list through. | |
| 33 | + # TODO: this should be refactored to the concern metric configuration | |
| 34 | + def metric_configuration_params | |
| 35 | + params[:metric_configuration] | |
| 36 | + end | |
| 37 | +end | |
| 0 | 38 | \ No newline at end of file | ... | ... |
app/controllers/compound_metric_configurations_controller.rb
| 1 | 1 | include OwnershipAuthentication |
| 2 | 2 | include MetricConfigurationsConcern |
| 3 | 3 | |
| 4 | -class CompoundMetricConfigurationsController < ApplicationController | |
| 4 | +class CompoundMetricConfigurationsController < BaseConfigurationsController | |
| 5 | 5 | before_action :authenticate_user!, except: [:show, :index] |
| 6 | 6 | before_action :mezuro_configuration_owner?, only: [:new, :create] |
| 7 | 7 | before_action :metric_configuration_owner?, only: [:edit, :update] |
| 8 | 8 | before_action :set_metric_configuration, only: [:show, :edit, :update] |
| 9 | 9 | before_action :set_metric_configurations, only: [:new, :edit] |
| 10 | - | |
| 11 | - # GET mezuro_configurations/1/compound_metric_configurations/new | |
| 12 | - def new | |
| 13 | - @compound_metric_configuration = MetricConfiguration.new | |
| 14 | - @compound_metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i | |
| 15 | - end | |
| 16 | 10 | |
| 17 | 11 | def create |
| 18 | - @compound_metric_configuration = MetricConfiguration.new(metric_configuration_params) | |
| 19 | - @compound_metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i | |
| 20 | - @compound_metric_configuration.metric.compound = true | |
| 12 | + super | |
| 13 | + metric_configuration.metric.compound = true | |
| 21 | 14 | respond_to do |format| |
| 22 | 15 | create_and_redir(format) |
| 23 | 16 | end |
| 24 | 17 | end |
| 25 | 18 | |
| 26 | 19 | def show |
| 27 | - @compound_metric_configuration = @metric_configuration | |
| 28 | - @reading_group = ReadingGroup.find(@compound_metric_configuration.reading_group_id) | |
| 29 | - @mezuro_ranges = @compound_metric_configuration.mezuro_ranges | |
| 30 | - @compound_metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i | |
| 20 | + update_metric_configuration(@metric_configuration) | |
| 21 | + super | |
| 31 | 22 | end |
| 32 | 23 | |
| 33 | 24 | def edit |
| ... | ... | @@ -35,6 +26,16 @@ class CompoundMetricConfigurationsController < ApplicationController |
| 35 | 26 | @compound_metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i |
| 36 | 27 | end |
| 37 | 28 | |
| 29 | + protected | |
| 30 | + | |
| 31 | + def metric_configuration | |
| 32 | + @compound_metric_configuration; | |
| 33 | + end | |
| 34 | + | |
| 35 | + def update_metric_configuration (new_metric_configuration) | |
| 36 | + @compound_metric_configuration = new_metric_configuration; | |
| 37 | + end | |
| 38 | + | |
| 38 | 39 | private |
| 39 | 40 | |
| 40 | 41 | # Never trust parameters from the scary internet, only allow the white list through. | ... | ... |
app/controllers/metric_configurations_controller.rb
| 1 | 1 | include OwnershipAuthentication |
| 2 | 2 | include MetricConfigurationsConcern |
| 3 | 3 | |
| 4 | -class MetricConfigurationsController < ApplicationController | |
| 4 | +class MetricConfigurationsController < BaseConfigurationsController | |
| 5 | 5 | before_action :authenticate_user!, except: [:show, :index] |
| 6 | 6 | before_action :metric_configuration_owner?, only: [:edit, :update, :destroy] |
| 7 | 7 | before_action :mezuro_configuration_owner?, only: [:new, :create, :choose_metric] |
| ... | ... | @@ -15,15 +15,13 @@ class MetricConfigurationsController < ApplicationController |
| 15 | 15 | end |
| 16 | 16 | |
| 17 | 17 | def new |
| 18 | - @metric_configuration = MetricConfiguration.new | |
| 19 | - @metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i | |
| 20 | - @metric_configuration.base_tool_name = params[:base_tool_name] | |
| 21 | - @metric_configuration.metric = KalibroGatekeeperClient::Entities::BaseTool.find_by_name(params[:base_tool_name]).metric params[:metric_name] | |
| 18 | + super | |
| 19 | + metric_configuration.base_tool_name = params[:base_tool_name] | |
| 20 | + metric_configuration.metric = KalibroGatekeeperClient::Entities::BaseTool.find_by_name(params[:base_tool_name]).metric params[:metric_name] | |
| 22 | 21 | end |
| 23 | 22 | |
| 24 | 23 | def create |
| 25 | - @metric_configuration = MetricConfiguration.new(metric_configuration_params) | |
| 26 | - @metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i | |
| 24 | + super | |
| 27 | 25 | @metric_configuration.metric = KalibroGatekeeperClient::Entities::BaseTool.find_by_name(params[:base_tool_name]).metric params[:metric_name] |
| 28 | 26 | @metric_configuration.base_tool_name = params[:base_tool_name] |
| 29 | 27 | respond_to do |format| |
| ... | ... | @@ -31,12 +29,6 @@ class MetricConfigurationsController < ApplicationController |
| 31 | 29 | end |
| 32 | 30 | end |
| 33 | 31 | |
| 34 | - def show | |
| 35 | - @reading_group = ReadingGroup.find(@metric_configuration.reading_group_id) | |
| 36 | - @mezuro_ranges = @metric_configuration.mezuro_ranges | |
| 37 | - @metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i | |
| 38 | - end | |
| 39 | - | |
| 40 | 32 | def edit |
| 41 | 33 | #FIXME: set the configuration id just once! |
| 42 | 34 | @mezuro_configuration_id = params[:mezuro_configuration_id] |
| ... | ... | @@ -63,14 +55,18 @@ class MetricConfigurationsController < ApplicationController |
| 63 | 55 | end |
| 64 | 56 | end |
| 65 | 57 | |
| 66 | - private | |
| 58 | + protected | |
| 67 | 59 | |
| 68 | - # Never trust parameters from the scary internet, only allow the white list through. | |
| 69 | - # TODO: this should be refactored to the concern metric configuration | |
| 70 | - def metric_configuration_params | |
| 71 | - params[:metric_configuration] | |
| 60 | + def metric_configuration | |
| 61 | + @metric_configuration; | |
| 72 | 62 | end |
| 73 | 63 | |
| 64 | + def update_metric_configuration (new_metric_configuration) | |
| 65 | + @metric_configuration = new_metric_configuration; | |
| 66 | + end | |
| 67 | + | |
| 68 | + private | |
| 69 | + | |
| 74 | 70 | # Duplicated code on create and update actions extracted here |
| 75 | 71 | def failed_action(format, destiny_action) |
| 76 | 72 | @mezuro_configuration_id = params[:mezuro_configuration_id] | ... | ... |