diff options
| author | Xinhui | 2017-05-18 15:25:17 +0200 |
|---|---|---|
| committer | Xinhui | 2017-05-18 15:27:04 +0200 |
| commit | 8ef916234f4968b542da50ba5b4d1304dacf92e9 (patch) | |
| tree | a061e1f18cdbaa137dad8dee689f99034028e4d4 | |
| parent | 7210f067b298d816dfc891d87c4407a291c1a706 (diff) | |
| download | chouette-core-8ef916234f4968b542da50ba5b4d1304dacf92e9.tar.bz2 | |
Wip TimeTableCombinations#new form & refactoring
Refs #3406
| -rw-r--r-- | app/controllers/time_table_combinations_controller.rb | 26 | ||||
| -rw-r--r-- | app/models/chouette/time_table.rb | 1 | ||||
| -rw-r--r-- | app/models/time_table_combination.rb | 33 | ||||
| -rw-r--r-- | app/views/time_table_combinations/_form.html.slim | 16 |
4 files changed, 49 insertions, 27 deletions
diff --git a/app/controllers/time_table_combinations_controller.rb b/app/controllers/time_table_combinations_controller.rb index db3025921..bbb262247 100644 --- a/app/controllers/time_table_combinations_controller.rb +++ b/app/controllers/time_table_combinations_controller.rb @@ -1,25 +1,25 @@ class TimeTableCombinationsController < ChouetteController - respond_to :js, :only => [:new,:create] - belongs_to :referential do belongs_to :time_table, :parent_class => Chouette::TimeTable end def new - @time_table_combination = TimeTableCombination.new(:source_id => parent.id) + @combination = TimeTableCombination.new(source_id: parent.id) end def create - @time_table_combination = TimeTableCombination.new( params[:time_table_combination].merge( :source_id => parent.id)) - if @time_table_combination.valid? - begin - @time_table = @time_table_combination.combine - flash[:notice] = t('time_table_combinations.success') - rescue => e - flash[:error] = t('time_table_combinations.failure') - render :new - end - else + @combination = TimeTableCombination.new(params[:time_table_combination].merge(source_id: parent.id)) + @combination.valid? ? perform_combination : render(:new) + end + + def perform_combination + begin + @time_table = @combination.combine + flash[:notice] = t('time_table_combinations.success') + redirect_to referential_time_table_path(referential, @time_table) + rescue => e + flash[:notice] = e.message + flash[:error] = t('time_table_combinations.failure') render :new end end diff --git a/app/models/chouette/time_table.rb b/app/models/chouette/time_table.rb index 10d7039a0..0ddfa14bf 100644 --- a/app/models/chouette/time_table.rb +++ b/app/models/chouette/time_table.rb @@ -518,6 +518,7 @@ class Chouette::TimeTable < Chouette::TridentActiveRecord self.dates.to_a.sort! { |a,b| a.date <=> b.date} self.save! end + self.convert_continuous_dates_to_periods end # remove dates form tt which aren't in another_tt diff --git a/app/models/time_table_combination.rb b/app/models/time_table_combination.rb index 519c02f2b..6cbdf942f 100644 --- a/app/models/time_table_combination.rb +++ b/app/models/time_table_combination.rb @@ -3,15 +3,17 @@ class TimeTableCombination include ActiveModel::Conversion extend ActiveModel::Naming - attr_accessor :source_id, :combined_id, :operation + attr_accessor :source_id, :combined_type, :target_id, :operation - validates_presence_of :source_id, :combined_id, :operation - validates_inclusion_of :operation, :in => %w( union intersection disjunction), :allow_nil => true + 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.combined_id = nil - self.operation = nil + self.source_id = nil + self.target_id = nil + self.time_table_id = nil + self.operation = nil self.errors.clear end @@ -29,14 +31,23 @@ class TimeTableCombination false end + def target + klass = combined_type == 'calendar' ? Calendar : Chouette::TimeTable + target = klass.find target_id + target = target.convert_to_time_table unless target.is_a? Chouette::TimeTable + target + end + def combine - source = Chouette::TimeTable.find( source_id) - combined = Chouette::TimeTable.find( combined_id) - if operation == "union" + source = Chouette::TimeTable.find source_id + combined = self.target + + case operation + when 'union' source.merge! combined - elsif operation == "intersection" + when 'intersection' source.intersect! combined - elsif operation == "disjunction" + when 'disjunction' source.disjoin! combined else raise "unknown operation" diff --git a/app/views/time_table_combinations/_form.html.slim b/app/views/time_table_combinations/_form.html.slim index e61ceee44..47a5cee73 100644 --- a/app/views/time_table_combinations/_form.html.slim +++ b/app/views/time_table_combinations/_form.html.slim @@ -1,8 +1,18 @@ -= simple_form_for [@referential, @time_table, @time_table_combination], html: {class: 'form-horizontal'}, wrapper: :horizontal_form do |form| + +h1 = @time_table.comment + += simple_form_for [@referential, @time_table, @combination], html: {class: 'form-horizontal'}, wrapper: :horizontal_form do |form| .row .col-lg-12 - = form.input :operation, as: :select, :collection => Hash[TimeTableCombination.operations.map {|b| [t( b, :scope => "time_table_combinations.operations"),b]}] + = form.input :combined_type, as: :select, collection: ['time_table', 'calendar'] + + = 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 :target_id, as: :select, 'data-select2ed': 'true', collection: Calendar.all.map{|t| [t.name, t.id]}, disabled: true + + = 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 - = form.button :submit, t('actions.submit'), class: 'btn btn-default formSubmitr' + |
