wrap resultset tuples in ProcedureDetail
This commit is contained in:
parent
5de0c8722e
commit
f1595d1f50
4 changed files with 34 additions and 49 deletions
|
@ -342,7 +342,7 @@ module Administrateurs
|
|||
|
||||
def all
|
||||
@filter = ProceduresFilter.new(current_administrateur, params)
|
||||
all_procedures = filter_procedures(@filter)
|
||||
all_procedures = filter_procedures(@filter).map { |p| ProcedureDetail.new(p) }
|
||||
|
||||
respond_to do |format|
|
||||
format.html do
|
||||
|
@ -350,11 +350,8 @@ module Administrateurs
|
|||
@procedures = all_procedures.page(params[:page]).per(25)
|
||||
end
|
||||
format.xlsx do
|
||||
render xlsx: SpreadsheetArchitect.to_xlsx(
|
||||
headers: export_procedures_headers(all_procedures),
|
||||
data: export_procedures_values(all_procedures)
|
||||
),
|
||||
filename: "demarches-#{@filter}"
|
||||
render xlsx: ProcedureDetail.to_xlsx(instances: all_procedures),
|
||||
filename: "demarches-#{@filter}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -369,21 +366,6 @@ module Administrateurs
|
|||
|
||||
private
|
||||
|
||||
def export_procedures_headers(all_procedures)
|
||||
all_procedures.to_a.first.keys.map do |key|
|
||||
I18n.t(key, scope: 'activerecord.attributes.procedure_export')
|
||||
end
|
||||
end
|
||||
|
||||
def export_procedures_values(all_procedures)
|
||||
aasm_state_index = all_procedures.to_a.first.keys.index("aasm_state")
|
||||
all_procedures.to_a.map(&:values).each do |procedure|
|
||||
procedure.tap do |p|
|
||||
p[aasm_state_index] = I18n.t(p[aasm_state_index], scope: 'activerecord.attributes.procedure.aasm_state')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def filter_procedures(filter)
|
||||
procedures_result = Procedure.select(:id).joins(:procedures_zones).distinct.publiees_ou_closes
|
||||
procedures_result = procedures_result.where(procedures_zones: { zone_id: filter.zone_ids }) if filter.zone_ids.present?
|
||||
|
|
9
app/models/procedure_detail.rb
Normal file
9
app/models/procedure_detail.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
class ProcedureDetail < OpenStruct
|
||||
include SpreadsheetArchitect
|
||||
|
||||
def spreadsheet_columns
|
||||
[:id, :libelle, :published_at, :aasm_state, :admin_count].map do |attribute|
|
||||
[I18n.t(attribute, scope: 'activerecord.attributes.procedure_export'), attribute]
|
||||
end
|
||||
end
|
||||
end
|
|
@ -46,9 +46,9 @@
|
|||
%td
|
||||
= button_to detail_admin_procedure_path(procedure["id"]), params: { show_detail: true}, method: :get, title: 'Afficher details', class: "fr-icon-add-line fr-icon--sm fr-mr-1w fr-mb-1w fr-text-action-high--blue-france fr-btn fr-btn--tertiary-no-outline" do
|
||||
Afficher details procedure
|
||||
%td= procedure["libelle"]
|
||||
%td= procedure["id"]
|
||||
%td= procedure["admin_count"]
|
||||
%td= t procedure["aasm_state"], scope: 'activerecord.values.procedure.aasm_state'
|
||||
%td= l(procedure["published_at"], format: :message_date_without_time)
|
||||
%td= procedure.libelle
|
||||
%td= procedure.id
|
||||
%td= procedure.admin_count
|
||||
%td= t procedure.aasm_state, scope: 'activerecord.values.procedure.aasm_state'
|
||||
%td= l(procedure.published_at, format: :message_date_without_time)
|
||||
.fr-mt-2w= paginate @procedures, views_prefix: 'administrateurs'
|
||||
|
|
|
@ -99,21 +99,21 @@ describe Administrateurs::ProceduresController, type: :controller do
|
|||
subject { get :all, format: :xlsx }
|
||||
|
||||
it 'exports result in xlsx' do
|
||||
allow(SpreadsheetArchitect).to receive(:to_xlsx)
|
||||
allow(ProcedureDetail).to receive(:to_xlsx)
|
||||
subject
|
||||
expect(SpreadsheetArchitect).to have_received(:to_xlsx)
|
||||
expect(ProcedureDetail).to have_received(:to_xlsx)
|
||||
end
|
||||
end
|
||||
|
||||
it 'display published or closed procedures' do
|
||||
subject
|
||||
expect(values_for_field(assigns(:procedures), "id")).to include(published_procedure.id)
|
||||
expect(values_for_field(assigns(:procedures), "id")).to include(closed_procedure.id)
|
||||
expect(assigns(:procedures).any? { |p| p.id == published_procedure.id }).to be_truthy
|
||||
expect(assigns(:procedures).any? { |p| p.id == closed_procedure.id }).to be_truthy
|
||||
end
|
||||
|
||||
it 'doesn’t display draft procedures' do
|
||||
subject
|
||||
expect(values_for_field(assigns(:procedures), "id")).not_to include(draft_procedure.id)
|
||||
expect(assigns(:procedures).any? { |p| p.id == draft_procedure.id }).to be_falsey
|
||||
end
|
||||
|
||||
context "for specific zones" do
|
||||
|
@ -126,8 +126,8 @@ describe Administrateurs::ProceduresController, type: :controller do
|
|||
|
||||
it 'display only procedures for specified zones' do
|
||||
subject
|
||||
expect(values_for_field(assigns(:procedures), "id")).to include(procedure2.id)
|
||||
expect(values_for_field(assigns(:procedures), "id")).not_to include(procedure1.id)
|
||||
expect(assigns(:procedures).any? { |p| p.id == procedure2.id }).to be_truthy
|
||||
expect(assigns(:procedures).any? { |p| p.id == procedure1.id }).to be_falsey
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -137,14 +137,14 @@ describe Administrateurs::ProceduresController, type: :controller do
|
|||
|
||||
it 'display only published procedures' do
|
||||
get :all, params: { statuses: ['publiee'] }
|
||||
expect(values_for_field(assigns(:procedures), "id")).to include(procedure1.id)
|
||||
expect(values_for_field(assigns(:procedures), "id")).not_to include(procedure2.id)
|
||||
expect(assigns(:procedures).any? { |p| p.id == procedure1.id }).to be_truthy
|
||||
expect(assigns(:procedures).any? { |p| p.id == procedure2.id }).to be_falsey
|
||||
end
|
||||
|
||||
it 'display only closed procedures' do
|
||||
get :all, params: { statuses: ['close'] }
|
||||
expect(values_for_field(assigns(:procedures), "id")).to include(procedure2.id)
|
||||
expect(values_for_field(assigns(:procedures), "id")).not_to include(procedure1.id)
|
||||
expect(assigns(:procedures).any? { |p| p.id == procedure2.id }).to be_truthy
|
||||
expect(assigns(:procedures).any? { |p| p.id == procedure1.id }).to be_falsey
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -156,9 +156,9 @@ describe Administrateurs::ProceduresController, type: :controller do
|
|||
|
||||
it 'display only procedures published after specific date' do
|
||||
get :all, params: { from_publication_date: after }
|
||||
expect(values_for_field(assigns(:procedures), "id")).to include(procedure1.id)
|
||||
expect(values_for_field(assigns(:procedures), "id")).to include(procedure2.id)
|
||||
expect(values_for_field(assigns(:procedures), "id")).not_to include(procedure3.id)
|
||||
expect(assigns(:procedures).any? { |p| p.id == procedure1.id }).to be_truthy
|
||||
expect(assigns(:procedures).any? { |p| p.id == procedure2.id }).to be_truthy
|
||||
expect(assigns(:procedures).any? { |p| p.id == procedure3.id }).to be_falsey
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -169,9 +169,9 @@ describe Administrateurs::ProceduresController, type: :controller do
|
|||
|
||||
it 'returns procedures with specific terms in libelle' do
|
||||
get :all, params: { libelle: 'entrepreneur' }
|
||||
expect(values_for_field(assigns(:procedures), "id")).to include(procedure2.id)
|
||||
expect(values_for_field(assigns(:procedures), "id")).to include(procedure3.id)
|
||||
expect(values_for_field(assigns(:procedures), "id")).not_to include(procedure1.id)
|
||||
expect(assigns(:procedures).any? { |p| p.id == procedure2.id }).to be_truthy
|
||||
expect(assigns(:procedures).any? { |p| p.id == procedure3.id }).to be_truthy
|
||||
expect(assigns(:procedures).any? { |p| p.id == procedure1.id }).to be_falsey
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -954,9 +954,3 @@ describe Administrateurs::ProceduresController, type: :controller do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
def values_for_field(array, field)
|
||||
array.map do |procedure|
|
||||
procedure[field]
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue