diff --git a/app/controllers/mezuro_ranges_controller.rb b/app/controllers/mezuro_ranges_controller.rb index 7f4049e..9f7183a 100644 --- a/app/controllers/mezuro_ranges_controller.rb +++ b/app/controllers/mezuro_ranges_controller.rb @@ -2,18 +2,22 @@ include OwnershipAuthentication class MezuroRangesController < ApplicationController + before_action :authenticate_user!, except: [:show, :index] + before_action :metric_configuration_owner?, except: [:index] + def new @mezuro_range = MezuroRange.new - @mezuro_range.metric_configuration_id = params[:metric_configuration_id].to_i - @mezuro_range.mezuro_configuration_id = params[:mezuro_configuration_id].to_i - @reading_group_id = MetricConfiguration.find(@mezuro_range.metric_configuration_id).reading_group_id - @readings = Reading.readings_of(@reading_group_id) - end - - def show + before_form end def create + @mezuro_range = MezuroRange.new(mezuro_range_params) + @mezuro_configuration_id = params[:mezuro_configuration_id].to_i + @metric_configuration_id = params[:metric_configuration_id].to_i + @mezuro_range.metric_configuration_id = params[:metric_configuration_id].to_i + respond_to do |format| + create_and_redir(format) + end end def destroy @@ -28,4 +32,32 @@ class MezuroRangesController < ApplicationController def edit end + private + + def mezuro_range_params + params[:mezuro_range] + end + + def create_and_redir(format) + if @mezuro_range.save + format.html { redirect_to mezuro_configuration_metric_configuration_path( + @mezuro_configuration_id, @metric_configuration_id), notice: 'The range was successfully created.' } + else + failed_action(format, 'new') + end + end + + def failed_action(format, destiny_action) + before_form + format.html { render action: destiny_action } + format.json { render json: @mezuro_range.errors, status: :unprocessable_entity } + end + + def before_form + @mezuro_configuration_id = params[:mezuro_configuration_id].to_i + @metric_configuration_id = params[:metric_configuration_id].to_i + @reading_group_id = MetricConfiguration.find(@metric_configuration_id).reading_group_id + @readings = Reading.readings_of(@reading_group_id) + end + end diff --git a/app/views/mezuro_ranges/_form.html.erb b/app/views/mezuro_ranges/_form.html.erb index 73823e8..ee357da 100644 --- a/app/views/mezuro_ranges/_form.html.erb +++ b/app/views/mezuro_ranges/_form.html.erb @@ -24,8 +24,7 @@
<% end %> - <%= f.submit 'Save', class: 'btn btn-primary' %> - <%= link_to 'Back', mezuro_configuration_metric_configuration_path(@mezuro_range.mezuro_configuration_id, @mezuro_range.metric_configuration_id), + <%= link_to 'Back', mezuro_configuration_metric_configuration_path(@mezuro_configuration_id, @metric_configuration_id), class: 'btn btn-default' %> diff --git a/app/views/mezuro_ranges/_no_readings.html.erb b/app/views/mezuro_ranges/_no_readings.html.erb index f217e6e..8d033a4 100644 --- a/app/views/mezuro_ranges/_no_readings.html.erb +++ b/app/views/mezuro_ranges/_no_readings.html.erb @@ -1,21 +1,13 @@
- -

There is not reading yet!

-

- You need to create reading in your associeated reading group before create a range -

+

You must have readings in your associated reading group to create a new range.

<% if reading_groups_owner? @reading_group_id %> - <%= link_to 'Create New Reading', new_reading_group_reading_path(@reading_group_id), class: 'btn btn-danger' %> +
<%= link_to 'Create New Reading', new_reading_group_reading_path(@reading_group_id), class: 'btn btn-danger' %> <% else %> - -

-
- -

You are not allowed to do this operation, your reading belongs to another person.

-
+ +

Your metric configurations' reading group belongs to another user and you are not allowed to modify it.

<% end %>

diff --git a/app/views/mezuro_ranges/new.html.erb b/app/views/mezuro_ranges/new.html.erb index c442237..9b91b99 100644 --- a/app/views/mezuro_ranges/new.html.erb +++ b/app/views/mezuro_ranges/new.html.erb @@ -1,6 +1,5 @@

New Range

-<%= form_for(@mezuro_range, :url => mezuro_configuration_metric_configuration_path(@mezuro_range.mezuro_configuration_id, - @mezuro_range.metric_configuration_id)) do |f| %> +<%= form_for(@mezuro_range, :url => mezuro_configuration_metric_configuration_mezuro_ranges_path(@mezuro_configuration_id, @metric_configuration_id)) do |f| %> <%= render partial: 'form', locals: {f: f} %> <% end %> \ No newline at end of file diff --git a/spec/controllers/mezuro_ranges_spec.rb b/spec/controllers/mezuro_ranges_spec.rb new file mode 100644 index 0000000..fe5fe58 --- /dev/null +++ b/spec/controllers/mezuro_ranges_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' + +describe MezuroRangesController do + + describe 'new' do + let(:mezuro_range) { FactoryGirl.build(:mezuro_range) } + let(:mezuro_configuration) { FactoryGirl.build(:mezuro_configuration) } + let(:metric_configuration) { FactoryGirl.build(:metric_configuration) } + + before :each do + sign_in FactoryGirl.create(:user) + end + + context 'when the current user owns the metric configuration' do + before :each do + subject.expects(:metric_configuration_owner?).returns true + MetricConfiguration.expects(:find).with(mezuro_range.metric_configuration_id).returns(metric_configuration) + Reading.expects(:readings_of).with(metric_configuration.reading_group_id).returns([]) + get :new, mezuro_configuration_id: mezuro_configuration.id, metric_configuration_id: mezuro_range.metric_configuration_id + end + + it { should respond_with(:success) } + it { should render_template(:new) } + end + + context "when the current user doesn't owns the metric configuration" do + before :each do + get :new, mezuro_configuration_id: mezuro_configuration.id, metric_configuration_id: mezuro_range.metric_configuration_id + end + + it { should redirect_to(mezuro_configurations_path) } + it { should respond_with(:redirect) } + end + end +end \ No newline at end of file diff --git a/spec/factories/mezuro_ranges.rb b/spec/factories/mezuro_ranges.rb index 59830e8..b29ee47 100644 --- a/spec/factories/mezuro_ranges.rb +++ b/spec/factories/mezuro_ranges.rb @@ -20,6 +20,7 @@ FactoryGirl.define do self.end 5.1 reading_id 3 comments "Comment" + metric_configuration_id 32 trait :another_comment do comments "Another Comment" -- libgit2 0.21.2