Commit 55caadf6475eaa61990d85933186933bd054ef0f
Committed by
Rafael Manzo
1 parent
abf3503c
Exists in
colab
and in
4 other branches
Compound metric configuration creation.
Showing
5 changed files
with
125 additions
and
11 deletions
Show diff stats
app/controllers/compound_metric_configurations_controller.rb
@@ -2,12 +2,46 @@ include OwnershipAuthentication | @@ -2,12 +2,46 @@ include OwnershipAuthentication | ||
2 | 2 | ||
3 | class CompoundMetricConfigurationsController < ApplicationController | 3 | class CompoundMetricConfigurationsController < ApplicationController |
4 | before_action :authenticate_user!, except: [:index] | 4 | before_action :authenticate_user!, except: [:index] |
5 | - before_action :mezuro_configuration_owner?, only: [:new] | 5 | + before_action :mezuro_configuration_owner?, only: [:new, :create] |
6 | 6 | ||
7 | # GET mezuro_configurations/1/compound_metric_configurations/new | 7 | # GET mezuro_configurations/1/compound_metric_configurations/new |
8 | def new | 8 | def new |
9 | @compound_metric_configuration = MetricConfiguration.new | 9 | @compound_metric_configuration = MetricConfiguration.new |
10 | @compound_metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i | 10 | @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 | ||
13 | + | ||
14 | + def create | ||
15 | + @compound_metric_configuration = MetricConfiguration.new(metric_configuration_params) | ||
16 | + @compound_metric_configuration.configuration_id = params[:mezuro_configuration_id].to_i | ||
17 | + @compound_metric_configuration.metric.compound = true | ||
18 | + respond_to do |format| | ||
19 | + create_and_redir(format) | ||
20 | + end | ||
21 | + end | ||
22 | + | ||
23 | + private | ||
24 | + | ||
25 | + # Never trust parameters from the scary internet, only allow the white list through. | ||
26 | + def metric_configuration_params | ||
27 | + params[:metric_configuration] | ||
28 | + end | ||
29 | + | ||
30 | + # Duplicated code on create and update actions extracted here | ||
31 | + def failed_action(format, destiny_action) | ||
32 | + @mezuro_configuration_id = params[:mezuro_configuration_id] | ||
33 | + | ||
34 | + format.html { render action: destiny_action } | ||
35 | + format.json { render json: @compound_metric_configuration.errors, status: :unprocessable_entity } | ||
36 | + end | ||
37 | + | ||
38 | + #Code extracted from create action | ||
39 | + def create_and_redir(format) | ||
40 | + if @compound_metric_configuration.save | ||
41 | + format.html { redirect_to mezuro_configuration_path(@compound_metric_configuration.configuration_id), notice: 'Compound Metric Configuration was successfully created.' } | ||
42 | + else | ||
43 | + failed_action(format, 'new') | ||
44 | + end | ||
11 | end | 45 | end |
12 | 46 | ||
13 | end | 47 | end |
app/views/compound_metric_configurations/new.html.erb
@@ -2,8 +2,38 @@ | @@ -2,8 +2,38 @@ | ||
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> | ||
24 | + | ||
5 | <br> | 25 | <br> |
6 | 26 | ||
7 | -<%= form_for(@compound_metric_configuration, :url => mezuro_configuration_metric_configurations_path(@compound_metric_configuration.configuration_id)) do |f| %> | 27 | +<%= form_for(@compound_metric_configuration, :url => mezuro_configuration_compound_metric_configurations_path(@compound_metric_configuration.configuration_id)) do |f| %> |
8 | <%= render partial: 'form', locals: {f: f} %> | 28 | <%= render partial: 'form', locals: {f: f} %> |
9 | <% end %> | 29 | <% end %> |
30 | + | ||
31 | +<script type="text/javascript"> | ||
32 | + //Loads the accordion | ||
33 | + $(function() { | ||
34 | + $( "#created-metrics-accordion" ).accordion({ | ||
35 | + heightStyle: "content", | ||
36 | + collapsible: true, | ||
37 | + }); | ||
38 | + }); | ||
39 | +</script> |
features/compound_metric_configuration/create.feature
@@ -3,25 +3,28 @@ Feature: Compound Metric Configuration Creation | @@ -3,25 +3,28 @@ Feature: Compound Metric Configuration Creation | ||
3 | As a regular user | 3 | As a regular user |
4 | I should be able to create compound metric configurations | 4 | I should be able to create compound metric configurations |
5 | 5 | ||
6 | - #Missing create action and native metrics name and code | ||
7 | - @kalibro_restart @wip | 6 | + @kalibro_restart @javascript |
8 | Scenario: compound metric configuration creation | 7 | Scenario: compound metric configuration creation |
9 | Given I am a regular user | 8 | Given I am a regular user |
10 | And I am signed in | 9 | And I am signed in |
11 | And I own a sample configuration | 10 | And I own a sample configuration |
12 | And I have a reading group named "Scholar" | 11 | And I have a reading group named "Scholar" |
12 | + And I have a sample metric configuration within the given mezuro configuration | ||
13 | And I am at the Sample Configuration page | 13 | And I am at the Sample Configuration page |
14 | And I click the Add Metric link | 14 | And I click the Add Metric link |
15 | And I click the Compound Metric link | 15 | And I click the Compound Metric link |
16 | - And I fill the Name field with "My Compound Metric" | 16 | + When I click the "Created Metrics" h3 |
17 | + Then I see the sample metric configuration name | ||
18 | + And I see the sample metric configuration code | ||
19 | + When I fill the Name field with "My Compound Metric" | ||
17 | And I fill the Description field with "Some description" | 20 | And I fill the Description field with "Some description" |
18 | - And I fill the Code field with "My Code" | 21 | + And I fill the Code field with "mcm" |
19 | And I fill the Script field with "8*8;" | 22 | And I fill the Script field with "8*8;" |
20 | - And I fill the Weight field with "2" | 23 | + And I fill the Weight field with "8" |
21 | And I set the select field "Scope" as "Class" | 24 | And I set the select field "Scope" as "Class" |
22 | And I set the select field "Aggregation Form" as "Average" | 25 | And I set the select field "Aggregation Form" as "Average" |
23 | And I set the select field "Reading Group" as "Scholar" | 26 | And I set the select field "Reading Group" as "Scholar" |
24 | - When I press the Save button | ||
25 | - #Then I should see "My Code" | ||
26 | - #Then I should see "Total Lines of Code" | ||
27 | - #Then I should see "2" | 27 | + And I press the Save button |
28 | + Then I should see "My Compound Metric" | ||
29 | + And I should see "mcm" | ||
30 | + And I should see "8" |
features/step_definitions/compound_metric_configuration_steps.rb
0 → 100644
spec/controllers/compound_metric_configurations_controller_spec.rb
@@ -9,8 +9,10 @@ describe CompoundMetricConfigurationsController do | @@ -9,8 +9,10 @@ describe CompoundMetricConfigurationsController do | ||
9 | end | 9 | end |
10 | 10 | ||
11 | context 'when the current user owns the mezuro configuration' do | 11 | context 'when the current user owns the mezuro configuration' do |
12 | + let!(:metric_configuration) { FactoryGirl.build(:metric_configuration) } | ||
12 | before :each do | 13 | before :each do |
13 | subject.expects(:mezuro_configuration_owner?).returns true | 14 | subject.expects(:mezuro_configuration_owner?).returns true |
15 | + MetricConfiguration.expects(:metric_configurations_of).with(mezuro_configuration.id).returns([metric_configuration]) | ||
14 | get :new, mezuro_configuration_id: mezuro_configuration.id | 16 | get :new, mezuro_configuration_id: mezuro_configuration.id |
15 | end | 17 | end |
16 | 18 | ||
@@ -27,4 +29,41 @@ describe CompoundMetricConfigurationsController do | @@ -27,4 +29,41 @@ describe CompoundMetricConfigurationsController do | ||
27 | it { should respond_with(:redirect) } | 29 | it { should respond_with(:redirect) } |
28 | end | 30 | end |
29 | end | 31 | end |
32 | + | ||
33 | + describe 'create' do | ||
34 | + 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 | ||
35 | + 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 | ||
36 | + let(:mezuro_configuration) { FactoryGirl.build(:mezuro_configuration) } | ||
37 | + | ||
38 | + before do | ||
39 | + sign_in FactoryGirl.create(:user) | ||
40 | + metric_configuration_params["metric"] = metric_params | ||
41 | + end | ||
42 | + | ||
43 | + context 'when the current user owns the reading group' do | ||
44 | + before :each do | ||
45 | + subject.expects(:mezuro_configuration_owner?).returns true | ||
46 | + end | ||
47 | + | ||
48 | + context 'with valid fields' do | ||
49 | + before :each do | ||
50 | + MetricConfiguration.any_instance.expects(:save).returns(true) | ||
51 | + | ||
52 | + post :create, mezuro_configuration_id: mezuro_configuration.id, metric_configuration: metric_configuration_params | ||
53 | + end | ||
54 | + | ||
55 | + it { should respond_with(:redirect) } | ||
56 | + end | ||
57 | + | ||
58 | + context 'with invalid fields' do | ||
59 | + before :each do | ||
60 | + MetricConfiguration.any_instance.expects(:save).returns(false) | ||
61 | + | ||
62 | + post :create, mezuro_configuration_id: mezuro_configuration.id, metric_configuration: metric_configuration_params | ||
63 | + end | ||
64 | + | ||
65 | + it { should render_template(:new) } | ||
66 | + end | ||
67 | + end | ||
68 | + end | ||
30 | end | 69 | end |