Commit 7bc0465b9cd99602b48eeba7cf1e3932a89acac8

Authored by Guilherme Rojas V. de Lima
Committed by Rafael Manzo
1 parent 3c9fce51

Created feature to add new ranges

Pending unit tests to create action and acceptance tests for range features.

signed-off-by: Fellipe Souto Sampaio <fllsouto@gmail.com>
signed-off-by: Renan Fichberg <rfichberg@gmail.com>
app/controllers/mezuro_ranges_controller.rb
... ... @@ -2,18 +2,22 @@ include OwnershipAuthentication
2 2  
3 3 class MezuroRangesController < ApplicationController
4 4  
  5 + before_action :authenticate_user!, except: [:show, :index]
  6 + before_action :metric_configuration_owner?, except: [:index]
  7 +
5 8 def new
6 9 @mezuro_range = MezuroRange.new
7   - @mezuro_range.metric_configuration_id = params[:metric_configuration_id].to_i
8   - @mezuro_range.mezuro_configuration_id = params[:mezuro_configuration_id].to_i
9   - @reading_group_id = MetricConfiguration.find(@mezuro_range.metric_configuration_id).reading_group_id
10   - @readings = Reading.readings_of(@reading_group_id)
11   - end
12   -
13   - def show
  10 + before_form
14 11 end
15 12  
16 13 def create
  14 + @mezuro_range = MezuroRange.new(mezuro_range_params)
  15 + @mezuro_configuration_id = params[:mezuro_configuration_id].to_i
  16 + @metric_configuration_id = params[:metric_configuration_id].to_i
  17 + @mezuro_range.metric_configuration_id = params[:metric_configuration_id].to_i
  18 + respond_to do |format|
  19 + create_and_redir(format)
  20 + end
17 21 end
18 22  
19 23 def destroy
... ... @@ -28,4 +32,32 @@ class MezuroRangesController &lt; ApplicationController
28 32 def edit
29 33 end
30 34  
  35 + private
  36 +
  37 + def mezuro_range_params
  38 + params[:mezuro_range]
  39 + end
  40 +
  41 + def create_and_redir(format)
  42 + if @mezuro_range.save
  43 + format.html { redirect_to mezuro_configuration_metric_configuration_path(
  44 + @mezuro_configuration_id, @metric_configuration_id), notice: 'The range was successfully created.' }
  45 + else
  46 + failed_action(format, 'new')
  47 + end
  48 + end
  49 +
  50 + def failed_action(format, destiny_action)
  51 + before_form
  52 + format.html { render action: destiny_action }
  53 + format.json { render json: @mezuro_range.errors, status: :unprocessable_entity }
  54 + end
  55 +
  56 + def before_form
  57 + @mezuro_configuration_id = params[:mezuro_configuration_id].to_i
  58 + @metric_configuration_id = params[:metric_configuration_id].to_i
  59 + @reading_group_id = MetricConfiguration.find(@metric_configuration_id).reading_group_id
  60 + @readings = Reading.readings_of(@reading_group_id)
  61 + end
  62 +
31 63 end
... ...
app/views/mezuro_ranges/_form.html.erb
... ... @@ -24,8 +24,7 @@
24 24 </div></br>
25 25 <% end %>
26 26  
27   -
28 27 <%= f.submit 'Save', class: 'btn btn-primary' %>
29 28  
30   - <%= link_to 'Back', mezuro_configuration_metric_configuration_path(@mezuro_range.mezuro_configuration_id, @mezuro_range.metric_configuration_id),
  29 + <%= link_to 'Back', mezuro_configuration_metric_configuration_path(@mezuro_configuration_id, @metric_configuration_id),
31 30 class: 'btn btn-default' %>
... ...
app/views/mezuro_ranges/_no_readings.html.erb
1 1 <div class="alert alert-error alert-dismissable">
2   - <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
3   - <h4 class="alert-heading">There is not reading yet!</h4>
4   - <p>
5   - You need to create reading in your associeated reading group before create a range
6   - </p>
  2 + <h4 class="alert-heading">You must have readings in your associated reading group to create a new range.</h4>
7 3 <p>
8 4 <% if reading_groups_owner? @reading_group_id %>
9 5  
10   - <%= link_to 'Create New Reading', new_reading_group_reading_path(@reading_group_id), class: 'btn btn-danger' %>
  6 + <br /><%= link_to 'Create New Reading', new_reading_group_reading_path(@reading_group_id), class: 'btn btn-danger' %>
11 7  
12 8 <% else %>
13   -
14   - </p></div>
15   - <div class="alert alert-error alert-dismissable">
16   - <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
17   - <p> You are not allowed to do this operation, your reading belongs to another person. </p>
18   - </div>
  9 +
  10 + <p> Your metric configurations' reading group belongs to another user and you are not allowed to modify it.</p>
19 11  
20 12 <% end %>
21 13 </p>
... ...
app/views/mezuro_ranges/new.html.erb
1 1 <h1>New Range</h1>
2 2  
3   -<%= form_for(@mezuro_range, :url => mezuro_configuration_metric_configuration_path(@mezuro_range.mezuro_configuration_id,
4   - @mezuro_range.metric_configuration_id)) do |f| %>
  3 +<%= form_for(@mezuro_range, :url => mezuro_configuration_metric_configuration_mezuro_ranges_path(@mezuro_configuration_id, @metric_configuration_id)) do |f| %>
5 4 <%= render partial: 'form', locals: {f: f} %>
6 5 <% end %>
7 6 \ No newline at end of file
... ...
spec/controllers/mezuro_ranges_spec.rb 0 → 100644
... ... @@ -0,0 +1,35 @@
  1 +require 'spec_helper'
  2 +
  3 +describe MezuroRangesController do
  4 +
  5 + describe 'new' do
  6 + let(:mezuro_range) { FactoryGirl.build(:mezuro_range) }
  7 + let(:mezuro_configuration) { FactoryGirl.build(:mezuro_configuration) }
  8 + let(:metric_configuration) { FactoryGirl.build(:metric_configuration) }
  9 +
  10 + before :each do
  11 + sign_in FactoryGirl.create(:user)
  12 + end
  13 +
  14 + context 'when the current user owns the metric configuration' do
  15 + before :each do
  16 + subject.expects(:metric_configuration_owner?).returns true
  17 + MetricConfiguration.expects(:find).with(mezuro_range.metric_configuration_id).returns(metric_configuration)
  18 + Reading.expects(:readings_of).with(metric_configuration.reading_group_id).returns([])
  19 + get :new, mezuro_configuration_id: mezuro_configuration.id, metric_configuration_id: mezuro_range.metric_configuration_id
  20 + end
  21 +
  22 + it { should respond_with(:success) }
  23 + it { should render_template(:new) }
  24 + end
  25 +
  26 + context "when the current user doesn't owns the metric configuration" do
  27 + before :each do
  28 + get :new, mezuro_configuration_id: mezuro_configuration.id, metric_configuration_id: mezuro_range.metric_configuration_id
  29 + end
  30 +
  31 + it { should redirect_to(mezuro_configurations_path) }
  32 + it { should respond_with(:redirect) }
  33 + end
  34 + end
  35 +end
0 36 \ No newline at end of file
... ...
spec/factories/mezuro_ranges.rb
... ... @@ -20,6 +20,7 @@ FactoryGirl.define do
20 20 self.end 5.1
21 21 reading_id 3
22 22 comments "Comment"
  23 + metric_configuration_id 32
23 24  
24 25 trait :another_comment do
25 26 comments "Another Comment"
... ...