diff options
| -rw-r--r-- | app/assets/stylesheets/main/vehicle_journey_frequencies.scss | 5 | ||||
| -rw-r--r-- | app/controllers/autocomplete_time_tables_controller.rb | 2 | ||||
| -rw-r--r-- | app/controllers/autocomplete_timebands_controller.rb | 31 | ||||
| -rw-r--r-- | app/inputs/search_timeband_input.rb | 44 | ||||
| -rw-r--r-- | app/views/autocomplete_timebands/index.rabl | 8 | ||||
| -rw-r--r-- | app/views/vehicle_journey_frequencies/_journey_frequency_fields.html.erb | 6 | ||||
| -rw-r--r-- | config/routes.rb | 1 |
7 files changed, 94 insertions, 3 deletions
diff --git a/app/assets/stylesheets/main/vehicle_journey_frequencies.scss b/app/assets/stylesheets/main/vehicle_journey_frequencies.scss index f973de209..8e077adae 100644 --- a/app/assets/stylesheets/main/vehicle_journey_frequencies.scss +++ b/app/assets/stylesheets/main/vehicle_journey_frequencies.scss @@ -51,13 +51,16 @@ display: table; width: 100%; padding-left: 25%; + .token-input-list { + width: 80%; + } .inline-errors { margin: 0; } > li { display: table-cell; width: 21%; - vertical-align: top; + vertical-align: middle; padding:0 ; margin: 0; &:first-child select { diff --git a/app/controllers/autocomplete_time_tables_controller.rb b/app/controllers/autocomplete_time_tables_controller.rb index d7e6ec885..ee0f8a0cb 100644 --- a/app/controllers/autocomplete_time_tables_controller.rb +++ b/app/controllers/autocomplete_time_tables_controller.rb @@ -4,7 +4,7 @@ class AutocompleteTimeTablesController < InheritedResources::Base before_action :switch_referential def switch_referential - Apartment::Database.switch(referential.slug) + Apartment::Tenant.switch!(referential.slug) end def referential diff --git a/app/controllers/autocomplete_timebands_controller.rb b/app/controllers/autocomplete_timebands_controller.rb new file mode 100644 index 000000000..3915bd69f --- /dev/null +++ b/app/controllers/autocomplete_timebands_controller.rb @@ -0,0 +1,31 @@ +class AutocompleteTimebandsController < InheritedResources::Base + respond_to :json, :only => [:index] + + before_action :switch_referential + + def switch_referential + Apartment::Tenant.switch!(referential.slug) + end + + def referential + @referential ||= current_organisation.referentials.find params[:referential_id] + end + + protected + + def select_timebands + if params[:route_id] + referential.timebands.joins( vehicle_journeys: :route).where( "routes.id IN (#{params[:route_id]})") + else + referential.timebands + end + end + + def referential_timebands + @referential_timebands ||= select_timebands + end + + def collection + @timebands = referential_timebands.select{ |p| p.fullname =~ /#{params[:q]}/i } + end +end diff --git a/app/inputs/search_timeband_input.rb b/app/inputs/search_timeband_input.rb new file mode 100644 index 000000000..bad262ef2 --- /dev/null +++ b/app/inputs/search_timeband_input.rb @@ -0,0 +1,44 @@ +class SearchTimebandInput < Formtastic::Inputs::SearchInput + + def search + if options[:json] + template.content_tag( :script, + ("$(document).ready(function() { + var timeband_formatter = function(item){ + return '<li>' + item.name + '</li>'; + }; + $('##{dom_id}').tokenInput('#{options[:json]}', { + zindex: 1061, + crossDomain: false, + tokenLimit: 1, + minChars: 2, + propertyToSearch: 'name', + preventDuplicates: true, + hintText: '#{options[:hint_text]}', + noResultsText: '#{options[:no_result_text]}', + searchingText: '#{options[:searching_text]}', + resultsFormatter: timeband_formatter, + tokenFormatter: timeband_formatter, + }); + });").html_safe) + end + end + + def to_html + input_wrapping do + label_html << + builder.search_field(method, input_html_options) << + search + end + end + + def input_html_options + super.merge({ + required: nil, + autofocus: nil, + class: 'token-input', + :'data-model-name' => object.class.model_name.human + }) + end + +end diff --git a/app/views/autocomplete_timebands/index.rabl b/app/views/autocomplete_timebands/index.rabl new file mode 100644 index 000000000..663b09285 --- /dev/null +++ b/app/views/autocomplete_timebands/index.rabl @@ -0,0 +1,8 @@ +collection @timebands + +node do |timeband| + { + id: timeband.id, + name: timeband.fullname + } +end diff --git a/app/views/vehicle_journey_frequencies/_journey_frequency_fields.html.erb b/app/views/vehicle_journey_frequencies/_journey_frequency_fields.html.erb index 9370fc8d3..9fe360666 100644 --- a/app/views/vehicle_journey_frequencies/_journey_frequency_fields.html.erb +++ b/app/views/vehicle_journey_frequencies/_journey_frequency_fields.html.erb @@ -1,6 +1,10 @@ <div class="nested-fields"> <ul class="ce-FrequencyFormFields"> - <%= f.input :timeband, collection: Chouette::Timeband.all.map{ |timeband| [timeband.fullname, timeband.id] }, label: false %> + <%= f.input :timeband_id, as: :search_timeband, json: referential_autocomplete_timebands_path(@referential, format: :json), + hint_text: t('search_hint'), no_result_text: t('no_result_text'), searching_text: t('searching_term'), label: false, + input_html: { :"data-pre" => Rabl::Renderer.new( 'autocomplete_timebands/index', + Chouette::Timeband.where(id: f.object.timeband_id), view_path: 'app/views', + format: :json, scope: :self ).render } %> <%= f.input :first_departure_time, label: false %> <%= f.input :last_departure_time, label: false %> <%= f.input :scheduled_headway_interval, label: false %> diff --git a/config/routes.rb b/config/routes.rb index fdcc506bf..8f41615d9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -46,6 +46,7 @@ ChouetteIhm::Application.routes.draw do resources :api_keys resources :autocomplete_stop_areas resources :autocomplete_time_tables + resources :autocomplete_timebands resources :group_of_lines do collection do get 'name_filter' |
