Commit 073a83daea5cb9ccd0d2ebbaf28a4000389884f7

Authored by Renan Fichberg
Committed by Rafael Manzo
1 parent f84f8d0b

Reading update

Missing show

Signed-off by: Rafael Reggiani Manzo <rr.manzo@gmail.com>
app/controllers/readings_controller.rb
... ... @@ -23,6 +23,20 @@ class ReadingsController &lt; ApplicationController
23 23 @reading_group_id = params[:reading_group_id]
24 24 end
25 25  
  26 + # PUT /reading_groups/1/readings/1
  27 + # PUT /reading_groups/1/readings/1.json
  28 + def update
  29 + @reading.group_id = params[:reading_group_id].to_i
  30 + respond_to do |format|
  31 + if @reading.update(reading_params)
  32 + format.html { redirect_to(reading_group_reading_path(params[:reading_group_id].to_i, @reading.id), notice: 'Reading was successfully updated.') }
  33 + format.json { head :no_content }
  34 + else
  35 + failed_action(format, 'edit')
  36 + end
  37 + end
  38 + end
  39 +
26 40 private
27 41  
28 42 # Never trust parameters from the scary internet, only allow the white list through.
... ...
config/routes.rb
... ... @@ -13,7 +13,7 @@ Mezuro::Application.routes.draw do
13 13 end
14 14  
15 15 resources :reading_groups do
16   - resources :readings, except: [:index, :show, :update]
  16 + resources :readings, except: [:index, :update]
17 17 put '/readings/:id' => 'readings#update', as: :reading_update
18 18 end
19 19  
... ...
features/reading/edit.feature
... ... @@ -3,7 +3,7 @@ Feature: Reading Edit
3 3 As a regular user
4 4 I should be able to edit my readings
5 5  
6   -@kalibro_restart @wip
  6 +@kalibro_restart @wip
7 7 Scenario: editing a reading successfully
8 8 Given I am a regular user
9 9 And I am signed in
... ... @@ -19,29 +19,29 @@ Feature: Reading Edit
19 19 And I should see "10.5"
20 20 And I should see "33dd33"
21 21  
22   - @kalibro_restart @wip
  22 + @kalibro_restart
23 23 Scenario: editing a reading with blank fields
24 24 Given I am a regular user
25 25 And I am signed in
26   - And I own a sample project
27   - And I have a sample configuration with native metrics
28   - And I have a sample repository within the sample project named "QtCalculator"
29   - And I am at repository edit page
30   - When I fill the Name field with " "
31   - And I fill the Address field with " "
  26 + And I own a sample reading group
  27 + And I have a sample reading within the sample reading group
  28 + And I am at the Edit Reading page
  29 + When I fill the Label field with " "
  30 + And I fill the Grade field with " "
  31 + And I fill the Color field with " "
32 32 And I press the Save button
33   - Then I should see "Name can't be blank"
34   - And I should see "Address can't be blank"
  33 + Then I should see "Label can't be blank"
  34 + And I should see "Grade can't be blank"
  35 + And I should see "Color can't be blank"
35 36  
36   - @kalibro_restart @wip
  37 + @kalibro_restart
37 38 Scenario: editing a reading with already taken name
38 39 Given I am a regular user
39 40 And I am signed in
40   - And I own a sample project
41   - And I have a sample configuration with native metrics
42   - And I have a sample repository within the sample project named "MedSquare"
43   - And I have a sample repository within the sample project named "QtCalculator"
44   - And I am at repository edit page
45   - When I fill the Name field with "MedSquare"
  41 + And I own a sample reading group
  42 + And I have a sample reading within the sample reading group labeled "Average"
  43 + And I have a sample reading within the sample reading group labeled "Good"
  44 + And I am at the Edit Reading page
  45 + When I fill the Label field with "Average"
46 46 And I press the Save button
47   - Then I should see "There's already"
48 47 \ No newline at end of file
  48 + Then I should see "There's already"
... ...
features/step_definitions/reading_steps.rb
... ... @@ -9,3 +9,7 @@ end
9 9 Given(/^I am at the Edit Reading page$/) do
10 10 visit edit_reading_group_reading_url(@reading_group.id, @reading.id)
11 11 end
  12 +
  13 +Given(/^I have a sample reading within the sample reading group labeled "(.*?)"$/) do |label|
  14 + @reading = FactoryGirl.create(:reading, {label: label, group_id: @reading_group.id, id: nil})
  15 +end
... ...
spec/controllers/readings_controller_spec.rb
... ... @@ -102,4 +102,63 @@ describe ReadingsController do
102 102 it { should redirect_to new_user_session_path }
103 103 end
104 104 end
  105 +
  106 + describe 'update' do
  107 + let(:reading) { FactoryGirl.build(:reading) }
  108 + let(:reading_params) { Hash[FactoryGirl.attributes_for(:reading).map { |k,v| [k.to_s, v.to_s] }] } #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers
  109 +
  110 + context 'when the user is logged in' do
  111 + before do
  112 + sign_in FactoryGirl.create(:user)
  113 + end
  114 +
  115 + context 'when user owns the reading' do
  116 + before :each do
  117 + subject.expects(:reading_owner?).returns true
  118 + end
  119 +
  120 + context 'with valid fields' do
  121 + before :each do
  122 + Reading.expects(:find).at_least_once.with(reading.id).returns(reading)
  123 + Reading.any_instance.expects(:update).with(reading_params).returns(true)
  124 +
  125 + post :update, reading_group_id: reading_group.id, id: reading.id, reading: reading_params
  126 + end
  127 +
  128 + it { should redirect_to(reading_group_reading_path(reading_group.id, reading.id)) }
  129 + it { should respond_with(:redirect) }
  130 + end
  131 +
  132 + context 'with an invalid field' do
  133 + before :each do
  134 + Reading.expects(:find).at_least_once.with(reading.id).returns(reading)
  135 + Reading.any_instance.expects(:update).with(reading_params).returns(false)
  136 +
  137 + post :update, reading_group_id: reading_group.id, id: reading.id, reading: reading_params
  138 + end
  139 +
  140 + it { should render_template(:edit) }
  141 + end
  142 + end
  143 +
  144 + context 'when the user does not own the reading' do
  145 + before :each do
  146 + Reading.expects(:find).at_least_once.with(reading.id).returns(reading)
  147 +
  148 + post :update, reading_group_id: reading_group.id, id: reading.id, reading: reading_params
  149 + end
  150 +
  151 + it { should redirect_to reading_group_path(reading_group.id) }
  152 + end
  153 + end
  154 +
  155 + context 'with no user logged in' do
  156 + before :each do
  157 + post :update, reading_group_id: reading_group.id, id: reading.id, reading: reading_params
  158 + end
  159 +
  160 + it { should redirect_to new_user_session_path }
  161 + end
  162 + end
  163 +
105 164 end
106 165 \ No newline at end of file
... ...
spec/routing/readings_routing_spec.rb
... ... @@ -8,7 +8,7 @@ describe ReadingsController do
8 8 to(controller: :readings, action: :new, reading_group_id: 1) }
9 9 it { should route(:get, '/reading_groups/1/readings/1/edit').
10 10 to(controller: :readings, action: :edit, reading_group_id: 1, id: 1) }
11   - it { should_not route(:get, '/reading_groups/1/readings/1').
  11 + it { should route(:get, '/reading_groups/1/readings/1').
12 12 to(controller: :readings, action: :show, reading_group_id: 1, id: 1) }
13 13 it { should route(:delete, '/reading_groups/1/readings/1').
14 14 to(controller: :readings, action: :destroy, reading_group_id: 1, id: 1) }
... ...