diff options
| author | Robert | 2017-08-21 14:45:53 +0200 | 
|---|---|---|
| committer | Robert | 2017-08-21 14:45:53 +0200 | 
| commit | 003c4689248673fbc8922a107475caa9e059bcab (patch) | |
| tree | 05aeed3ca6f2ec724815717372173ca9cecb5e16 | |
| parent | f4863d7ea2e580ee1eb5f5cf498c07441e3ccff3 (diff) | |
| download | chouette-core-003c4689248673fbc8922a107475caa9e059bcab.tar.bz2 | |
Refs: #4189@4h; Allowing change of `name` and `public_name` in `Chouette::Route#duplicate`
| -rw-r--r-- | app/models/chouette/route.rb | 9 | ||||
| -rw-r--r-- | spec/models/chouette/route/route_duplication_spec.rb | 18 | ||||
| -rw-r--r-- | spec/support/helpers/model_compare_helpers.rb | 7 | 
3 files changed, 29 insertions, 5 deletions
| diff --git a/app/models/chouette/route.rb b/app/models/chouette/route.rb index 3e20dc43e..049ee2881 100644 --- a/app/models/chouette/route.rb +++ b/app/models/chouette/route.rb @@ -76,11 +76,16 @@ class Chouette::Route < Chouette::TridentActiveRecord    after_commit :journey_patterns_control_route_sections -  def duplicate +  def duplicate name: nil, published_name: nil +    overrides = { +      'name' => name || self.name, +      'published_name' => published_name || self.published_name, +      'object_version' => object_version - 1 +    }      keys_for_create = attributes.keys - %w{id objectid created_at updated_at}      atts_for_create = attributes        .slice(*keys_for_create) -      .merge('object_version' => object_version - 1) +      .merge(overrides)      new_route = self.class.create!(atts_for_create)      duplicate_stop_points(for_route: new_route)      new_route diff --git a/spec/models/chouette/route/route_duplication_spec.rb b/spec/models/chouette/route/route_duplication_spec.rb index f24435cf6..74e944181 100644 --- a/spec/models/chouette/route/route_duplication_spec.rb +++ b/spec/models/chouette/route/route_duplication_spec.rb @@ -13,6 +13,22 @@ RSpec.describe Route do          route.duplicate          expect( values_for_create(Route.last, except: %w{objectid}) ).to eq( values_for_create( route, except: %w{objectid} ) )        end +      it 'but some can be redefined optionally', :wip do +        excluded_attributes = %w{objectid checksum checksum_source} +        expected_attributes = values_for_create( +          route, +          name: 'new name', +          published_name: 'new published name', +          except: excluded_attributes )  + +        route.duplicate name: 'new name', published_name: 'new published name' +        expect( +          values_for_create(Route.last, except: excluded_attributes) +        ).to eq( expected_attributes ) +      end +      it 'and others cannot' do +        expect{ route.duplicate name: 'YAN', line_id: 42  }.to raise_error(ArgumentError) +      end        it 'same associated stop_areeas' do          expect( route.duplicate.stop_areas.pluck(:id) ).to eq(route.stop_areas.pluck(:id))        end @@ -33,7 +49,7 @@ RSpec.describe Route do      describe 'is idempotent, concerning' do        let( :first_duplicate ){ route.duplicate  }        let( :second_duplicate ){ first_duplicate.reload.duplicate } -       +        it 'the required attributes' do          expect( values_for_create(first_duplicate, except: %w{objectid}) ).to eq( values_for_create( second_duplicate, except: %w{objectid} ) )        end  diff --git a/spec/support/helpers/model_compare_helpers.rb b/spec/support/helpers/model_compare_helpers.rb index 97e26f300..a10892af0 100644 --- a/spec/support/helpers/model_compare_helpers.rb +++ b/spec/support/helpers/model_compare_helpers.rb @@ -1,8 +1,11 @@  module Support::ModelCompareHelpers -  def values_for_create obj, except: [] +  def values_for_create obj, **overrides +    except = overrides.delete(:except) || []      keys = obj.attributes.keys - except - %w{id created_at updated_at} -    obj.attributes.slice(*keys) +    overrides.inject(obj.attributes.slice(*keys)){ |atts, (k,v)| +      atts.merge k.to_s => v +    }    end  end | 
