Commit 52d1958d59c7049971feba1092da0cbdb5638e6f

Authored by Rafael Manzo
1 parent 96cd3a3f

Compound Metric Configuration edition

Controller method
Refactored new into partials
Fixed destroy route
Factories for compound metrics
Update route with a helper

Missing:
  Acceptance tests
  Update action

Signed off by: Diego Araújo <diegoamc90@gmail.com>
app/controllers/compound_metric_configurations_controller.rb
1 include OwnershipAuthentication 1 include OwnershipAuthentication
  2 +include MetricConfigurationsConcern
2 3
3 class CompoundMetricConfigurationsController < ApplicationController 4 class CompoundMetricConfigurationsController < ApplicationController
4 before_action :authenticate_user!, except: [:index] 5 before_action :authenticate_user!, except: [:index]
5 before_action :mezuro_configuration_owner?, only: [:new, :create] 6 before_action :mezuro_configuration_owner?, only: [:new, :create]
6 - 7 + before_action :metric_configuration_owner?, only: [:edit]
  8 + before_action :set_metric_configuration, only: [:edit]
  9 + before_action :set_metric_configurations, only: [:new, :edit]
  10 +
7 # GET mezuro_configurations/1/compound_metric_configurations/new 11 # GET mezuro_configurations/1/compound_metric_configurations/new
8 def new 12 def new
9 @compound_metric_configuration = MetricConfiguration.new 13 @compound_metric_configuration = MetricConfiguration.new
10 @compound_metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i 14 @compound_metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i
11 - @metric_configurations = MetricConfiguration.metric_configurations_of(params[:mezuro_configuration_id].to_i)  
12 end 15 end
13 16
14 def create 17 def create
@@ -20,6 +23,11 @@ class CompoundMetricConfigurationsController &lt; ApplicationController @@ -20,6 +23,11 @@ class CompoundMetricConfigurationsController &lt; ApplicationController
20 end 23 end
21 end 24 end
22 25
  26 + def edit
  27 + @compound_metric_configuration = @metric_configuration
  28 + @compound_metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i
  29 + end
  30 +
23 private 31 private
24 32
25 # Never trust parameters from the scary internet, only allow the white list through. 33 # Never trust parameters from the scary internet, only allow the white list through.
@@ -44,4 +52,8 @@ class CompoundMetricConfigurationsController &lt; ApplicationController @@ -44,4 +52,8 @@ class CompoundMetricConfigurationsController &lt; ApplicationController
44 end 52 end
45 end 53 end
46 54
  55 + def set_metric_configurations
  56 + @metric_configurations = MetricConfiguration.metric_configurations_of(params[:mezuro_configuration_id].to_i)
  57 + end
  58 +
47 end 59 end
app/controllers/concerns/metric_configurations_concern.rb 0 → 100644
@@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
  1 +module MetricConfigurationsConcern
  2 + extend ActiveSupport::Concern
  3 +
  4 + def set_metric_configuration
  5 + @metric_configuration = MetricConfiguration.find(params[:id].to_i)
  6 + end
  7 +end
0 \ No newline at end of file 8 \ No newline at end of file
app/controllers/metric_configurations_controller.rb
1 include OwnershipAuthentication 1 include OwnershipAuthentication
  2 +include MetricConfigurationsConcern
2 3
3 class MetricConfigurationsController < ApplicationController 4 class MetricConfigurationsController < ApplicationController
4 before_action :authenticate_user!, except: [:index] 5 before_action :authenticate_user!, except: [:index]
@@ -29,6 +30,7 @@ class MetricConfigurationsController &lt; ApplicationController @@ -29,6 +30,7 @@ class MetricConfigurationsController &lt; ApplicationController
29 end 30 end
30 31
31 def edit 32 def edit
  33 + #FIXME: set the configuration id just once!
32 @mezuro_configuration_id = params[:mezuro_configuration_id] 34 @mezuro_configuration_id = params[:mezuro_configuration_id]
33 @metric_configuration.configuration_id = @mezuro_configuration_id 35 @metric_configuration.configuration_id = @mezuro_configuration_id
34 end 36 end
@@ -76,8 +78,4 @@ class MetricConfigurationsController &lt; ApplicationController @@ -76,8 +78,4 @@ class MetricConfigurationsController &lt; ApplicationController
76 failed_action(format, 'new') 78 failed_action(format, 'new')
77 end 79 end
78 end 80 end
79 -  
80 - def set_metric_configuration  
81 - @metric_configuration = MetricConfiguration.find(params[:id].to_i)  
82 - end  
83 end 81 end
app/views/compound_metric_configurations/_created_metric.html.erb 0 → 100644
@@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
  1 +<tr>
  2 + <td><%= metric_configuration.metric.name %></td>
  3 + <td><%= metric_configuration.code %></td>
  4 +</tr>
