Merge pull request #1843 from betagouv/fix_1832_cloned_procedure_stat

Fix 1832 cloned procedure stat
This commit is contained in:
LeSim 2018-04-24 15:53:01 +02:00 committed by GitHub
commit 062de5dedb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 103 additions and 11 deletions

View file

@ -160,7 +160,7 @@ class Admin::ProceduresController < AdminController
render '/admin/procedures/transfer', formats: 'js', status: 404
else
procedure = current_administrateur.procedures.find(params[:procedure_id])
clone_procedure = procedure.clone(admin)
clone_procedure = procedure.clone(admin, false)
clone_procedure.save
@ -184,13 +184,13 @@ class Admin::ProceduresController < AdminController
def clone
procedure = Procedure.find(params[:procedure_id])
new_procedure = procedure.clone(current_administrateur)
new_procedure = procedure.clone(current_administrateur, cloned_from_library?)
if new_procedure.save
flash.notice = 'Procédure clonée'
redirect_to edit_admin_procedure_path(id: new_procedure.id)
else
if params[:from_new_from_existing].present?
if cloned_from_library?
flash.alert = new_procedure.errors.full_messages
redirect_to new_from_existing_admin_procedures_path
else
@ -249,6 +249,10 @@ class Admin::ProceduresController < AdminController
private
def cloned_from_library?
params[:from_new_from_existing].present?
end
def procedure_params
editable_params = [:libelle, :description, :organisation, :direction, :lien_site_web, :notice, :web_hook_url, :euro_flag, :logo, :auto_archive_on]
if @procedure.try(:locked?)

View file

@ -32,10 +32,28 @@ class StatsController < ApplicationController
@motivation_usage_dossier = motivation_usage_dossier
@motivation_usage_procedure = motivation_usage_procedure
@cloned_from_library_procedures_ratio = cloned_from_library_procedures_ratio
end
private
def cloned_from_library_procedures_ratio
[3.weeks.ago, 2.weeks.ago, 1.week.ago].map do |date|
min_date = date.beginning_of_week
max_date = min_date.end_of_week
all_procedures = Procedure.created_during(min_date..max_date)
cloned_from_library_procedures = all_procedures.cloned_from_library
denominator = [1, all_procedures.count].max
ratio = percentage(cloned_from_library_procedures.count, denominator)
[l(max_date, format: '%d/%m/%Y'), ratio]
end
end
def max_date
if administration_signed_in?
Time.now.to_date

View file

