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 @@ | @@ -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 | \ No newline at end of file | 38 | \ No newline at end of file |
app/controllers/compound_metric_configurations_controller.rb
1 | include OwnershipAuthentication | 1 | include OwnershipAuthentication |
2 | include MetricConfigurationsConcern | 2 | include MetricConfigurationsConcern |
3 | 3 | ||
4 | -class CompoundMetricConfigurationsController < ApplicationController | 4 | +class CompoundMetricConfigurationsController < BaseConfigurationsController |
5 | before_action :authenticate_user!, except: [:show, :index] | 5 | before_action :authenticate_user!, except: [:show, :index] |
6 | before_action :mezuro_configuration_owner?, only: [:new, :create] | 6 | before_action :mezuro_configuration_owner?, only: [:new, :create] |
7 | before_action :metric_configuration_owner?, only: [:edit, :update] | 7 | before_action :metric_configuration_owner?, only: [:edit, :update] |
8 | before_action :set_metric_configuration, only: [:show, :edit, :update] | 8 | before_action :set_metric_configuration, only: [:show, :edit, :update] |
9 | before_action :set_metric_configurations, only: [:new, :edit] | 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 | def create | 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 | respond_to do |format| | 14 | respond_to do |format| |
22 | create_and_redir(format) | 15 | create_and_redir(format) |
23 | end | 16 | end |
24 | end | 17 | end |
25 | 18 | ||
26 | def show | 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 | end | 22 | end |
32 | 23 | ||
33 | def edit | 24 | def edit |
@@ -35,6 +26,16 @@ class CompoundMetricConfigurationsController < ApplicationController | @@ -35,6 +26,16 @@ class CompoundMetricConfigurationsController < ApplicationController | ||
35 | @compound_metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i | 26 | @compound_metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i |
36 | end | 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 | private | 39 | private |
39 | 40 | ||
40 | # Never trust parameters from the scary internet, only allow the white list through. | 41 | # Never trust parameters from the scary internet, only allow the white list through. |
app/controllers/metric_configurations_controller.rb
1 | include OwnershipAuthentication | 1 | include OwnershipAuthentication |
2 | include MetricConfigurationsConcern | 2 | include MetricConfigurationsConcern |
3 | 3 | ||
4 | -class MetricConfigurationsController < ApplicationController | 4 | +class MetricConfigurationsController < BaseConfigurationsController |
5 | before_action :authenticate_user!, except: [:show, :index] | 5 | before_action :authenticate_user!, except: [:show, :index] |
6 | before_action :metric_configuration_owner?, only: [:edit, :update, :destroy] | 6 | before_action :metric_configuration_owner?, only: [:edit, :update, :destroy] |
7 | before_action :mezuro_configuration_owner?, only: [:new, :create, :choose_metric] | 7 | before_action :mezuro_configuration_owner?, only: [:new, :create, :choose_metric] |
@@ -15,15 +15,13 @@ class MetricConfigurationsController < ApplicationController | @@ -15,15 +15,13 @@ class MetricConfigurationsController < ApplicationController | ||
15 | end | 15 | end |
16 | 16 | ||
17 | def new | 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 | end | 21 | end |
23 | 22 | ||
24 | def create | 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 | @metric_configuration.metric = KalibroGatekeeperClient::Entities::BaseTool.find_by_name(params[:base_tool_name]).metric params[:metric_name] | 25 | @metric_configuration.metric = KalibroGatekeeperClient::Entities::BaseTool.find_by_name(params[:base_tool_name]).metric params[:metric_name] |
28 | @metric_configuration.base_tool_name = params[:base_tool_name] | 26 | @metric_configuration.base_tool_name = params[:base_tool_name] |
29 | respond_to do |format| | 27 | respond_to do |format| |
@@ -31,12 +29,6 @@ class MetricConfigurationsController < ApplicationController | @@ -31,12 +29,6 @@ class MetricConfigurationsController < ApplicationController | ||
31 | end | 29 | end |
32 | end | 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 | def edit | 32 | def edit |
41 | #FIXME: set the configuration id just once! | 33 | #FIXME: set the configuration id just once! |
42 | @mezuro_configuration_id = params[:mezuro_configuration_id] | 34 | @mezuro_configuration_id = params[:mezuro_configuration_id] |
@@ -63,14 +55,18 @@ class MetricConfigurationsController < ApplicationController | @@ -63,14 +55,18 @@ class MetricConfigurationsController < ApplicationController | ||
63 | end | 55 | end |
64 | end | 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 | end | 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 | # Duplicated code on create and update actions extracted here | 70 | # Duplicated code on create and update actions extracted here |
75 | def failed_action(format, destiny_action) | 71 | def failed_action(format, destiny_action) |
76 | @mezuro_configuration_id = params[:mezuro_configuration_id] | 72 | @mezuro_configuration_id = params[:mezuro_configuration_id] |