0 \ No newline at end of file 5 \ No newline at end of file
app/views/compound_metric_configurations/_created_metrics.html.erb 0 → 100644
@@ -0,0 +1,14 @@ @@ -0,0 +1,14 @@
  1 +<div id="created-metrics-accordion">
  2 + <h3> Created Metrics </h3>
  3 + <div>
  4 + <table class="table table-hover">
  5 + <thead>
  6 + <th>Name</th>
  7 + <th>Code</th>
  8 + </thead>
  9 + <tbody>
  10 + <%= render partial: 'created_metric', collection: metric_configurations, as: 'metric_configuration' %>
  11 + </tbody>
  12 + </table>
  13 + </div>
  14 +</div>
0 \ No newline at end of file 15 \ No newline at end of file
app/views/compound_metric_configurations/edit.html.erb 0 → 100644
@@ -0,0 +1,21 @@ @@ -0,0 +1,21 @@
  1 +<div class="page-header">
  2 + <h1>Edit Compound Metric Configuration</h1>
  3 +</div>
  4 +
  5 +<%= render partial: 'created_metrics', locals: {metric_configurations: @metric_configurations} %>
  6 +
  7 +<br>
  8 +
  9 +<%= form_for(@compound_metric_configuration, :url => mezuro_configuration_compound_metric_configuration_update_path(@compound_metric_configuration.configuration_id, @compound_metric_configuration.id)) do |f| %>
  10 + <%= render partial: 'form', locals: {f: f} %>
  11 +<% end %>
  12 +
  13 +<script type="text/javascript">
  14 + //Loads the accordion
  15 + $(function() {
  16 + $( "#created-metrics-accordion" ).accordion({
  17 + heightStyle: "content",
  18 + collapsible: true,
  19 + });
  20 + });
  21 +</script>
0 \ No newline at end of file 22 \ No newline at end of file
app/views/compound_metric_configurations/new.html.erb
@@ -2,25 +2,7 @@ @@ -2,25 +2,7 @@
2 <h1>New Compound Metric Configuration</h1> 2 <h1>New Compound Metric Configuration</h1>
3 </div> 3 </div>
4 4
5 -<div id="created-metrics-accordion">  
6 - <h3> Created Metrics </h3>  
7 - <div>  
8 - <table class="table table-hover">  
9 - <thead>  
10 - <th>Name</th>  
11 - <th>Code</th>  
12 - </thead>  
13 - <tbody>  
14 - <% @metric_configurations.each do |metric_configuration| %>  
15 - <tr>  
16 - <td><%= metric_configuration.metric.name %></td>  
17 - <td><%= metric_configuration.code %></td>  
18 - </tr>  
19 - <% end %>  
20 - </tbody>  
21 - </table>  
22 - </div>  
23 -</div> 5 +<%= render partial: 'created_metrics', locals: {metric_configurations: @metric_configurations} %>
24 6
25 <br> 7 <br>
26 8
config/routes.rb
@@ -16,7 +16,8 @@ Mezuro::Application.routes.draw do @@ -16,7 +16,8 @@ Mezuro::Application.routes.draw do
16 get '/metric_configurations/:metric_name/:base_tool_name/new' => 'metric_configurations#new', as: :new_metric_configuration 16 get '/metric_configurations/:metric_name/:base_tool_name/new' => 'metric_configurations#new', as: :new_metric_configuration
17 put '/metric_configurations/:id' => 'metric_configurations#update', as: :metric_configuration_update 17 put '/metric_configurations/:id' => 'metric_configurations#update', as: :metric_configuration_update
18 18
19 - resources :compound_metric_configurations, except: [:delete] 19 + resources :compound_metric_configurations, except: [:destroy, :update]
  20 + put '/compound_metric_configurations/:id' => 'compound_metric_configurations#update', as: :compound_metric_configuration_update