@ -10,6 +10,7 @@ class Procedure < ApplicationRecord
has_one :attestation_template, dependent: :destroy
belongs_to :administrateur
belongs_to :parent_procedure, class_name: 'Procedure'
has_many :assign_to, dependent: :destroy
has_many :administrateurs_procedures
@ -39,6 +40,8 @@ class Procedure < ApplicationRecord
scope :archivees, -> { where.not(archived_at: nil) }
scope :publiees_ou_archivees, -> { where.not(published_at: nil) }
scope :by_libelle, -> { order(libelle: :asc) }
scope :created_during, -> (range) { where(created_at: range) }
scope :cloned_from_library, -> { where(cloned_from_library: true) }
validates :libelle, presence: true, allow_blank: false, allow_nil: false
validates :description, presence: true, allow_blank: false, allow_nil: false
@ -104,7 +107,7 @@ class Procedure < ApplicationRecord
publiee_ou_archivee?
end
def clone(admin)
def clone(admin, from_library)
procedure = self.deep_clone(include:
{
types_de_piece_justificative: nil,
@ -125,6 +128,9 @@ class Procedure < ApplicationRecord
procedure.refused_mail = refused_mail.try(:dup)
procedure.without_continuation_mail = without_continuation_mail.try(:dup)
procedure.cloned_from_library = from_library
procedure.parent_procedure = self
procedure
end

View file

@ -103,3 +103,12 @@
= column_chart @motivation_usage_procedure, ytitle: 'procedures avec motivation / total procedures', xtitle: 'semaines'
.clearfix
%h2.new-h2 Utilisation de la bibliothèque
.stat-cards
.stat-card.stat-card-half.pull-left
%span.stat-card-title Taux d'utilisation de la bibliothèque
= column_chart @cloned_from_library_procedures_ratio, ytitle: 'procédures clonées / total procédure', xtitle: 'semaines'
.clearfix

View file

@ -0,0 +1,5 @@
class AddClonedFromLibraryColumnToProcedure < ActiveRecord::Migration[5.2]
def change
add_column :procedures, :cloned_from_library, :boolean, default: false
end
end

View file

@ -0,0 +1,5 @@
class AddParentProcedureToProcedures < ActiveRecord::Migration[5.2]
def change
add_reference :procedures, :parent_procedure, foreign_key: { to_table: :procedures }
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_04_05_131207) do
ActiveRecord::Schema.define(version: 2018_04_24_130548) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -462,7 +462,10 @@ ActiveRecord::Schema.define(version: 2018_04_05_131207) do
t.datetime "whitelisted_at"
t.boolean "ask_birthday", default: false, null: false
t.string "web_hook_url"
t.boolean "cloned_from_library", default: false
t.bigint "parent_procedure_id"
t.index ["hidden_at"], name: "index_procedures_on_hidden_at"
t.index ["parent_procedure_id"], name: "index_procedures_on_parent_procedure_id"
end
create_table "quartier_prioritaires", id: :serial, force: :cascade do |t|
@ -571,6 +574,7 @@ ActiveRecord::Schema.define(version: 2018_04_05_131207) do
add_foreign_key "procedure_paths", "administrateurs"
add_foreign_key "procedure_paths", "procedures"
add_foreign_key "procedure_presentations", "assign_tos"
add_foreign_key "procedures", "procedures", column: "parent_procedure_id"
add_foreign_key "received_mails", "procedures"
add_foreign_key "refused_mails", "procedures"
add_foreign_key "without_continuation_mails", "procedures"

View file

@ -456,19 +456,25 @@ describe Admin::ProceduresController, type: :controller do
describe 'PUT #clone' do
let!(:procedure) { create(:procedure, administrateur: admin) }
subject { put :clone, params: { procedure_id: procedure.id } }
let(:params) { { procedure_id: procedure.id } }
subject { put :clone, params: params }
it { expect { subject }.to change(Procedure, :count).by(1) }
context 'when admin is the owner of the procedure' do
before do
subject
end
before { subject }
it 'creates a new procedure and redirect to it' do
expect(response).to redirect_to edit_admin_procedure_path(id: Procedure.last.id)
expect(Procedure.last.cloned_from_library).to be(false)
expect(flash[:notice]).to have_content 'Procédure clonée'
end
context 'when the procedure is cloned from the library' do
let(:params) { { procedure_id: procedure.id, from_new_from_existing: true } }
it { expect(Procedure.last.cloned_from_library).to be(true) }
end
end
context 'when admin is not the owner of the procedure' do

View file

@ -310,4 +310,25 @@ describe StatsController, type: :controller do
it { expect(subject).to match([[I18n.l(3.weeks.ago.end_of_week, format: '%d/%m/%Y'), 0], [I18n.l(2.weeks.ago.end_of_week, format: '%d/%m/%Y'), 0], [I18n.l(1.week.ago.end_of_week, format: '%d/%m/%Y'), 33.33]]) }
end
describe "#cloned_from_library_procedures_ratio" do
let!(:procedure1) { create(:procedure, created_at: 3.weeks.ago) }
let!(:procedure2) { create(:procedure, created_at: 2.weeks.ago) }
let!(:procedure3) { create(:procedure, created_at: 2.weeks.ago, cloned_from_library: true) }
before { Timecop.freeze(Time.now) }
after { Timecop.return }
subject { StatsController.new.send(:cloned_from_library_procedures_ratio) }
let(:result) do
[
[I18n.l(3.weeks.ago.end_of_week, format: '%d/%m/%Y'), 0],
[I18n.l(2.weeks.ago.end_of_week, format: '%d/%m/%Y'), 50.0],
[I18n.l(1.week.ago.end_of_week, format: '%d/%m/%Y'), 0]
]
end
it { expect(subject).to match(result) }
end
end

View file

@ -272,12 +272,13 @@ describe Procedure do
let!(:piece_justificative_0) { create(:type_de_piece_justificative, procedure: procedure, order_place: 0) }
let!(:piece_justificative_1) { create(:type_de_piece_justificative, procedure: procedure, order_place: 1) }
let(:received_mail){ create(:received_mail) }
let(:from_library) { false }
before do
@logo = File.open('spec/fixtures/white.png')
@signature = File.open('spec/fixtures/black.png')
@attestation_template = create(:attestation_template, procedure: procedure, logo: @logo, signature: @signature)
@procedure = procedure.clone(procedure.administrateur)
@procedure = procedure.clone(procedure.administrateur, from_library)
@procedure.save
end
@ -288,9 +289,10 @@ describe Procedure do
subject { @procedure }
it { expect(subject.parent_procedure).to eq(procedure) }
it 'should duplicate specific objects with different id' do
expect(subject.id).not_to eq(procedure.id)
expect(subject).to have_same_attributes_as(procedure)
expect(subject.module_api_carto).to have_same_attributes_as(procedure.module_api_carto)
expect(subject.types_de_piece_justificative.size).to eq procedure.types_de_piece_justificative.size
@ -312,6 +314,18 @@ describe Procedure do
end
expect(subject.attestation_template.title).to eq(procedure.attestation_template.title)
expect(subject.cloned_from_library).to be(false)
cloned_procedure = subject
cloned_procedure.parent_procedure_id = nil
expect(cloned_procedure).to have_same_attributes_as(procedure)
end
context 'when the procedure is clone from the library' do
let(:from_library) { true }
it { expect(subject.cloned_from_library).to be(true) }
end
it 'should duplicate existing mail_templates' do