From 1b9a38b2033ed80ad9a6ee4d7d7cd5e140087753 Mon Sep 17 00:00:00 2001 From: Jens Ulferts Date: Wed, 8 May 2013 10:48:57 +0200 Subject: [PATCH] cleans up cost_types_controller --- app/controllers/cost_types_controller.rb | 52 ++++-------------------- 1 file changed, 7 insertions(+), 45 deletions(-) diff --git a/app/controllers/cost_types_controller.rb b/app/controllers/cost_types_controller.rb index c173b6b1ad..13156ee417 100644 --- a/app/controllers/cost_types_controller.rb +++ b/app/controllers/cost_types_controller.rb @@ -3,8 +3,7 @@ class CostTypesController < ApplicationController # Allow only admins here before_filter :require_admin - before_filter :find_cost_type, :only => [:set_rate, :toggle_delete] - before_filter :find_optional_cost_type, :only => [:edit, :update] + before_filter :find_cost_type, :only => [:edit, :update, :set_rate, :toggle_delete] helper :sort include SortHelper @@ -32,38 +31,18 @@ class CostTypesController < ApplicationController end def edit - if !@cost_type - @cost_type = CostType.new() - end - - @cost_type.attributes = permitted_params.cost_type if params[:cost_type] - - if request.post? && @cost_type.save - flash[:notice] = l(:notice_successful_update) - redirect_back_or_default(:action => 'index') - else - @cost_type.rates.build({:valid_from => Date.today}) if @cost_type.rates.empty? - render :action => "edit", :layout => !request.xhr? - end - rescue ActiveRecord::StaleObjectError - # Optimistic locking exception - flash.now[:error] = l(:notice_locking_conflict) + render :action => "edit", :layout => !request.xhr? end def update # TODO: method is copied over from edit # remove code as appropriate - if !@cost_type - @cost_type = CostType.new() - end - - @cost_type.attributes = permitted_params.cost_type if params[:cost_type] + @cost_type.attributes = permitted_params.cost_type if @cost_type.save flash[:notice] = l(:notice_successful_update) redirect_back_or_default(:action => 'index') else - @cost_type.rates.build({:valid_from => Date.today}) if @cost_type.rates.empty? render :action => "edit", :layout => !request.xhr? end rescue ActiveRecord::StaleObjectError @@ -74,28 +53,17 @@ class CostTypesController < ApplicationController def new # TODO: method is copied over from edit # remove code as appropriate - if !@cost_type - @cost_type = CostType.new() - end + @cost_type = CostType.new() - @cost_type.attributes = permitted_params.cost_type if params[:cost_type] + @cost_type.rates.build({:valid_from => Date.today}) if @cost_type.rates.empty? - if request.post? && @cost_type.save - flash[:notice] = l(:notice_successful_update) - redirect_back_or_default(:action => 'index') - else - @cost_type.rates.build({:valid_from => Date.today}) if @cost_type.rates.empty? - render :action => "edit", :layout => !request.xhr? - end - rescue ActiveRecord::StaleObjectError - # Optimistic locking exception - flash.now[:error] = l(:notice_locking_conflict) + render :action => "edit", :layout => !request.xhr? end def create # TODO: method is copied over from edit # remove code as appropriate - @cost_type = CostType.new(params[:cost_type]) + @cost_type = CostType.new(permitted_params.cost_type) if @cost_type.save flash[:notice] = l(:notice_successful_update) @@ -145,12 +113,6 @@ private render_404 end - def find_optional_cost_type - if !params[:id].blank? - @cost_type = CostType.find(params[:id]) - end - end - def default_breadcrumb l(:caption_cost_type_plural) end