From 55caadf6475eaa61990d85933186933bd054ef0f Mon Sep 17 00:00:00 2001 From: Diego Araújo Date: Tue, 11 Feb 2014 14:21:13 -0200 Subject: [PATCH] Compound metric configuration creation. --- app/controllers/compound_metric_configurations_controller.rb | 36 +++++++++++++++++++++++++++++++++++- app/views/compound_metric_configurations/new.html.erb | 32 +++++++++++++++++++++++++++++++- features/compound_metric_configuration/create.feature | 21 ++++++++++++--------- features/step_definitions/compound_metric_configuration_steps.rb | 8 ++++++++ spec/controllers/compound_metric_configurations_controller_spec.rb | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 125 insertions(+), 11 deletions(-) create mode 100644 features/step_definitions/compound_metric_configuration_steps.rb diff --git a/app/controllers/compound_metric_configurations_controller.rb b/app/controllers/compound_metric_configurations_controller.rb index 8cc0616..857b67d 100644 --- a/app/controllers/compound_metric_configurations_controller.rb +++ b/app/controllers/compound_metric_configurations_controller.rb @@ -2,12 +2,46 @@ include OwnershipAuthentication class CompoundMetricConfigurationsController < ApplicationController before_action :authenticate_user!, except: [:index] - before_action :mezuro_configuration_owner?, only: [:new] + before_action :mezuro_configuration_owner?, only: [:new, :create] # GET mezuro_configurations/1/compound_metric_configurations/new def new @compound_metric_configuration = MetricConfiguration.new @compound_metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i + @metric_configurations = MetricConfiguration.metric_configurations_of(params[:mezuro_configuration_id].to_i) + end + + def create + @compound_metric_configuration = MetricConfiguration.new(metric_configuration_params) + @compound_metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i + @compound_metric_configuration.metric.compound = true + respond_to do |format| + create_and_redir(format) + end + end + + private + + # Never trust parameters from the scary internet, only allow the white list through. + def metric_configuration_params + params[:metric_configuration] + end + + # Duplicated code on create and update actions extracted here + def failed_action(format, destiny_action) + @mezuro_configuration_id = params[:mezuro_configuration_id] + + format.html { render action: destiny_action } + format.json { render json: @compound_metric_configuration.errors, status: :unprocessable_entity } + end + + #Code extracted from create action + def create_and_redir(format) + if @compound_metric_configuration.save + format.html { redirect_to mezuro_configuration_path(@compound_metric_configuration.configuration_id), notice: 'Compound Metric Configuration was successfully created.' } + else + failed_action(format, 'new') + end end end diff --git a/app/views/compound_metric_configurations/new.html.erb b/app/views/compound_metric_configurations/new.html.erb index 468adf4..5f37546 100644 --- a/app/views/compound_metric_configurations/new.html.erb +++ b/app/views/compound_metric_configurations/new.html.erb @@ -2,8 +2,38 @@

New Compound Metric Configuration

+
+

Created Metrics

+
+ + + + + + + <% @metric_configurations.each do |metric_configuration| %> + + + + + <% end %> + +
NameCode
<%= metric_configuration.metric.name %><%= metric_configuration.code %>
+
+
+
-<%= form_for(@compound_metric_configuration, :url => mezuro_configuration_metric_configurations_path(@compound_metric_configuration.configuration_id)) do |f| %> +<%= form_for(@compound_metric_configuration, :url => mezuro_configuration_compound_metric_configurations_path(@compound_metric_configuration.configuration_id)) do |f| %> <%= render partial: 'form', locals: {f: f} %> <% end %> + + diff --git a/features/compound_metric_configuration/create.feature b/features/compound_metric_configuration/create.feature index 59bc0ac..2d508d7 100644 --- a/features/compound_metric_configuration/create.feature +++ b/features/compound_metric_configuration/create.feature @@ -3,25 +3,28 @@ Feature: Compound Metric Configuration Creation As a regular user I should be able to create compound metric configurations - #Missing create action and native metrics name and code - @kalibro_restart @wip + @kalibro_restart @javascript Scenario: compound metric configuration creation Given I am a regular user And I am signed in And I own a sample configuration And I have a reading group named "Scholar" + And I have a sample metric configuration within the given mezuro configuration And I am at the Sample Configuration page And I click the Add Metric link And I click the Compound Metric link - And I fill the Name field with "My Compound Metric" + When I click the "Created Metrics" h3 + Then I see the sample metric configuration name + And I see the sample metric configuration code + When I fill the Name field with "My Compound Metric" And I fill the Description field with "Some description" - And I fill the Code field with "My Code" + And I fill the Code field with "mcm" And I fill the Script field with "8*8;" - And I fill the Weight field with "2" + And I fill the Weight field with "8" And I set the select field "Scope" as "Class" And I set the select field "Aggregation Form" as "Average" And I set the select field "Reading Group" as "Scholar" - When I press the Save button - #Then I should see "My Code" - #Then I should see "Total Lines of Code" - #Then I should see "2" + And I press the Save button + Then I should see "My Compound Metric" + And I should see "mcm" + And I should see "8" diff --git a/features/step_definitions/compound_metric_configuration_steps.rb b/features/step_definitions/compound_metric_configuration_steps.rb new file mode 100644 index 0000000..9dabe52 --- /dev/null +++ b/features/step_definitions/compound_metric_configuration_steps.rb @@ -0,0 +1,8 @@ +Given(/^I see the sample metric configuration name$/) do + page.should have_content(@metric_configuration.metric.name) +end + +Given(/^I see the sample metric configuration code$/) do + page.should have_content(@metric_configuration.code) +end + diff --git a/spec/controllers/compound_metric_configurations_controller_spec.rb b/spec/controllers/compound_metric_configurations_controller_spec.rb index 7b7d0b1..c3602fb 100644 --- a/spec/controllers/compound_metric_configurations_controller_spec.rb +++ b/spec/controllers/compound_metric_configurations_controller_spec.rb @@ -9,8 +9,10 @@ describe CompoundMetricConfigurationsController do end context 'when the current user owns the mezuro configuration' do + let!(:metric_configuration) { FactoryGirl.build(:metric_configuration) } before :each do subject.expects(:mezuro_configuration_owner?).returns true + MetricConfiguration.expects(:metric_configurations_of).with(mezuro_configuration.id).returns([metric_configuration]) get :new, mezuro_configuration_id: mezuro_configuration.id end @@ -27,4 +29,41 @@ describe CompoundMetricConfigurationsController do it { should respond_with(:redirect) } end end + + describe 'create' do + 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 + let!(:metric_params) { Hash[FactoryGirl.attributes_for(:metric).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 + let(:mezuro_configuration) { FactoryGirl.build(:mezuro_configuration) } + + before do + sign_in FactoryGirl.create(:user) + metric_configuration_params["metric"] = metric_params + end + + context 'when the current user owns the reading group' do + before :each do + subject.expects(:mezuro_configuration_owner?).returns true + end + + context 'with valid fields' do + before :each do + MetricConfiguration.any_instance.expects(:save).returns(true) + + post :create, mezuro_configuration_id: mezuro_configuration.id, metric_configuration: metric_configuration_params + end + + it { should respond_with(:redirect) } + end + + context 'with invalid fields' do + before :each do + MetricConfiguration.any_instance.expects(:save).returns(false) + + post :create, mezuro_configuration_id: mezuro_configuration.id, metric_configuration: metric_configuration_params + end + + it { should render_template(:new) } + end + end + end end -- libgit2 0.21.2