Commit e9f589a5ed9550689d7c06ee7c37b07449265699
1 parent
951244bc
Exists in
colab
and in
4 other branches
Moving before_action method calls to metric configuration SuperClass
Showing
5 changed files
with
148 additions
and
28 deletions
Show diff stats
app/controllers/base_configurations_controller.rb
... | ... | @@ -2,16 +2,24 @@ include OwnershipAuthentication |
2 | 2 | include MetricConfigurationsConcern |
3 | 3 | |
4 | 4 | class BaseConfigurationsController < 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] | |
5 | 9 | |
6 | - def new | |
10 | + def new | |
7 | 11 | update_metric_configuration(MetricConfiguration.new) |
8 | 12 | metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i |
9 | 13 | end |
10 | 14 | |
11 | 15 | 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 | |
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 | |
15 | 23 | end |
16 | 24 | |
17 | 25 | def create |
... | ... | @@ -22,12 +30,12 @@ class BaseConfigurationsController < ApplicationController |
22 | 30 | protected |
23 | 31 | |
24 | 32 | def metric_configuration |
25 | - raise "SubclassResponsibility"; | |
26 | - end | |
33 | + raise NotImplementedError | |
34 | + end | |
27 | 35 | |
28 | 36 | def update_metric_configuration (new_metric_configuration) |
29 | - raise "SubclassResponsibility"; | |
30 | - end | |
37 | + raise NotImplementedError | |
38 | + end | |
31 | 39 | |
32 | 40 | # Never trust parameters from the scary internet, only allow the white list through. |
33 | 41 | # TODO: this should be refactored to the concern metric configuration | ... | ... |
app/controllers/compound_metric_configurations_controller.rb
1 | -include OwnershipAuthentication | |
2 | -include MetricConfigurationsConcern | |
3 | - | |
4 | 1 | class CompoundMetricConfigurationsController < BaseConfigurationsController |
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] | |
9 | 2 | before_action :set_metric_configurations, only: [:new, :edit] |
10 | 3 | |
11 | 4 | def create |
... | ... | @@ -29,11 +22,11 @@ class CompoundMetricConfigurationsController < BaseConfigurationsController |
29 | 22 | protected |
30 | 23 | |
31 | 24 | def metric_configuration |
32 | - @compound_metric_configuration; | |
25 | + @compound_metric_configuration | |
33 | 26 | end |
34 | 27 | |
35 | 28 | def update_metric_configuration (new_metric_configuration) |
36 | - @compound_metric_configuration = new_metric_configuration; | |
29 | + @compound_metric_configuration = new_metric_configuration | |
37 | 30 | end |
38 | 31 | |
39 | 32 | private | ... | ... |
app/controllers/concerns/ownership_authentication.rb
... | ... | @@ -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?(BaseConfigurationsController)) | |
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 | 1 | class MetricConfigurationsController < BaseConfigurationsController |
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 | 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 |
... | ... | @@ -58,11 +50,11 @@ class MetricConfigurationsController < BaseConfigurationsController |
58 | 50 | protected |
59 | 51 | |
60 | 52 | def metric_configuration |
61 | - @metric_configuration; | |
53 | + @metric_configuration | |
62 | 54 | end |
63 | 55 | |
64 | 56 | def update_metric_configuration (new_metric_configuration) |
65 | - @metric_configuration = new_metric_configuration; | |
57 | + @metric_configuration = new_metric_configuration | |
66 | 58 | end |
67 | 59 | |
68 | 60 | private | ... | ... |
spec/controllers/base_metric_configurations_controller_spec.rb
0 → 100644
... | ... | @@ -0,0 +1,127 @@ |
1 | +require 'spec_helper' | |
2 | + | |
3 | +class InheritsFromBaseConfigurationsController < BaseConfigurationsController | |
4 | + | |
5 | + def new | |
6 | + render :nothing => true | |
7 | + super | |
8 | + end | |
9 | + | |
10 | + def create | |
11 | + render :nothing => true | |
12 | + super | |
13 | + end | |
14 | + | |
15 | + def show | |
16 | + render :nothing => true | |
17 | + update_metric_configuration(@metric_configuration) | |
18 | + super | |
19 | + end | |
20 | + | |
21 | + def metric_configuration | |
22 | + @metric_configuration | |
23 | + end | |
24 | + | |
25 | + def update_metric_configuration (new_metric_configuration) | |
26 | + @metric_configuration = new_metric_configuration | |
27 | + end | |
28 | + | |
29 | + def mezuro_ranges | |
30 | + @mezuro_ranges | |
31 | + end | |
32 | + | |
33 | + def reading_group | |
34 | + @reading_group | |
35 | + end | |
36 | +end | |
37 | + | |
38 | + | |
39 | +describe InheritsFromBaseConfigurationsController do | |
40 | + let(:mezuro_configuration) { FactoryGirl.build(:mezuro_configuration) } | |
41 | + | |
42 | + before do | |
43 | + Rails.application.routes.draw do | |
44 | + resources :mezuro_configurations do | |
45 | + match '/metric_configurations/choose_metric' => 'metric_configurations#choose_metric', as: :choose_metric, :via => [:get] | |
46 | + resources :inherits_from_base_configurations do | |
47 | + #match '/metric_configurations/choose_metric' => "inherits_from_base_configurations#show", :via => [:get] | |
48 | + match '/metric_configurations/new' => 'metric_configurations#new', as: :new_metric_configuration, :via => [:post] | |
49 | + match '/metric_configurations/:id' => 'metric_configurations#update', as: :metric_configuration_update, :via => [:put] | |
50 | + end | |
51 | + end | |
52 | + end | |
53 | + end | |
54 | + | |
55 | + after do | |
56 | + Rails.application.reload_routes! | |
57 | + end | |
58 | + | |
59 | + describe 'new' do | |
60 | + before :each do | |
61 | + sign_in FactoryGirl.create(:user) | |
62 | + end | |
63 | + | |
64 | + context 'when the current user owns the mezuro configuration' do | |
65 | + let!(:metric_configuration) { FactoryGirl.build(:metric_configuration) } | |
66 | + before :each do | |
67 | + subject.expects(:mezuro_configuration_owner?).returns true | |
68 | + get :new, mezuro_configuration_id: mezuro_configuration.id | |
69 | + end | |
70 | + | |
71 | + it { metric_configuration.should_not be_nil } | |
72 | + it { should respond_with(:success) } | |
73 | + end | |
74 | + | |
75 | + context "when the current user doesn't owns the mezuro configuration" do | |
76 | + before :each do | |
77 | + get :new, mezuro_configuration_id: mezuro_configuration.id | |
78 | + end | |
79 | + | |
80 | + it { should redirect_to(mezuro_configurations_url(mezuro_configuration.id)) } | |
81 | + it { should respond_with(:redirect) } | |
82 | + end | |
83 | + end | |
84 | + | |
85 | + describe 'create' do | |
86 | + 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 | |
87 | + 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 | |
88 | + let(:base_tool) { FactoryGirl.build(:base_tool) } | |
89 | + | |
90 | + before :each do | |
91 | + sign_in FactoryGirl.create(:user) | |
92 | + end | |
93 | + | |
94 | + context 'when the current user owns the mezuro configuration' do | |
95 | + before :each do | |
96 | + subject.expects(:mezuro_configuration_owner?).returns true | |
97 | + end | |
98 | + | |
99 | + context 'with valid fields' do | |
100 | + before :each do | |
101 | + post :create, mezuro_configuration_id: mezuro_configuration.id, metric_configuration: metric_configuration_params, base_tool_name: base_tool.name | |
102 | + end | |
103 | + | |
104 | + it { subject.metric_configuration.should_not be_nil } | |
105 | + it { should respond_with(:success) } | |
106 | + end | |
107 | + end | |
108 | + end | |
109 | + | |
110 | + describe 'show' do | |
111 | + let(:metric_configuration) { FactoryGirl.build(:metric_configuration) } | |
112 | + let(:reading_group) { FactoryGirl.build(:reading_group) } | |
113 | + let(:mezuro_range) { FactoryGirl.build(:mezuro_range) } | |
114 | + | |
115 | + before :each do | |
116 | + ReadingGroup.expects(:find).with(metric_configuration.reading_group_id).returns(reading_group) | |
117 | + MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration) | |
118 | + MezuroRange.expects(:ranges_of).with(metric_configuration.id).returns([mezuro_range]) | |
119 | + | |
120 | + get :show, mezuro_configuration_id: metric_configuration.configuration_id.to_s, id: metric_configuration.id | |
121 | + end | |
122 | + | |
123 | + it { subject.mezuro_ranges.should_not be_nil} | |
124 | + it { subject.reading_group.should_not be_nil } | |
125 | + end | |
126 | + | |
127 | +end | ... | ... |