diff options
| author | Robert | 2017-08-02 05:54:33 +0200 |
|---|---|---|
| committer | Robert | 2017-08-02 05:59:47 +0200 |
| commit | bfc4aa67c0ff06aaafbade489fa210970aaf2763 (patch) | |
| tree | 0f83fb65d26b615ca4d19e45966fcf420352bcaf /app | |
| parent | 4ac49da3e416a0ec24a2c0169d5e9fd934c61f01 (diff) | |
| parent | 9387e31a76d9bae4cdb94622081246eea6d6c8d7 (diff) | |
| download | chouette-core-bfc4aa67c0ff06aaafbade489fa210970aaf2763.tar.bz2 | |
conflict resolution
Diffstat (limited to 'app')
| -rw-r--r-- | app/controllers/api/v1/netex_imports_controller.rb | 46 | ||||
| -rw-r--r-- | app/controllers/application_controller.rb | 1 | ||||
| -rw-r--r-- | app/controllers/concerns/control_flow.rb | 14 |
3 files changed, 47 insertions, 14 deletions
diff --git a/app/controllers/api/v1/netex_imports_controller.rb b/app/controllers/api/v1/netex_imports_controller.rb index b4d3caf16..17eec2ef8 100644 --- a/app/controllers/api/v1/netex_imports_controller.rb +++ b/app/controllers/api/v1/netex_imports_controller.rb @@ -1,6 +1,7 @@ module Api module V1 class NetexImportsController < ChouetteController + include ControlFlow def create respond_to do | format | @@ -11,26 +12,43 @@ module Api private + def find_workbench + @workbench = Workbench.find(netex_import_params['workbench_id']) + rescue ActiveRecord::RecordNotFound + render json: {errors: {'workbench_id' => 'missing'}}, status: 406 + finish_action! + end + def create_models - require 'pry' - binding.pry - workbench = Workbench.where(id: netex_import_params['workbench_id']).first - return render json: {errors: {'workbench_id' => 'missing'}}, status: 406 unless workbench - - @referential = Referential.new(name: netex_import_params['name'], organisation_id: workbench.organisation_id, workbench_id: workbench.id) - @netex_import = NetexImport.new(netex_import_params.merge(referential_id: @referential.id)) - if @netex_import.valid? && @referential.valid? - @netex_import.save! - @referential.save! - else - render json: {errors: @netex_import.errors.to_h.merge( @referential.errors.to_h )}, status: 406 - end + find_workbench + create_referential + create_netex_import + end + + def create_netex_import + @netex_import = NetexImport.new(netex_import_params.merge(referential_id: @new_referential.id)) + @netex_import.save! + rescue ActiveRecord::RecordInvalid + render json: {errors: @netex_import.errors}, status: 406 + finish_action! + end + + def create_referential + @new_referential = + Referential.new( + name: netex_import_params['name'], + organisation_id: @workbench.organisation_id, + workbench_id: @workbench.id) + @new_referential.save! + rescue ActiveRecord::RecordInvalid + render json: {errors: @new_referential.errors}, status: 406 + finish_action! end def netex_import_params params .require('netex_import') - .permit(:file, :name, :referential_id, :workbench_id) + .permit(:file, :name, :workbench_id) end end end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 8fcaa3b1b..d15aa336d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -37,6 +37,7 @@ class ApplicationController < ActionController::Base current_organisation end + # Overwriting the sign_out redirect path method def after_sign_out_path_for(resource_or_scope) new_user_session_path diff --git a/app/controllers/concerns/control_flow.rb b/app/controllers/concerns/control_flow.rb new file mode 100644 index 000000000..0f41a1cda --- /dev/null +++ b/app/controllers/concerns/control_flow.rb @@ -0,0 +1,14 @@ +module ControlFlow + FinishAction = Class.new RuntimeError + + def self.included into + into.rescue_from FinishAction, with: :catch_finish_action + end + + # Allow to exit locally inside an action after rendering (especially in error cases) + def catch_finish_action; end + + def finish_action! msg = 'finish action' + raise FinishAction, msg + end +end |