20 end 21 end
21 22
22 resources :reading_groups do 23 resources :reading_groups do
features/compound_metric_configuration/edition.feature 0 → 100644
@@ -0,0 +1,58 @@ @@ -0,0 +1,58 @@
  1 +Feature: Compound Metric Configuration edition
  2 + In order to interact with compound metric configurations
  3 + As a regular user
  4 + I should edit the informations of compound 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 + And I have a sample compound metric configuration within the given mezuro configuration
  14 + When I am at the Sample Configuration page
  15 + Then I should not see "Edit"
  16 +
  17 + @kalibro_restart
  18 + Scenario: editing a metric configuration successfully
  19 + Given I am a regular user
  20 + And I am signed in
  21 + And I own a sample configuration
  22 + And I have a sample reading group
  23 + And I have a sample metric configuration within the given mezuro configuration
  24 + And I have a sample compound metric configuration within the given mezuro configuration
  25 + And I am at the Sample Configuration page
  26 + When I click the Edit link
  27 + And I fill the Code field with "Another_Code"
  28 + And I press the Save button
  29 + Then I should see "Another_Code"
  30 +
  31 + @kalibro_restart @wip
  32 + Scenario: trying to edit with an existing code
  33 + Given I am a regular user
  34 + And I am signed in
  35 + And I own a sample configuration
  36 + And I have a sample reading group
  37 + And I have a sample metric configuration within the given mezuro configuration
  38 + And I have a sample compound metric configuration within the given mezuro configuration
  39 + And I have another compound metric configuration with code "Another_Code" within the given mezuro configuration
  40 + When I visit the sample compound metric configuration edit page
  41 + And I fill the Code field with "Another_Code"
  42 + And I press the Save button
  43 + Then I should see "Code There's already"
  44 +
  45 + @kalibro_restart @wip
  46 + Scenario: trying to edit with blank fields
  47 + Given I am a regular user
  48 + And I am signed in
  49 + And I own a sample configuration
  50 + And I have a sample reading group
  51 + And I have a sample metric configuration within the given mezuro configuration
  52 + And I have a sample compound metric configuration within the given mezuro configuration
  53 + When I visit the sample compound metric configuration edit page
  54 + And I fill the Code field with " "
  55 + And I fill the Weight field with " "
  56 + And I press the Save button
  57 + Then I should see "Code can't be blank"
  58 + And I should see "Weight can't be blank"
0 \ No newline at end of file 59 \ No newline at end of file
features/step_definitions/compound_metric_configuration_steps.rb
@@ -6,3 +6,14 @@ Given(/^I see the sample metric configuration code$/) do @@ -6,3 +6,14 @@ Given(/^I see the sample metric configuration code$/) do
6 page.should have_content(@metric_configuration.code) 6 page.should have_content(@metric_configuration.code)
7 end 7 end
8 8
  9 +Given(/^I have a sample compound metric configuration within the given mezuro configuration$/) do
  10 + @compound_metric_configuration = FactoryGirl.create(:compound_metric_configuration, {id: nil, configuration_id: @mezuro_configuration.id})
  11 +end
  12 +
  13 +Given(/^I have another compound metric configuration with code "(.*?)" within the given mezuro configuration$/) do |code|
  14 + FactoryGirl.create(:compound_metric_configuration, {id: nil, configuration_id: @mezuro_configuration.id, code: code})
  15 +end
  16 +
  17 +When(/^I visit the sample compound metric configuration edit page$/) do
  18 + visit edit_mezuro_configuration_compound_metric_configuration_path(@compound_metric_configuration.configuration_id, @compound_metric_configuration.id)
  19 +end
spec/controllers/compound_metric_configurations_controller_spec.rb
@@ -66,4 +66,42 @@ describe CompoundMetricConfigurationsController do @@ -66,4 +66,42 @@ describe CompoundMetricConfigurationsController do
66 end 66 end
67 end 67 end
68 end 68 end
  69 +
  70 + describe 'edit' do
  71 + let(:compound_metric_configuration) { FactoryGirl.build(:compound_metric_configuration) }
  72 +
  73 + context 'with an User logged in' do
  74 + before do
  75 + sign_in FactoryGirl.create(:user)
  76 + end
  77 +
  78 + context 'when the user owns the compound metric configuration' do
  79 + before :each do
  80 + subject.expects(:metric_configuration_owner?).returns(true)
  81 + MetricConfiguration.expects(:find).at_least_once.with(compound_metric_configuration.id).returns(compound_metric_configuration)
  82 + get :edit, id: compound_metric_configuration.id, mezuro_configuration_id: compound_metric_configuration.configuration_id.to_s
  83 + end
  84 +
  85 + it { should render_template(:edit) }
  86 + end
  87 +
  88 + context 'when the user does not own the compound metric configuration' do
  89 + before do
  90 + get :edit, id: compound_metric_configuration.id, mezuro_configuration_id: compound_metric_configuration.configuration_id.to_s
  91 + end
  92 +
  93 + it { should redirect_to(mezuro_configurations_path) } #FIXME : It should redirect to configuration show page
  94 + it { should respond_with(:redirect) }
  95 + it { should set_the_flash[:notice].to("You're not allowed to do this operation") }
  96 + end
  97 + end
  98 +
  99 + context 'with no user logged in' do
  100 + before :each do
  101 + get :edit, id: compound_metric_configuration.id, mezuro_configuration_id: compound_metric_configuration.configuration_id.to_s
  102 + end
  103 +
  104 + it { should redirect_to new_user_session_path }
  105 + end
  106 + end
