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 @@
- You need to create reading in your associeated reading group before create a 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 @@