Commit 8c234ac72b1bf0df12bbdee0defacd02846bbdc2
Committed by
Rafael Manzo
1 parent
422afd23
Exists in
colab
and in
4 other branches
Edit feature for metric configuration.
signed-off-by: Guilherme Rojas V. de Lima <guilhermehrojas@gmail.com>
Showing
5 changed files
with
135 additions
and
6 deletions
Show diff stats
app/controllers/metric_configurations_controller.rb
| ... | ... | @@ -2,8 +2,8 @@ include OwnershipAuthentication |
| 2 | 2 | |
| 3 | 3 | class MetricConfigurationsController < ApplicationController |
| 4 | 4 | before_action :authenticate_user!, except: [:index] |
| 5 | - before_action :set_metric_configuration, only: [:edit, :destroy] | |
| 6 | - before_action :metric_configuration_owner?, only: [:edit, :destroy] | |
| 5 | + before_action :set_metric_configuration, only: [:edit, :update, :destroy] | |
| 6 | + before_action :metric_configuration_owner?, only: [:edit, :update, :destroy] | |
| 7 | 7 | before_action :mezuro_configuration_owner?, only: [:new, :create, :choose_metric] |
| 8 | 8 | |
| 9 | 9 | def choose_metric |
| ... | ... | @@ -33,6 +33,18 @@ class MetricConfigurationsController < ApplicationController |
| 33 | 33 | @metric_configuration.configuration_id = @mezuro_configuration_id |
| 34 | 34 | end |
| 35 | 35 | |
| 36 | + def update | |
| 37 | + respond_to do |format| | |
| 38 | + @metric_configuration.configuration_id = params[:mezuro_configuration_id] | |
| 39 | + if @metric_configuration.update(metric_configuration_params) | |
| 40 | + format.html { redirect_to(mezuro_configuration_path(@metric_configuration.configuration_id), notice: 'Metric Configuration was successfully updated.') } | |
| 41 | + format.json { head :no_content } | |
| 42 | + else | |
| 43 | + failed_action(format, 'edit') | |
| 44 | + end | |
| 45 | + end | |
| 46 | + end | |
| 47 | + | |
| 36 | 48 | def destroy |
| 37 | 49 | @metric_configuration.destroy |
| 38 | 50 | respond_to do |format| | ... | ... |
app/views/metric_configurations/_form.html.erb
| ... | ... | @@ -15,10 +15,14 @@ |
| 15 | 15 | <%= f.select( :aggregation_form, aggregation_options, {class: 'form-control'} ) %> |
| 16 | 16 | </div> |
| 17 | 17 | |
| 18 | -<div class="form-group"> | |
| 19 | - <%= f.label :reading_group_id, 'Reading Group', class: 'control-label' %> | |
| 20 | - <%= f.select( :reading_group_id, reading_group_options, {class: 'form-control'} ) %> | |
| 21 | -</div> | |
| 18 | +<% if @metric_configuration.persisted? %> | |
| 19 | + <%= hidden_field_tag(:reading_group_id, @metric_configuration.reading_group_id) %> | |
| 20 | +<% else %> | |
| 21 | + <div class="form-group"> | |
| 22 | + <%= f.label :reading_group_id, 'Reading Group', class: 'control-label' %> | |
| 23 | + <%= f.select( :reading_group_id, reading_group_options, {class: 'form-control'} ) %> | |
| 24 | + </div> | |
| 25 | +<% end %> | |
| 22 | 26 | |
| 23 | 27 | <%= hidden_field_tag(:metric_name, @metric_configuration.metric.name) %> |
| 24 | 28 | <%= hidden_field_tag(:base_tool_name, @metric_configuration.base_tool_name) %> | ... | ... |
| ... | ... | @@ -0,0 +1,54 @@ |
| 1 | +Feature: Metric Configuration edition | |
| 2 | + In order to interact with metric configurations | |
| 3 | + As a regular user | |
| 4 | + I should edit the informations of metric configurations | |
| 5 | + | |
| 6 | + @kalibro_restart | |
| 7 | + Scenario: the configuration is not mine | |
| 8 | + Given I am a regular user | |
| 9 | + And I am signed in | |
| 10 | + And I have a sample configuration | |
| 11 | + And I have a sample reading group | |
| 12 | + And I have a sample metric configuration within the given mezuro configuration | |
| 13 | + When I am at the Sample Configuration page | |
| 14 | + Then I should not see "Edit" | |
| 15 | + | |
| 16 | + @kalibro_restart | |
| 17 | + Scenario: editing a metric configuration successfully | |
| 18 | + Given I am a regular user | |
| 19 | + And I am signed in | |
| 20 | + And I own a sample configuration | |
| 21 | + And I have a sample reading group | |
| 22 | + And I have a sample metric configuration within the given mezuro configuration | |
| 23 | + And I am at the Sample Configuration page | |
| 24 | + When I click the Edit link | |
| 25 | + And I fill the Code field with "Another_Code" | |
| 26 | + And I press the Save button | |
| 27 | + Then I should see "Another_Code" | |
| 28 | + | |
| 29 | + @kalibro_restart | |
| 30 | + Scenario: trying to edit with an existing code | |
| 31 | + Given I am a regular user | |
| 32 | + And I am signed in | |
| 33 | + And I own a sample configuration | |
| 34 | + And I have a sample reading group | |
| 35 | + And I have a sample metric configuration within the given mezuro configuration | |
| 36 | + And I have another metric configuration with code "Another_Code" within the given mezuro configuration | |
| 37 | + When I visit the sample metric configuration edit page | |
| 38 | + And I fill the Code field with "Another_Code" | |
| 39 | + And I press the Save button | |
| 40 | + Then I should see "Code There's already" | |
| 41 | + | |
| 42 | + @kalibro_restart | |
| 43 | + Scenario: trying to edit with blank fields | |
| 44 | + Given I am a regular user | |
| 45 | + And I am signed in | |
| 46 | + And I own a sample configuration | |
| 47 | + And I have a sample reading group | |
| 48 | + And I have a sample metric configuration within the given mezuro configuration | |
| 49 | + When I visit the sample metric configuration edit page | |
| 50 | + And I fill the Code field with " " | |
| 51 | + And I fill the Weight field with " " | |
| 52 | + And I press the Save button | |
| 53 | + Then I should see "Code can't be blank" | |
| 54 | + And I should see "Weight can't be blank" | |
| 0 | 55 | \ No newline at end of file | ... | ... |
features/step_definitions/metric_configuration_steps.rb
| ... | ... | @@ -3,6 +3,15 @@ Given(/^I have a sample metric configuration within the given mezuro configurati |
| 3 | 3 | {id: nil, configuration_id: @mezuro_configuration.id, reading_group_id: @reading_group.id} ) |
| 4 | 4 | end |
| 5 | 5 | |
| 6 | +Given(/^I have another metric configuration with code "(.*?)" within the given mezuro configuration$/) do |code| | |
| 7 | + @another_metric_configuration = FactoryGirl.create(:metric_configuration, | |
| 8 | + {id: nil, configuration_id: @mezuro_configuration.id, reading_group_id: @reading_group.id, code: code} ) | |
| 9 | +end | |
| 10 | + | |
| 11 | +When(/^I visit the sample metric configuration edit page$/) do | |
| 12 | + visit edit_mezuro_configuration_metric_configuration_path(@metric_configuration.configuration_id, @metric_configuration.id) | |
| 13 | +end | |
| 14 | + | |
| 6 | 15 | Then(/^I should see the sample metric configuration content$/) do |
| 7 | 16 | page.should have_content(@metric_configuration.metric.name) |
| 8 | 17 | page.should have_content(@metric_configuration.code) | ... | ... |
spec/controllers/metric_configurations_controller_spec.rb
| ... | ... | @@ -124,6 +124,56 @@ describe MetricConfigurationsController do |
| 124 | 124 | end |
| 125 | 125 | end |
| 126 | 126 | |
| 127 | + describe 'update' do | |
| 128 | + let(:metric_configuration) { FactoryGirl.build(:metric_configuration) } | |
| 129 | + 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 sybols and integers | |
| 130 | + | |
| 131 | + context 'when the user is logged in' do | |
| 132 | + before do | |
| 133 | + sign_in FactoryGirl.create(:user) | |
| 134 | + end | |
| 135 | + | |
| 136 | + context 'when user owns the metric configuration' do | |
| 137 | + before :each do | |
| 138 | + subject.expects(:metric_configuration_owner?).returns true | |
| 139 | + end | |
| 140 | + | |
| 141 | + context 'with valid fields' do | |
| 142 | + before :each do | |
| 143 | + MetricConfiguration.expects(:find).at_least_once.with(metric_configuration.id).returns(metric_configuration) | |
| 144 | + MetricConfiguration.any_instance.expects(:update).with(metric_configuration_params).returns(true) | |
| 145 | + | |
| 146 | + post :update, mezuro_configuration_id: metric_configuration.configuration_id, id: metric_configuration.id, metric_configuration: metric_configuration_params | |
| 147 | + end | |
| 148 | + | |
| 149 | + it { should redirect_to(mezuro_configuration_path(metric_configuration.configuration_id)) } | |
| 150 | + it { should respond_with(:redirect) } | |
| 151 | + end | |
| 152 | + | |
| 153 | + context 'with an invalid field' do | |
| 154 | + before :each do | |
| 155 | + MetricConfiguration.expects(:find).at_least_once.with(metric_configuration.id).returns(metric_configuration) | |
| 156 | + MetricConfiguration.any_instance.expects(:update).with(metric_configuration_params).returns(false) | |
| 157 | + | |
| 158 | + post :update, mezuro_configuration_id: metric_configuration.configuration_id, id: metric_configuration.id, metric_configuration: metric_configuration_params | |
| 159 | + end | |
| 160 | + | |
| 161 | + it { should render_template(:edit) } | |
| 162 | + end | |
| 163 | + end | |
| 164 | + | |
| 165 | + context 'when the user does not own the reading' do | |
| 166 | + before :each do | |
| 167 | + MetricConfiguration.expects(:find).at_least_once.with(metric_configuration.id).returns(metric_configuration) | |
| 168 | + | |
| 169 | + post :update, mezuro_configuration_id: metric_configuration.configuration_id, id: metric_configuration.id, metric_configuration: metric_configuration_params | |
| 170 | + end | |
| 171 | + | |
| 172 | + it { should redirect_to mezuro_configurations_path } | |
| 173 | + end | |
| 174 | + end | |
| 175 | + end | |
| 176 | + | |
| 127 | 177 | |
| 128 | 178 | describe 'destroy' do |
| 129 | 179 | let(:metric_configuration) { FactoryGirl.build(:metric_configuration) } | ... | ... |