Skip to content

Commit f530956

Browse files
authored
Merge pull request #69 from etalab/features/enhance-start-habilitation
Enhance start habilitation
2 parents 3143241 + 65a6bfc commit f530956

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+796
-179
lines changed

.rubocop.yml

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ Lint/ConstantDefinitionInBlock:
5757
Exclude:
5858
- "spec/**/*"
5959

60+
Lint/UnexpectedBlockArity:
61+
Enabled: false
62+
6063
Metrics/BlockLength:
6164
Exclude:
6265
- "spec/spec_helper.rb"

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ ruby '3.3.0'
44

55
gem 'active_storage_validations'
66
gem 'bootsnap', require: false
7+
gem 'draper'
78
gem 'importmap-rails', '~> 1.0'
89
gem 'interactor'
910
gem 'kaminari'
@@ -55,6 +56,5 @@ group :test do
5556
gem 'database_cleaner-active_record'
5657
gem 'rack_session_access'
5758
gem 'rspec-rails', '6.1.1'
58-
gem 'selenium-webdriver'
5959
gem 'simplecov', require: false
6060
end

Gemfile.lock

+14-8
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ GEM
6060
globalid (>= 0.3.6)
6161
activemodel (7.1.3)
6262
activesupport (= 7.1.3)
63+
activemodel-serializers-xml (1.0.2)
64+
activemodel (> 5.x)
65+
activesupport (> 5.x)
66+
builder (~> 3.1)
6367
activerecord (7.1.3)
6468
activemodel (= 7.1.3)
6569
activesupport (= 7.1.3)
@@ -156,6 +160,13 @@ GEM
156160
reline (>= 0.3.8)
157161
diff-lcs (1.5.0)
158162
docile (1.4.0)
163+
draper (4.0.2)
164+
actionpack (>= 5.0)
165+
activemodel (>= 5.0)
166+
activemodel-serializers-xml (>= 1.0)
167+
activesupport (>= 5.0)
168+
request_store (>= 1.0)
169+
ruby2_keywords
159170
drb (2.2.0)
160171
ruby2_keywords
161172
erubi (1.12.0)
@@ -378,6 +389,8 @@ GEM
378389
regexp_parser (2.9.0)
379390
reline (0.4.2)
380391
io-console (~> 0.5)
392+
request_store (1.5.1)
393+
rack (>= 1.4)
381394
rexml (3.2.6)
382395
rspec (3.12.0)
383396
rspec-core (~> 3.12.0)
@@ -431,12 +444,6 @@ GEM
431444
rubocop-factory_bot (~> 2.22)
432445
ruby-progressbar (1.13.0)
433446
ruby2_keywords (0.0.5)
434-
rubyzip (2.3.2)
435-
selenium-webdriver (4.17.0)
436-
base64 (~> 0.2)
437-
rexml (~> 3.2, >= 3.2.5)
438-
rubyzip (>= 1.2.2, < 3.0)
439-
websocket (~> 1.0)
440447
shellany (0.0.1)
441448
simplecov (0.22.0)
442449
docile (~> 1.1)
@@ -483,7 +490,6 @@ GEM
483490
bindex (>= 0.4.0)
484491
railties (>= 6.0.0)
485492
webrick (1.8.1)
486-
websocket (1.2.10)
487493
websocket-driver (0.7.6)
488494
websocket-extensions (>= 0.1.0)
489495
websocket-extensions (0.1.5)
@@ -511,6 +517,7 @@ DEPENDENCIES
511517
cuprite
512518
database_cleaner-active_record
513519
debug
520+
draper
514521
factory_bot_rails
515522
faker
516523
guard
@@ -539,7 +546,6 @@ DEPENDENCIES
539546
rubocop-performance
540547
rubocop-rails
541548
rubocop-rspec
542-
selenium-webdriver
543549
simplecov
544550
state_machines-activerecord
545551
stimulus-rails

app/assets/stylesheets/dsfr-extensions.css

+4
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@
3131
.fr-text-warning {
3232
color: var(--text-default-warning);
3333
}
34+
35+
.fr-bg-alt-blue-france {
36+
background-color: var(--background-action-low-blue-france);
37+
}

app/controllers/authorization_request_forms_controller.rb

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
class AuthorizationRequestFormsController < AuthenticatedUserController
22
helper AuthorizationRequestsHelpers
33

