diff options
Diffstat (limited to 'app/models/chouette/stop_area.rb')
| -rw-r--r-- | app/models/chouette/stop_area.rb | 77 |
1 files changed, 75 insertions, 2 deletions
diff --git a/app/models/chouette/stop_area.rb b/app/models/chouette/stop_area.rb index cc7170728..bb8747faa 100644 --- a/app/models/chouette/stop_area.rb +++ b/app/models/chouette/stop_area.rb @@ -2,14 +2,17 @@ require 'geokit' require 'geo_ruby' module Chouette class StopArea < Chouette::ActiveRecord - has_paper_trail + has_paper_trail class_name: 'PublicVersion' include ProjectionFields include StopAreaRestrictions include StopAreaReferentialSupport include ObjectidSupport extend Enumerize - enumerize :area_type, in: %i(zdep zder zdlp zdlr lda) + enumerize :area_type, in: Chouette::AreaType::ALL + enumerize :kind, in: %i(commercial non_commercial) + + AVAILABLE_LOCALIZATIONS = %i(gb nl de fr it es) with_options dependent: :destroy do |assoc| assoc.has_many :stop_points @@ -31,6 +34,7 @@ module Chouette validates_format_of :registration_number, :with => %r{\A[\d\w_\-]+\Z}, :allow_blank => true validates_presence_of :name + validates_presence_of :kind validates_presence_of :latitude, :if => :longitude validates_presence_of :longitude, :if => :latitude validates_numericality_of :latitude, :less_than_or_equal_to => 90, :greater_than_or_equal_to => -90, :allow_nil => true @@ -39,11 +43,36 @@ module Chouette validates_format_of :coordinates, :with => %r{\A *-?(0?[0-9](\.[0-9]*)?|[0-8][0-9](\.[0-9]*)?|90(\.[0]*)?) *\, *-?(0?[0-9]?[0-9](\.[0-9]*)?|1[0-7][0-9](\.[0-9]*)?|180(\.[0]*)?) *\Z}, :allow_nil => true, :allow_blank => true validates_format_of :url, :with => %r{\Ahttps?:\/\/([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?\Z}, :allow_nil => true, :allow_blank => true + validates_numericality_of :waiting_time, greater_than_or_equal_to: 0, only_integer: true, if: :waiting_time + validate :parent_area_type_must_be_greater + validate :area_type_of_right_kind + def self.nullable_attributes [:registration_number, :street_name, :country_code, :fare_code, :nearest_topic_name, :comment, :long_lat_type, :zip_code, :city_name, :url, :time_zone] end + def localized_names + val = read_attribute(:localized_names) || {} + Hash[*AVAILABLE_LOCALIZATIONS.map{|k| [k, val[k.to_s]]}.flatten] + end + + def parent_area_type_must_be_greater + return unless self.parent + + parent_area_type = Chouette::AreaType.find(self.parent.area_type) + if Chouette::AreaType.find(self.area_type) >= parent_area_type + errors.add(:parent_id, I18n.t('stop_areas.errors.parent_area_type', area_type: parent_area_type.label)) + end + end + + def area_type_of_right_kind + return unless self.kind + unless Chouette::AreaType.send(self.kind).map(&:to_s).include?(self.area_type) + errors.add(:area_type, I18n.t('stop_areas.errors.incorrect_kind_area_type')) + end + end + after_update :clean_invalid_access_links before_save :coordinates_to_lat_lng @@ -72,6 +101,10 @@ module Chouette end end + def full_name + "#{name} #{zip_code} #{city_name} - #{user_objectid}" + end + def user_objectid if objectid =~ /^.*:([0-9A-Za-z_-]+):STIF$/ $1 @@ -80,6 +113,8 @@ module Chouette end end + alias_method :local_id, :user_objectid + def children_in_depth return [] if self.children.empty? @@ -196,10 +231,12 @@ module Chouette GeoRuby::SimpleFeatures::Envelope.from_coordinates coordinates end + # DEPRECATED use StopArea#area_type def stop_area_type area_type ? area_type : " " end + # DEPRECATED use StopArea#area_type def stop_area_type=(stop_area_type) self.area_type = (stop_area_type ? stop_area_type.camelcase : nil) end @@ -324,5 +361,41 @@ module Chouette end end + def activated? + deleted_at.nil? + end + + def deactivated? + !activated? + end + + def activate! + update_attribute :deleted_at, nil + end + + def deactivate! + update_attribute :deleted_at, Time.now + end + + def time_zone_offset + return 0 unless time_zone.present? + ActiveSupport::TimeZone[time_zone]&.utc_offset + end + + def country_name + return unless country_code + + country = ISO3166::Country[country_code] + country.translations[I18n.locale.to_s] || country.name + end + + def time_zone_formatted_offset + return nil unless time_zone.present? + ActiveSupport::TimeZone[time_zone]&.formatted_offset + end + + def commercial? + kind == "commercial" + end end end |
