diff --git a/app/models/assign_to.rb b/app/models/assign_to.rb index 07af61ccf..5865e61bc 100644 --- a/app/models/assign_to.rb +++ b/app/models/assign_to.rb @@ -1,4 +1,9 @@ class AssignTo < ActiveRecord::Base belongs_to :procedure belongs_to :gestionnaire + has_one :procedure_presentation, dependent: :destroy + + def procedure_presentation_or_default + self.procedure_presentation || build_procedure_presentation + end end diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb new file mode 100644 index 000000000..e2f45b492 --- /dev/null +++ b/app/models/procedure_presentation.rb @@ -0,0 +1,3 @@ +class ProcedurePresentation < ActiveRecord::Base + belongs_to :assign_to +end diff --git a/db/migrate/20170915080151_create_procedure_presentations.rb b/db/migrate/20170915080151_create_procedure_presentations.rb new file mode 100644 index 000000000..1798e0b15 --- /dev/null +++ b/db/migrate/20170915080151_create_procedure_presentations.rb @@ -0,0 +1,10 @@ +class CreateProcedurePresentations < ActiveRecord::Migration[5.0] + def change + create_table :procedure_presentations do |t| + t.references :assign_to, index: { unique: true }, foreign_key: true + t.text :displayed_fields, array: true, default: [{ "label" => "Demandeur", "table" => "user", "column" => "email" }.to_json], null: false + t.json :sort, default: { "table" => "self", "column" => "id", "order" => "desc" }.to_json, null: false + t.json :filters, default: { "a-suivre" => [], "suivis" => [], "traites" => [], "tous" => [], "archives" => [] }.to_json, null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index e2bd74e20..f08c3a1c4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170926092716) do +ActiveRecord::Schema.define(version: 20170927092716) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -382,6 +382,14 @@ ActiveRecord::Schema.define(version: 20170926092716) do t.index ["path"], name: "index_procedure_paths_on_path", using: :btree end + create_table "procedure_presentations", force: :cascade do |t| + t.integer "assign_to_id" + t.text "displayed_fields", default: ["{\"label\":\"Demandeur\",\"table\":\"user\",\"column\":\"email\"}"], null: false, array: true + t.json "sort", default: "{\"table\":\"self\",\"column\":\"id\",\"order\":\"desc\"}", null: false + t.json "filters", default: "{\"a-suivre\":[],\"suivis\":[],\"traites\":[],\"tous\":[],\"archives\":[]}", null: false + t.index ["assign_to_id"], name: "index_procedure_presentations_on_assign_to_id", unique: true, using: :btree + end + create_table "procedures", force: :cascade do |t| t.string "libelle" t.string "description" @@ -503,6 +511,7 @@ ActiveRecord::Schema.define(version: 20170926092716) do add_foreign_key "initiated_mails", "procedures" add_foreign_key "procedure_paths", "administrateurs" add_foreign_key "procedure_paths", "procedures" + add_foreign_key "procedure_presentations", "assign_tos" add_foreign_key "received_mails", "procedures" add_foreign_key "refused_mails", "procedures" add_foreign_key "without_continuation_mails", "procedures" diff --git a/spec/models/assign_to_spec.rb b/spec/models/assign_to_spec.rb new file mode 100644 index 000000000..9420fbd64 --- /dev/null +++ b/spec/models/assign_to_spec.rb @@ -0,0 +1,16 @@ +describe AssignTo, type: :model do + describe '#procedure_presentation_or_default' do + context "without a procedure_presentation" do + let!(:assign_to) { AssignTo.create } + + it { expect(assign_to.procedure_presentation_or_default.persisted?).to be_falsey } + end + + context "with a procedure_presentation" do + let!(:assign_to) { AssignTo.create } + let!(:procedure_presentation) { ProcedurePresentation.create(assign_to: assign_to) } + + it { expect(assign_to.procedure_presentation_or_default).to eq(procedure_presentation) } + end + end +end