aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXinhui2017-05-02 15:44:25 +0200
committerRobert2017-05-02 16:44:25 +0200
commitb6a0ea532f9c75da693ba1a012327807976a3e27 (patch)
tree863a49f6b1a83249cff63f88781789c2d4ec0b9c
parentb8b4a59fdb1b6550c86d32fb7445ff83ab3e6d80 (diff)
downloadchouette-core-b6a0ea532f9c75da693ba1a012327807976a3e27.tar.bz2
Duplicate TimeTable
Refs #3188
-rw-r--r--app/controllers/time_tables_controller.rb16
-rw-r--r--app/models/chouette/time_table.rb2
-rw-r--r--app/views/time_tables/_form.html.slim6
-rw-r--r--db/migrate/20170502130327_add_created_from_to_time_tables.rb5
-rw-r--r--db/schema.rb14
5 files changed, 34 insertions, 9 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'
diff --git a/db/migrate/20170502130327_add_created_from_to_time_tables.rb b/db/migrate/20170502130327_add_created_from_to_time_tables.rb
new file mode 100644
index 000000000..6c0815eaf
--- /dev/null
+++ b/db/migrate/20170502130327_add_created_from_to_time_tables.rb
@@ -0,0 +1,5 @@
+class AddCreatedFromToTimeTables < ActiveRecord::Migration
+ def change
+ add_reference :time_tables, :created_from, index: true
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 3214c1e78..17df3c34f 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20170428133742) do
+ActiveRecord::Schema.define(version: 20170502130327) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -696,21 +696,23 @@ ActiveRecord::Schema.define(version: 20170428133742) do
add_index "time_table_periods", ["time_table_id"], name: "index_time_table_periods_on_time_table_id", using: :btree
create_table "time_tables", id: :bigserial, force: :cascade do |t|
- t.string "objectid", null: false
- t.integer "object_version", limit: 8, default: 1
+ t.string "objectid", null: false
+ t.integer "object_version", limit: 8, default: 1
t.string "creator_id"
t.string "version"
t.string "comment"
- t.integer "int_day_types", default: 0
+ t.integer "int_day_types", default: 0
t.date "start_date"
t.date "end_date"
- t.integer "calendar_id", limit: 8
+ t.integer "calendar_id", limit: 8
t.datetime "created_at"
t.datetime "updated_at"
- t.string "color", limit: 255
+ t.string "color", limit: 255
+ t.integer "created_from_id"
end
add_index "time_tables", ["calendar_id"], name: "index_time_tables_on_calendar_id", using: :btree
+ add_index "time_tables", ["created_from_id"], name: "index_time_tables_on_created_from_id", using: :btree
add_index "time_tables", ["objectid"], name: "time_tables_objectid_key", unique: true, using: :btree
create_table "time_tables_vehicle_journeys", id: false, force: :cascade do |t|