diff options
Diffstat (limited to 'app/models')
25 files changed, 381 insertions, 81 deletions
| diff --git a/app/models/chouette/access_point.rb b/app/models/chouette/access_point.rb index 43c8e1b3a..317b19b26 100644 --- a/app/models/chouette/access_point.rb +++ b/app/models/chouette/access_point.rb @@ -5,6 +5,8 @@ class Chouette::AccessPoint < Chouette::TridentActiveRecord    # FIXME http://jira.codehaus.org/browse/JRUBY-6358    self.primary_key = "id"    include Geokit::Mappable +  include ProjectionFields +    has_many :access_links, :dependent => :destroy    belongs_to :stop_area diff --git a/app/models/chouette/company.rb b/app/models/chouette/company.rb index d0375b2e6..8ddd4c37d 100644 --- a/app/models/chouette/company.rb +++ b/app/models/chouette/company.rb @@ -1,4 +1,5 @@  class Chouette::Company < Chouette::TridentActiveRecord +  include CompanyRestrictions    has_many :lines    validates_format_of :registration_number, :with => %r{\A[0-9A-Za-z_-]+\Z}, :allow_nil => true, :allow_blank => true diff --git a/app/models/chouette/connection_link.rb b/app/models/chouette/connection_link.rb index 045f7c1d9..e225c2fae 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 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 1c1ae5f4c..f00db3164 100644 --- a/app/models/chouette/group_of_line.rb +++ b/app/models/chouette/group_of_line.rb @@ -1,4 +1,5 @@  class Chouette::GroupOfLine < Chouette::TridentActiveRecord +  include GroupOfLineRestrictions    # FIXME http://jira.codehaus.org/browse/JRUBY-6358    self.primary_key = "id" diff --git a/app/models/chouette/journey_pattern.rb b/app/models/chouette/journey_pattern.rb index d48733edb..9b2b2a9da 100644 --- a/app/models/chouette/journey_pattern.rb +++ b/app/models/chouette/journey_pattern.rb @@ -1,4 +1,5 @@  class Chouette::JourneyPattern < Chouette::TridentActiveRecord +  include JourneyPatternRestrictions    # FIXME http://jira.codehaus.org/browse/JRUBY-6358    self.primary_key = "id" diff --git a/app/models/chouette/line.rb b/app/models/chouette/line.rb index d69203233..b1e9940d3 100644 --- a/app/models/chouette/line.rb +++ b/app/models/chouette/line.rb @@ -1,4 +1,5 @@  class Chouette::Line < Chouette::TridentActiveRecord +  include LineRestrictions    # FIXME http://jira.codehaus.org/browse/JRUBY-6358    self.primary_key = "id" diff --git a/app/models/chouette/network.rb b/app/models/chouette/network.rb index a631d70ec..c8926506c 100644 --- a/app/models/chouette/network.rb +++ b/app/models/chouette/network.rb @@ -1,4 +1,5 @@  class Chouette::Network < Chouette::TridentActiveRecord +  include NetworkRestrictions    # FIXME http://jira.codehaus.org/browse/JRUBY-6358    self.primary_key = "id" diff --git a/app/models/chouette/route.rb b/app/models/chouette/route.rb index d5e39ac12..8949a6bc2 100644 --- a/app/models/chouette/route.rb +++ b/app/models/chouette/route.rb @@ -1,4 +1,6 @@  class Chouette::Route < Chouette::TridentActiveRecord +  include RouteRestrictions +    # FIXME http://jira.codehaus.org/browse/JRUBY-6358    self.primary_key = "id" diff --git a/app/models/chouette/stop_area.rb b/app/models/chouette/stop_area.rb index b7cdd313a..70d2edcbd 100644 --- a/app/models/chouette/stop_area.rb +++ b/app/models/chouette/stop_area.rb @@ -5,6 +5,9 @@ class Chouette::StopArea < Chouette::TridentActiveRecord    # FIXME http://jira.codehaus.org/browse/JRUBY-6358    self.primary_key = "id"    include Geokit::Mappable +  include ProjectionFields +  include StopAreaRestrictions +    has_many :stop_points, :dependent => :destroy    has_many :access_points, :dependent => :destroy    has_many :access_links, :dependent => :destroy @@ -152,7 +155,7 @@ class Chouette::StopArea < Chouette::TridentActiveRecord    def default_position      # for first StopArea ... the bounds is nil :( -    Chouette::StopArea.bounds and Chouette::StopArea.bounds.center +      Chouette::StopArea.bounds ? Chouette::StopArea.bounds.center : self.referential.envelope.center    end    def self.near(origin, distance = 0.3) diff --git a/app/models/chouette/time_table.rb b/app/models/chouette/time_table.rb index 033c39f1c..9af80fbb7 100644 --- a/app/models/chouette/time_table.rb +++ b/app/models/chouette/time_table.rb @@ -1,4 +1,5 @@  class Chouette::TimeTable < Chouette::TridentActiveRecord +  include TimeTableRestrictions    # FIXME http://jira.codehaus.org/browse/JRUBY-6358    self.primary_key = "id" @@ -29,6 +30,10 @@ class Chouette::TimeTable < Chouette::TridentActiveRecord    validates_associated :dates    validates_associated :periods +  def presenter +    @presenter ||= ::TimeTablePresenter.new( self) +  end +    def self.start_validity_period      [Chouette::TimeTable.minimum(:start_date)].compact.min    end @@ -445,6 +450,5 @@ class Chouette::TimeTable < Chouette::TridentActiveRecord      tt.comment = I18n.t("activerecord.copy", :name => self.comment)      tt    end -  end diff --git a/app/models/chouette/trident_active_record.rb b/app/models/chouette/trident_active_record.rb index b89b85863..225d7bb4b 100644 --- a/app/models/chouette/trident_active_record.rb +++ b/app/models/chouette/trident_active_record.rb @@ -12,10 +12,19 @@ class Chouette::TridentActiveRecord < Chouette::ActiveRecord      def self.object_id_key        model_name      end + +    def referential +      @referential ||= Referential.where(:slug => Apartment::Tenant.current).first! +    end + +    def hub_restricted? +      referential.data_format == "hub" +    end      def prefix -      "NINOXE" +      self.referential.prefix      end +      def prepare_auto_columns        # logger.info 'calling before_validation'        # logger.info 'start before_validation : '+self.objectid.to_s diff --git a/app/models/chouette/vehicle_journey.rb b/app/models/chouette/vehicle_journey.rb index 44a9f8975..17d4d952f 100644 --- a/app/models/chouette/vehicle_journey.rb +++ b/app/models/chouette/vehicle_journey.rb @@ -1,6 +1,6 @@  module Chouette    class VehicleJourney < TridentActiveRecord - +    include VehicleJourneyRestrictions      # FIXME http://jira.codehaus.org/browse/JRUBY-6358      self.primary_key = "id" @@ -43,6 +43,11 @@ module Chouette      accepts_nested_attributes_for :vehicle_journey_at_stops, :allow_destroy => true + +    def presenter +      @presenter ||= ::VehicleJourneyPresenter.new( self) +    end +      def transport_mode_name        # return nil if transport_mode is nil        transport_mode && Chouette::TransportMode.new( transport_mode.underscore) diff --git a/app/models/concerns/company_restrictions.rb b/app/models/concerns/company_restrictions.rb new file mode 100644 index 000000000..3a5f17da8 --- /dev/null +++ b/app/models/concerns/company_restrictions.rb @@ -0,0 +1,25 @@ +module CompanyRestrictions +  extend ActiveSupport::Concern + +  included do +    include ObjectidRestrictions + +    with_options if: :hub_restricted? do |g| +      # HUB-7 +      g.validate :specific_objectid +      # HUB-8 +      #g.validates_length_of :name, :minimum => 1, :maximum => 75 +      g.validates :name, length: { in: 1..75 } +      # HUB-9 +      #g.validates_format_of :registration_number, :with => %r{\A[\d]{1,8}\z} +      #g.validates_uniqueness_of :registration_number +      g.validates :registration_number, +                  uniqueness: true, +                  length: { in: 1..8 }, +                  numericality: { only_integer: true } +    end +  end +  def specific_objectid +    validate_specific_objectid(3) +  end +end diff --git a/app/models/concerns/connection_link_restrictions.rb b/app/models/concerns/connection_link_restrictions.rb new file mode 100644 index 000000000..dd10d3659 --- /dev/null +++ b/app/models/concerns/connection_link_restrictions.rb @@ -0,0 +1,13 @@ +module ConnectionLinkRestrictions +  extend ActiveSupport::Concern + +  included do +    include ObjectidRestrictions + +    with_options if: :hub_restricted? do |cl| +      # HUB-34 +      cl.validates :link_distance, numericality: { less_than_or_equal_to: 10000.to_f } +    end +  end +end + diff --git a/app/models/concerns/group_of_line_restrictions.rb b/app/models/concerns/group_of_line_restrictions.rb new file mode 100644 index 000000000..a3bea08a8 --- /dev/null +++ b/app/models/concerns/group_of_line_restrictions.rb @@ -0,0 +1,25 @@ +module GroupOfLineRestrictions +  extend ActiveSupport::Concern + +  included do +    include ObjectidRestrictions + +    with_options if: :hub_restricted? do |g| +      # HUB-11 +      g.validate :specific_objectid +      # HUB-12 +      #g.validates_length_of :name, :minimum => 1, :maximum => 75 +      g.validates :name, length: { in: 1..75 } +      # HUB-13 +      #g.validates_format_of :registration_number, :with => %r{\A[\d]{1,8}\z} +      #g.validates_uniqueness_of :registration_number +      g.validates :registration_number, +                  uniqueness: true, +                  numericality: { only_integer: true }, +                  length: { in: 1..8 } +    end +  end +  def specific_objectid +    validate_specific_objectid(6) +  end +end diff --git a/app/models/concerns/journey_pattern_restrictions.rb b/app/models/concerns/journey_pattern_restrictions.rb new file mode 100644 index 000000000..e8d5d203d --- /dev/null +++ b/app/models/concerns/journey_pattern_restrictions.rb @@ -0,0 +1,20 @@ +module JourneyPatternRestrictions +  extend ActiveSupport::Concern + +  included do +    include ObjectidRestrictions + +    with_options if: :hub_restricted? do |jp| +      # HUB-39 +      jp.validate :specific_objectid +      # HUB-40 +      jp.validates :registration_number, numericality: { less_than: 10 ** 8 } +      # HUB-41 +      #jp.validates_length_of :name, :maximum => 75, :allow_blank => true, :allow_nil => true +      jp.validates :name, length: { maximum: 75 }, allow_blank: true +    end +  end +  def specific_objectid +    validate_specific_objectid( 30) +  end +end diff --git a/app/models/concerns/line_restrictions.rb b/app/models/concerns/line_restrictions.rb new file mode 100644 index 000000000..c76d5ba19 --- /dev/null +++ b/app/models/concerns/line_restrictions.rb @@ -0,0 +1,29 @@ +module LineRestrictions +  extend ActiveSupport::Concern + + +  included do +    include ObjectidRestrictions + +    with_options if: :hub_restricted? do |l| +      # HUB-15 +      #l.validates_format_of :objectid, :with => %r{\A\w+:\w+:[\w]{1,14}\z} +      l.validate :specific_objectid +      # HUB-16 +      #l.validates_format_of :number, :with => %r{\A[\w]{1,6}\z} +      l.validates :number, length: { in: 1..6 }, format: { with: /\A[\w]+\z/ } +      # HUB-17 & HUB-22 +      #l.validates_length_of :name, :maximum => 75 +      l.validates :name, length: { maximum: 75 }, uniqueness: true, allow_blank: true +      # HUB-21 +      #l.validates :registration_number, :numericality => { :less_than => 10 ** 8 } +      l.validates :registration_number, presence: true, numericality: { less_than: 10 ** 8 } +      # HUB-22 +      #l.validates_uniqueness_of :name, :allow_blank => true +    end +  end + +  def specific_objectid +    validate_specific_objectid( 14) +  end +end diff --git a/app/models/concerns/network_restrictions.rb b/app/models/concerns/network_restrictions.rb new file mode 100644 index 000000000..9c5d159b6 --- /dev/null +++ b/app/models/concerns/network_restrictions.rb @@ -0,0 +1,25 @@ +module NetworkRestrictions +  extend ActiveSupport::Concern + +  included do +    include ObjectidRestrictions + +    with_options if: :hub_restricted? do |n| +      # HUB-3 +      n.validate :specific_objectid +      # HUB-4 +      #n.validates_length_of :name, :minimum => 1, :maximum => 75 +      n.validates :name, length: { in: 1..75 } +      # HUB-5 +      #n.validates_format_of :registration_number, :with => %r{\A[\d]{1,8}\z} +      #n.validates_uniqueness_of :registration_number +      n.validates :registration_number, +                  uniqueness: true, +                  numericality: { only_integer: true }, +                  length: { in: 1..8 } +    end +  end +  def specific_objectid +    validate_specific_objectid( 3) +  end +end diff --git a/app/models/concerns/objectid_restrictions.rb b/app/models/concerns/objectid_restrictions.rb new file mode 100644 index 000000000..4e905bdc2 --- /dev/null +++ b/app/models/concerns/objectid_restrictions.rb @@ -0,0 +1,28 @@ +module ObjectidRestrictions +    extend ActiveSupport::Concern + +    included do +      # HUB 1 +      validate :third_part_objectid_uniqueness +    end + +    def validate_specific_objectid( size_max ) +      #errors.add( :objectid, I18n.t('hub.invalid')) if ( %r{\A\w+:\w+:\w+\z}).match( self.objectid).nil? + +      if third_part_objectid.nil? || ( !third_part_objectid.include?( "_pending_" ) && third_part_objectid.size > size_max) +        errors.add( :objectid, I18n.t('hub.invalid')) +      end +    end +    def third_part_objectid +      return nil if ( %r{\A\w+:\w+:[0-9A-Za-z_-]+\z}).match( self.objectid).nil? +      self.objectid.match(/:([0-9A-Za-z_-]+)\z/)[1] +    end +    def third_part_objectid_uniqueness +      return unless hub_restricted? + +      return true unless third_part_objectid +      likes = self.class.where( "objectid LIKE ?", "%:#{self.third_part_objectid}" ) +      likes.size.zero? || ( likes.size==1 && likes.first.id==self.id) +    end +end + diff --git a/app/models/concerns/projection_fields.rb b/app/models/concerns/projection_fields.rb new file mode 100644 index 000000000..2d7222f73 --- /dev/null +++ b/app/models/concerns/projection_fields.rb @@ -0,0 +1,60 @@ +module ProjectionFields +  extend ActiveSupport::Concern + +  included do +    #attr_accessible :projection_x,:projection_y,:projection_xy + +    # add projection_type set on pre-insert and pre_update action +  before_save :set_projections +  def set_projections +    if ! self.coordinates.blank? +      self.long_lat_type = 'WGS84' +    else +      self.long_lat_type = nil +    end +  end + +  def projection +    if self.referential.projection_type.nil? || self.referential.projection_type.empty? +      nil +    else +      self.referential.projection_type +    end +  end +  @point = nil + +  def projection_x +    if self.long_lat_type.nil? || self.projection.nil? +      nil +    else +      @point ||= GeoRuby::SimpleFeatures::Point::from_lat_lng(Geokit::LatLng.new(self.latitude,self.longitude)).project_to(self.projection.to_i) +      @point.x +    end +  end +  def projection_y +    if self.long_lat_type.nil? || self.projection.nil? +      nil +    else +      @point ||= GeoRuby::SimpleFeatures::Point::from_lat_lng(Geokit::LatLng.new(self.latitude,self.longitude)).project_to(self.projection.to_i) +      @point.y +    end +  end +  def projection_xy +    if self.long_lat_type.nil? || self.projection.nil? +      nil +    else +      @point ||= GeoRuby::SimpleFeatures::Point::from_lat_lng(Geokit::LatLng.new(self.latitude,self.longitude)).project_to(self.projection.to_i) +      @point.x.to_s+","+@point.y.to_s +    end +  end +  def projection_x=(dummy) +    # dummy method +  end +  def projection_y=(dummy) +    # dummy method +  end +  def projection_xy=(dummy) +    # dummy method +  end +  end +end diff --git a/app/models/concerns/route_restrictions.rb b/app/models/concerns/route_restrictions.rb new file mode 100644 index 000000000..5a098d86f --- /dev/null +++ b/app/models/concerns/route_restrictions.rb @@ -0,0 +1,25 @@ +module RouteRestrictions +  extend ActiveSupport::Concern + +  included do +    include ObjectidRestrictions +    validate :max_instance_limitation, :wayback_code_limitation + +    # HUB-37 +    def wayback_code_limitation +      return unless hub_restricted? +      errors.add( :wayback_code, I18n.t('hub.routes.wayback_code_exclusive')) if line.routes.reject {|r| r.id==id}.map(&:wayback_code).include?( wayback_code) +    end + +    # HUB-37 +    def max_instance_limitation +      return unless hub_restricted? +      errors.add( :flash, I18n.t('hub.routes.max_by_line')) if 2 < line.routes.size +    end + +    # HUB-38 +    with_options if: :hub_restricted? do |route| +      route.validates_format_of :objectid, :with => %r{\A\w+:\w+:([\w]{1,8}|__pending_id__\d+)\z} +    end +  end +end diff --git a/app/models/concerns/stop_area_restrictions.rb b/app/models/concerns/stop_area_restrictions.rb new file mode 100644 index 000000000..38a57b55a --- /dev/null +++ b/app/models/concerns/stop_area_restrictions.rb @@ -0,0 +1,63 @@ +module StopAreaRestrictions +  extend ActiveSupport::Concern + +  def physical? +    self.area_type=="BoardingPosition" ||  self.area_type=="Quay" +  end +  def commercial? +    self.area_type=="CommercialStopPoint" +  end +  def physical_hub_restricted? +    hub_restricted? && physical? +  end +  def commercial_hub_restricted? +    hub_restricted? && commercial? +  end +  def commercial_and_physical_hub_restricted? +    physical_hub_restricted? || commercial_hub_restricted? +  end +  # def specific_objectid +  #   validate_specific_objectid( 12) +  # end + +  included do +    include ObjectidRestrictions + + +    with_options if: :commercial_and_physical_hub_restricted? do |sa| +      # HUB-23 +      sa.validate :specific_objectid +      #sa.validates_length_of :name, :minimum => 1, :maximum => 75 +      sa.validates :name, length: { in: 1..75 } +    end + +    with_options if: :commercial_hub_restricted? do |sa| +      # HUB-24 +      #sa.validates_length_of :nearest_topic_name, :maximum => 255, :allow_blank => true, :allow_nil => true +      sa.validates :nearest_topic_name, length: { maximum: 255 }, allow_blank: true +    end + +    with_options if: :physical_hub_restricted? do |sa| +      # HUB-25 +      #sa.validates_length_of :nearest_topic_name, :maximum => 60, :allow_blank => true, :allow_nil => true +      sa.validates :nearest_topic_name, length: { maximum: 60 }, allow_blank: true +      # HUB-28 +      #sa.validates_presence_of :coordinates +      sa.validates :coordinates, presence: true +      # HUB-29 +      #sa.validates_length_of :city_name, :minimum => 1, :maximum => 80 +      sa.validates :city_name, length: { in: 1..80 } +      # HUB-30 +      #sa.validates_format_of :country_code, :with => %r{\A[\d]{5}\z} +      sa.validates :country_code, presence: true, numericality: { only_integer: true }, length: { is: 5 } +      # HUB-31 +      #sa.validates_length_of :comment, :maximum => 255, :allow_blank => true, :allow_nil => true +      sa.validates :comment, length: { maximum: 255 }, allow_blank: true +      #sa.validates :registration_number, :numericality => { :less_than => 10 ** 8 } +      sa.validates :registration_number, presence: true, numericality: { less_than: 10 ** 8 } +    end +  end +  def specific_objectid +    validate_specific_objectid(12) +  end +end diff --git a/app/models/concerns/time_table_restrictions.rb b/app/models/concerns/time_table_restrictions.rb new file mode 100644 index 000000000..51576833c --- /dev/null +++ b/app/models/concerns/time_table_restrictions.rb @@ -0,0 +1,18 @@ +module TimeTableRestrictions +  extend ActiveSupport::Concern + +  included do +    include ObjectidRestrictions + +    with_options if: :hub_restricted? do |tt| +      # HUB-44 +      tt.validate :specific_objectid +      # HUB-45 +      #tt.validates_length_of :comment, :maximum => 75, :allow_blank => true, :allow_nil => true +      tt.validates :comment, length: { maximum: 75 }, allow_blank: true +    end +  end +  def specific_objectid +    validate_specific_objectid( 6) +  end +end diff --git a/app/models/concerns/vehicle_journey_restrictions.rb b/app/models/concerns/vehicle_journey_restrictions.rb new file mode 100644 index 000000000..1f024eb04 --- /dev/null +++ b/app/models/concerns/vehicle_journey_restrictions.rb @@ -0,0 +1,15 @@ +module VehicleJourneyRestrictions +  extend ActiveSupport::Concern + +  included do +    include ObjectidRestrictions + +    # HUB-42 +    with_options if: :hub_restricted? do |vj| +      vj.validate :specific_objectid +    end +  end +  def specific_objectid +    validate_specific_objectid( 8) +  end +end diff --git a/app/models/referential.rb b/app/models/referential.rb index 7c6de6bf9..1f0c5bcfd 100644 --- a/app/models/referential.rb +++ b/app/models/referential.rb @@ -201,81 +201,4 @@ class Referential < ActiveRecord::Base      bounds = read_attribute(:bounds)      GeoRuby::SimpleFeatures::Geometry.from_ewkt(bounds.present? ? bounds : default_bounds ).envelope    end - -  ## -  # In Development environment where cache_classes = false -  # each time a controller rb file is saved -  # ninoxe models are reloaded without after_initialize from config/initializers -  # so for development confort, it's better to keep here that after_initialize -Rails.application.config.after_initialize do - -  Chouette::TridentActiveRecord - -  class Chouette::TridentActiveRecord - -    # add referential relationship for objectid and localization functions -    def referential -      @referential ||= Referential.where(:slug => Apartment::Tenant.current).first! -    end - -    def hub_restricted? -      referential.data_format == "hub" -    end - -    # override prefix for good prefix in objectid generation -    def prefix -      self.referential.prefix -    end - -  end - -  # Hub constraints -  Chouette::Route; class Chouette::Route; include NinoxeExtension::Hub::RouteRestrictions; end -  Chouette::JourneyPattern; class Chouette::JourneyPattern; include NinoxeExtension::Hub::JourneyPatternRestrictions; end -  Chouette::VehicleJourney; class Chouette::VehicleJourney; include NinoxeExtension::Hub::VehicleJourneyRestrictions; end -  Chouette::TimeTable; class Chouette::TimeTable; include NinoxeExtension::Hub::TimeTableRestrictions; end -  Chouette::ConnectionLink; class Chouette::ConnectionLink; include NinoxeExtension::Hub::ConnectionLinkRestrictions; end -  Chouette::StopArea; class Chouette::StopArea; include NinoxeExtension::Hub::StopAreaRestrictions; end -  Chouette::Line; class Chouette::Line; include NinoxeExtension::Hub::LineRestrictions; end -  Chouette::GroupOfLine; class Chouette::GroupOfLine; include NinoxeExtension::Hub::GroupOfLineRestrictions; end -  Chouette::Company; class Chouette::Company; include NinoxeExtension::Hub::CompanyRestrictions; end -  Chouette::Network; class Chouette::Network; include NinoxeExtension::Hub::NetworkRestrictions; end - -  Chouette::TimeTable - -  class Chouette::TimeTable -    def presenter -      @presenter ||= ::TimeTablePresenter.new( self) -    end -  end - -  Chouette::VehicleJourney - -  class Chouette::VehicleJourney -    def presenter -      @presenter ||= ::VehicleJourneyPresenter.new( self) -    end -  end - -  Chouette::StopArea - -  class Chouette::StopArea -    include  NinoxeExtension::ProjectionFields - -    # override default_position method to add referential envelope when no stoparea is positioned -    def default_position -      # for first StopArea ... the bounds is nil , set to referential center -      Chouette::StopArea.bounds ? Chouette::StopArea.bounds.center : self.referential.envelope.center -    end - - -  end - -  Chouette::AccessPoint - -  class Chouette::AccessPoint -    include  NinoxeExtension::ProjectionFields -  end - -end  end | 
