diff options
38 files changed, 405 insertions, 200 deletions
| diff --git a/app/models/chouette/access_link.rb b/app/models/chouette/access_link.rb index b43dcfb7f..543f1cdb7 100644 --- a/app/models/chouette/access_link.rb +++ b/app/models/chouette/access_link.rb @@ -1,5 +1,6 @@  module Chouette    class AccessLink < TridentActiveRecord +    include ObjectidSupport      # FIXME http://jira.codehaus.org/browse/JRUBY-6358      self.primary_key = "id" diff --git a/app/models/chouette/access_point.rb b/app/models/chouette/access_point.rb index 4a1ae8a0e..679d3862b 100644 --- a/app/models/chouette/access_point.rb +++ b/app/models/chouette/access_point.rb @@ -5,9 +5,9 @@ class Chouette::AccessPoint < Chouette::ActiveRecord    # FIXME http://jira.codehaus.org/browse/JRUBY-6358    self.primary_key = "id" -  include StifReflexAttributesSupport    include Geokit::Mappable    include ProjectionFields +  include ObjectidSupport    has_many :access_links, :dependent => :destroy    belongs_to :stop_area @@ -29,11 +29,6 @@ class Chouette::AccessPoint < Chouette::ActiveRecord      [:street_name, :country_code, :comment, :long_lat_type, :zip_code, :city_name]    end - -  def referential -    @referential ||= Referential.where(:slug => Apartment::Tenant.current).first! -  end -    def referential      @referential ||= Referential.where(:slug => Apartment::Tenant.current).first!    end diff --git a/app/models/chouette/company.rb b/app/models/chouette/company.rb index 7c6f8e4f2..148329f2e 100644 --- a/app/models/chouette/company.rb +++ b/app/models/chouette/company.rb @@ -1,8 +1,7 @@  class Chouette::Company < Chouette::ActiveRecord    include CompanyRestrictions -  include StifCodifligneAttributesSupport    include LineReferentialSupport -  include ObjectIdFormatToReferential +  include ObjectidSupport    has_many :lines diff --git a/app/models/chouette/connection_link.rb b/app/models/chouette/connection_link.rb index e225c2fae..b3d999152 100644 --- a/app/models/chouette/connection_link.rb +++ b/app/models/chouette/connection_link.rb @@ -1,4 +1,5 @@  class Chouette::ConnectionLink < Chouette::TridentActiveRecord +  include ObjectidSupport    include ConnectionLinkRestrictions    # FIXME http://jira.codehaus.org/browse/JRUBY-6358    self.primary_key = "id" diff --git a/app/models/chouette/group_of_line.rb b/app/models/chouette/group_of_line.rb index a987d6311..006a7c039 100644 --- a/app/models/chouette/group_of_line.rb +++ b/app/models/chouette/group_of_line.rb @@ -1,5 +1,5 @@  class Chouette::GroupOfLine < Chouette::ActiveRecord -  include StifCodifligneAttributesSupport +  include ObjectidSupport    include GroupOfLineRestrictions    include LineReferentialSupport diff --git a/app/models/chouette/journey_pattern.rb b/app/models/chouette/journey_pattern.rb index 1ba8ff61a..4503bef8b 100644 --- a/app/models/chouette/journey_pattern.rb +++ b/app/models/chouette/journey_pattern.rb @@ -1,185 +1,186 @@ -class Chouette::JourneyPattern < Chouette::TridentActiveRecord -  include ChecksumSupport -  include JourneyPatternRestrictions -  include ObjectIdFormatToReferential -  # FIXME http://jira.codehaus.org/browse/JRUBY-6358 -  self.primary_key = "id" - -  belongs_to :route -  has_many :vehicle_journeys, :dependent => :destroy -  has_many :vehicle_journey_at_stops, :through => :vehicle_journeys -  has_and_belongs_to_many :stop_points, -> { order("stop_points.position") }, :before_add => :vjas_add, :before_remove => :vjas_remove, :after_add => :shortcuts_update_for_add, :after_remove => :shortcuts_update_for_remove -  has_many :stop_areas, through: :stop_points -  has_many :journey_pattern_sections -  has_many :route_sections, through: :journey_pattern_sections, dependent: :destroy - -  validates_presence_of :route -  validates_presence_of :name - -  #validates :stop_points, length: { minimum: 2, too_short: :minimum }, on: :update -  enum section_status: { todo: 0, completed: 1, control: 2 } - -  attr_accessor  :control_checked -  after_update :control_route_sections, :unless => "control_checked" - - -  def local_id -    "IBOO-#{self.referential.id}-#{self.try(:route).try(:line).try(:objectid).try(:local_id)}-#{self.id}" -  end +module Chouette +  class JourneyPattern < Chouette::TridentActiveRecord +    include ChecksumSupport +    include JourneyPatternRestrictions +    include ObjectidSupport +    # FIXME http://jira.codehaus.org/browse/JRUBY-6358 +    self.primary_key = "id" + +    belongs_to :route +    has_many :vehicle_journeys, :dependent => :destroy +    has_many :vehicle_journey_at_stops, :through => :vehicle_journeys +    has_and_belongs_to_many :stop_points, -> { order("stop_points.position") }, :before_add => :vjas_add, :before_remove => :vjas_remove, :after_add => :shortcuts_update_for_add, :after_remove => :shortcuts_update_for_remove +    has_many :stop_areas, through: :stop_points +    has_many :journey_pattern_sections +    has_many :route_sections, through: :journey_pattern_sections, dependent: :destroy + +    validates_presence_of :route +    validates_presence_of :name + +    #validates :stop_points, length: { minimum: 2, too_short: :minimum }, on: :update +    enum section_status: { todo: 0, completed: 1, control: 2 } + +    attr_accessor  :control_checked +    after_update :control_route_sections, :unless => "control_checked" + + +    def local_id +      "IBOO-#{self.referential.id}-#{self.try(:route).try(:line).try(:objectid).try(:local_id)}-#{self.id}" +    end -  def checksum_attributes -    values = self.slice(*['name', 'published_name', 'registration_number']).values -    values << self.stop_points.map(&:stop_area).map(&:user_objectid) -    values.flatten -  end +    def checksum_attributes +      values = self.slice(*['name', 'published_name', 'registration_number']).values +      values << self.stop_points.map(&:stop_area).map(&:user_objectid) +      values.flatten +    end -  def self.state_update route, state -    transaction do -      state.each do |item| -        item.delete('errors') -        jp = find_by(objectid: item['object_id']) || state_create_instance(route, item) -        next if item['deletable'] && jp.persisted? && jp.destroy -        # Update attributes and stop_points associations -        jp.update_attributes(state_permited_attributes(item)) unless item['new_record'] -        jp.state_stop_points_update(item) if !jp.errors.any? && jp.persisted? -        item['errors'] = jp.errors if jp.errors.any? +    def self.state_update route, state +      transaction do +        state.each do |item| +          item.delete('errors') +          jp = find_by(objectid: item['object_id']) || state_create_instance(route, item) +          next if item['deletable'] && jp.persisted? && jp.destroy +          # Update attributes and stop_points associations +          jp.update_attributes(state_permited_attributes(item)) unless item['new_record'] +          jp.state_stop_points_update(item) if !jp.errors.any? && jp.persisted? +          item['errors'] = jp.errors if jp.errors.any? +        end + +        if state.any? {|item| item['errors']} +          state.map {|item| item.delete('object_id') if item['new_record']} +          raise ActiveRecord::Rollback +        end        end -      if state.any? {|item| item['errors']} -        state.map {|item| item.delete('object_id') if item['new_record']} -        raise ActiveRecord::Rollback -      end +      state.map {|item| item.delete('new_record')} +      state.delete_if {|item| item['deletable']}      end -    state.map {|item| item.delete('new_record')} -    state.delete_if {|item| item['deletable']} -  end - -  def self.state_permited_attributes item -    { -      name: item['name'], -      published_name: item['published_name'], -      registration_number: item['registration_number'] -    } -  end - -  def self.state_create_instance route, item -    # Flag new record, so we can unset object_id if transaction rollback -    jp = route.journey_patterns.create(state_permited_attributes(item)) +    def self.state_permited_attributes item +      { +        name: item['name'], +        published_name: item['published_name'], +        registration_number: item['registration_number'] +      } +    end -    # FIXME -    # DefaultAttributesSupport will trigger some weird validation on after save -    # wich will call to valid?, wich will populate errors -    # In this case, we mark jp to be valid if persisted? return true -    jp.errors.clear if jp.persisted? +    def self.state_create_instance route, item +      # Flag new record, so we can unset object_id if transaction rollback +      jp = route.journey_patterns.create(state_permited_attributes(item)) -    item['object_id']  = jp.objectid -    item['new_record'] = true -    jp -  end +      # FIXME +      # DefaultAttributesSupport will trigger some weird validation on after save +      # wich will call to valid?, wich will populate errors +      # In this case, we mark jp to be valid if persisted? return true +      jp.errors.clear if jp.persisted? -  def state_stop_points_update item -    item['stop_points'].each do |sp| -      exist = stop_area_ids.include?(sp['id']) -      next if exist && sp['checked'] +      item['object_id']  = jp.objectid +      item['new_record'] = true +      jp +    end -      stop_point = route.stop_points.find_by(stop_area_id: sp['id']) -      if !exist && sp['checked'] -        stop_points << stop_point -      end -      if exist && !sp['checked'] -        stop_points.delete(stop_point) +    def state_stop_points_update item +      item['stop_points'].each do |sp| +        exist = stop_area_ids.include?(sp['id']) +        next if exist && sp['checked'] + +        stop_point = route.stop_points.find_by(stop_area_id: sp['id']) +        if !exist && sp['checked'] +          stop_points << stop_point +        end +        if exist && !sp['checked'] +          stop_points.delete(stop_point) +        end        end      end -  end -  # TODO: this a workarround -  # otherwise, we loose the first stop_point -  # when creating a new journey_pattern -  def special_update -    bck_sp = self.stop_points.map {|s| s} -    self.update_attributes :stop_points => [] -    self.update_attributes :stop_points => bck_sp -  end +    # TODO: this a workarround +    # otherwise, we loose the first stop_point +    # when creating a new journey_pattern +    def special_update +      bck_sp = self.stop_points.map {|s| s} +      self.update_attributes :stop_points => [] +      self.update_attributes :stop_points => bck_sp +    end -  def departure_stop_point -    return unless departure_stop_point_id -    Chouette::StopPoint.find( departure_stop_point_id) -  end +    def departure_stop_point +      return unless departure_stop_point_id +      Chouette::StopPoint.find( departure_stop_point_id) +    end -  def arrival_stop_point -    return unless arrival_stop_point_id -    Chouette::StopPoint.find( arrival_stop_point_id) -  end +    def arrival_stop_point +      return unless arrival_stop_point_id +      Chouette::StopPoint.find( arrival_stop_point_id) +    end -  def shortcuts_update_for_add( stop_point) -    stop_points << stop_point unless stop_points.include?( stop_point) +    def shortcuts_update_for_add( stop_point) +      stop_points << stop_point unless stop_points.include?( stop_point) -    ordered_stop_points = stop_points -    ordered_stop_points = ordered_stop_points.sort { |a,b| a.position <=> b.position} unless ordered_stop_points.empty? +      ordered_stop_points = stop_points +      ordered_stop_points = ordered_stop_points.sort { |a,b| a.position <=> b.position} unless ordered_stop_points.empty? -    self.update_attributes( :departure_stop_point_id => (ordered_stop_points.first && ordered_stop_points.first.id), -                             :arrival_stop_point_id => (ordered_stop_points.last && ordered_stop_points.last.id)) -  end +      self.update_attributes( :departure_stop_point_id => (ordered_stop_points.first && ordered_stop_points.first.id), +                              :arrival_stop_point_id => (ordered_stop_points.last && ordered_stop_points.last.id)) +    end -  def shortcuts_update_for_remove( stop_point) -    stop_points.delete( stop_point) if stop_points.include?( stop_point) +    def shortcuts_update_for_remove( stop_point) +      stop_points.delete( stop_point) if stop_points.include?( stop_point) -    ordered_stop_points = stop_points -    ordered_stop_points = ordered_stop_points.sort { |a,b| a.position <=> b.position} unless ordered_stop_points.empty? +      ordered_stop_points = stop_points +      ordered_stop_points = ordered_stop_points.sort { |a,b| a.position <=> b.position} unless ordered_stop_points.empty? -    self.update_attributes( :departure_stop_point_id => (ordered_stop_points.first && ordered_stop_points.first.id), -                             :arrival_stop_point_id => (ordered_stop_points.last && ordered_stop_points.last.id)) -  end +      self.update_attributes( :departure_stop_point_id => (ordered_stop_points.first && ordered_stop_points.first.id), +                              :arrival_stop_point_id => (ordered_stop_points.last && ordered_stop_points.last.id)) +    end -  def vjas_add( stop_point) -    return if new_record? +    def vjas_add( stop_point) +      return if new_record? -    vehicle_journeys.each do |vj| -      vjas = vj.vehicle_journey_at_stops.create :stop_point_id => stop_point.id +      vehicle_journeys.each do |vj| +        vjas = vj.vehicle_journey_at_stops.create :stop_point_id => stop_point.id +      end      end -  end -  def vjas_remove( stop_point) -    return if new_record? +    def vjas_remove( stop_point) +      return if new_record? -    vehicle_journey_at_stops.where( :stop_point_id => stop_point.id).each do |vjas| -      vjas.destroy +      vehicle_journey_at_stops.where( :stop_point_id => stop_point.id).each do |vjas| +        vjas.destroy +      end      end -  end -  def control_route_sections -    stop_area_ids = self.stop_points.map(&:stop_area_id) -    control_route_sections_by_stop_areas(stop_area_ids) -  end +    def control_route_sections +      stop_area_ids = self.stop_points.map(&:stop_area_id) +      control_route_sections_by_stop_areas(stop_area_ids) +    end -  def control_route_sections_by_stop_areas(stop_area_ids) -    journey_pattern_section_all -    i = 0 -    to_control = false -    stop_area_ids.each_cons(2) do |a| -      jps = @route_sections_orders[i] -      i += 1 -      unless jps -        to_control = true -        next -      end -      unless [jps.route_section.departure.id, jps.route_section.arrival.id] == a -        jps.destroy -        to_control = true +    def control_route_sections_by_stop_areas(stop_area_ids) +      journey_pattern_section_all +      i = 0 +      to_control = false +      stop_area_ids.each_cons(2) do |a| +        jps = @route_sections_orders[i] +        i += 1 +        unless jps +          to_control = true +          next +        end +        unless [jps.route_section.departure.id, jps.route_section.arrival.id] == a +          jps.destroy +          to_control = true +        end        end +      self.control_checked = true +      to_control ? self.control! : self.completed!      end -    self.control_checked = true -    to_control ? self.control! : self.completed! -  end -  protected +    protected -  def journey_pattern_section_all -    @route_sections_orders = {} -    self.journey_pattern_sections.all.map do |journey_pattern_section| -      @route_sections_orders[journey_pattern_section.rank] = journey_pattern_section +    def journey_pattern_section_all +      @route_sections_orders = {} +      self.journey_pattern_sections.all.map do |journey_pattern_section| +        @route_sections_orders[journey_pattern_section.rank] = journey_pattern_section +      end      end    end - -end +end
\ No newline at end of file diff --git a/app/models/chouette/line.rb b/app/models/chouette/line.rb index 665eca779..e08eaf4d0 100644 --- a/app/models/chouette/line.rb +++ b/app/models/chouette/line.rb @@ -1,8 +1,7 @@  class Chouette::Line < Chouette::ActiveRecord -  include StifCodifligneAttributesSupport    include LineRestrictions    include LineReferentialSupport -  include ObjectIdFormatToReferential +  include ObjectidSupport    extend StifTransportModeEnumerations    extend StifTransportSubmodeEnumerations @@ -40,8 +39,6 @@ class Chouette::Line < Chouette::ActiveRecord    validates_presence_of :name -  alias_method :line_referential, :referential -    scope :by_text, ->(text) { where('lower(name) LIKE :t or lower(published_name) LIKE :t or lower(objectid) LIKE :t or lower(comment) LIKE :t or lower(number) LIKE :t',      t: "%#{text.downcase}%") } diff --git a/app/models/chouette/network.rb b/app/models/chouette/network.rb index 6c8a12bda..d4c9a19dd 100644 --- a/app/models/chouette/network.rb +++ b/app/models/chouette/network.rb @@ -1,8 +1,7 @@  class Chouette::Network < Chouette::ActiveRecord -  include StifCodifligneAttributesSupport    include NetworkRestrictions    include LineReferentialSupport -  include ObjectIdFormatToReferential +  include ObjectidSupport    # FIXME http://jira.codehaus.org/browse/JRUBY-6358    self.primary_key = "id" diff --git a/app/models/chouette/objectid/netex.rb b/app/models/chouette/objectid/netex.rb new file mode 100644 index 000000000..254ce6c6e --- /dev/null +++ b/app/models/chouette/objectid/netex.rb @@ -0,0 +1,36 @@ +module Chouette +  module Objectid +    class Netex +      include ActiveModel::Model + +      attr_accessor :provider_id, :object_type, :local_id, :creation_id +      validates_presence_of :provider_id, :object_type, :local_id, :creation_id + +      def initialize(**attributes) +        @provider_id ||= 'chouette' +        @object_type = attributes[:object_type] +        @local_id = attributes[:local_id] +        @creation_id ||= 'LOC' +      end + +      @@format = /^([A-Za-z_-]+):([A-Za-z]+):([0-9A-Za-z_-]+):([A-Za-z]+)$/ +      cattr_reader :format + +      def to_s +        "#{self.provider_id}:#{self.object_type}:#{self.local_id}:#{self.creation_id}" +      end + +      def parts +        self.to_s.match(format).try(:captures) +      end + +      def valid? +        parts.present? +      end + +      def short_id +        local_id +      end +    end +  end +end diff --git a/app/models/chouette/objectid/stif_codifligne.rb b/app/models/chouette/objectid/stif_codifligne.rb new file mode 100644 index 000000000..9d83a1432 --- /dev/null +++ b/app/models/chouette/objectid/stif_codifligne.rb @@ -0,0 +1,25 @@ +module Chouette +  module Objectid +    class StifCodifligne < Chouette::Objectid::Netex + +      attr_accessor :sync_id +      validates_presence_of :sync_id +      validates :creation_id, presence: false + +      @@format = /^([A-Za-z_]+):([A-Za-z]+):([A-Za-z]+):([0-9A-Za-z_-]+)$/ + +      def initialize(**attributes) +        @provider_id = attributes[:provider_id] +        @object_type = attributes[:object_type] +        @local_id = attributes[:local_id] +        @sync_id = attributes[:sync_id] +        super +      end + +      def to_s +        "#{self.provider_id}:#{self.sync_id}:#{self.object_type}:#{self.local_id}" +      end + +    end +  end +end diff --git a/app/models/chouette/objectid/stif_netex.rb b/app/models/chouette/objectid/stif_netex.rb new file mode 100644 index 000000000..e9d40a00f --- /dev/null +++ b/app/models/chouette/objectid/stif_netex.rb @@ -0,0 +1,15 @@ +module Chouette +  module Objectid +    class StifNetex < Chouette::Objectid::Netex + +      def initialize(**attributes) +        @provider_id = 'stif' +        super +      end + +      def short_id +        local_id.try(:split, "-").try(:[], -1) +      end +    end +  end +end diff --git a/app/models/chouette/objectid/stif_reflex.rb b/app/models/chouette/objectid/stif_reflex.rb new file mode 100644 index 000000000..1ea3ec0ce --- /dev/null +++ b/app/models/chouette/objectid/stif_reflex.rb @@ -0,0 +1,24 @@ +module Chouette +  module Objectid +    class StifReflex < Chouette::Objectid::Netex + +      attr_accessor :country_code, :zip_code +      validates_presence_of :country_code, :zip_code +      validates :creation_id, presence: false + +      @@format = /^([A-Za-z_]+):([0-9A-Za-z_-]+):([A-Za-z]+):([0-9A-Za-z_-]+):([A-Za-z]+)$/ + +      def initialize(**attributes) +        @provider_id = attributes[:provider_id] +        @country_code = attributes[:country_code] +        @zip_code = attributes[:zip_code] +        super +      end + +      def to_s +        "#{self.country_code}:#{self.zip_code}:#{self.object_type}:#{self.local_id}:#{self.provider_id}" +      end + +    end +  end +end diff --git a/app/models/chouette/objectid_formater/netex.rb b/app/models/chouette/objectid_formater/netex.rb new file mode 100644 index 000000000..0736b6ff9 --- /dev/null +++ b/app/models/chouette/objectid_formater/netex.rb @@ -0,0 +1,18 @@ +module Chouette +  module ObjectidFormater +    class Netex +      def before_validation(model) +        model.objectid ||= Chouette::Objectid::Netex.new(local_id: SecureRandom.uuid, object_type: model.class.name.gsub(/Chouette::/,'')).to_s +      end + +      def after_commit(model) +        # unused method in this context +      end + +      def parse_objectid(definition) +        parts = definition.split(":") +        Chouette::Objectid::Netex.new(provider_id: parts[0], object_type: parts[1], local_id: parts[2], creation_id: parts[3]).to_s rescue nil +      end +    end +  end +end
\ No newline at end of file diff --git a/app/models/chouette/objectid_formater/stif_codifligne.rb b/app/models/chouette/objectid_formater/stif_codifligne.rb new file mode 100644 index 000000000..53fd28c82 --- /dev/null +++ b/app/models/chouette/objectid_formater/stif_codifligne.rb @@ -0,0 +1,18 @@ +module Chouette +  module ObjectidFormater +    class StifCodifligne +      def before_validation(model)  +        # unused method in this context +      end + +      def after_commit(model) +        # unused method in this context +      end + +      def parse_objectid(definition) +        parts = definition.split(":") +        Chouette::Objectid::StifCodifligne.new(provider_id: parts[0], sync_id: parts[1], object_type: parts[2], local_id: parts[3]).to_s rescue nil +      end +    end +  end +end
\ No newline at end of file diff --git a/app/models/chouette/objectid_formater/stif_netex.rb b/app/models/chouette/objectid_formater/stif_netex.rb new file mode 100644 index 000000000..88995dd05 --- /dev/null +++ b/app/models/chouette/objectid_formater/stif_netex.rb @@ -0,0 +1,20 @@ +module Chouette +  module ObjectidFormater +    class StifNetex +      def before_validation(model)  +        model.objectid ||= "__pending_id__#{rand(50)+ rand(50)}" +      end + +      def after_commit(model) +        if model.objectid.include? ':__pending_id__' +          model.objectid = Chouette::Objectid::StifNetex.new(provider_id: "stif", object_type: model.class.name.gsub(/Chouette::/,''), local_id: model.local_id).to_s +        end +      end + +      def parse_objectid(definition) +        parts = definition.split(":") +        Chouette::Objectid::StifNetex.new(provider_id: parts[0], object_type: parts[1], local_id: parts[2], creation_id: parts[3]).to_s rescue nil +      end +    end +  end +end
\ No newline at end of file diff --git a/app/models/chouette/objectid_formater/stif_reflex.rb b/app/models/chouette/objectid_formater/stif_reflex.rb new file mode 100644 index 000000000..e6c6a6a70 --- /dev/null +++ b/app/models/chouette/objectid_formater/stif_reflex.rb @@ -0,0 +1,18 @@ +module Chouette +  module ObjectidFormater +    class StifReflex +      def before_validation(model)  +        # unused method in this context +      end + +      def after_commit(model) +        # unused method in this context +      end + +      def parse_objectid(definition) +        parts = definition.split(":") +        Chouette::Objectid::StifReflex.new(country_code: parts[0], zip_code: parts[1], object_type: parts[2], local_id: parts[3], provider_id: parts[4]).to_s rescue nil +      end +    end +  end +end
\ No newline at end of file diff --git a/app/models/chouette/route.rb b/app/models/chouette/route.rb index d0c1adf1c..853ecee85 100644 --- a/app/models/chouette/route.rb +++ b/app/models/chouette/route.rb @@ -1,7 +1,7 @@  class Chouette::Route < Chouette::TridentActiveRecord    include RouteRestrictions    include ChecksumSupport -  include ObjectIdFormatToReferential +  include ObjectidSupport    extend Enumerize    extend ActiveModel::Naming diff --git a/app/models/chouette/routing_constraint_zone.rb b/app/models/chouette/routing_constraint_zone.rb index 124884830..5f6e5affd 100644 --- a/app/models/chouette/routing_constraint_zone.rb +++ b/app/models/chouette/routing_constraint_zone.rb @@ -1,6 +1,6 @@  class Chouette::RoutingConstraintZone < Chouette::TridentActiveRecord    include ChecksumSupport -  include ObjectIdFormatToReferential +  include ObjectidSupport    belongs_to :route    has_array_of :stop_points, class_name: 'Chouette::StopPoint' diff --git a/app/models/chouette/stop_area.rb b/app/models/chouette/stop_area.rb index 7872581cb..513eb4d48 100644 --- a/app/models/chouette/stop_area.rb +++ b/app/models/chouette/stop_area.rb @@ -6,10 +6,11 @@ class Chouette::StopArea < Chouette::ActiveRecord    self.primary_key = "id"    include Geokit::Mappable -  include StifReflexAttributesSupport +  # include StifReflexAttributesSupport    include ProjectionFields    include StopAreaRestrictions    include StopAreaReferentialSupport +  include ObjectidSupport    extend Enumerize    enumerize :area_type, in: %i(zdep zder zdlp zdlr lda) @@ -26,8 +27,6 @@ class Chouette::StopArea < Chouette::ActiveRecord    belongs_to :stop_area_referential    validates_presence_of :stop_area_referential_id -  alias_method :stop_area_referential, :referential -    acts_as_tree :foreign_key => 'parent_id', :order => "name"    attr_accessor :stop_area_type @@ -63,10 +62,6 @@ class Chouette::StopArea < Chouette::ActiveRecord      end    end -  def objectid_format -    "#{self.stop_area_referential.objectid_format}_attributes_support".camelcase.constantize if self.stop_area_referential.objectid_format -  end -    def coordinates        @coordinates || combine_lat_lng    end diff --git a/app/models/chouette/stop_point.rb b/app/models/chouette/stop_point.rb index c4034e2d4..2c7bb3355 100644 --- a/app/models/chouette/stop_point.rb +++ b/app/models/chouette/stop_point.rb @@ -7,7 +7,7 @@ module Chouette      include ForBoardingEnumerations      include ForAlightingEnumerations -    include ObjectIdFormatToReferential +    include ObjectidSupport      # FIXME http://jira.codehaus.org/browse/JRUBY-6358      self.primary_key = "id" @@ -19,7 +19,6 @@ module Chouette      acts_as_list :scope => :route, top_of_list: 0 -    validates_presence_of :objectid_format      validates_presence_of :stop_area      validate :stop_area_id_validation diff --git a/app/models/chouette/time_table.rb b/app/models/chouette/time_table.rb index 6572dbfe4..a17fb5799 100644 --- a/app/models/chouette/time_table.rb +++ b/app/models/chouette/time_table.rb @@ -1,7 +1,7 @@  class Chouette::TimeTable < Chouette::TridentActiveRecord    include ChecksumSupport    include TimeTableRestrictions -  include ObjectIdFormatToReferential +  include ObjectidSupport    # FIXME http://jira.codehaus.org/browse/JRUBY-6358    self.primary_key = "id" diff --git a/app/models/chouette/trident_active_record.rb b/app/models/chouette/trident_active_record.rb index e8223e3d6..f2d654efc 100644 --- a/app/models/chouette/trident_active_record.rb +++ b/app/models/chouette/trident_active_record.rb @@ -1,5 +1,4 @@  class Chouette::TridentActiveRecord < Chouette::ActiveRecord -  include StifNetexAttributesSupport    self.abstract_class = true diff --git a/app/models/chouette/vehicle_journey.rb b/app/models/chouette/vehicle_journey.rb index 5e1b76ceb..1cb2d7045 100644 --- a/app/models/chouette/vehicle_journey.rb +++ b/app/models/chouette/vehicle_journey.rb @@ -2,7 +2,7 @@ module Chouette    class VehicleJourney < TridentActiveRecord      include ChecksumSupport      include VehicleJourneyRestrictions -    include ObjectIdFormatToReferential +    include ObjectidSupport      extend StifTransportModeEnumerations      # FIXME http://jira.codehaus.org/browse/JRUBY-6358      self.primary_key = "id" diff --git a/app/models/concerns/objectid_formater_support.rb b/app/models/concerns/objectid_formater_support.rb new file mode 100644 index 000000000..1064b156f --- /dev/null +++ b/app/models/concerns/objectid_formater_support.rb @@ -0,0 +1,15 @@ +module ObjectidFormaterSupport +  extend ActiveSupport::Concern + +  included do +    validates_presence_of :objectid_formater_class +     +    def objectid_formater +      objectid_formater_class.new +    end + +    def objectid_formater_class +      "Chouette::ObjectidFormater::#{read_attribute(:objectid_format).camelcase}".constantize if read_attribute(:objectid_format) +    end +  end +end
\ No newline at end of file diff --git a/app/models/concerns/objectid_support.rb b/app/models/concerns/objectid_support.rb new file mode 100644 index 000000000..1c8bdf751 --- /dev/null +++ b/app/models/concerns/objectid_support.rb @@ -0,0 +1,26 @@ +module ObjectidSupport +  extend ActiveSupport::Concern + +  included do +    before_validation :before_validation_objectid +    after_commit :after_commit_objectid +    validates_presence_of :objectid_format, :objectid +    validates_uniqueness_of :objectid  + +    def before_validation_objectid +      self.referential.objectid_formater.before_validation self +    end + +    def after_commit_objectid +      self.referential.objectid_formater.after_commit self +    end + +    def objectid +      self.referential.objectid_formater.parse_objectid read_attribute(:objectid) if objectid_format && read_attribute(:objectid) +    end + +    def objectid_format +      self.referential.objectid_format +    end +  end +end
\ No newline at end of file diff --git a/app/models/line_referential.rb b/app/models/line_referential.rb index d81644080..90a045935 100644 --- a/app/models/line_referential.rb +++ b/app/models/line_referential.rb @@ -1,4 +1,5 @@  class LineReferential < ActiveRecord::Base +  include ObjectidFormaterSupport    extend StifTransportModeEnumerations    extend Enumerize @@ -10,8 +11,7 @@ class LineReferential < ActiveRecord::Base    has_many :networks, class_name: 'Chouette::Network'    has_many :line_referential_syncs, -> { order created_at: :desc }    has_many :workbenches -  enumerize :objectid_format, in: %w(netex stif_netex) -  validates_presence_of :objectid_format +  enumerize :objectid_format, in: %w(netex stif_netex stif_reflex stif_codifligne)    def add_member(organisation, options = {})      attributes = options.merge organisation: organisation @@ -22,6 +22,7 @@ class LineReferential < ActiveRecord::Base    validates :sync_interval, presence: true    # need to define precise validation rules    validates_inclusion_of :sync_interval, :in => 1..30 +  validates_presence_of :objectid_format    def operating_lines      lines.where(deactivated: false) diff --git a/app/models/referential.rb b/app/models/referential.rb index 87ac61f56..04b5e3102 100644 --- a/app/models/referential.rb +++ b/app/models/referential.rb @@ -1,10 +1,12 @@  class Referential < ActiveRecord::Base    include DataFormatEnumerations +  include ObjectidFormaterSupport    extend Enumerize    validates_presence_of :name    validates_presence_of :slug    validates_presence_of :prefix +  validates_presence_of :objectid_format    # Fixme #3657    # validates_presence_of :time_zone    # validates_presence_of :upper_corner @@ -54,8 +56,7 @@ class Referential < ActiveRecord::Base    belongs_to :referential_suite -  enumerize :objectid_format, in: %w(netex stif_netex) -  validates_presence_of :objectid_format +  enumerize :objectid_format, in: %w(netex stif_netex stif_reflex stif_codifligne)    scope :ready, -> { where(ready: true) }    scope :in_periode, ->(periode) { where(id: referential_ids_in_periode(periode)) } diff --git a/app/models/stop_area_referential.rb b/app/models/stop_area_referential.rb index 2ef1278d8..1e544e116 100644 --- a/app/models/stop_area_referential.rb +++ b/app/models/stop_area_referential.rb @@ -1,12 +1,13 @@  class StopAreaReferential < ActiveRecord::Base    extend Enumerize +  include ObjectidFormaterSupport    has_many :stop_area_referential_memberships    has_many :organisations, through: :stop_area_referential_memberships    has_many :stop_areas, class_name: 'Chouette::StopArea'    has_many :stop_area_referential_syncs, -> {order created_at: :desc}    has_many :workbenches -  enumerize :objectid_format, in: %w(netex stif_netex) +  enumerize :objectid_format, in: %w(netex stif_netex stif_reflex stif_codifligne)    validates_presence_of :objectid_format    def add_member(organisation, options = {}) diff --git a/app/models/workbench.rb b/app/models/workbench.rb index 8a48b0f9f..5c8bfa782 100644 --- a/app/models/workbench.rb +++ b/app/models/workbench.rb @@ -1,11 +1,11 @@  class Workbench < ActiveRecord::Base +  include ObjectidFormaterSupport    extend Enumerize    belongs_to :organisation    belongs_to :line_referential    belongs_to :stop_area_referential    belongs_to :output, class_name: 'ReferentialSuite' -  enumerize :objectid_format, in: %w(netex stif_netex) -  validates_presence_of :objectid_format +  enumerize :objectid_format, in: %w(netex stif_netex stif_reflex stif_codifligne)    has_many :lines, -> (workbench) { Stif::MyWorkbenchScopes.new(workbench).line_scope(self) }, through: :line_referential    has_many :networks, through: :line_referential @@ -20,6 +20,7 @@ class Workbench < ActiveRecord::Base    validates :name, presence: true    validates :organisation, presence: true    validates :output, presence: true +  validates_presence_of :objectid_format    has_many :referentials    has_many :referential_metadatas, through: :referentials, source: :metadatas diff --git a/spec/factories/chouette_lines.rb b/spec/factories/chouette_lines.rb index 423ab99f2..fb15e12de 100644 --- a/spec/factories/chouette_lines.rb +++ b/spec/factories/chouette_lines.rb @@ -8,10 +8,7 @@ FactoryGirl.define do      association :network, :factory => :network      association :company, :factory => :company - -    before(:create) do |line| -      line.line_referential ||= LineReferential.find_by! name: "first" -    end +    association :line_referential, :factory => :line_referential      sequence(:registration_number) { |n| "test-#{n}" } diff --git a/spec/factories/line_referentials.rb b/spec/factories/line_referentials.rb index 6db4aab18..e9e6dce5a 100644 --- a/spec/factories/line_referentials.rb +++ b/spec/factories/line_referentials.rb @@ -1,6 +1,6 @@  FactoryGirl.define do    factory :line_referential do      sequence(:name) { |n| "Line Referential #{n}" } -    objectid_format 'netex' +    objectid_format 'stif_codifligne'    end  end diff --git a/spec/factories/referentials.rb b/spec/factories/referentials.rb index ece48a54e..0276a47be 100644 --- a/spec/factories/referentials.rb +++ b/spec/factories/referentials.rb @@ -8,7 +8,7 @@ FactoryGirl.define do      association :organisation      time_zone "Europe/Paris"      ready { true } -    objectid_format "netex" +    objectid_format "stif_netex"      factory :workbench_referential do        association :workbench diff --git a/spec/factories/stop_area_referentials.rb b/spec/factories/stop_area_referentials.rb index 253ef9715..fcba996e4 100644 --- a/spec/factories/stop_area_referentials.rb +++ b/spec/factories/stop_area_referentials.rb @@ -1,6 +1,6 @@  FactoryGirl.define do    factory :stop_area_referential, :class => StopAreaReferential do      sequence(:name) { |n| "StopArea Referential #{n}" } -    objectid_format 'netex' +    objectid_format 'stif_reflex'    end  end diff --git a/spec/factories/workbenches.rb b/spec/factories/workbenches.rb index f231fcdeb..0f26559d8 100644 --- a/spec/factories/workbenches.rb +++ b/spec/factories/workbenches.rb @@ -1,7 +1,7 @@  FactoryGirl.define do    factory :workbench do      name "Gestion de l'offre" -    objectid_format 'netex' +    objectid_format 'stif_netex'      association :organisation      association :line_referential diff --git a/spec/models/chouette/access_link_spec.rb b/spec/models/chouette/access_link_spec.rb index 5a31b8f0c..ecd78e079 100644 --- a/spec/models/chouette/access_link_spec.rb +++ b/spec/models/chouette/access_link_spec.rb @@ -6,8 +6,10 @@ describe Chouette::AccessLink, :type => :model do    it { is_expected.to validate_uniqueness_of :objectid }    describe '#objectid' do -    subject { super().objectid } -    it { is_expected.to be_kind_of(Chouette::StifNetexObjectid) } +    it "should have the same class as stop_area and access_point objectid" do +      expect(subject.objectid.class).to eq(subject.stop_area.objectid.class) +      expect(subject.objectid.class).to eq(subject.access_point.objectid.class) +    end    end    it { is_expected.to validate_presence_of :name } diff --git a/spec/models/chouette/access_point_spec.rb b/spec/models/chouette/access_point_spec.rb index e0f4b1501..a6798ec3b 100644 --- a/spec/models/chouette/access_point_spec.rb +++ b/spec/models/chouette/access_point_spec.rb @@ -4,8 +4,9 @@ describe Chouette::AccessPoint, :type => :model do    subject { create(:access_point) }    describe '#objectid' do -    subject { super().objectid } -    it { is_expected.to be_kind_of(Chouette::StifReflexObjectid) } +    it "should have the same class as stop_area objectid" do +      expect(subject.objectid.class).to eq(subject.stop_area.objectid.class) +    end    end    it { is_expected.to validate_presence_of :name } diff --git a/spec/models/chouette/connection_link_spec.rb b/spec/models/chouette/connection_link_spec.rb index 57eb7d66c..04a12c0bb 100644 --- a/spec/models/chouette/connection_link_spec.rb +++ b/spec/models/chouette/connection_link_spec.rb @@ -11,7 +11,7 @@ describe Chouette::ConnectionLink, :type => :model do    describe '#objectid' do      subject { super().objectid } -    it { is_expected.to be_kind_of(Chouette::StifNetexObjectid) } +    it { is_expected.to be_kind_of(Chouette::Objectid::Netex) }    end    it { is_expected.to validate_presence_of :name } diff --git a/spec/models/chouette/journey_pattern_spec.rb b/spec/models/chouette/journey_pattern_spec.rb index d631511a3..a3017ee7a 100644 --- a/spec/models/chouette/journey_pattern_spec.rb +++ b/spec/models/chouette/journey_pattern_spec.rb @@ -70,7 +70,7 @@ describe Chouette::JourneyPattern, :type => :model do      end      it 'should create journey_pattern' do -      new_state = journey_pattern_to_state(build(:journey_pattern, objectid: nil, route: route)) +      new_state = journey_pattern_to_state(create(:journey_pattern, route: route))        Chouette::JourneyPattern.state_create_instance route, new_state        expect(new_state['object_id']).to be_truthy        expect(new_state['new_record']).to be_truthy | 
