diff options
| author | Robert | 2017-12-05 11:37:25 +0100 |
|---|---|---|
| committer | Robert | 2017-12-05 12:19:42 +0100 |
| commit | 642d2bd3a46a4b459acf18748dbfebf0cb6ca7df (patch) | |
| tree | 773a01b7a4dc86a5dabfab98f6f44bdc9f949a50 | |
| parent | 089c6935ca0e036de484834381e5beda7545d499 (diff) | |
| download | chouette-core-642d2bd3a46a4b459acf18748dbfebf0cb6ca7df.tar.bz2 | |
Refs: #5179@1.25h; Referential Creation Enforces objectid_format from associated Workbench
- changed ObjectIdFormatterSupport as requested
- respeced all defined cases
- implemented respec
Missing:
What to do in `define_default_attributes` if ref has no workbench and no `objectid_format`?
| -rw-r--r-- | app/models/concerns/objectid_formatter_support.rb | 13 | ||||
| -rw-r--r-- | app/models/referential.rb | 6 | ||||
| -rw-r--r-- | spec/models/referential/referential_oid_format_from_wkbch_spec.rb | 77 | ||||
| -rw-r--r-- | spec/support/random.rb | 9 |
4 files changed, 61 insertions, 44 deletions
diff --git a/app/models/concerns/objectid_formatter_support.rb b/app/models/concerns/objectid_formatter_support.rb index d0b85d8ac..3ef45334c 100644 --- a/app/models/concerns/objectid_formatter_support.rb +++ b/app/models/concerns/objectid_formatter_support.rb @@ -1,19 +1,16 @@ module ObjectidFormatterSupport extend ActiveSupport::Concern - class << self - def legal_formats - %w(netex stif_netex stif_reflex stif_codifligne) - end - - def default_format; 'netex' end + def self.legal_formats + %w(netex stif_netex stif_reflex stif_codifligne) end + included do extend Enumerize - enumerize :objectid_format, in: ObjectidFormatterSupport.legal_formats, default: ObjectidFormatterSupport.default_format + enumerize :objectid_format, in: ObjectidFormatterSupport.legal_formats validates_presence_of :objectid_format - + def objectid_formatter objectid_formatter_class.new end diff --git a/app/models/referential.rb b/app/models/referential.rb index 233a9f24a..e8fb2888e 100644 --- a/app/models/referential.rb +++ b/app/models/referential.rb @@ -127,10 +127,11 @@ class Referential < ActiveRecord::Base Chouette::RoutingConstraintZone.all end - after_initialize :define_default_attributes + before_validation :define_default_attributes def define_default_attributes self.time_zone ||= Time.zone.name + self.objectid_format ||= workbench.try(:objectid_format) || ObjectidFormatterSupport.legal_formats.first end def switch @@ -140,7 +141,7 @@ class Referential < ActiveRecord::Base end def self.new_from(from, functional_scope) -e Referential.new( + Referential.new( name: I18n.t("activerecord.copy", name: from.name), slug: "#{from.slug}_clone", prefix: from.prefix, @@ -149,6 +150,7 @@ e Referential.new( line_referential: from.line_referential, stop_area_referential: from.stop_area_referential, created_from: from, + objectid_format: from.objectid_format, metadatas: from.metadatas.map { |m| ReferentialMetadata.new_from(m, functional_scope) } ) end diff --git a/spec/models/referential/referential_oid_format_from_wkbch_spec.rb b/spec/models/referential/referential_oid_format_from_wkbch_spec.rb index fe359ac6b..b0d78fcf2 100644 --- a/spec/models/referential/referential_oid_format_from_wkbch_spec.rb +++ b/spec/models/referential/referential_oid_format_from_wkbch_spec.rb @@ -1,39 +1,48 @@ RSpec.describe Referential do + let( :legal_formats ){ ObjectidFormatterSupport.legal_formats } + + describe 'default attributes' do - let( :referential ){ described_class.new } - let( :workbench ){ Workbench.new } - it "of a referential with a workbench contain the workbench's objectid_format" do - ObjectidFormatterSupport.legal_formats.each do |legal_format| - workbench.objectid_format = legal_format + context 'referential w/o an objectid_format' do + let( :referential ){ described_class.new } + let( :workbench ){ build_stubbed :workbench, objectid_format: random_element(legal_formats) } + + it "but a workbench will take the format of the workbench" do referential.workbench = workbench referential.define_default_attributes - expect( referential.objectid_format ).to eq(legal_format) + expect( referential.objectid_format ).to eq(workbench.objectid_format) + end + + xit 'and w/o a workbench will take the default objectid_format' do + referential.define_default_attributes + expect( referential.objectid_format ).to eq(some_value_to_be_defined) end end - it 'of a referential w/o a workbench contain the default objectid_format' do - referential.define_default_attributes - expect( referential.objectid_format ).to eq(ObjectidFormatterSupport.default_format) + + context 'referential with an objectid_format' do + let( :distinct_formats ){ distinct_random_elements(legal_formats, count: 2) } + + let( :referential ){ build_stubbed :referential, objectid_format: distinct_formats.first} + let( :workbench ){ build_stubbed :workbench, objectid_format: distinct_formats.second } + + it "and a workbench will not take the format of the workbench" do + referential.workbench = workbench + expect{ referential.define_default_attributes }.not_to change{ referential.objectid_format } + end + + it 'and w/o a workbench will not take the default objectid_format' do + expect{ referential.define_default_attributes }.not_to change{ referential.objectid_format } + end end + end - + + describe 'self.new_from' do - - # Referential.new( - # name: - # slug: "#{from.slug}_clone", - # prefix: from.prefix, - # time_zone: from.time_zone, - # bounds: from.bounds, - # line_referential: from.line_referential, - # stop_area_referential: from.stop_area_referential, - # created_from: from, - # metadatas: from.metadatas.map { |m| ReferentialMetadata.new_from(m, functional_scope) } - # ) - # let( :source ){ build :referential } let( :functional_scope ){ double('functional scope') } @@ -41,17 +50,17 @@ RSpec.describe Referential do it 'copies objectid_format from source' do expect( described_class ) .to receive(:new) - .with(name: I18n.t("activerecord.copy", name: source.name), - slug: "#{source.slug}_clone", - prefix: source.prefix, - time_zone: source.time_zone, - bounds: source.bounds, - line_referential: source.line_referential, - stop_area_referential: source.stop_area_referential, - created_from: source, - objectid_format: source.objectid_format, - metadatas: source.metadatas.map { |m| ReferentialMetadata.new_from(m, functional_scope) }) - + .with(name: I18n.t("activerecord.copy", name: source.name), + slug: "#{source.slug}_clone", + prefix: source.prefix, + time_zone: source.time_zone, + bounds: source.bounds, + line_referential: source.line_referential, + stop_area_referential: source.stop_area_referential, + created_from: source, + objectid_format: source.objectid_format, + metadatas: source.metadatas.map { |m| ReferentialMetadata.new_from(m, functional_scope) }) + described_class.new_from( source, functional_scope ) end diff --git a/spec/support/random.rb b/spec/support/random.rb index 0ebc2ee5e..16d8b4df3 100644 --- a/spec/support/random.rb +++ b/spec/support/random.rb @@ -10,6 +10,15 @@ module Support def random_element from from[random_int(from.size)] end + + def random_elements( from, count: ) + (1..count).map{ |_| random_element from } + end + + def distinct_random_elements( from, count: ) + f = from.dup + (1..count).map { |_| f.delete_at( random_int(f.size) ) } + end def random_int max_plus_one=PRETTY_LARGE_INT (random_number * max_plus_one).to_i |
