aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXinhui2017-05-19 12:10:09 +0200
committerXinhui2017-05-19 16:47:53 +0200
commit4c1a2a33fde5b29f3b4c4024281ad80277a967d5 (patch)
tree5a65ea426a2e8fdb4848faebc2e29ef36b484c79
parent6566451b49ba70b5bf47174de80c4bc6f49caa0d (diff)
downloadchouette-core-4c1a2a33fde5b29f3b4c4024281ad80277a967d5.tar.bz2
Refactoring TimeTableCombination with calendar support
-rw-r--r--app/assets/javascripts/select2.coffee58
-rw-r--r--app/models/time_table_combination.rb14
-rw-r--r--app/views/autocomplete_time_tables/index.rabl1
-rw-r--r--app/views/time_table_combinations/_form.html.slim7
-rw-r--r--spec/models/time_table_combination_spec.rb6
5 files changed, 61 insertions, 25 deletions
diff --git a/app/assets/javascripts/select2.coffee b/app/assets/javascripts/select2.coffee
index e8d9c313e..510e6fe9b 100644
--- a/app/assets/javascripts/select2.coffee
+++ b/app/assets/javascripts/select2.coffee
@@ -1,20 +1,52 @@
+bind_select2 = (el, cfg = {}) ->
+ target = $(el)
+ default_cfg =
+ theme: 'bootstrap'
+ language: 'fr'
+ placeholder: target.data('select2ed-placeholder')
+ allowClear: true
+
+ target.select2 $.extend({}, default_cfg, cfg)
+
+bind_select2_ajax = (el, cfg = {}) ->
+ target = $(el)
+ cfg =
+ ajax:
+ data: (params) ->
+ q:
+ "#{target.data('term')}": params.term
+ url: target.data('url'),
+ dataType: 'json',
+ delay: 125,
+ processResults: (data, params) -> results: data
+ minimumInputLength: 3
+ templateResult: eval(target.data('formater'))
+ templateSelection: (item) ->
+ item.text
+
+ bind_select2(el, cfg)
+
+select2_time_table = (item) ->
+ return item.text if item.loading
+ wrap = $('<div>', "class":'select2-result clearfix')
+ wrap.html(["<h5>Time table : #{item.comment}</h5>"].join("\n"))
+
+select2_calendar = (item) ->
+ return item.text if item.loading
+ wrap = $('<div>', "class":'select2-result clearfix')
+ wrap.html(["<h5>Calendar : #{item.name}</h5>"].join("\n"))
+
+
@select_2 = ->
$("[data-select2ed='true']").each ->
- target = $(this)
- target.select2
- theme: 'bootstrap'
- language: 'fr'
- placeholder: target.data('select2ed-placeholder')
- allowClear: true
+ bind_select2(this)
+
+ $("[data-select2-ajax='true']").each ->
+ bind_select2_ajax(this)
$('select.form-control.tags').each ->
- target = $(this)
- target.select2
- theme: 'bootstrap'
- language: 'fr'
- placeholder: target.data('select2ed-placeholder')
- allowClear: true
- tags: true
+ bind_select2(this, {tags: true})
+
$(document).on 'turbolinks:load', select_2
diff --git a/app/models/time_table_combination.rb b/app/models/time_table_combination.rb
index f9b63340a..d7e16665c 100644
--- a/app/models/time_table_combination.rb
+++ b/app/models/time_table_combination.rb
@@ -3,15 +3,20 @@ class TimeTableCombination
include ActiveModel::Conversion
extend ActiveModel::Naming
- attr_accessor :source_id, :combined_type, :target_id, :operation
+ attr_accessor :source_id, :combined_type, :operation, :time_table_id, :calendar_id
+
+ validates_presence_of :source_id, :combined_type, :operation
+
+ validates :time_table_id, presence: true, if: "calendar_id.blank?"
+ validates :calendar_id, presence: true, if: "time_table_id.blank?"
- validates_presence_of :source_id, :combined_type, :operation, :target_id
validates_inclusion_of :operation, :in => %w(union intersection disjunction), :allow_nil => true
validates_inclusion_of :combined_type, :in => %w(time_table calendar)
def clean
self.source_id = nil
- self.target_id = nil
+ self.time_table_id = nil
+ self.calendar = nil
self.combined_type = nil
self.operation = nil
self.errors.clear
@@ -32,8 +37,9 @@ class TimeTableCombination
end
def target
+ id = self.send("#{combined_type}_id")
klass = combined_type == 'calendar' ? Calendar : Chouette::TimeTable
- target = klass.find target_id
+ target = klass.find id
target = target.convert_to_time_table unless target.is_a? Chouette::TimeTable
target
end
diff --git a/app/views/autocomplete_time_tables/index.rabl b/app/views/autocomplete_time_tables/index.rabl
index 8ec7b314e..bd478301d 100644
--- a/app/views/autocomplete_time_tables/index.rabl
+++ b/app/views/autocomplete_time_tables/index.rabl
@@ -5,6 +5,7 @@ node do |time_table|
:time_table_bounding => time_table.presenter.time_table_bounding,
:composition_info => time_table.presenter.composition_info,
:tags => time_table.tags.join(','),
+ :text => time_table.comment,
:day_types => %w(monday tuesday wednesday thursday friday saturday sunday).select{ |d| time_table.send(d) }.map{ |d| time_table.human_attribute_name(d).first(2)}.join('')}
end
diff --git a/app/views/time_table_combinations/_form.html.slim b/app/views/time_table_combinations/_form.html.slim
index 47a5cee73..8f4924bd3 100644
--- a/app/views/time_table_combinations/_form.html.slim
+++ b/app/views/time_table_combinations/_form.html.slim
@@ -8,11 +8,8 @@ h1 = @time_table.comment
= form.input :operation, as: :select, collection: TimeTableCombination.operations
- = form.input :target_id, as: :select, 'data-select2ed': 'true', collection: Chouette::TimeTable.all.map{|t| [t.comment, t.id]}
+ = form.input :time_table_id, as: :select, input_html: { data: { select2_ajax: 'true', term: 'comment_cont', url: referential_autocomplete_time_tables_path(@referential, format: :json), formater: 'select2_time_table'}}
- = form.input :target_id, as: :select, 'data-select2ed': 'true', collection: Calendar.all.map{|t| [t.name, t.id]}, disabled: true
+ = form.input :calendar_id, as: :select, input_html: { data: { select2_ajax: 'true', term: 'name_cont', url: referential_autocomplete_time_tables_path(@referential, format: :json), formater: 'select2_calendar'}}
= form.button :submit, t('actions.submit')
- / = form.input :combined_id, :label => t('.time_tables'), as: :search_time_table, :json => referential_autocomplete_time_tables_path(@referential, :format => :json), hint_text: t('search_hint'), no_result_text: t('no_result_text'), :searching_text => t('searching_term'), :tokenLimit => 1
-
-
diff --git a/spec/models/time_table_combination_spec.rb b/spec/models/time_table_combination_spec.rb
index 60dcdb5b0..0a8b3296a 100644
--- a/spec/models/time_table_combination_spec.rb
+++ b/spec/models/time_table_combination_spec.rb
@@ -51,7 +51,7 @@ describe TimeTableCombination, :type => :model do
subject.operation = 'union'
subject.source_id = source.id
- subject.target_id = combined.id
+ subject.time_table_id = combined.id
subject.combined_type = 'time_table'
subject.combine
@@ -79,7 +79,7 @@ describe TimeTableCombination, :type => :model do
subject.operation = 'intersection'
subject.source_id = source.id
- subject.target_id = combined.id
+ subject.time_table_id = combined.id
subject.combined_type = 'time_table'
subject.combine
@@ -111,7 +111,7 @@ describe TimeTableCombination, :type => :model do
subject.operation = 'disjunction'
subject.source_id = source.id
- subject.target_id = combined.id
+ subject.time_table_id = combined.id
subject.combined_type = 'time_table'
subject.combine