4-
before_action :extract_authorization_request_form
4+
before_action :extract_authorization_request_form, except: [:index]
55
before_action :extract_authorization_request, only: %i[show update]
66

7+
def index
8+
@authorization_definition = AuthorizationDefinition.find(params[:authorization_definition_id])
9+
end
10+
711
def new
812
authorize @authorization_request_form, :new?
913

10-
@authorization_request = authorization_request_class.new(
11-
applicant: current_user,
12-
organization: current_organization,
13-
form_uid: @authorization_request_form.uid,
14-
)
14+
@authorization_definition = @authorization_request_form.authorization_definition
1515

16-
render view_path
16+
render 'authorization_requests/new'
1717
end
1818

1919
def create
@@ -78,8 +78,6 @@ def create_for_single_page_form
7878
@authorization_request = organizer.authorization_request
7979

8080
if organizer.success?
81-
success_message(title: t('authorization_request_forms.create_for_single_page_form.success', name: @authorization_request.name))
82-
8381
redirect_to authorization_request_form_path(form_uid: @authorization_request.form_uid, id: @authorization_request.id)
8482
else
8583
error_message(title: t('authorization_request_forms.create_for_single_page_form.error.title'), description: t('authorization_request_forms.create_for_single_page_form.error.description'))
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class ApplicationDecorator < Draper::Decorator
2+
# Define methods for all decorated objects.
3+
# Helpers are accessed through `helpers` (aka `h`). For example:
4+
#
5+
# def percent_amount
6+
# h.number_to_percentage object.amount, precision: 2
7+
# end
8+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class AuthorizationRequestFormDecorator < ApplicationDecorator
2+
delegate_all
3+
4+
def tags
5+
tags = []
6+
7+
tags << 'default' if default
8+
tags << editor.id if editor
9+
tags << use_case if use_case
10+
11+
tags
12+
end
13+
end

app/helpers/authorization_requests_helpers.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module AuthorizationRequestsHelpers
22
def start_authorization_request_form(form)
33
authorization_request_form(form.authorization_request_class.new(form_uid: form.uid)) do |f|
4-
f.submit t('start_authorization_request_form.cta'), name: :start, id: :start_authorization_request, class: %w[fr-btn fr-icon-save-line fr-btn--icon-left]
4+
f.submit t('start_authorization_request_form.cta', authorization_name: form.authorization_definition.name), name: :start, id: dom_id(form, :start_authorization_request), class: %w[fr-btn fr-icon-save-line fr-btn--icon-left]
55
end
66
end
77

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import { Controller } from '@hotwired/stimulus'
2+
3+
export default class extends Controller {
4+
static targets = ['editors', 'editor', 'forms', 'form', 'noTeamDisclaimer', 'noEditorDisclaimer']
5+
static values = {
6+
editors: Array,
7+
targetUseCase: String
8+
}
9+
10+
chooseTeam (event) {
11+
this._hideForms()
12+
13+
switch (event.target.value) {
14+
case 'internal':
15+
this._hideEditors()
16+
this._hideEditorForms()
17+
if (this.hasTargetUseCaseValue) {
18+
this._hideOtherUseCaseForms()
19+
}
20+
this._showFormsBlock()
21+
break
22+
case 'editor':
23+
this._showEditors()
24+
break
25+
case 'none':
26+
this._hideEditors()
27+
this._showNoTeamDisclaimer()
28+
break
29+
}
30+
}
31+
32+
chooseEditor (event) {
33+
const editor = event.target.value
34+
35+
this.formTargets.forEach((form) => {
36+
const formTags = this._getFormTags(form)
37+
38+
if (formTags.includes(editor)) {
39+
form.classList.remove('fr-hidden')
40+
} else {
41+
form.classList.add('fr-hidden')
42+
}
43+
})
44+
45+
if (this.hasTargetUseCaseValue) {
46+
this._hideOtherUseCaseForms()
47+
}
48+
49+
if (this._noForm()) {
50+
this._showNoEditorDisclaimer()
51+
} else {
52+
this._showFormsBlock()
53+
}
54+
}
55+
56+
_getFormTags (form) {
57+
return JSON.parse(form.getAttribute('data-choose-authorization-request-form-tags'))
58+
}
59+
60+
_showEditors () {
61+
this.editorsTarget.classList.remove('fr-hidden')
62+
}
63+
64+
_hideEditors () {
65+
this.editorsTarget.classList.add('fr-hidden')
66+
this.editorTargets.forEach((editor) => {
67+
editor.checked = false
68+
})
69+
}
70+
71+
_hideEditorForms () {
72+
this._showAllForms()
73+
74+
this.editorsValue.forEach((editor) => {
75+
this.formTargets.forEach((form) => {
76+
const formTags = this._getFormTags(form)
77+
78+
if (formTags.includes(editor)) {
79+
form.classList.add('fr-hidden')
80+
}
81+
})
82+
})
83+
}
84+
85+
_showFormsBlock () {
86+
this.formsTarget.classList.remove('fr-hidden')
87+
}
88+
89+
_showAllForms () {
90+
this.formTargets.forEach((form) => {
91+
form.classList.remove('fr-hidden')
92+
})
93+
}
94+
95+
_hideForms () {
96+
this.formTargets.forEach((form) => {
97+
form.classList.add('fr-hidden')
98+
})
99+
this.formsTarget.classList.add('fr-hidden')
100+
this.noTeamDisclaimerTarget.classList.add('fr-hidden')
101+
this.noEditorDisclaimerTarget.classList.add('fr-hidden')
102+
}
103+
104+
_showNoTeamDisclaimer () {
105+
this.noTeamDisclaimerTarget.classList.remove('fr-hidden')
106+
}
107+
108+
_showNoEditorDisclaimer () {
109+
this.noEditorDisclaimerTarget.classList.remove('fr-hidden')
110+
this.formsTarget.classList.add('fr-hidden')
111+
}
112+
113+
_noForm () {
114+
return this.formTargets.every((form) => {
115+
return form.classList.contains('fr-hidden')
116+
})
117+
}
118+
119+
_hideOtherUseCaseForms () {
120+
this.formTargets.forEach((form) => {
121+
const formTags = this._getFormTags(form)
122+
123+
if (!formTags.includes(this.targetUseCaseValue) && !formTags.includes('default')) {
124+
form.classList.add('fr-hidden')
125+
}
126+
})
127+
}
128+
}

