aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlban Peignier2017-12-20 09:47:30 +0100
committerGitHub2017-12-20 09:47:30 +0100
commitc13540b1b10451c9b26045cbfcb5ec397d1ddbc0 (patch)
treeaad16c680a6f0dfe407d0306d381cdd71fab86d5
parentfe7f4003569630a73980e87c370d4130e1d18098 (diff)
parent79c64f5f5cd550671c4f8dd81c26100137f05f4d (diff)
downloadchouette-core-c13540b1b10451c9b26045cbfcb5ec397d1ddbc0.tar.bz2
Merge pull request #157 from af83/5311-stop-area-type-customizable
Make customizable StopArea type. Refs #5311
-rw-r--r--app/controllers/stop_areas_controller.rb2
-rw-r--r--app/models/chouette/area_type.rb37
-rw-r--r--app/models/chouette/stop_area.rb4
-rw-r--r--app/views/stop_areas/_filters.html.slim2
-rw-r--r--app/views/stop_areas/_form.html.slim2
-rw-r--r--app/views/stop_areas/index.html.slim2
-rw-r--r--app/views/stop_areas/show.html.slim2
-rw-r--r--app/views/stop_points/_stop_point.html.slim4
-rw-r--r--spec/factories/chouette_stop_areas.rb2
-rw-r--r--spec/models/chouette/area_type_spec.rb32
10 files changed, 80 insertions, 9 deletions
diff --git a/app/controllers/stop_areas_controller.rb b/app/controllers/stop_areas_controller.rb
index 133518324..d4d996adb 100644
--- a/app/controllers/stop_areas_controller.rb
+++ b/app/controllers/stop_areas_controller.rb
@@ -171,7 +171,7 @@ class StopAreasController < ChouetteController
helper_method :current_referential
def stop_area_params
- params.require(:stop_area).permit( :routing_stop_ids, :routing_line_ids, :children_ids, :stop_area_type, :parent_id, :objectid, :object_version, :name, :comment, :area_type, :registration_number, :nearest_topic_name, :fare_code, :longitude, :latitude, :long_lat_type, :country_code, :street_name, :zip_code, :city_name, :mobility_restricted_suitability, :stairs_availability, :lift_availability, :int_user_needs, :coordinates, :url, :time_zone )
+ params.require(:stop_area).permit( :routing_stop_ids, :routing_line_ids, :children_ids, :parent_id, :objectid, :object_version, :name, :comment, :area_type, :registration_number, :nearest_topic_name, :fare_code, :longitude, :latitude, :long_lat_type, :country_code, :street_name, :zip_code, :city_name, :mobility_restricted_suitability, :stairs_availability, :lift_availability, :int_user_needs, :coordinates, :url, :time_zone )
end
end
diff --git a/app/models/chouette/area_type.rb b/app/models/chouette/area_type.rb
new file mode 100644
index 000000000..f295fe7ae
--- /dev/null
+++ b/app/models/chouette/area_type.rb
@@ -0,0 +1,37 @@
+class Chouette::AreaType
+
+ ALL = %i(zdep zder zdlp zdlr lda).freeze
+
+ @@all = ALL
+ mattr_accessor :all
+
+ def self.all=(values)
+ @@all = ALL & values
+ reset_caches!
+ end
+
+ @@instances = {}
+ def self.find(code)
+ code = code.to_sym
+ @@instances[code] ||= new(code) if ALL.include? code
+ end
+
+ def self.reset_caches!
+ @@instances = {}
+ @@options = nil
+ end
+
+ def self.options
+ @@options ||= all.map { |c| find(c) }.map { |t| [ t.label, t.code ] }
+ end
+
+ attr_reader :code
+ def initialize(code)
+ @code = code
+ end
+
+ def label
+ I18n.translate code, scope: 'area_types.label'
+ end
+
+end
diff --git a/app/models/chouette/stop_area.rb b/app/models/chouette/stop_area.rb
index cc7170728..f216ce449 100644
--- a/app/models/chouette/stop_area.rb
+++ b/app/models/chouette/stop_area.rb
@@ -9,7 +9,7 @@ module Chouette
include ObjectidSupport
extend Enumerize
- enumerize :area_type, in: %i(zdep zder zdlp zdlr lda)
+ enumerize :area_type, in: Chouette::AreaType::ALL
with_options dependent: :destroy do |assoc|
assoc.has_many :stop_points
@@ -196,10 +196,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
diff --git a/app/views/stop_areas/_filters.html.slim b/app/views/stop_areas/_filters.html.slim
index 3b99f377c..90368dff4 100644
--- a/app/views/stop_areas/_filters.html.slim
+++ b/app/views/stop_areas/_filters.html.slim
@@ -12,7 +12,7 @@
.form-group.togglable
= f.label Chouette::StopArea.human_attribute_name(:area_type), required: false, class: 'control-label'
- = f.input :area_type_eq_any, collection: Chouette::StopArea.area_type.options.sort, as: :check_boxes, label: false, label_method: lambda{|w| ("<span>" + t("enumerize.stop_area.area_type.#{w[1]}") + "</span>").html_safe}, required: false, wrapper_html: { class: 'checkbox_list' }
+ = f.input :area_type_eq_any, collection: Chouette::AreaType.options, as: :check_boxes, label: false, label_method: lambda{|w| ("<span>" + w[0] + "</span>").html_safe}, required: false, wrapper_html: { class: 'checkbox_list' }
.actions
= link_to 'Effacer', @workbench, class: 'btn btn-link'
diff --git a/app/views/stop_areas/_form.html.slim b/app/views/stop_areas/_form.html.slim
index 20c7c0468..ac2cb4e87 100644
--- a/app/views/stop_areas/_form.html.slim
+++ b/app/views/stop_areas/_form.html.slim
@@ -6,7 +6,7 @@
/= @map.to_html
= f.input :id, as: :hidden
= f.input :name, :input_html => {:title => t("formtastic.titles#{format_restriction_for_locales(@referential)}.stop_area.name")}
- = f.input :area_type, as: :select, :input_html => {:disabled => !@stop_area.new_record?}, :collection => Chouette::StopArea.area_type.options, :include_blank => false
+ = f.input :area_type, as: :select, :input_html => {:disabled => !@stop_area.new_record?}, :collection => Chouette::AreaType.options, :include_blank => false
.location_info
h3 = t("stop_areas.stop_area.localisation")
diff --git a/app/views/stop_areas/index.html.slim b/app/views/stop_areas/index.html.slim
index dbf3b848d..63e99fd75 100644
--- a/app/views/stop_areas/index.html.slim
+++ b/app/views/stop_areas/index.html.slim
@@ -48,7 +48,7 @@
), \
TableBuilderHelper::Column.new( \
key: :area_type, \
- attribute: Proc.new { |s| (s.area_type.nil? ? '-' : t("enumerize.stop_area.area_type.#{s.try(:area_type)}")) } \
+ attribute: Proc.new { |s| Chouette::AreaType.find(s.area_type).try :label } \
), \
],
links: [:show],
diff --git a/app/views/stop_areas/show.html.slim b/app/views/stop_areas/show.html.slim
index af673bb25..1b1209a68 100644
--- a/app/views/stop_areas/show.html.slim
+++ b/app/views/stop_areas/show.html.slim
@@ -17,7 +17,7 @@
.col-lg-6.col-md-6.col-sm-12.col-xs-12
= definition_list t('metadatas'),
{ t('id_reflex') => @stop_area.get_objectid.short_id,
- @stop_area.human_attribute_name(:stop_area_type) => t("area_types.label.#{@stop_area.stop_area_type}"),
+ @stop_area.human_attribute_name(:stop_area_type) => Chouette::AreaType.find(@stop_area.area_type).try(:label),
@stop_area.human_attribute_name(:registration_number) => @stop_area.registration_number,
'Coordonnées' => geo_data(@stop_area, @stop_area_referential),
@stop_area.human_attribute_name(:zip_code) => @stop_area.zip_code,
diff --git a/app/views/stop_points/_stop_point.html.slim b/app/views/stop_points/_stop_point.html.slim
index ca86e339a..e54158cef 100644
--- a/app/views/stop_points/_stop_point.html.slim
+++ b/app/views/stop_points/_stop_point.html.slim
@@ -5,7 +5,7 @@
= link_to [@referential, stop_point.stop_area], class: "preview", title: "#{Chouette::StopArea.model_name.human.capitalize} #{stop_point.stop_area.name}" do
span.name
span.label.label-primary = stop_point.position + 1
- = image_tag "map/" + stop_point.stop_area.stop_area_type + ".png"
+ = image_tag "map/" + stop_point.stop_area.area_type + ".png"
= truncate(stop_point.stop_area.name, length: 20)
.panel-body
@@ -27,4 +27,4 @@
= t(".no_object")
- else
- stop_point.stop_area.lines.each do |line|
- span.label.label-default.line = line.number || truncate( line.name, length: 4 ) \ No newline at end of file
+ span.label.label-default.line = line.number || truncate( line.name, length: 4 )
diff --git a/spec/factories/chouette_stop_areas.rb b/spec/factories/chouette_stop_areas.rb
index 8b64c227b..7f937e361 100644
--- a/spec/factories/chouette_stop_areas.rb
+++ b/spec/factories/chouette_stop_areas.rb
@@ -3,7 +3,7 @@ FactoryGirl.define do
sequence(:objectid) { |n| "FR:#{n}:ZDE:#{n}:STIF" }
sequence(:name) { |n| "stop_area_#{n}" }
sequence(:registration_number) { |n| "test-#{n}" }
- area_type { Chouette::StopArea.area_type.values.sample }
+ area_type { Chouette::AreaType.all.sample }
latitude {10.0 * rand}
longitude {10.0 * rand}
diff --git a/spec/models/chouette/area_type_spec.rb b/spec/models/chouette/area_type_spec.rb
new file mode 100644
index 000000000..470e3a066
--- /dev/null
+++ b/spec/models/chouette/area_type_spec.rb
@@ -0,0 +1,32 @@
+require "rails_helper"
+
+RSpec.describe Chouette::AreaType do
+
+ describe "::ALL" do
+ it "includes all supported types" do
+ expect(Chouette::AreaType::ALL).to match_array( %i(zdep zder zdlp zdlr lda) )
+ end
+ end
+
+ describe ".find" do
+ it "returns nil if the given code is unknown" do
+ expect(Chouette::AreaType.find('dummy')).to be_nil
+ end
+
+ it "returns an AreaType associated to the code" do
+ expect(Chouette::AreaType.find('zdep').code).to eq :zdep
+ end
+ end
+
+ describe ".options" do
+ before do
+ Chouette::AreaType.reset_caches!
+ end
+
+ it "returns an array with label and code for each type" do
+ allow(Chouette::AreaType).to receive(:all).and_return(%i{zdep lda})
+ expect(Chouette::AreaType.options).to eq([["ZDEp", :zdep], ["LDA", :lda]])
+ end
+ end
+
+end