Commit d2d4183a96f62104a9e1aed006561a42445fa57f
Exists in
colab
and in
4 other branches
Merge pull request #64 from mezuro/I53-controller_inheritance
I53 controller inheritance
Showing
5 changed files
with
234 additions
and
49 deletions
Show diff stats
app/controllers/base_metric_configurations_controller.rb
0 → 100644
| ... | ... | @@ -0,0 +1,45 @@ |
| 1 | +include OwnershipAuthentication | |
| 2 | +include MetricConfigurationsConcern | |
| 3 | + | |
| 4 | +class BaseMetricConfigurationsController < ApplicationController | |
| 5 | + before_action :authenticate_user!, except: [:show, :index] | |
| 6 | + before_action :metric_configuration_owner?, only: [:edit, :update, :destroy] | |
| 7 | + before_action :mezuro_configuration_owner?, only: [:new, :create, :choose_metric] | |
| 8 | + before_action :set_metric_configuration, only: [:show, :edit, :update, :destroy] | |
| 9 | + | |
| 10 | + def new | |
| 11 | + update_metric_configuration(MetricConfiguration.new) | |
| 12 | + metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i | |
| 13 | + end | |
| 14 | + | |
| 15 | + def show | |
| 16 | + if metric_configuration | |
| 17 | + @reading_group = ReadingGroup.find(metric_configuration.reading_group_id) | |
| 18 | + @mezuro_ranges = metric_configuration.mezuro_ranges | |
| 19 | + metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i | |
| 20 | + else | |
| 21 | + raise NotImplementedError | |
| 22 | + end | |
| 23 | + end | |
| 24 | + | |
| 25 | + def create | |
| 26 | + update_metric_configuration(MetricConfiguration.new(metric_configuration_params)) | |
| 27 | + metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i | |
| 28 | + end | |
| 29 | + | |
| 30 | + protected | |
| 31 | + | |
| 32 | + def metric_configuration | |
| 33 | + raise NotImplementedError | |
| 34 | + end | |
| 35 | + | |
| 36 | + def update_metric_configuration (new_metric_configuration) | |
| 37 | + raise NotImplementedError | |
| 38 | + end | |
| 39 | + | |
| 40 | + # Never trust parameters from the scary internet, only allow the white list through. | |
| 41 | + # TODO: this should be refactored to the concern metric configuration | |
| 42 | + def metric_configuration_params | |
| 43 | + params[:metric_configuration] | |
| 44 | + end | |
| 45 | +end | |
| 0 | 46 | \ No newline at end of file | ... | ... |
app/controllers/compound_metric_configurations_controller.rb
| 1 | -include OwnershipAuthentication | |
| 2 | -include MetricConfigurationsConcern | |
| 3 | - | |
| 4 | -class CompoundMetricConfigurationsController < ApplicationController | |
| 5 | - before_action :authenticate_user!, except: [:show, :index] | |
| 6 | - before_action :mezuro_configuration_owner?, only: [:new, :create] | |
| 7 | - before_action :metric_configuration_owner?, only: [:edit, :update] | |
| 8 | - before_action :set_metric_configuration, only: [:show, :edit, :update] | |
| 1 | +class CompoundMetricConfigurationsController < BaseMetricConfigurationsController | |
| 9 | 2 | 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 | 3 | |
| 17 | 4 | 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 | |
| 5 | + super | |
| 6 | + metric_configuration.metric.compound = true | |
| 21 | 7 | respond_to do |format| |
| 22 | 8 | create_and_redir(format) |
| 23 | 9 | end |
| 24 | 10 | end |
| 25 | 11 | |
| 26 | 12 | 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 | |
| 13 | + update_metric_configuration(@metric_configuration) | |
| 14 | + super | |
| 31 | 15 | end |
| 32 | 16 | |
| 33 | 17 | def edit |
| ... | ... | @@ -35,6 +19,16 @@ class CompoundMetricConfigurationsController < ApplicationController |
| 35 | 19 | @compound_metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i |
| 36 | 20 | end |
| 37 | 21 | |
| 22 | + protected | |
| 23 | + | |
| 24 | + def metric_configuration | |
| 25 | + @compound_metric_configuration | |
| 26 | + end | |
| 27 | + | |
| 28 | + def update_metric_configuration (new_metric_configuration) | |
| 29 | + @compound_metric_configuration = new_metric_configuration | |
| 30 | + end | |
| 31 | + | |
| 38 | 32 | private |
| 39 | 33 | |
| 40 | 34 | # Never trust parameters from the scary internet, only allow the white list through. | ... | ... |
app/controllers/concerns/ownership_authentication.rb
| ... | ... | @@ -16,7 +16,7 @@ module OwnershipAuthentication |
| 16 | 16 | def repository_owner? |
| 17 | 17 | check_project_ownership(params[:project_id]) |
| 18 | 18 | end |
| 19 | - | |
| 19 | + | |
| 20 | 20 | def reading_group_owner? |
| 21 | 21 | if self.kind_of?(ReadingGroupsController) |
| 22 | 22 | id = params[:id] |
| ... | ... | @@ -36,7 +36,7 @@ module OwnershipAuthentication |
| 36 | 36 | def mezuro_configuration_owner? |
| 37 | 37 | if self.kind_of?(MezuroConfigurationsController) |
| 38 | 38 | id = params[:id] |
| 39 | - elsif (self.kind_of?(MetricConfigurationsController) || self.kind_of?(CompoundMetricConfigurationsController)) | |
| 39 | + elsif (self.kind_of?(BaseMetricConfigurationsController)) | |
| 40 | 40 | id = params[:mezuro_configuration_id] |
| 41 | 41 | else |
| 42 | 42 | raise "Not supported" | ... | ... |
app/controllers/metric_configurations_controller.rb
| 1 | -include OwnershipAuthentication | |
| 2 | -include MetricConfigurationsConcern | |
| 3 | - | |
| 4 | -class MetricConfigurationsController < ApplicationController | |
| 5 | - before_action :authenticate_user!, except: [:show, :index] | |
| 6 | - before_action :metric_configuration_owner?, only: [:edit, :update, :destroy] | |
| 7 | - before_action :mezuro_configuration_owner?, only: [:new, :create, :choose_metric] | |
| 8 | - before_action :set_metric_configuration, only: [:show, :edit, :update, :destroy] | |
| 9 | - | |
| 1 | +class MetricConfigurationsController < BaseMetricConfigurationsController | |
| 10 | 2 | def choose_metric |
| 11 | 3 | @mezuro_configuration_id = params[:mezuro_configuration_id].to_i |
| 12 | 4 | @metric_configuration_id = params[:metric_configuration_id].to_i |
| ... | ... | @@ -15,15 +7,13 @@ class MetricConfigurationsController < ApplicationController |
| 15 | 7 | end |
| 16 | 8 | |
| 17 | 9 | 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] | |
| 10 | + super | |
| 11 | + metric_configuration.base_tool_name = params[:base_tool_name] | |
| 12 | + metric_configuration.metric = KalibroGatekeeperClient::Entities::BaseTool.find_by_name(params[:base_tool_name]).metric params[:metric_name] | |
| 22 | 13 | end |
| 23 | 14 | |
| 24 | 15 | def create |
| 25 | - @metric_configuration = MetricConfiguration.new(metric_configuration_params) | |
| 26 | - @metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i | |
| 16 | + super | |
| 27 | 17 | @metric_configuration.metric = KalibroGatekeeperClient::Entities::BaseTool.find_by_name(params[:base_tool_name]).metric params[:metric_name] |
| 28 | 18 | @metric_configuration.base_tool_name = params[:base_tool_name] |
| 29 | 19 | respond_to do |format| |
| ... | ... | @@ -31,12 +21,6 @@ class MetricConfigurationsController < ApplicationController |
| 31 | 21 | end |
| 32 | 22 | end |
| 33 | 23 | |
| 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 | 24 | def edit |
| 41 | 25 | #FIXME: set the configuration id just once! |
| 42 | 26 | @mezuro_configuration_id = params[:mezuro_configuration_id] |
| ... | ... | @@ -63,14 +47,18 @@ class MetricConfigurationsController < ApplicationController |
| 63 | 47 | end |
| 64 | 48 | end |
| 65 | 49 | |
| 66 | - private | |
| 50 | + protected | |
| 51 | + | |
| 52 | + def metric_configuration | |
| 53 | + @metric_configuration | |
| 54 | + end | |
| 67 | 55 | |
| 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] | |
| 56 | + def update_metric_configuration (new_metric_configuration) | |
| 57 | + @metric_configuration = new_metric_configuration | |
| 72 | 58 | end |
| 73 | 59 | |
| 60 | + private | |
| 61 | + | |
| 74 | 62 | # Duplicated code on create and update actions extracted here |
| 75 | 63 | def failed_action(format, destiny_action) |
| 76 | 64 | @mezuro_configuration_id = params[:mezuro_configuration_id] | ... | ... |
spec/controllers/base_metric_configurations_controller_spec.rb
0 → 100644
| ... | ... | @@ -0,0 +1,158 @@ |
| 1 | +require 'spec_helper' | |
| 2 | + | |
| 3 | +class CleanInheritsFromBaseMetricConfigurationsController < BaseMetricConfigurationsController | |
| 4 | + def metric_configuration; super; end | |
| 5 | + def update_metric_configuration (new_metric_configuration); super; end | |
| 6 | +end | |
| 7 | + | |
| 8 | +class InheritsFromBaseMetricConfigurationsController < BaseMetricConfigurationsController | |
| 9 | + def new | |
| 10 | + render :nothing => true | |
| 11 | + super | |
| 12 | + end | |
| 13 | + | |
| 14 | + def create | |
| 15 | + render :nothing => true | |
| 16 | + super | |
| 17 | + end | |
| 18 | + | |
| 19 | + def show | |
| 20 | + update_metric_configuration(@metric_configuration) | |
| 21 | + super | |
| 22 | + render :nothing => true | |
| 23 | + end | |
| 24 | + | |
| 25 | + def metric_configuration | |
| 26 | + @metric_configuration | |
| 27 | + end | |
| 28 | + | |
| 29 | + def update_metric_configuration (new_metric_configuration) | |
| 30 | + @metric_configuration = new_metric_configuration | |
| 31 | + end | |
| 32 | + | |
| 33 | + def mezuro_ranges | |
| 34 | + @mezuro_ranges | |
| 35 | + end | |
| 36 | + | |
| 37 | + def reading_group | |
| 38 | + @reading_group | |
| 39 | + end | |
| 40 | +end | |
| 41 | + | |
| 42 | + | |
| 43 | +describe InheritsFromBaseMetricConfigurationsController do | |
| 44 | + let(:mezuro_configuration) { FactoryGirl.build(:mezuro_configuration) } | |
| 45 | + | |
| 46 | + before do | |
| 47 | + Rails.application.routes.draw do | |
| 48 | + resources :mezuro_configurations do | |
| 49 | + match '/metric_configurations/choose_metric' => 'metric_configurations#choose_metric', as: :choose_metric, :via => [:get] | |
| 50 | + resources :inherits_from_base_metric_configurations do | |
| 51 | + match '/metric_configurations/new' => 'metric_configurations#new', as: :new_metric_configuration, :via => [:post] | |
| 52 | + match '/metric_configurations/:id' => 'metric_configurations#update', as: :metric_configuration_update, :via => [:put] | |
| 53 | + end | |
| 54 | + end | |
| 55 | + end | |
| 56 | + end | |
| 57 | + | |
| 58 | + after do | |
| 59 | + Rails.application.reload_routes! | |
| 60 | + end | |
| 61 | + | |
| 62 | + describe 'new' do | |
| 63 | + before :each do | |
| 64 | + sign_in FactoryGirl.create(:user) | |
| 65 | + end | |
| 66 | + | |
| 67 | + context 'when the current user owns the mezuro configuration' do | |
| 68 | + let!(:metric_configuration) { FactoryGirl.build(:metric_configuration) } | |
| 69 | + before :each do | |
| 70 | + subject.expects(:mezuro_configuration_owner?).returns true | |
| 71 | + get :new, mezuro_configuration_id: mezuro_configuration.id | |
| 72 | + end | |
| 73 | + | |
| 74 | + it { metric_configuration.should_not be_nil } | |
| 75 | + it { should respond_with(:success) } | |
| 76 | + end | |
| 77 | + | |
| 78 | + context "when the current user doesn't owns the mezuro configuration" do | |
| 79 | + before :each do | |
| 80 | + get :new, mezuro_configuration_id: mezuro_configuration.id | |
| 81 | + end | |
| 82 | + | |
| 83 | + it { should redirect_to(mezuro_configurations_url(mezuro_configuration.id)) } | |
| 84 | + it { should respond_with(:redirect) } | |
| 85 | + end | |
| 86 | + end | |
| 87 | + | |
| 88 | + describe 'create' do | |
| 89 | + let!(:metric_configuration_params) { Hash[FactoryGirl.attributes_for(:metric_configuration).map { |k,v| [k.to_s, v.to_s] }] } #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with symbols and integers | |
| 90 | + let!(:metric_params) { Hash[FactoryGirl.attributes_for(:metric).map { |k,v| [k.to_s, v.to_s] }] } #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with symbols and integers | |
| 91 | + let(:base_tool) { FactoryGirl.build(:base_tool) } | |
| 92 | + | |
| 93 | + before :each do | |
| 94 | + sign_in FactoryGirl.create(:user) | |
| 95 | + end | |
| 96 | + | |
| 97 | + context 'when the current user owns the mezuro configuration' do | |
| 98 | + before :each do | |
| 99 | + subject.expects(:mezuro_configuration_owner?).returns true | |
| 100 | + end | |
| 101 | + | |
| 102 | + context 'with valid fields' do | |
| 103 | + before :each do | |
| 104 | + post :create, mezuro_configuration_id: mezuro_configuration.id, metric_configuration: metric_configuration_params, base_tool_name: base_tool.name | |
| 105 | + end | |
| 106 | + | |
| 107 | + it { subject.metric_configuration.should_not be_nil } | |
| 108 | + it { should respond_with(:success) } | |
| 109 | + end | |
| 110 | + end | |
| 111 | + end | |
| 112 | + | |
| 113 | + describe 'show' do | |
| 114 | + let(:metric_configuration) { FactoryGirl.build(:metric_configuration) } | |
| 115 | + let(:reading_group) { FactoryGirl.build(:reading_group) } | |
| 116 | + let(:mezuro_range) { FactoryGirl.build(:mezuro_range) } | |
| 117 | + | |
| 118 | + context 'with a valid metric_configuration' do | |
| 119 | + before :each do | |
| 120 | + ReadingGroup.expects(:find).with(metric_configuration.reading_group_id).returns(reading_group) | |
| 121 | + MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration) | |
| 122 | + MezuroRange.expects(:ranges_of).with(metric_configuration.id).returns([mezuro_range]) | |
| 123 | + | |
| 124 | + get :show, mezuro_configuration_id: metric_configuration.configuration_id.to_s, id: metric_configuration.id | |
| 125 | + end | |
| 126 | + | |
| 127 | + it { subject.mezuro_ranges.should_not be_nil} | |
| 128 | + it { subject.reading_group.should_not be_nil } | |
| 129 | + end | |
| 130 | + | |
| 131 | + context 'with a invalid metric_configuration' do | |
| 132 | + before :each do | |
| 133 | + subject.expects(:metric_configuration).returns(false) | |
| 134 | + end | |
| 135 | + | |
| 136 | + it 'should raise a NotImplementedError' do | |
| 137 | + expect { subject.show }.to raise_error(NotImplementedError) | |
| 138 | + end | |
| 139 | + end | |
| 140 | + end | |
| 141 | + | |
| 142 | + | |
| 143 | + context 'with a inheritance without overrides methods' do | |
| 144 | + subject { CleanInheritsFromBaseMetricConfigurationsController.new } | |
| 145 | + | |
| 146 | + describe 'metric_configuration' do | |
| 147 | + it 'should raise a NotImplementedError' do | |
| 148 | + expect { subject.metric_configuration }.to raise_error(NotImplementedError) | |
| 149 | + end | |
| 150 | + end | |
| 151 | + | |
| 152 | + describe 'update_metric_configuration' do | |
| 153 | + it 'should raise a NotImplementedError' do | |
| 154 | + expect { subject.update_metric_configuration(nil) }.to raise_error(NotImplementedError) | |
| 155 | + end | |
| 156 | + end | |
| 157 | + end | |
| 158 | +end | ... | ... |