Skip to content

Commit 15b8d28

Browse files
committed
add custom text fields
1 parent 641bb08 commit 15b8d28

File tree

21 files changed

+267
-62
lines changed

21 files changed

+267
-62
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ Currently, the following ENV variables are supported:
101101
If your use case include fields not defined in this module, it is possible to define custom fields of different types:
102102

103103
1. **Select fields** This configuration option allows you to define any number of extra user fields of the type "Select".
104+
2. **Boolean fields** This configuration option allows you to define any number of extra user fields of the type "Boolean". These fields can be used for true/false values, such as "Accept terms and conditions" or "Subscribe to newsletter".
105+
3. **Text fields** This configuration option allows you to define any number of extra user fields of the type "Text". These fields can be used for free-form text input, such as "Hobbies" or "Favorite quote".
104106

105107

106108
See the next section "Configuration through an initializer" for more information.
@@ -144,6 +146,29 @@ Decidim::ExtraUserFields.configure do |config|
144146
}
145147
}
146148
end
149+
150+
# If extra boolean fields are needed, they can be added as an Array here.
151+
# For the user interface, you can defined labels and descriptions for the fields (optionally):
152+
# decidim.extra_user_fields.boolean_fields.field_name.label
153+
# decidim.extra_user_fields.boolean_fields.field_name.description
154+
# For the admin interface, you can defined labels and descriptions for the fields (optionally):
155+
# decidim.extra_user_fields.admin.extra_user_fields.boolean_fields.field_name.label
156+
# decidim.extra_user_fields.admin.extra_user_fields.boolean_fields.field_name.description
157+
config_accessor :boolean_fields do
158+
[:ngo, :newsletter]
159+
end
160+
161+
# If extra text fields are needed, they can be added as an Array here.
162+
# For the user interface, you can define labels and descriptions for the fields (optionally):
163+
# decidim.extra_user_fields.text_fields.field_name.label
164+
# decidim.extra_user_fields.text_fields.field_name.description
165+
# For the admin interface, you can define labels and descriptions for the fields (optionally):
166+
# decidim.extra_user_fields.admin.extra_user_fields.text_fields.field_name.label
167+
# decidim.extra_user_fields.admin.extra_user_fields.text_fields.field_name.description
168+
config_accessor :text_fields do
169+
[:hobbies, :favorite_quote]
170+
end
171+
147172
end
148173
```
149174

app/forms/concerns/decidim/extra_user_fields/forms_definitions.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module FormsDefinitions
2222
attribute :statutory_representative_email, String
2323
attribute :select_fields, Hash, default: {}
2424
attribute :boolean_fields, Array, default: []
25-
attribute :text_fields, Array, default: []
25+
attribute :text_fields, Hash, default: {}
2626

2727
validates :country, presence: true, if: :country?
2828
validates :postal_code, presence: true, if: :postal_code?
@@ -59,7 +59,7 @@ def map_model(model)
5959
self.underage = extended_data[:underage]
6060
self.select_fields = extended_data[:select_fields] || {}
6161
self.boolean_fields = extended_data[:boolean_fields] || []
62-
self.text_fields = extended_data[:text_fields] || []
62+
self.text_fields = extended_data[:text_fields] || {}
6363
self.statutory_representative_email = extended_data[:statutory_representative_email]
6464
end
6565

app/forms/decidim/extra_user_fields/admin/extra_user_fields_form.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ExtraUserFieldsForm < Decidim::Form
2222

2323
attribute :select_fields, Array, default: []
2424
attribute :boolean_fields, Array, default: []
25+
attribute :text_fields, Array, default: []
2526

2627
def map_model(model)
2728
self.enabled = model.extra_user_fields["enabled"]
@@ -55,7 +56,7 @@ def boolean_fields
5556

5657
def text_fields
5758
super.filter do |field|
58-
Decidim::ExtraUserFields.text_fields.map(&:to_s).include?(field)
59+
Decidim::ExtraUserFields.text_fields.keys.map(&:to_s).include?(field)
5960
end
6061
end
6162
end

app/helpers/decidim/extra_user_fields/admin/application_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ def custom_boolean_fields(form)
3636
end
3737

3838
def custom_text_fields(form)
39-
return {} unless Decidim::ExtraUserFields.text_fields.is_a?(Array)
39+
return {} unless Decidim::ExtraUserFields.text_fields.is_a?(Hash)
4040

41-
Decidim::ExtraUserFields.text_fields.index_with do |field|
41+
Decidim::ExtraUserFields.text_fields.keys.index_with do |field|
4242
form.object.text_fields.include?(field.to_s)
4343
end
4444
end

app/helpers/decidim/extra_user_fields/application_helper.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,13 @@ def custom_boolean_fields
5252
end
5353

5454
def custom_text_fields
55-
return [] unless Decidim::ExtraUserFields.text_fields.is_a?(Array)
55+
return [] unless Decidim::ExtraUserFields.text_fields.is_a?(Hash)
5656

57-
Decidim::ExtraUserFields.text_fields.filter do |field|
57+
Decidim::ExtraUserFields.text_fields.filter_map do |field, mandatory|
5858
current_organization.extra_user_field_configuration(:text_fields).include?(field.to_s)
59+
60+
[field, mandatory.present?]
61+
end.to_h
5962
end
6063
end
6164
end
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
<% f.object.custom_boolean_fields.each do |field, label| %>
2-
<div class="fieldset">
3-
<%= label_tag f.field_id("boolean_fields_#{field}") do %>
4-
<%= check_box_tag "user[boolean_fields][]",
5-
field,
6-
f.object.boolean_fields.map(&:to_s).include?(field.to_s),
7-
id: f.field_id("boolean_fields_#{field}") %>
1+
<% f.object.custom_boolean_fields.each do |field, label| %>
2+
<div class="fieldset">
3+
<%= label_tag f.field_id("boolean_fields_#{field}") do %>
4+
<%= check_box_tag "user[boolean_fields][]",
5+
field,
6+
f.object.boolean_fields.include?(field),
7+
id: f.field_id("boolean_fields_#{field}") %>
88

9-
<%= t("decidim.extra_user_fields.boolean_fields.#{field}.label", default: field.to_s.humanize) %>
10-
<% if (help = t("decidim.extra_user_fields.boolean_fields.#{field}.description", default: "")).present? %>
11-
<span class="help-text"><%= help %></span>
12-
<% end %>
13-
<% end %>
14-
</div>
9+
<%= t("decidim.extra_user_fields.boolean_fields.#{field}.label", default: field.to_s.humanize) %>
10+
<% if (help = t("decidim.extra_user_fields.boolean_fields.#{field}.description", default: "")).present? %>
11+
<span class="help-text"><%= help %></span>
12+
<% end %>
1513
<% end %>
14+
</div>
15+
<% end %>
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
<% f.object.custom_select_fields_options.each do |field, options| %>
2-
<div class="fieldset">
3-
<%= label_tag f.field_id("select_fields_#{field}") do %>
4-
<%= t("decidim.extra_user_fields.select_fields.#{field}.label", default: field.to_s.humanize) %>
5-
<% if (help = t("decidim.extra_user_fields.select_fields.#{field}.description", default: "")).present? %>
6-
<span class="help-text"><%= help %></span>
7-
<% end %>
1+
<% f.object.custom_select_fields_options.each do |field, options| %>
2+
<div class="fieldset">
3+
<%= label_tag f.field_id("select_fields_#{field}") do %>
4+
<%= t("decidim.extra_user_fields.select_fields.#{field}.label", default: field.to_s.humanize) %>
5+
<% if (help = t("decidim.extra_user_fields.select_fields.#{field}.description", default: "")).present? %>
6+
<span class="help-text"><%= help %></span>
7+
<% end %>
88

9-
<%= select_tag "user[select_fields][#{field}]",
10-
options_for_select(options, f.object.select_fields[field]),
11-
id: f.field_id("select_fields_#{field}") %>
12-
<% end %>
13-
</div>
9+
<%= select_tag "user[select_fields][#{field}]",
10+
options_for_select(options, f.object.select_fields[field]),
11+
id: f.field_id("select_fields_#{field}") %>
1412
<% end %>
13+
</div>
14+
<% end %>
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
<% f.object.custom_text_fields.each do |field, label| %>
2-
<div class="fieldset">
3-
<%= label_tag f.field_id("text_fields_#{field}") do %>
4-
<%= t("decidim.extra_user_fields.text_fields.#{field}.label", default: field.to_s.humanize) %>
5-
<% if (help = t("decidim.extra_user_fields.text_fields.#{field}.description", default: "")).present? %>
6-
<span class="help-text"><%= help %></span>
7-
<% end %>
8-
<%= text_field_tag "user[text_fields][]",
9-
field,
10-
f.object.text_fields.map(&:to_s).include?(field.to_s),
11-
id: f.field_id("text_fields_#{field}") %>
12-
<% end %>
13-
</div>
14-
<% end %>
1+
<% f.object.custom_text_fields.each do |field, required| %>
2+
<div class="fieldset">
3+
<%= f.text_field :text_fields, name: "user[text_fields][#{field}]",
4+
value: f.object.text_fields[field],
5+
label: t("decidim.extra_user_fields.text_fields.#{field}.label", default: field.to_s.humanize),
6+
help_text: t("decidim.extra_user_fields.text_fields.#{field}.description", default: nil),
7+
id: f.field_id("text_fields_#{field}"),
8+
label_options: {
9+
for: f.field_id("text_fields_#{field}"),
10+
},
11+
required: %>
12+
</div>
13+
<% end %>

app/views/decidim/extra_user_fields/admin/extra_user_fields/_form.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
<%= render partial: "decidim/extra_user_fields/admin/extra_user_fields/fields/select_fields", locals: { form: form } %>
4343
<%= render partial: "decidim/extra_user_fields/admin/extra_user_fields/fields/boolean_fields", locals: { form: form } %>
44+
<%= render partial: "decidim/extra_user_fields/admin/extra_user_fields/fields/text_fields", locals: { form: form } %>
4445
</div>
4546
</div>
4647
</div>

config/locales/en.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ en:
3535
exports:
3636
users: Participants
3737
extra_user_fields:
38-
text_fields:
39-
moto:
40-
description: This field is a String field. If checked, user will have
41-
to fill in a moto
42-
label: Enable moto field
4338
boolean_fields:
4439
ngo:
4540
description: This field is a Boolean field. User will be able to check
@@ -107,6 +102,11 @@ en:
107102
description: This field is a list of participant types. If checked,
108103
user will have to choose a participant type
109104
label: Enable participant type field
105+
text_fields:
106+
motto:
107+
description: This field is a String field. If checked, user can fill
108+
in a personal phrase or motto
109+
label: Enable "My Motto" field
110110
update:
111111
failure: An error occurred on update
112112
success: Extra user fields correctly updated in organization
@@ -116,9 +116,6 @@ en:
116116
61_or_more: 61 or older
117117
prefer_not_to_say: Prefer not to say
118118
up_to_16: 16 or younger
119-
text_fields:
120-
moto:
121-
label: What is your moto?
122119
boolean_fields:
123120
ngo:
124121
label: I am a member of a non-governmental organization (NGO)
@@ -137,6 +134,9 @@ en:
137134
participant_type:
138135
label: Are you participating as an individual, or officially on behalf of
139136
an organization?
137+
text_fields:
138+
motto:
139+
label: What is your motto?
140140
statutory_representative:
141141
inform:
142142
body: |

config/locales/fr.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ fr:
104104
description: Ce champ est une liste de types de participants. L'utilisateur
105105
devra choisir un type de participant.
106106
label: Activer le champ type de participant
107+
text_fields:
108+
motto:
109+
description: Ce champ permet l'ajout de texte. L'utilisateur pourra
110+
choisir un slogan.
111+
label: Activer le champ slogan
107112
update:
108113
failure: Une erreur est survenue lors de la mise à jour
109114
success: Les champs d'inscription ont été mis à jour avec succ§s
@@ -131,6 +136,9 @@ fr:
131136
select_fields:
132137
participant_type:
133138
label: Activer le champ type de participant
139+
text_fields:
140+
motto:
141+
label: Activer le champ "Mon slogan"
134142
statutory_representative:
135143
inform:
136144
body: |

lib/decidim/extra_user_fields.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,17 @@ module ExtraUserFields
6464
[:ngo]
6565
end
6666

67-
# If extra text fields are needed, they can be added as an Array here.
67+
# If extra text fields are needed, they can be added as a Hash here (key is the field, value whether mandatory or not).
6868
# For the user interface, you can defined labels and descriptions for the fields (optionally):
6969
# decidim.extra_user_fields.text_fields.field_name.label
7070
# decidim.extra_user_fields.text_fields.field_name.description
7171
# For the admin interface, you can defined labels and descriptions for the fields (optionally):
7272
# decidim.extra_user_fields.admin.extra_user_fields.text_fields.field_name.label
7373
# decidim.extra_user_fields.admin.extra_user_fields.text_fields.field_name.description
7474
config_accessor :text_fields do
75-
[:moto]
75+
{
76+
motto: false
77+
}
7678
end
7779
end
7880
end

spec/commands/decidim/create_omniauth_registration_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ module Comments
2828
let(:boolean_fields) do
2929
["ngo"]
3030
end
31+
let(:text_fields) do
32+
{
33+
"motto" => "I think, therefore I am"
34+
}
35+
end
3136
let(:statutory_representative_email) { nil }
3237
let(:extended_data) do
3338
{
@@ -41,6 +46,7 @@ module Comments
4146
underage:,
4247
select_fields:,
4348
boolean_fields:,
49+
text_fields:,
4450
statutory_representative_email:
4551
}
4652
end
@@ -66,6 +72,7 @@ module Comments
6672
"underage" => underage,
6773
"select_fields" => select_fields,
6874
"boolean_fields" => boolean_fields,
75+
"text_fields" => text_fields,
6976
"statutory_representative_email" => statutory_representative_email
7077
}
7178
}
@@ -138,6 +145,7 @@ module Comments
138145
expect(user.extended_data["underage"]).to eq(underage)
139146
expect(user.extended_data["select_fields"]).to eq(select_fields)
140147
expect(user.extended_data["boolean_fields"]).to eq(boolean_fields)
148+
expect(user.extended_data["text_fields"]).to eq(text_fields)
141149
end
142150

143151
# NOTE: This is important so that the users who are only

spec/commands/decidim/create_registration_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ module Comments
3131
let(:boolean_fields) do
3232
["ngo"]
3333
end
34+
let(:text_fields) do
35+
{
36+
motto: "I think, therefore I am"
37+
}
38+
end
3439
let(:statutory_representative_email) { nil }
3540
let(:extended_data) do
3641
{
@@ -65,6 +70,7 @@ module Comments
6570
"underage" => underage,
6671
"select_fields" => select_fields,
6772
"boolean_fields" => boolean_fields,
73+
"text_fields" => text_fields,
6874
"statutory_representative_email" => statutory_representative_email
6975
}
7076
}
@@ -142,6 +148,7 @@ module Comments
142148
underage:,
143149
select_fields:,
144150
boolean_fields:,
151+
text_fields:,
145152
statutory_representative_email:
146153
}
147154
).and_call_original

spec/commands/decidim/extra_user_fields/admin/update_extra_user_fields_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ module Admin
3737
"underage" => underage,
3838
"underage_limit" => underage_limit,
3939
"select_fields" => %w(participant_type non_existing_field),
40-
"boolean_fields" => %w(ngo non_existing_field)
40+
"boolean_fields" => %w(ngo non_existing_field),
41+
"text_fields" => %w(motto non_existing_field)
4142
}
4243
end
4344
let(:form) do
@@ -91,6 +92,7 @@ module Admin
9192
expect(extra_user_fields).to include("underage_limit" => 18)
9293
expect(extra_user_fields).to include("select_fields" => ["participant_type"])
9394
expect(extra_user_fields).to include("boolean_fields" => ["ngo"])
95+
expect(extra_user_fields).to include("text_fields" => ["motto"])
9496
end
9597
end
9698
end

0 commit comments

Comments
 (0)