diff options
| -rw-r--r-- | app/assets/javascripts/select2.coffee | 17 | ||||
| -rw-r--r-- | app/controllers/stop_areas_controller.rb | 12 | ||||
| -rw-r--r-- | app/models/chouette/area_type.rb | 5 | ||||
| -rw-r--r-- | app/models/chouette/stop_area.rb | 12 | ||||
| -rw-r--r-- | app/views/autocomplete_stop_areas/index.rabl | 3 | ||||
| -rw-r--r-- | app/views/stop_areas/_form.html.slim | 3 | ||||
| -rw-r--r-- | app/views/stop_areas/autocomplete.rabl | 24 | ||||
| -rw-r--r-- | app/views/stop_areas/show.html.slim | 1 | ||||
| -rw-r--r-- | config/locales/stop_areas.en.yml | 1 | ||||
| -rw-r--r-- | config/locales/stop_areas.fr.yml | 1 | ||||
| -rw-r--r-- | config/routes.rb | 6 | ||||
| -rw-r--r-- | spec/models/chouette/stop_area_spec.rb | 36 |
12 files changed, 106 insertions, 15 deletions
diff --git a/app/assets/javascripts/select2.coffee b/app/assets/javascripts/select2.coffee index 2e3884d7f..4cf5f42d0 100644 --- a/app/assets/javascripts/select2.coffee +++ b/app/assets/javascripts/select2.coffee @@ -9,24 +9,27 @@ bind_select2 = (el, cfg = {}) -> target.select2 $.extend({}, default_cfg, cfg) bind_select2_ajax = (el, cfg = {}) -> - target = $(el) + _this = $(el) cfg = ajax: data: (params) -> - q: - "#{target.data('term')}": params.term - url: target.data('url'), + if _this.data('term') + { q: "#{_this.data('term')}": params.term } + else + { q: params.term } + url: _this.data('url'), dataType: 'json', delay: 125, processResults: (data, params) -> results: data - minimumInputLength: 1 - placeholder: target.data('select2ed-placeholder') templateResult: (item) -> item.text templateSelection: (item) -> item.text escapeMarkup: (markup) -> markup + initSelection : (item, callback) -> + if _this.data('initvalue') + callback(_this.data('initvalue')) bind_select2(el, cfg) @@ -40,7 +43,5 @@ bind_select2_ajax = (el, cfg = {}) -> $('select.form-control.tags').each -> bind_select2(this, {tags: true}) - - $ -> select_2() diff --git a/app/controllers/stop_areas_controller.rb b/app/controllers/stop_areas_controller.rb index b478d38fa..178a2413f 100644 --- a/app/controllers/stop_areas_controller.rb +++ b/app/controllers/stop_areas_controller.rb @@ -1,7 +1,7 @@ class StopAreasController < ChouetteController include ApplicationHelper include Activatable - + defaults :resource_class => Chouette::StopArea belongs_to :stop_area_referential @@ -14,10 +14,12 @@ class StopAreasController < ChouetteController respond_to :html, :kml, :xml, :json respond_to :js, :only => :index - # def complete - # @stop_areas = line.stop_areas - # render :layout => false - # end + def autocomplete + scope = stop_area_referential.stop_areas.where(deleted_at: nil) + args = [].tap{|arg| 4.times{arg << "%#{params[:q]}%"}} + @stop_areas = scope.where("unaccent(name) ILIKE unaccent(?) OR unaccent(city_name) ILIKE unaccent(?) OR registration_number ILIKE ? OR objectid ILIKE ?", *args).limit(50) + @stop_areas + end def select_parent @stop_area = stop_area diff --git a/app/models/chouette/area_type.rb b/app/models/chouette/area_type.rb index 33cbfbb48..43d96b391 100644 --- a/app/models/chouette/area_type.rb +++ b/app/models/chouette/area_type.rb @@ -1,4 +1,5 @@ class Chouette::AreaType + include Comparable ALL = %i(zdep zder zdlp zdlr lda gdl).freeze @@ -30,6 +31,10 @@ class Chouette::AreaType @code = code end + def <=>(other) + all.index(code) <=> all.index(other.code) + end + def label I18n.translate code, scope: 'area_types.label' end diff --git a/app/models/chouette/stop_area.rb b/app/models/chouette/stop_area.rb index 2f8d7c096..4f1359ff8 100644 --- a/app/models/chouette/stop_area.rb +++ b/app/models/chouette/stop_area.rb @@ -40,12 +40,20 @@ module Chouette 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 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 parent_area_type_must_be_greater + return unless self.parent + if Chouette::AreaType.find(self.area_type) >= Chouette::AreaType.find(self.parent.area_type) + errors.add(:parent_id, I18n.t('stop_areas.errors.parent_area_type', area_type: self.parent.area_type)) + end + end + after_update :clean_invalid_access_links before_save :coordinates_to_lat_lng @@ -74,6 +82,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 diff --git a/app/views/autocomplete_stop_areas/index.rabl b/app/views/autocomplete_stop_areas/index.rabl index 5a9f76a47..d20051ad5 100644 --- a/app/views/autocomplete_stop_areas/index.rabl +++ b/app/views/autocomplete_stop_areas/index.rabl @@ -14,7 +14,8 @@ node do |stop_area| :longitude => stop_area.longitude, :latitude => stop_area.latitude, :area_type => stop_area.area_type, - :comment => stop_area.comment + :comment => stop_area.comment, + :text => stop_area.full_name } end diff --git a/app/views/stop_areas/_form.html.slim b/app/views/stop_areas/_form.html.slim index e44680499..ef19d248a 100644 --- a/app/views/stop_areas/_form.html.slim +++ b/app/views/stop_areas/_form.html.slim @@ -6,6 +6,9 @@ /= @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 :parent_id, as: :select, :collection => [f.object.parent_id], input_html: { data: { select2_ajax: 'true', url: autocomplete_stop_area_referential_stop_areas_path(@stop_area_referential), initvalue: {id: f.object.parent_id, text: f.object.parent.try(:full_name)}}} + = f.input :area_type, as: :select, :input_html => {:disabled => !@stop_area.new_record?}, :collection => Chouette::AreaType.options, :include_blank => false .location_info diff --git a/app/views/stop_areas/autocomplete.rabl b/app/views/stop_areas/autocomplete.rabl new file mode 100644 index 000000000..3208289b5 --- /dev/null +++ b/app/views/stop_areas/autocomplete.rabl @@ -0,0 +1,24 @@ +collection @stop_areas + +node do |stop_area| + { + :id => stop_area.id, + :registration_number => stop_area.registration_number || "", + :short_registration_number => truncate(stop_area.registration_number, :length => 10) || "", + :name => stop_area.name || "", + :short_name => truncate(stop_area.name, :length => 30) || "", + :zip_code => stop_area.zip_code || "", + :city_name => stop_area.city_name || "", + :short_city_name => truncate(stop_area.city_name, :length => 15) || "", + :user_objectid => stop_area.user_objectid, + :longitude => stop_area.longitude, + :latitude => stop_area.latitude, + :area_type => stop_area.area_type, + :comment => stop_area.comment, + :text => stop_area.full_name + } +end + +node(:stop_area_path) { |stop_area| + stop_area_picture_url(stop_area) || "" +} diff --git a/app/views/stop_areas/show.html.slim b/app/views/stop_areas/show.html.slim index 0c23710b6..238a45b6b 100644 --- a/app/views/stop_areas/show.html.slim +++ b/app/views/stop_areas/show.html.slim @@ -16,6 +16,7 @@ .row .col-lg-6.col-md-6.col-sm-12.col-xs-12 - attributes = { t('id_reflex') => @stop_area.get_objectid.short_id, + @stop_area.human_attribute_name(:parent) => @stop_area.parent ? link_to(@stop_area.parent.name, stop_area_referential_stop_area_path(@stop_area_referential, @stop_area.parent)) : "-", @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, } diff --git a/config/locales/stop_areas.en.yml b/config/locales/stop_areas.en.yml index 4d84d1191..f6fe97c9a 100644 --- a/config/locales/stop_areas.en.yml +++ b/config/locales/stop_areas.en.yml @@ -4,6 +4,7 @@ en: search_no_results: "No stop area matching your query" errors: empty: Aucun stop_area_id + parent_area_type: can not be of type %{area_type} default_geometry_success: "%{count} modified stop areas" stop_area: no_position: "No Position" diff --git a/config/locales/stop_areas.fr.yml b/config/locales/stop_areas.fr.yml index eda1e4e3d..9dd7e0264 100644 --- a/config/locales/stop_areas.fr.yml +++ b/config/locales/stop_areas.fr.yml @@ -4,6 +4,7 @@ fr: search_no_results: "Aucun arrêt ne correspond à votre recherche" errors: empty: Aucun stop_area_id + parent_area_type: ne peut être de type %{area_type} default_geometry_success: "%{count} arrêts édités" stop_area: no_position: "Pas de position" diff --git a/config/routes.rb b/config/routes.rb index 22ff58724..bf796a385 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -93,7 +93,11 @@ ChouetteIhm::Application.routes.draw do resources :stop_area_referentials, :only => [:show] do post :sync, on: :member - resources :stop_areas, &deactivable + resources :stop_areas do + put :deactivate, on: :member + put :activate, on: :member + get :autocomplete, on: :collection + end end resources :line_referentials, :only => [:show, :edit, :update] do diff --git a/spec/models/chouette/stop_area_spec.rb b/spec/models/chouette/stop_area_spec.rb index bec8c0868..9db0f11a5 100644 --- a/spec/models/chouette/stop_area_spec.rb +++ b/spec/models/chouette/stop_area_spec.rb @@ -426,6 +426,42 @@ describe Chouette::StopArea, :type => :model do # end # end + describe "#parent" do + + let(:stop_area) { FactoryGirl.build :stop_area, parent: FactoryGirl.build(:stop_area) } + + it "is valid when parent has an 'higher' type" do + stop_area.area_type = 'zdep' + stop_area.parent.area_type = 'zdlp' + + stop_area.valid? + expect(stop_area.errors).to_not have_key(:parent_id) + end + + it "is valid when parent is undefined" do + stop_area.parent = nil + + stop_area.valid? + expect(stop_area.errors).to_not have_key(:parent_id) + end + + it "isn't valid when parent has the same type" do + stop_area.parent.area_type = stop_area.area_type = 'zdep' + + stop_area.valid? + expect(stop_area.errors).to have_key(:parent_id) + end + + it "isn't valid when parent has a lower type" do + stop_area.area_type = 'lda' + stop_area.parent.area_type = 'zdep' + + stop_area.valid? + expect(stop_area.errors).to have_key(:parent_id) + end + + end + describe '#waiting_time' do let(:stop_area) { FactoryGirl.build :stop_area } |
