diff options
| author | Xinhui | 2017-05-02 15:44:25 +0200 |
|---|---|---|
| committer | Robert | 2017-05-02 16:44:25 +0200 |
| commit | b6a0ea532f9c75da693ba1a012327807976a3e27 (patch) | |
| tree | 863a49f6b1a83249cff63f88781789c2d4ec0b9c /app | |
| parent | b8b4a59fdb1b6550c86d32fb7445ff83ab3e6d80 (diff) | |
| download | chouette-core-b6a0ea532f9c75da693ba1a012327807976a3e27.tar.bz2 | |
Duplicate TimeTable
Refs #3188
Diffstat (limited to 'app')
| -rw-r--r-- | app/controllers/time_tables_controller.rb | 16 | ||||
| -rw-r--r-- | app/models/chouette/time_table.rb | 2 | ||||
| -rw-r--r-- | app/views/time_tables/_form.html.slim | 6 |
3 files changed, 21 insertions, 3 deletions
diff --git a/app/controllers/time_tables_controller.rb b/app/controllers/time_tables_controller.rb index 3b8e390b6..8436dc020 100644 --- a/app/controllers/time_tables_controller.rb +++ b/app/controllers/time_tables_controller.rb @@ -37,7 +37,10 @@ class TimeTablesController < ChouetteController calendar = current_organisation.calendars.find_by_id(tt_params[:calendar_id]) tt_params[:calendar_id] = nil if tt_params.has_key?(:dates_attributes) || tt_params.has_key?(:periods_attributes) end - @time_table = Chouette::TimeTable.new(tt_params) + + created_from = duplicate_source + @time_table = created_from ? created_from.duplicate : Chouette::TimeTable.new(tt_params) + if calendar calendar.dates.each_with_index do |date, i| @time_table.dates << Chouette::TimeTableDate.new(date: date, position: i) @@ -48,7 +51,10 @@ class TimeTablesController < ChouetteController end create! do |success, failure| - success.html { redirect_to edit_referential_time_table_path(@referential, @time_table) } + success.html do + path = @time_table.created_from ? 'referential_time_table_path' : 'edit_referential_time_table_path' + redirect_to send(path, @referential, @time_table) + end failure.html { render :new } end end @@ -158,6 +164,11 @@ class TimeTablesController < ChouetteController %w[asc desc].include?(params[:direction]) ? params[:direction] : 'asc' end + def duplicate_source + from_id = time_table_params['created_from_id'] + Chouette::TimeTable.find(from_id) if from_id + end + def time_table_params params.require(:time_table).permit( :objectid, @@ -175,6 +186,7 @@ class TimeTablesController < ChouetteController :sunday, :start_date, :end_date, + :created_from_id, { :dates_attributes => [:date, :in_out, :id, :_destroy] }, { :periods_attributes => [:period_start, :period_end, :_destroy, :id] }, {tag_list: []}, diff --git a/app/models/chouette/time_table.rb b/app/models/chouette/time_table.rb index 11e7293f1..798fa81b4 100644 --- a/app/models/chouette/time_table.rb +++ b/app/models/chouette/time_table.rb @@ -18,6 +18,7 @@ class Chouette::TimeTable < Chouette::TridentActiveRecord has_many :periods, -> {order(:period_start)}, inverse_of: :time_table, :validate => :true, :class_name => "Chouette::TimeTablePeriod", :dependent => :destroy belongs_to :calendar + belongs_to :created_from, class_name: 'Chouette::TimeTable' after_save :save_shortcuts @@ -525,6 +526,7 @@ class Chouette::TimeTable < Chouette::TridentActiveRecord def duplicate tt = self.deep_clone :include => [:periods, :dates], :except => :object_version tt.uniq_objectid + tt.created_from = self tt.comment = I18n.t("activerecord.copy", :name => self.comment) tt end diff --git a/app/views/time_tables/_form.html.slim b/app/views/time_tables/_form.html.slim index 8152e7f94..196682823 100644 --- a/app/views/time_tables/_form.html.slim +++ b/app/views/time_tables/_form.html.slim @@ -4,7 +4,11 @@ .col-lg-12 = form.input :comment, :input_html => { :title => t("formtastic.titles#{format_restriction_for_locales(@referential)}.time_table.comment")} - - if @time_table.new_record? + - if @time_table.new_record? && !@time_table.created_from = form.input :calendar, as: :select, collection: current_organisation.calendars + - if @time_table.created_from + = form.input :created_from, disabled: true, input_html: { value: @time_table.created_from.comment } + .hidden = form.input :created_from_id, as: :hidden + = form.button :submit, t('actions.submit'), class: 'btn btn-default formSubmitr', form: 'timetable_form' |
