Commit eea6f6fd9884b5f5a908eb07921ccae4e98793a3
Committed by
Rafael Manzo
1 parent
b51b35d7
Exists in
colab
and in
4 other branches
Destroy action to mezuro ranges
Showing
2 changed files
with
56 additions
and
9 deletions
Show diff stats
app/controllers/mezuro_ranges_controller.rb
| ... | ... | @@ -2,7 +2,8 @@ include OwnershipAuthentication |
| 2 | 2 | |
| 3 | 3 | class MezuroRangesController < ApplicationController |
| 4 | 4 | before_action :authenticate_user!, except: [:show] |
| 5 | - before_action :metric_configuration_owner?, only: [:new, :create] | |
| 5 | + before_action :metric_configuration_owner?, only: [:new, :create, :destroy] | |
| 6 | + before_action :get_url_params, only: [:create, :destroy] | |
| 6 | 7 | |
| 7 | 8 | def new |
| 8 | 9 | @mezuro_range = MezuroRange.new |
| ... | ... | @@ -11,8 +12,6 @@ class MezuroRangesController < ApplicationController |
| 11 | 12 | |
| 12 | 13 | def create |
| 13 | 14 | @mezuro_range = MezuroRange.new(mezuro_range_params) |
| 14 | - @mezuro_configuration_id = params[:mezuro_configuration_id].to_i | |
| 15 | - @metric_configuration_id = params[:metric_configuration_id].to_i | |
| 16 | 15 | @mezuro_range.metric_configuration_id = params[:metric_configuration_id].to_i |
| 17 | 16 | respond_to do |format| |
| 18 | 17 | create_and_redir(format) |
| ... | ... | @@ -20,6 +19,13 @@ class MezuroRangesController < ApplicationController |
| 20 | 19 | end |
| 21 | 20 | |
| 22 | 21 | def destroy |
| 22 | + @mezuro_range = MezuroRange.find(params[:id].to_i) | |
| 23 | + @mezuro_range.destroy | |
| 24 | + respond_to do |format| | |
| 25 | + format.html { redirect_to mezuro_configuration_metric_configuration_path( | |
| 26 | + @mezuro_configuration_id, @metric_configuration_id) } | |
| 27 | + format.json { head :no_content } | |
| 28 | + end | |
| 23 | 29 | end |
| 24 | 30 | |
| 25 | 31 | def update |
| ... | ... | @@ -50,10 +56,14 @@ class MezuroRangesController < ApplicationController |
| 50 | 56 | end |
| 51 | 57 | |
| 52 | 58 | def before_form |
| 53 | - @mezuro_configuration_id = params[:mezuro_configuration_id].to_i | |
| 54 | - @metric_configuration_id = params[:metric_configuration_id].to_i | |
| 59 | + get_url_params | |
| 55 | 60 | @reading_group_id = MetricConfiguration.find(@metric_configuration_id).reading_group_id |
| 56 | 61 | @readings = Reading.readings_of(@reading_group_id) |
| 57 | 62 | end |
| 58 | 63 | |
| 64 | + def get_url_params | |
| 65 | + @mezuro_configuration_id = params[:mezuro_configuration_id].to_i | |
| 66 | + @metric_configuration_id = params[:metric_configuration_id].to_i | |
| 67 | + end | |
| 68 | + | |
| 59 | 69 | end | ... | ... |
spec/controllers/mezuro_ranges_spec.rb
| 1 | 1 | require 'spec_helper' |
| 2 | 2 | |
| 3 | 3 | describe MezuroRangesController do |
| 4 | + let(:mezuro_range) { FactoryGirl.build(:mezuro_range, id: 1) } | |
| 5 | + let(:metric_configuration) { FactoryGirl.build(:metric_configuration) } | |
| 6 | + | |
| 4 | 7 | describe 'new' do |
| 5 | - let(:mezuro_range) { FactoryGirl.build(:mezuro_range) } | |
| 6 | 8 | let(:mezuro_configuration) { FactoryGirl.build(:mezuro_configuration) } |
| 7 | - let(:metric_configuration) { FactoryGirl.build(:metric_configuration) } | |
| 8 | 9 | |
| 9 | 10 | before :each do |
| 10 | 11 | sign_in FactoryGirl.create(:user) |
| ... | ... | @@ -35,8 +36,6 @@ describe MezuroRangesController do |
| 35 | 36 | describe 'create' do |
| 36 | 37 | let(:mezuro_range_params) { Hash[FactoryGirl.attributes_for(:mezuro_range).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 |
| 37 | 38 | let(:mezuro_configuration) { FactoryGirl.build(:mezuro_configuration) } |
| 38 | - let(:metric_configuration) { FactoryGirl.build(:metric_configuration) } | |
| 39 | - let(:mezuro_range) { FactoryGirl.build(:mezuro_range) } | |
| 40 | 39 | |
| 41 | 40 | before do |
| 42 | 41 | sign_in FactoryGirl.create(:user) |
| ... | ... | @@ -70,4 +69,42 @@ describe MezuroRangesController do |
| 70 | 69 | end |
| 71 | 70 | end |
| 72 | 71 | end |
| 72 | + | |
| 73 | + describe 'destroy' do | |
| 74 | + context 'with an User logged in' do | |
| 75 | + before do | |
| 76 | + sign_in FactoryGirl.create(:user) | |
| 77 | + end | |
| 78 | + | |
| 79 | + context 'when the user owns the metric configuration' do | |
| 80 | + before :each do | |
| 81 | + subject.expects(:metric_configuration_owner?).returns true | |
| 82 | + mezuro_range.expects(:destroy) | |
| 83 | + MezuroRange.expects(:find).at_least_once.with(mezuro_range.id).returns(mezuro_range) | |
| 84 | + | |
| 85 | + delete :destroy, id: mezuro_range.id.to_s, metric_configuration_id: metric_configuration.id.to_s, mezuro_configuration_id: metric_configuration.configuration_id.to_s | |
| 86 | + end | |
| 87 | + | |
| 88 | + it { should redirect_to(mezuro_configuration_metric_configuration_path(metric_configuration.configuration_id, metric_configuration.id)) } | |
| 89 | + it { should respond_with(:redirect) } | |
| 90 | + end | |
| 91 | + | |
| 92 | + context "when the user doesn't own the metric configuration" do | |
| 93 | + before :each do | |
| 94 | + delete :destroy, id: mezuro_range.id.to_s, metric_configuration_id: metric_configuration.id.to_s, mezuro_configuration_id: metric_configuration.configuration_id.to_s | |
| 95 | + end | |
| 96 | + | |
| 97 | + it { should redirect_to(mezuro_configurations_path) } #FIXME : It should redirect to configuration show page | |
| 98 | + it { should respond_with(:redirect) } | |
| 99 | + end | |
| 100 | + end | |
| 101 | + | |
| 102 | + context 'with no User logged in' do | |
| 103 | + before :each do | |
| 104 | + delete :destroy, id: mezuro_range.id.to_s, metric_configuration_id: metric_configuration.id.to_s, mezuro_configuration_id: metric_configuration.configuration_id.to_s | |
| 105 | + end | |
| 106 | + | |
| 107 | + it { should redirect_to new_user_session_path } | |
| 108 | + end | |
| 109 | + end | |
| 73 | 110 | end | ... | ... |