From 22c38fb750843f0c74996175a6bd17a1f20a943c Mon Sep 17 00:00:00 2001 From: Zog Date: Fri, 26 Jan 2018 16:17:22 +0100 Subject: Refs #5750 @1h; Add a "kind" attribute to StopAreas This determines if the StopArea is commercial or not The useless fields are hidden in the form for the non-commercials ones --- app/models/chouette/area_type.rb | 25 ++++++++++++++++++------- app/models/chouette/stop_area.rb | 9 +++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) (limited to 'app/models') diff --git a/app/models/chouette/area_type.rb b/app/models/chouette/area_type.rb index 4703ea646..e17d2ee8d 100644 --- a/app/models/chouette/area_type.rb +++ b/app/models/chouette/area_type.rb @@ -1,13 +1,22 @@ class Chouette::AreaType include Comparable - ALL = %i(zdep zder zdlp zdlr lda gdl).freeze + COMMERCIAL = %i(zdep zder zdlp zdlr lda gdl).freeze + NON_COMMERCIAL = %i(deposit border service_area relief other).freeze + ALL = COMMERCIAL + NON_COMMERCIAL + @@commercial = COMMERCIAL + @@non_commercial = NON_COMMERCIAL @@all = ALL - mattr_accessor :all + mattr_accessor :all, :commercial, :non_commercial - def self.all=(values) - @@all = ALL & values + def self.commercial=(values) + @@commercial = COMMERCIAL & values + reset_caches! + end + + def self.non_commercial=(values) + @@non_commercial = NON_COMMERCIAL & values reset_caches! end @@ -20,12 +29,14 @@ class Chouette::AreaType end def self.reset_caches! + @@all = @@commercial + @@non_commercial @@instances = {} - @@options = nil + @@options = {} end - def self.options - @@options ||= all.map { |c| find(c) }.map { |t| [ t.label, t.code ] } + def self.options(kind=:all) + @@options ||= {} + @@options[kind] ||= self.send(kind).map { |c| find(c) }.map { |t| [ t.label, t.code ] } end attr_reader :code diff --git a/app/models/chouette/stop_area.rb b/app/models/chouette/stop_area.rb index ea1855ea8..d270a8696 100644 --- a/app/models/chouette/stop_area.rb +++ b/app/models/chouette/stop_area.rb @@ -10,6 +10,7 @@ module Chouette extend Enumerize enumerize :area_type, in: Chouette::AreaType::ALL + enumerize :kind, in: %i(commercial non_commercial) with_options dependent: :destroy do |assoc| assoc.has_many :stop_points @@ -96,6 +97,10 @@ module Chouette end end + def local_id + id.to_s + end + def children_in_depth return [] if self.children.empty? @@ -374,5 +379,9 @@ module Chouette return nil unless time_zone.present? ActiveSupport::TimeZone[time_zone]&.formatted_offset end + + def commercial? + kind == "commercial" + end end end -- cgit v1.2.3 From 05bc96db48a0a84fd2c50e457dc767f88950a9b4 Mon Sep 17 00:00:00 2001 From: Zog Date: Mon, 29 Jan 2018 08:45:02 +0100 Subject: Refs #5750 @1h; Manage non-commercial StopAreas - Add a `kind` attribute - Hide irrelevant fields in the form --- app/models/chouette/stop_area.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'app/models') diff --git a/app/models/chouette/stop_area.rb b/app/models/chouette/stop_area.rb index d270a8696..75a4a34bb 100644 --- a/app/models/chouette/stop_area.rb +++ b/app/models/chouette/stop_area.rb @@ -32,6 +32,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 @@ -42,6 +43,7 @@ module Chouette 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, @@ -57,6 +59,12 @@ module Chouette end end + def area_type_of_right_kind + unless Chouette::AreaType.send(self.kind).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 -- cgit v1.2.3 From 0101f0ca0a7354420118b0470532f804674edc34 Mon Sep 17 00:00:00 2001 From: Zog Date: Wed, 31 Jan 2018 10:46:02 +0100 Subject: Refs #5750; Fix validation --- app/models/chouette/stop_area.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'app/models') diff --git a/app/models/chouette/stop_area.rb b/app/models/chouette/stop_area.rb index 75a4a34bb..ad42d54ae 100644 --- a/app/models/chouette/stop_area.rb +++ b/app/models/chouette/stop_area.rb @@ -60,7 +60,8 @@ module Chouette end def area_type_of_right_kind - unless Chouette::AreaType.send(self.kind).include?(self.area_type) + + 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 -- cgit v1.2.3