diff --git a/app/controllers/new_gestionnaire/procedures_controller.rb b/app/controllers/new_gestionnaire/procedures_controller.rb index 330df66f1..8d7ea32c5 100644 --- a/app/controllers/new_gestionnaire/procedures_controller.rb +++ b/app/controllers/new_gestionnaire/procedures_controller.rb @@ -102,11 +102,9 @@ module NewGestionnaire fields = values.map do |value| table, column = value.split("/") - c = procedure.fields.find do |field| + procedure.fields.find do |field| field['table'] == table && field['column'] == column end - - c.to_json end procedure_presentation.update(displayed_fields: fields) @@ -134,7 +132,7 @@ module NewGestionnaire 'table' => table, 'column' => column, 'order' => order - }.to_json + } procedure_presentation.update(sort: sort) @@ -154,7 +152,7 @@ module NewGestionnaire 'value' => params[:value] } - procedure_presentation.update(filters: filters.to_json) + procedure_presentation.update(filters: filters) end redirect_back(fallback_location: gestionnaire_procedure_url(procedure)) @@ -168,7 +166,7 @@ module NewGestionnaire filters[statut] = filters[statut] - [filter_to_remove] - procedure_presentation.update(filters: filters.to_json) + procedure_presentation.update(filters: filters) redirect_back(fallback_location: gestionnaire_procedure_url(procedure)) end diff --git a/app/models/procedure.rb b/app/models/procedure.rb index f83227269..2f2c10855 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -307,7 +307,7 @@ class Procedure < ApplicationRecord 'table' => 'self', 'column' => 'id', 'order' => 'desc' - }.to_json + } end def whitelist! diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index ce0fc8b9d..b3a54252b 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -1,17 +1,3 @@ class ProcedurePresentation < ApplicationRecord belongs_to :assign_to - - def displayed_fields - read_attribute(:displayed_fields).map do |field| - field = JSON.parse(field) - end - end - - def sort - JSON.parse(read_attribute(:sort)) - end - - def filters - JSON.parse(read_attribute(:filters)) - end end diff --git a/db/migrate/20180919084403_change_procedure_presentation_fields_to_jsonb.rb b/db/migrate/20180919084403_change_procedure_presentation_fields_to_jsonb.rb new file mode 100644 index 000000000..df00c18f6 --- /dev/null +++ b/db/migrate/20180919084403_change_procedure_presentation_fields_to_jsonb.rb @@ -0,0 +1,10 @@ +class ChangeProcedurePresentationFieldsToJsonb < ActiveRecord::Migration[5.2] + def change + change_table(:procedure_presentations) do |t| + t.rename :displayed_fields, :old_displayed_fields + t.column :displayed_fields, :jsonb, null: false, default: [{ label: "Demandeur", table: "user", column: "email" }] + t.change :sort, :jsonb, default: { table: "notifications", column: "notifications", order: "desc" } + t.change :filters, :jsonb, default: { "a-suivre": [], suivis: [], traites: [], tous: [], archives: [] } + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 659191725..c4326d89f 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: 2018_09_13_160415) do +ActiveRecord::Schema.define(version: 2018_09_19_084403) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -444,11 +444,12 @@ ActiveRecord::Schema.define(version: 2018_09_13_160415) do create_table "procedure_presentations", id: :serial, 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\":\"notifications\",\"column\":\"notifications\",\"order\":\"desc\"}", null: false - t.json "filters", default: "{\"a-suivre\":[],\"suivis\":[],\"traites\":[],\"tous\":[],\"archives\":[]}", null: false + t.text "old_displayed_fields", default: ["{\"label\":\"Demandeur\",\"table\":\"user\",\"column\":\"email\"}"], null: false, array: true + t.jsonb "sort", default: {"order"=>"desc", "table"=>"notifications", "column"=>"notifications"}, null: false + t.jsonb "filters", default: {"tous"=>[], "suivis"=>[], "traites"=>[], "a-suivre"=>[], "archives"=>[]}, null: false t.datetime "created_at" t.datetime "updated_at" + t.jsonb "displayed_fields", default: [{"label"=>"Demandeur", "table"=>"user", "column"=>"email"}], null: false t.index ["assign_to_id"], name: "index_procedure_presentations_on_assign_to_id", unique: true end diff --git a/lib/tasks/2018_09_20_procedure_presentation_jsonb.rake b/lib/tasks/2018_09_20_procedure_presentation_jsonb.rake new file mode 100644 index 000000000..b2eeecfcd --- /dev/null +++ b/lib/tasks/2018_09_20_procedure_presentation_jsonb.rake @@ -0,0 +1,11 @@ +namespace :'2018_09_20_procedure_presentation_jsonb' do + task run: :environment do + ProcedurePresentation.update_all( + <<~SQL + displayed_fields = ('[' || ARRAY_TO_STRING(old_displayed_fields, ',') || ']')::JSONB, + sort = (sort #>> '{}')::jsonb, + filters = (filters #>> '{}')::jsonb + SQL + ) + end +end diff --git a/spec/models/procedure_presentation_spec.rb b/spec/models/procedure_presentation_spec.rb index 4d018ec23..cdf7099b6 100644 --- a/spec/models/procedure_presentation_spec.rb +++ b/spec/models/procedure_presentation_spec.rb @@ -4,11 +4,11 @@ describe ProcedurePresentation do let (:procedure_presentation_id) { ProcedurePresentation.create( displayed_fields: [ - { "label" => "test1", "table" => "user" }.to_json, - { "label" => "test2", "table" => "champs" }.to_json + { "label" => "test1", "table" => "user" }, + { "label" => "test2", "table" => "champs" } ], - sort: { "table" => "user","column" => "email","order" => "asc" }.to_json, - filters: { "a-suivre" => [], "suivis" => [{ "label" => "label1", "table" => "table1", "column" => "column1" }] }.to_json + sort: { "table" => "user","column" => "email","order" => "asc" }, + filters: { "a-suivre" => [], "suivis" => [{ "label" => "label1", "table" => "table1", "column" => "column1" }] } ).id } let (:procedure_presentation) { ProcedurePresentation.find(procedure_presentation_id) } diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 43ae0c653..d41f2413a 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -690,7 +690,7 @@ describe Procedure do end describe ".default_sort" do - it { expect(Procedure.default_sort).to eq("{\"table\":\"self\",\"column\":\"id\",\"order\":\"desc\"}") } + it { expect(Procedure.default_sort).to eq({ "table" => "self", "column" => "id", "order" => "desc" }) } end describe "#export_filename" do