Commit b0caefdf66e240afa003fcc07b67a5cded624ef5
Committed by
Rafael Manzo
1 parent
5e4b1166
Exists in
colab
and in
4 other branches
View to create new Metric Configuration.
Pending combobox to basetool, its metrics, reading groups and agregation form. Still missing to some acceptance tests and fix the ownership helper to metric configuration controller. signed-off-by: Fellipe Souto Sampaio <fllsouto@gmail.com> signed-off-by: Renan Fichberg <rfichberg@gmail.com>
Showing
5 changed files
with
155 additions
and
1 deletions
Show diff stats
app/controllers/metric_configurations_controller.rb
| 1 | 1 | 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] | |
| 6 | + | |
| 7 | + def new | |
| 8 | + @mezuro_configuration_id = params[:mezuro_configuration_id].to_i | |
| 9 | + @metric_configuration = MetricConfiguration.new | |
| 10 | + end | |
| 11 | + | |
| 12 | + def create | |
| 13 | + @metric_configuration = MetricConfiguration.new(metric_configuration_params) | |
| 14 | + @metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i | |
| 15 | + respond_to do |format| | |
| 16 | + create_and_redir(format) | |
| 17 | + end | |
| 18 | + end | |
| 19 | + | |
| 20 | + private | |
| 21 | + | |
| 22 | + # Never trust parameters from the scary internet, only allow the white list through. | |
| 23 | + def metric_configuration_params | |
| 24 | + params[:metric_configuration] | |
| 25 | + end | |
| 26 | + | |
| 27 | + # Duplicated code on create and update actions extracted here | |
| 28 | + def failed_action(format, destiny_action) | |
| 29 | + @mezuro_configuration_id = params[:mezuro_configuration_id] | |
| 30 | + | |
| 31 | + format.html { render action: destiny_action } | |
| 32 | + format.json { render json: @metric_configuration.errors, status: :unprocessable_entity } | |
| 33 | + end | |
| 34 | + | |
| 35 | + #Code extracted from create action | |
| 36 | + def create_and_redir(format) | |
| 37 | + if @metric_configuration.save | |
| 38 | + format.html { redirect_to mezuro_configuration_path(@metric_configuration.configuration_id), notice: 'Metric Configuration was successfully created.' } | |
| 39 | + else | |
| 40 | + failed_action(format, 'new') | |
| 41 | + end | |
| 42 | + end | |
| 5 | 43 | end | ... | ... |
| ... | ... | @@ -0,0 +1,14 @@ |
| 1 | +<%= render :partial => 'shared/form_errors', :locals => {:object => @metric_configuration} %> | |
| 2 | + | |
| 3 | +<div class="form-group"> | |
| 4 | + <%= f.label :code, class: 'control-label' %><br> | |
| 5 | + <%= f.text_field :code, class: 'form-control' %> | |
| 6 | +</div> | |
| 7 | + | |
| 8 | +<div class="form-group"> | |
| 9 | + <%= f.label :weight, class: 'control-label' %><br> | |
| 10 | + <%= f.text_field :weight, class: 'form-control' %> | |
| 11 | +</div> | |
| 12 | + | |
| 13 | +<%= f.submit 'Save', class: 'btn btn-primary' %> | |
| 14 | +<%= link_to 'Back', mezuro_configuration_path(@mezuro_configuration_id), class: 'btn btn-default' %> | ... | ... |
| ... | ... | @@ -0,0 +1,7 @@ |
| 1 | +<div class="page-header"> | |
| 2 | + <h1>New Metric Configuration</h1> | |
| 3 | +</div> | |
| 4 | + | |
| 5 | +<%= form_for(@metric_configuration, :url => mezuro_configuration_metric_configurations_path(@mezuro_configuration_id)) do |f| %> | |
| 6 | + <%= render partial: 'form', locals: {f: f} %> | |
| 7 | +<% end %> | ... | ... |
| ... | ... | @@ -0,0 +1,30 @@ |
| 1 | +Feature: Metric Configuration Creation | |
| 2 | + In order to register my metric configurations | |
| 3 | + As a regular user | |
| 4 | + I should be able to create metric configurations | |
| 5 | + | |
| 6 | + @kalibro_restart | |
| 7 | + Scenario: Should not create metric configurations without login | |
| 8 | + Given I have a sample configuration | |
| 9 | + And I am at the Sample Configuration page | |
| 10 | + Then I should not see New Metric Configuration | |
| 11 | + | |
| 12 | + @kalibro_restart @wip | |
| 13 | + Scenario: metric configuration creation | |
| 14 | + Given I am a regular user | |
| 15 | + And I am signed in | |
| 16 | + And I own a sample configuration | |
| 17 | + And I have a reading group named "Schoolar" | |
| 18 | + And I am at the Sample Configuration page | |
| 19 | + And I click the Add Metric link | |
| 20 | + And I fill the Code field with "My Code" | |
| 21 | + And I set the select field "BaseTool" as "Analizo" | |
| 22 | + And I set the select field "Metric" as "Lines Of Code" | |
| 23 | + And I fill the Description field with "Web Service to collect metrics" | |
| 24 | + And I fill the Weigth field with "2" | |
| 25 | + And I set the select field "AgregationForm" as "Average" | |
| 26 | + And I set the select field "ReadingGroup" as "Schoolar" | |
| 27 | + When I press the Save button | |
| 28 | + Then I should see "My Code" | |
| 29 | + Then I should see "Lines Of Code" | |
| 30 | + Then I should see "2" | |
| 0 | 31 | \ No newline at end of file | ... | ... |
spec/controllers/metric_configurations_controller_spec.rb
0 → 100644
| ... | ... | @@ -0,0 +1,65 @@ |
| 1 | +require 'spec_helper' | |
| 2 | + | |
| 3 | +describe MetricConfigurationsController do | |
| 4 | + let(:mezuro_configuration) { FactoryGirl.build(:mezuro_configuration) } | |
| 5 | + | |
| 6 | + describe 'new' do | |
| 7 | + before :each do | |
| 8 | + sign_in FactoryGirl.create(:user) | |
| 9 | + end | |
| 10 | + | |
| 11 | + context 'when the current user owns the mezuro configuration' do | |
| 12 | + before :each do | |
| 13 | + subject.expects(:mezuro_configuration_owner?).returns true | |
| 14 | + get :new, mezuro_configuration_id: mezuro_configuration.id | |
| 15 | + end | |
| 16 | + | |
| 17 | + it { should respond_with(:success) } | |
| 18 | + it { should render_template(:new) } | |
| 19 | + end | |
| 20 | + | |
| 21 | + context "when the current user doesn't owns the mezuro configuration" do | |
| 22 | + before :each do | |
| 23 | + get :new, mezuro_configuration_id: mezuro_configuration.id | |
| 24 | + end | |
| 25 | + | |
| 26 | + it { should redirect_to(mezuro_configurations_url) } | |
| 27 | + it { should respond_with(:redirect) } | |
| 28 | + end | |
| 29 | + end | |
| 30 | + | |
| 31 | + describe 'create' do | |
| 32 | + 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 | |
| 33 | + let(:mezuro_configuration) {FactoryGirl.build(:mezuro_configuration)} | |
| 34 | + | |
| 35 | + before do | |
| 36 | + sign_in FactoryGirl.create(:user) | |
| 37 | + end | |
| 38 | + | |
| 39 | + context 'when the current user owns the reading group' do | |
| 40 | + before :each do | |
| 41 | + subject.expects(:mezuro_configuration_owner?).returns true | |
| 42 | + end | |
| 43 | + | |
| 44 | + context 'with valid fields' do | |
| 45 | + before :each do | |
| 46 | + MetricConfiguration.any_instance.expects(:save).returns(true) | |
| 47 | + | |
| 48 | + post :create, mezuro_configuration_id: mezuro_configuration.id, metric_configuration: metric_configuration_params | |
| 49 | + end | |
| 50 | + | |
| 51 | + it { should respond_with(:redirect) } | |
| 52 | + end | |
| 53 | + | |
| 54 | + context 'with invalid fields' do | |
| 55 | + before :each do | |
| 56 | + MetricConfiguration.any_instance.expects(:save).returns(false) | |
| 57 | + | |
| 58 | + post :create, mezuro_configuration_id: mezuro_configuration.id, metric_configuration: metric_configuration_params | |
| 59 | + end | |
| 60 | + | |
| 61 | + it { should render_template(:new) } | |
| 62 | + end | |
| 63 | + end | |
| 64 | + end | |
| 65 | +end | |
| 0 | 66 | \ No newline at end of file | ... | ... |