Commit 06ce021c5055bb016c969c068123d398cf32a650
Committed by
Rafael Manzo
1 parent
e02dd3ba
Exists in
colab
and in
4 other branches
Destroy Metric Configurations feature
Pending acceptance tests. Removed cache for listing metric configurations. signed-off-by: Fellipe Souto Sampaio <fllsouto@gmail.com> signed-off-by: Renan Fichberg <rfichberg@gmail.com>
Showing
5 changed files
with
65 additions
and
7 deletions
Show diff stats
app/controllers/concerns/ownership_authentication.rb
| ... | ... | @@ -36,6 +36,11 @@ module OwnershipAuthentication |
| 36 | 36 | check_mezuro_configuration_ownership(id) |
| 37 | 37 | end |
| 38 | 38 | |
| 39 | + def metric_configuration_owner? | |
| 40 | + check_mezuro_configuration_ownership(params[:mezuro_configuration_id]) | |
| 41 | + end | |
| 42 | + | |
| 43 | + | |
| 39 | 44 | private |
| 40 | 45 | |
| 41 | 46 | def check_project_ownership(id) | ... | ... |
app/controllers/metric_configurations_controller.rb
| ... | ... | @@ -2,7 +2,9 @@ include OwnershipAuthentication |
| 2 | 2 | |
| 3 | 3 | class MetricConfigurationsController < ApplicationController |
| 4 | 4 | before_action :authenticate_user!, except: [:index] |
| 5 | - before_action :mezuro_configuration_owner?, except: [:show] | |
| 5 | + before_action :set_metric_configuration, only: [:destroy] | |
| 6 | + before_action :metric_configuration_owner?, only: [:destroy] | |
| 7 | + before_action :mezuro_configuration_owner?, only: [:new, :create, :choose_metric] | |
| 6 | 8 | |
| 7 | 9 | def choose_metric |
| 8 | 10 | @mezuro_configuration_id = params[:mezuro_configuration_id].to_i |
| ... | ... | @@ -26,6 +28,14 @@ class MetricConfigurationsController < ApplicationController |
| 26 | 28 | end |
| 27 | 29 | end |
| 28 | 30 | |
| 31 | + def destroy | |
| 32 | + @metric_configuration.destroy | |
| 33 | + respond_to do |format| | |
| 34 | + format.html { redirect_to mezuro_configuration_path(@metric_configuration.configuration_id) } | |
| 35 | + format.json { head :no_content } | |
| 36 | + end | |
| 37 | + end | |
| 38 | + | |
| 29 | 39 | private |
| 30 | 40 | |
| 31 | 41 | # Never trust parameters from the scary internet, only allow the white list through. |
| ... | ... | @@ -49,4 +59,8 @@ class MetricConfigurationsController < ApplicationController |
| 49 | 59 | failed_action(format, 'new') |
| 50 | 60 | end |
| 51 | 61 | end |
| 62 | + | |
| 63 | + def set_metric_configuration | |
| 64 | + @metric_configuration = MetricConfiguration.find(params[:id].to_i) | |
| 65 | + end | |
| 52 | 66 | end | ... | ... |
app/views/mezuro_configurations/_metric_configurations.html.erb
| ... | ... | @@ -8,7 +8,7 @@ |
| 8 | 8 | class: 'btn btn-info' %> |
| 9 | 9 | </td> |
| 10 | 10 | <td> |
| 11 | - <%= link_to 'Destroy', mezuro_configuration_metric_configurations_path(@mezuro_configuration.id, metric_configuration.id), | |
| 11 | + <%= link_to 'Destroy', mezuro_configuration_metric_configuration_path(@mezuro_configuration.id, metric_configuration.id), | |
| 12 | 12 | method: :delete, data: { confirm: 'Are you sure that you want to destroy this metric configuration?' }, |
| 13 | 13 | class: 'btn btn-danger' %> |
| 14 | 14 | </td> | ... | ... |
app/views/mezuro_configurations/show.html.erb
| ... | ... | @@ -27,9 +27,7 @@ |
| 27 | 27 | <% if @mezuro_configuration_metric_configurations.size == 0 %> |
| 28 | 28 | <%= render partial: 'no_metric_configurations' %> |
| 29 | 29 | <% else %> |
| 30 | - <% cache do %> | |
| 31 | - <%= render partial: 'metric_configurations', collection: @mezuro_configuration_metric_configurations, as: :metric_configuration %> | |
| 32 | - <% end %> | |
| 30 | + <%= render partial: 'metric_configurations', collection: @mezuro_configuration_metric_configurations, as: :metric_configuration %> | |
| 33 | 31 | <% end %> |
| 34 | 32 | </tbody> |
| 35 | 33 | </table> | ... | ... |
spec/controllers/metric_configurations_controller_spec.rb
| ... | ... | @@ -2,7 +2,6 @@ require 'spec_helper' |
| 2 | 2 | |
| 3 | 3 | describe MetricConfigurationsController do |
| 4 | 4 | let(:mezuro_configuration) { FactoryGirl.build(:mezuro_configuration) } |
| 5 | - | |
| 6 | 5 | describe 'choose_metric' do |
| 7 | 6 | let(:base_tool) { FactoryGirl.build(:base_tool) } |
| 8 | 7 | before :each do |
| ... | ... | @@ -50,7 +49,7 @@ describe MetricConfigurationsController do |
| 50 | 49 | |
| 51 | 50 | describe 'create' do |
| 52 | 51 | 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 |
| 53 | - let(:mezuro_configuration) {FactoryGirl.build(:mezuro_configuration)} | |
| 52 | + let(:mezuro_configuration) { FactoryGirl.build(:mezuro_configuration) } | |
| 54 | 53 | let(:base_tool) { FactoryGirl.build(:base_tool) } |
| 55 | 54 | |
| 56 | 55 | before do |
| ... | ... | @@ -85,4 +84,46 @@ describe MetricConfigurationsController do |
| 85 | 84 | end |
| 86 | 85 | end |
| 87 | 86 | end |
| 87 | + | |
| 88 | + describe 'destroy' do | |
| 89 | + let(:metric_configuration) { FactoryGirl.build(:metric_configuration) } | |
| 90 | + | |
| 91 | + context 'with an User logged in' do | |
| 92 | + before do | |
| 93 | + sign_in FactoryGirl.create(:user) | |
| 94 | + end | |
| 95 | + | |
| 96 | + context 'when the user owns the configuration' do | |
| 97 | + before :each do | |
| 98 | + subject.expects(:metric_configuration_owner?).returns true | |
| 99 | + metric_configuration.expects(:destroy) | |
| 100 | + MetricConfiguration.expects(:find).at_least_once.with(metric_configuration.id).returns(metric_configuration) | |
| 101 | + | |
| 102 | + delete :destroy, id: metric_configuration.id, mezuro_configuration_id: metric_configuration.configuration_id.to_s | |
| 103 | + end | |
| 104 | + | |
| 105 | + it { should redirect_to(mezuro_configuration_path(metric_configuration.configuration_id)) } | |
| 106 | + it { should respond_with(:redirect) } | |
| 107 | + end | |
| 108 | + | |
| 109 | + context "when the user doesn't own the configuration" do | |
| 110 | + before :each do | |
| 111 | + MetricConfiguration.expects(:find).at_least_once.with(metric_configuration.id).returns(metric_configuration) | |
| 112 | + | |
| 113 | + delete :destroy, id: metric_configuration.id, mezuro_configuration_id: mezuro_configuration.id.to_s | |
| 114 | + end | |
| 115 | + | |
| 116 | + it { should redirect_to(mezuro_configurations_path) } | |
| 117 | + it { should respond_with(:redirect) } | |
| 118 | + end | |
| 119 | + end | |
| 120 | + | |
| 121 | + context 'with no User logged in' do | |
| 122 | + before :each do | |
| 123 | + delete :destroy, id: metric_configuration.id, mezuro_configuration_id: mezuro_configuration.id.to_s | |
| 124 | + end | |
| 125 | + | |
| 126 | + it { should redirect_to new_user_session_path } | |
| 127 | + end | |
| 128 | + end | |
| 88 | 129 | end | ... | ... |