diff options
| author | cedricnjanga | 2017-09-11 16:06:42 +0200 |
|---|---|---|
| committer | cedricnjanga | 2017-09-11 16:21:41 +0200 |
| commit | 8fe9a39516f6fb71545d6329c448680d6f8c75d1 (patch) | |
| tree | 0b0e231aea62d32fe91544cd79c8224a755ceaf6 | |
| parent | ecef9d98cbe08e42434753477da4886dcfcb02bb (diff) | |
| download | chouette-core-8fe9a39516f6fb71545d6329c448680d6f8c75d1.tar.bz2 | |
Update of ReferentialsController#new :
- Add some context to referential and workbench decorator to access current_referential and assign it to the cloned referential
- Add a build_referential method to avoir duplicates of code (in the controller and in the model), need to clean a bit more
- Need to add some Spec features to take into account the functional scope of the current_user
| -rw-r--r-- | app/controllers/referentials_controller.rb | 35 | ||||
| -rw-r--r-- | app/controllers/workbenches_controller.rb | 2 | ||||
| -rw-r--r-- | app/decorators/referential_decorator.rb | 2 | ||||
| -rw-r--r-- | app/models/referential.rb | 18 | ||||
| -rw-r--r-- | app/models/referential_metadata.rb | 1 | ||||
| -rw-r--r-- | app/views/workbenches/index.html.slim | 2 | ||||
| -rw-r--r-- | app/views/workbenches/show.html.slim | 2 | ||||
| -rw-r--r-- | spec/features/referentials_spec.rb | 67 | ||||
| -rw-r--r-- | spec/models/referential_metadata_spec.rb | 4 |
9 files changed, 104 insertions, 29 deletions
diff --git a/app/controllers/referentials_controller.rb b/app/controllers/referentials_controller.rb index bd0544a74..c8984076a 100644 --- a/app/controllers/referentials_controller.rb +++ b/app/controllers/referentials_controller.rb @@ -7,17 +7,19 @@ class ReferentialsController < BreadcrumbController respond_to :js, :only => :show def new - if params[:from] - source_referential = Referential.find(params[:from]) - @referential = Referential.new_from(source_referential, current_functional_scope) + new! do + build_referenial end + end - new! do - @referential.data_format = current_organisation.data_format - @referential.workbench_id ||= params[:workbench_id] + def create + create! do |format| + build_referenial - if @referential.in_workbench? - @referential.init_metadatas default_date_range: Range.new(Date.today, Date.today.advance(months: 1)) + if !!@referential.created_from_id + format.html { redirect_to workbench_path(@referential.workbench) } + else + build_breadcrumb :new end end end @@ -25,7 +27,7 @@ class ReferentialsController < BreadcrumbController def show resource.switch show! do |format| - @referential = @referential.decorate(context: { workbench_id: params[:workbench_id] } ) + @referential = @referential.decorate(context: { current_workbench_id: params[:current_workbench_id] } ) @reflines = lines_collection.paginate(page: params[:page], per_page: 10) @reflines = ModelDecorator.decorate( @reflines, @@ -123,6 +125,21 @@ class ReferentialsController < BreadcrumbController super end + def build_referenial + if params[:from] + source_referential = Referential.find(params[:from]) + @referential = Referential.new_from(source_referential, current_functional_scope) + @referential.workbench_id = params[:current_workbench_id] + end + + @referential.data_format = current_organisation.data_format + @referential.workbench_id ||= params[:workbench_id] + + if @referential.in_workbench? + @referential.init_metadatas default_date_range: Range.new(Date.today, Date.today.advance(months: 1)) + end + end + private def sort_column sortable_columns = Chouette::Line.column_names + ['networks.name', 'companies.name'] diff --git a/app/controllers/workbenches_controller.rb b/app/controllers/workbenches_controller.rb index bae3fcff2..54ddb8be1 100644 --- a/app/controllers/workbenches_controller.rb +++ b/app/controllers/workbenches_controller.rb @@ -24,7 +24,7 @@ class WorkbenchesController < BreadcrumbController @wbench_refs, with: ReferentialDecorator, context: { - workbench_id: params[:id] + current_workbench_id: params[:id] } ) show! do diff --git a/app/decorators/referential_decorator.rb b/app/decorators/referential_decorator.rb index 4e9c242fd..dccf0052c 100644 --- a/app/decorators/referential_decorator.rb +++ b/app/decorators/referential_decorator.rb @@ -13,7 +13,7 @@ class ReferentialDecorator < Draper::Decorator if policy.clone? links << Link.new( content: h.t('actions.clone'), - href: h.new_referential_path(from: object.id, workbench_id: context[:workbench_id]) + href: h.new_referential_path(from: object.id, current_workbench_id: context[:current_workbench_id]) ) end if policy.archive? diff --git a/app/models/referential.rb b/app/models/referential.rb index b508e2f33..af08aa868 100644 --- a/app/models/referential.rb +++ b/app/models/referential.rb @@ -11,6 +11,9 @@ class Referential < ActiveRecord::Base # validates_presence_of :lower_corner validates_uniqueness_of :slug + + validates_presence_of :line_referential + validates_presence_of :stop_area_referential validates_format_of :slug, :with => %r{\A[a-z][0-9a-z_]+\Z} validates_format_of :prefix, :with => %r{\A[0-9a-zA-Z_]+\Z} validates_format_of :upper_corner, :with => %r{\A-?[0-9]+\.?[0-9]*\,-?[0-9]+\.?[0-9]*\Z} @@ -180,8 +183,7 @@ class Referential < ActiveRecord::Base projection_type || "" end - before_validation :assign_line_and_stop_area_referential, :on => :create, if: :workbench, unless: :created_from - before_validation :clone_associations, :on => :create, if: :created_from + before_validation :assign_line_and_stop_area_referential, :on => :create, if: :workbench before_validation :assign_slug, :on => :create before_validation :assign_prefix, :on => :create before_create :create_schema @@ -202,18 +204,6 @@ class Referential < ActiveRecord::Base end end - def clone_associations - self.line_referential = created_from.line_referential - self.stop_area_referential = created_from.stop_area_referential - self.workbench = created_from.workbench - end - - def clone_metadatas - created_from.metadatas.each do |meta| - self.metadatas << ReferentialMetadata.new_from(meta) - end - end - def metadatas_period query = "select min(lower), max(upper) from (select lower(unnest(periodes)) as lower, upper(unnest(periodes)) as upper from public.referential_metadata where public.referential_metadata.referential_id = #{id}) bounds;" diff --git a/app/models/referential_metadata.rb b/app/models/referential_metadata.rb index 839a937f4..393dc70d3 100644 --- a/app/models/referential_metadata.rb +++ b/app/models/referential_metadata.rb @@ -157,6 +157,7 @@ class ReferentialMetadata < ActiveRecord::Base def self.new_from(from, functional_scope) from.dup.tap do |metadata| + metadata.referential_source_id = from.referential_id metadata.line_ids = from.referential.lines.where(id: metadata.line_ids, objectid: functional_scope).collect(&:id) metadata.referential_id = nil end diff --git a/app/views/workbenches/index.html.slim b/app/views/workbenches/index.html.slim index eece51bca..6140c4f8c 100644 --- a/app/views/workbenches/index.html.slim +++ b/app/views/workbenches/index.html.slim @@ -41,7 +41,7 @@ - if @referentials.any? .list-group - @referentials.each_with_index do |referential, i| - = link_to referential.name, referential_path(referential, workbench_id: referential.workbench_id), class: 'list-group-item' if i < 6 + = link_to referential.name, referential_path(referential, workbench_id: referential.workbench_id, current_workbench_id: @workbench.id), class: 'list-group-item' if i < 6 - else .panel-body diff --git a/app/views/workbenches/show.html.slim b/app/views/workbenches/show.html.slim index 50c719d12..fd72979ea 100644 --- a/app/views/workbenches/show.html.slim +++ b/app/views/workbenches/show.html.slim @@ -29,7 +29,7 @@ key: :name, \ attribute: 'name', \ link_to: lambda do |referential| \ - referential_path(referential, workbench_id: referential.workbench_id) \ + referential_path(referential, current_workbench_id: params[:id]) \ end \ ), \ TableBuilderHelper::Column.new( \ diff --git a/spec/features/referentials_spec.rb b/spec/features/referentials_spec.rb index 337271fea..a38577aba 100644 --- a/spec/features/referentials_spec.rb +++ b/spec/features/referentials_spec.rb @@ -120,6 +120,73 @@ describe "Referentials", :type => :feature do end + describe "new_from" do + # let(:cloning) + let(:worker) { ReferentialCloningWorker.new } + + let(:line) { create(:line_with_stop_areas) } + let(:jp) { create(:journey_pattern, route: line.routes.first) } + let(:tt) { create(:time_table) } + let(:vj) { create(:vehicle_journey, journey_pattern: jp, time_table: tt) } + let(:ref_metadata) { create(:referential_metadata, lines: [line], referential: referential) } + + context "when user is from the same organisation" do + + xit "should" do + visit new_referential_path(from: referential.id, current_workbench_id: @user.organisation.workbenches.first.id) + + select "2018", :from => "referential_metadatas_attributes_0_periods_attributes_0_begin_1i" + + select "2018", :from => "referential_metadatas_attributes_0_periods_attributes_0_end_1i" + + click_button "Valider" + + clone = Referential.where(name: "Copie de first") + + expect(clone.lines).to include(line) + expect(clone.lines.first.routes).to match_array(referential.lines.first.routes) + + clone_jp = clone.lines.first.routes.first.journey_patterns + expect(clone_jp).to include(jp) + + clone_vj = clone.lines.first.routes.first.journey_patterns.first.vehicle_journeys + expect(clone_vj).to include(vj) + + clone_tt = clone.lines.first.routes.first.journey_patterns.first.vehicle_journeys.first.time_tables + expect(clone_tt).to include(tt) + end + + # it "should have the lines from source" do + # expect(clone.lines).to include(line) + # end + # + # it "should have the routes from source" do + # expect(clone.lines.first.routes).to match_array(referential.lines.first.routes) + # end + # + # it "should have the journey patterns from source" do + # clone_jp = clone.lines.first.routes.first.journey_patterns + # expect(clone_jp).to include(jp) + # end + # + # it "should have the vehicle journeys from source" do + # clone_vj = clone.lines.first.routes.first.journey_patterns.first.vehicle_journeys + # expect(clone_vj).to include(vj) + # end + # + # it "should have the timetables from source" do + # clone_tt = clone.lines.first.routes.first.journey_patterns.first.vehicle_journeys.first.time_tables + # expect(clone_tt).to include(tt) + # end + end + + # context "when user is from another organisation" do + # before :each do + # + # end + # end + end + describe "destroy" do let(:referential) { create(:referential, :organisation => @user.organisation) } diff --git a/spec/models/referential_metadata_spec.rb b/spec/models/referential_metadata_spec.rb index 775e6f228..291ed974a 100644 --- a/spec/models/referential_metadata_spec.rb +++ b/spec/models/referential_metadata_spec.rb @@ -30,8 +30,8 @@ RSpec.describe ReferentialMetadata, :type => :model do expect(new_referential_metadata.referential).to be(nil) end - it "should have the same referential_source" do - expect(new_referential_metadata.referential_source).to eq(referential_metadata.referential_source) + it "should have the right referential_source" do + expect(new_referential_metadata.referential_source).to eq(referential_metadata.referential) end end |