app/models/authorization_definition.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ def self.all
1717
end
1818
end
1919

20+
def editors
21+
available_forms.select { |form|
22+
form.editor.present?
23+
}.map(&:editor).uniq
24+
end
25+
2026
def self.indexable
2127
where(
2228
public: true,
@@ -47,7 +53,9 @@ def unique?
4753
end
4854

4955
def available_forms
50-
AuthorizationRequestForm.where(authorization_request_class:)
56+
AuthorizationRequestForm.where(authorization_request_class:).sort do |form|
57+
form.default ? 1 : 0
58+
end
5159
end
5260

5361
def public

app/models/authorization_request_form.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
class AuthorizationRequestForm < StaticApplicationRecord
22
attr_accessor :uid,
33
:name,
4+
:editor,
5+
:default,
6+
:use_case,
47
:authorization_request_class,
58
:templates,
69
:steps
@@ -22,17 +25,19 @@ def self.build(uid, hash)
2225
:name,
2326
:description,
2427
:public,
28+
:use_case,
2529
:data,
2630
:startable_by_applicant
2731
).merge(
2832
uid: uid.to_s,
33+
editor: hash[:editor_id].present? ? Editor.find(hash[:editor_id]) : nil,
34+
default: hash[:default] || false,
2935
authorization_request_class: AuthorizationRequest.const_get(hash[:authorization_request]),
3036
templates: (hash[:templates] || []).map { |template_key, template_attributes| AuthorizationRequestTemplate.new(template_key, template_attributes) },
3137
steps: hash[:steps] || []
3238
)
3339
)
3440
end
35-
3641
delegate :provider, :unique?, to: :authorization_definition
3742

3843
def id

app/models/editor.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Editor < StaticApplicationRecord
2+
attr_accessor :id,
3+
:name,
4+
:siret
5+
6+
def self.all
7+
Rails.application.config_for(:editors).map do |uid, hash|
8+
build(uid, hash)
9+
end
10+
end
11+
12+
def self.build(uid, hash)
13+
new(
14+
hash.slice(
15+
:name,
16+
:siret,
17+
).merge(
18+
id: uid.to_s,
19+
)
20+
)
21+
end
22+
end

app/models/static_application_record.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class StaticApplicationRecord
22
include ActiveModel::Model
3+
include Draper::Decoratable
34

45
def self.inherited(base)
56
base.extend ClassMethods

0 commit comments

Comments
 (0)