69 end 107 end
spec/controllers/concerns/metric_configurations_concern_spec.rb 0 → 100644
@@ -0,0 +1,17 @@ @@ -0,0 +1,17 @@
  1 +require 'spec_helper'
  2 +
  3 +describe MetricConfigurationsConcern, type: :controller do
  4 + describe 'set_metric_configuration' do
  5 + let! (:metric_configuration){ FactoryGirl.build(:metric_configuration) }
  6 + let! (:metric_configurations_controller) { MetricConfigurationsController.new }
  7 +
  8 + before :each do
  9 + MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration)
  10 + metric_configurations_controller.params = {id: metric_configuration.id}
  11 + end
  12 +
  13 + it 'should assign metric_configuration' do
  14 + metric_configurations_controller.set_metric_configuration
  15 + end
  16 + end
  17 +end
0 \ No newline at end of file 18 \ No newline at end of file
spec/factories/metric_configurations.rb
1 FactoryGirl.define do 1 FactoryGirl.define do
2 factory :metric_configuration, class: MetricConfiguration do 2 factory :metric_configuration, class: MetricConfiguration do
3 id 1 3 id 1
4 - code 'code' 4 + code 'native'
5 metric {FactoryGirl.build(:metric)} 5 metric {FactoryGirl.build(:metric)}
6 base_tool_name "Analizo" 6 base_tool_name "Analizo"
7 weight 1 7 weight 1
@@ -9,4 +9,14 @@ FactoryGirl.define do @@ -9,4 +9,14 @@ FactoryGirl.define do
9 reading_group_id 1 9 reading_group_id 1
10 configuration_id 1 10 configuration_id 1
11 end 11 end
  12 +
  13 + factory :compound_metric_configuration, class: MetricConfiguration do
  14 + id 1
  15 + code 'compound'
  16 + metric {FactoryGirl.build(:compound_metric, {script: 'native*2;'})}
  17 + weight 1
  18 + aggregation_form "AVERAGE"
  19 + reading_group_id 1
  20 + configuration_id 1
  21 + end
12 end 22 end
13 \ No newline at end of file 23 \ No newline at end of file
spec/factories/metrics.rb
@@ -16,4 +16,13 @@ FactoryGirl.define do @@ -16,4 +16,13 @@ FactoryGirl.define do
16 script "" 16 script ""
17 language ["C", "CPP", "JAVA"] 17 language ["C", "CPP", "JAVA"]
18 end 18 end
  19 +
  20 + factory :compound_metric, class: KalibroGem::Entities::Metric do
  21 + name "Compound"
  22 + compound true
  23 + scope "CLASS"
  24 + description nil
  25 + script ""
  26 + language ["C", "CPP", "JAVA"]
  27 + end
19 end 28 end
20 \ No newline at end of file 29 \ No newline at end of file
spec/routing/compound_metric_configurations_routing_spec.rb
@@ -14,5 +14,7 @@ describe CompoundMetricConfigurationsController do @@ -14,5 +14,7 @@ describe CompoundMetricConfigurationsController do
14 to(controller: :compound_metric_configurations, action: :edit, mezuro_configuration_id: "1", id: "1") } 14 to(controller: :compound_metric_configurations, action: :edit, mezuro_configuration_id: "1", id: "1") }
15 it { should route(:put, '/mezuro_configurations/1/compound_metric_configurations/1'). 15 it { should route(:put, '/mezuro_configurations/1/compound_metric_configurations/1').
16 to(controller: :compound_metric_configurations, action: :update, mezuro_configuration_id: "1", id: "1") } 16 to(controller: :compound_metric_configurations, action: :update, mezuro_configuration_id: "1", id: "1") }
  17 + it { should_not route(:delete, '/mezuro_configurations/1/compound_metric_configurations/1').
  18 + to(controller: :compound_metric_configurations, action: :destroy, mezuro_configuration_id: "1", id: "1") }
17 end 19 end
18 end 20 end