From 13087614ceb0e9e3a604cd08a023f11cd0567bea Mon Sep 17 00:00:00 2001 From: Zog Date: Fri, 13 Apr 2018 11:06:27 +0200 Subject: Refs #6536; Cleanup value passed for secondary_company_ids by the lines form --- app/controllers/lines_controller.rb | 4 ++- ...180413082929_clean_lines_secondary_companies.rb | 5 ++++ spec/controllers/lines_controller_spec.rb | 29 +++++++++++++++++++++- 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20180413082929_clean_lines_secondary_companies.rb diff --git a/app/controllers/lines_controller.rb b/app/controllers/lines_controller.rb index ae8c9ed0c..cd8091252 100644 --- a/app/controllers/lines_controller.rb +++ b/app/controllers/lines_controller.rb @@ -122,7 +122,7 @@ class LinesController < ChouetteController end def line_params - params.require(:line).permit( + out = params.require(:line).permit( :transport_mode, :network_id, :company_id, @@ -148,6 +148,8 @@ class LinesController < ChouetteController :secondary_company_ids => [], footnotes_attributes: [:code, :label, :_destroy, :id] ) + out[:secondary_company_ids] = (out[:secondary_company_ids] || []).select(&:present?) + out end # Fake ransack filter diff --git a/db/migrate/20180413082929_clean_lines_secondary_companies.rb b/db/migrate/20180413082929_clean_lines_secondary_companies.rb new file mode 100644 index 000000000..4c5659e8f --- /dev/null +++ b/db/migrate/20180413082929_clean_lines_secondary_companies.rb @@ -0,0 +1,5 @@ +class CleanLinesSecondaryCompanies < ActiveRecord::Migration + def up + Chouette::Line.where("secondary_company_ids = '{NULL}'").update_all secondary_company_ids: nil + end +end diff --git a/spec/controllers/lines_controller_spec.rb b/spec/controllers/lines_controller_spec.rb index 96f49bb36..78020484b 100644 --- a/spec/controllers/lines_controller_spec.rb +++ b/spec/controllers/lines_controller_spec.rb @@ -1,9 +1,36 @@ RSpec.describe LinesController, :type => :controller do login_user - let(:line_referential) { create :line_referential, member: @user.organisation } + let(:line_referential) { create :line_referential, member: @user.organisation, objectid_format: :netex } let(:line) { create :line, line_referential: line_referential } + describe 'POST create' do + let(:line_attrs){{ + name: "test", + transport_mode: "bus" + }} + let(:request){ post :create, line_referential_id: line_referential.id, line: line_attrs } + + with_permission "lines.create" do + it "should create a new line" do + expect{request}.to change{ line_referential.lines.count }.by 1 + end + + context "with an empty value in secondary_company_ids" do + let(:line_attrs){{ + name: "test", + transport_mode: "bus", + secondary_company_ids: [""] + }} + + it "should cleanup secondary_company_ids" do + expect{request}.to change{ line_referential.lines.count }.by 1 + expect(line_referential.lines.last.secondary_company_ids).to eq [] + end + end + end + end + describe 'PUT deactivate' do let(:request){ put :deactivate, id: line.id, line_referential_id: line_referential.id } -- cgit v1.2.3