Skip to content

Commit 1b5e6b0

Browse files
committed
Harden decorators class methods delegation
Decorator does not play nice with `class` instance method (it does not forward), we have to define an instance methods to access these attributes. Moreover, `model_name` renders the parent class instead of child, make it global in main decorator to avoid this error.
1 parent 553cc60 commit 1b5e6b0

File tree

6 files changed

+10
-5
lines changed

6 files changed

+10
-5
lines changed

app/decorators/application_decorator.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
class ApplicationDecorator < Draper::Decorator
22
delegate :t, to: I18n
33

4+
delegate :model_name, to: :object
5+
46
# Define methods for all decorated objects.
57
# Helpers are accessed through `helpers` (aka `h`). For example:
68
#

app/form_builders/authorization_request_form_builder.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def info_for(block)
4040
def contacts_infos(contacts = nil)
4141
return if @template.namespace?(:instruction)
4242

43-
contacts ||= @object.class.contact_types
43+
contacts ||= @object.contact_types
4444

4545
dsfr_accordion(
4646
I18n.t('authorization_request_forms.default.contacts.info.title'),

app/form_builders/dsfr_form_builder.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def hint_for(attribute)
193193
end
194194

195195
def label_value(attribute)
196-
@object.class.human_attribute_name(attribute)
196+
(@object.try(:object) || @object).class.human_attribute_name(attribute)
197197
end
198198

199199
def enhance_input_options(opts)

app/models/concerns/authorization_core/contacts.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ def self.contact(kind, validation_condition: nil)
3131
contact_types << kind
3232
end
3333
end
34-
3534
end
3635
end
36+
37+
def contact_types
38+
self.class.contact_types
39+
end
3740
end

app/views/authorization_request_forms/shared/_contacts.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<div class="fr-container-fluid">
44
<div class="fr-grid-row">
5-
<% @authorization_request.class.contact_types.each do |contact_type| %>
5+
<% @authorization_request.contact_types.each do |contact_type| %>
66
<%= render partial: 'authorization_request_forms/shared/contact', locals: { contact_type:, f: } %>
77
<% end %>
88
</div>

spec/factories/authorization_requests.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
after(:build) do |authorization_request, evaluator|
1313
if authorization_request.need_complete_validation? || evaluator.fill_all_attributes
14-
authorization_request.class.contact_types.each do |contact_type|
14+
authorization_request.contact_types.each do |contact_type|
1515
{
1616
'family_name' => "Dupont #{contact_type.to_s.humanize}",
1717
'given_name' => "Jean #{contact_type.to_s.humanize}",

0 commit comments

Comments
 (0)