Merge pull request #1533 from betagouv/less_sql_query_in_user_ui

Less sql query in user ui
This commit is contained in:
LeSim 2018-03-27 16:53:47 +02:00 committed by GitHub
commit 0fda88512c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 7 deletions

View file

@ -95,11 +95,11 @@ module NewUser
end
def dossier_with_champs
@dossier_with_champs ||= current_user.dossiers.includes(champs: :type_de_champ).find(params[:id])
@dossier_with_champs ||= current_user.dossiers.with_ordered_champs.find(params[:id])
end
def ensure_ownership!
if dossier.user != current_user
if dossier.user_id != current_user.id
flash[:alert] = "Vous n'avez pas accès à ce dossier"
redirect_to root_path
end

View file

@ -60,6 +60,7 @@ class Dossier < ApplicationRecord
scope :en_cours, -> { not_archived.state_en_construction_ou_instruction }
scope :without_followers, -> { left_outer_joins(:follows).where(follows: { id: nil }) }
scope :followed_by, -> (gestionnaire) { joins(:follows).where(follows: { gestionnaire: gestionnaire }) }
scope :with_ordered_champs, -> { includes(champs: :type_de_champ).order('types_de_champ.order_place') }
accepts_nested_attributes_for :individual
@ -108,10 +109,6 @@ class Dossier < ApplicationRecord
champs.joins(', types_de_champ').where("champs.type_de_champ_id = types_de_champ.id AND types_de_champ.procedure_id = #{procedure.id}").order('order_place')
end
def ordered_champs_v2
champs.includes(:type_de_champ).order('types_de_champ.order_place')
end
def ordered_champs_private
# TODO: use the line below when the procedure preview does not leak champ with dossier_id == 0
# champs_private.includes(:type_de_champ).order('types_de_champ.order_place')

View file

@ -7,7 +7,7 @@
%p.thanks Les champs avec une asterisque (*) sont obligatoires.
= form_for @dossier, html: { class: 'form', multipart: true } do |f|
= f.fields_for :champs, @dossier.ordered_champs_v2 do |champ_form|
= f.fields_for :champs, @dossier.champs do |champ_form|
- champ = champ_form.object
= render partial: "shared/dossiers/editable_champs/editable_champ",
locals: { champ: champ, form: champ_form }

View file

@ -10,6 +10,21 @@ describe Dossier do
it { expect(Dossier.without_followers.to_a).to eq([dossier2]) }
end
describe 'with_ordered_champs' do
let(:procedure) { create(:procedure) }
let(:dossier) { Dossier.create(user: create(:user), procedure: procedure) }
before do
create(:type_de_champ, libelle: 'l1', order_place: 1, procedure: procedure)
create(:type_de_champ, libelle: 'l3', order_place: 3, procedure: procedure)
create(:type_de_champ, libelle: 'l2', order_place: 2, procedure: procedure)
end
it do
expect(Dossier.with_ordered_champs.find(dossier.id).champs.map(&:libelle)).to match(%w(l1 l2 l3))
end
end
describe 'methods' do
let(:dossier) { create(:dossier, :with_entreprise, user: user) }