Merge pull request #2640 from betagouv/frederic/fix_2622_instructeur_filter_json

Utilisation de colonnes JSONB par les filtres instructeur
This commit is contained in:
Frederic Merizen 2018-09-24 19:55:44 +02:00 committed by GitHub
commit f4b95d3f40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 36 additions and 30 deletions

View file

@ -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

View file

@ -307,7 +307,7 @@ class Procedure < ApplicationRecord
'table' => 'self',
'column' => 'id',
'order' => 'desc'
}.to_json
}
end
def whitelist!

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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) }

View file

@ -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