Skip to content

Commit 903e315

Browse files
authored
add custom text fields (#6)
* add custom text fields * add custom text fields * readme
1 parent 1442d52 commit 903e315

File tree

29 files changed

+326
-33
lines changed

29 files changed

+326
-33
lines changed

README.md

Lines changed: 27 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,31 @@ 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 a Hash here (key is the field, value whether mandatory or not).
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+
{
170+
hobbies: false,
171+
favorite_quote: true
172+
}
173+
end
147174
end
148175
```
149176

app/commands/concerns/decidim/extra_user_fields/create_registrations_commands_overrides.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def extended_data
5656
underage: form.underage,
5757
select_fields: form.select_fields,
5858
boolean_fields: form.boolean_fields,
59+
text_fields: form.text_fields,
5960
statutory_representative_email: form.statutory_representative_email
6061
)
6162
end

app/commands/concerns/decidim/extra_user_fields/omniauth_commands_overrides.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def extended_data
8383
underage: form.underage,
8484
select_fields: form.select_fields,
8585
boolean_fields: form.boolean_fields,
86+
text_fields: form.text_fields,
8687
statutory_representative_email: form.statutory_representative_email
8788
)
8889
end

app/commands/concerns/decidim/extra_user_fields/update_account_commands_overrides.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def extended_data
3232
underage: @form.underage,
3333
select_fields: @form.select_fields,
3434
boolean_fields: @form.boolean_fields,
35+
text_fields: @form.text_fields,
3536
statutory_representative_email: @form.statutory_representative_email
3637
)
3738
end

app/commands/decidim/extra_user_fields/admin/update_extra_user_fields.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ def extra_user_fields
5757
"underage" => { "enabled" => form.underage || false },
5858
"underage_limit" => form.underage_limit || Decidim::ExtraUserFields.underage_limit,
5959
"select_fields" => form.select_fields.to_a,
60-
"boolean_fields" => form.boolean_fields.to_a
60+
"boolean_fields" => form.boolean_fields.to_a,
61+
"text_fields" => form.text_fields.to_a
6162
}
6263
end
6364
# rubocop:enable Metrics/CyclomaticComplexity

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +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, Hash, default: {}
2526

2627
validates :country, presence: true, if: :country?
2728
validates :postal_code, presence: true, if: :postal_code?
@@ -57,7 +58,8 @@ def map_model(model)
5758
self.location = extended_data[:location]
5859
self.underage = extended_data[:underage]
5960
self.select_fields = extended_data[:select_fields] || {}
60-
self.boolean_fields = extended_data[:boolean_fields] || {}
61+
self.boolean_fields = extended_data[:boolean_fields] || []
62+
self.text_fields = extended_data[:text_fields] || {}
6163
self.statutory_representative_email = extended_data[:statutory_representative_email]
6264
end
6365

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

Lines changed: 8 additions & 0 deletions
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"]
@@ -38,6 +39,7 @@ def map_model(model)
3839
self.phone_number_placeholder = model.extra_user_fields.dig("phone_number", "placeholder")
3940
self.select_fields = model.extra_user_fields["select_fields"] || []
4041
self.boolean_fields = model.extra_user_fields["boolean_fields"] || []
42+
self.text_fields = model.extra_user_fields["text_fields"] || []
4143
end
4244

4345
def select_fields
@@ -51,6 +53,12 @@ def boolean_fields
5153
Decidim::ExtraUserFields.boolean_fields.map(&:to_s).include?(field)
5254
end
5355
end
56+
57+
def text_fields
58+
super.filter do |field|
59+
Decidim::ExtraUserFields.text_fields.keys.map(&:to_s).include?(field)
60+
end
61+
end
5462
end
5563
end
5664
end

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ def custom_boolean_fields(form)
3434
form.object.boolean_fields.include?(field.to_s)
3535
end
3636
end
37+
38+
def custom_text_fields(form)
39+
return {} unless Decidim::ExtraUserFields.text_fields.is_a?(Hash)
40+
41+
Decidim::ExtraUserFields.text_fields.keys.index_with do |field|
42+
form.object.text_fields.include?(field.to_s)
43+
end
44+
end
3745
end
3846
end
3947
end

app/helpers/decidim/extra_user_fields/application_helper.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ def custom_boolean_fields
5050
current_organization.extra_user_field_configuration(:boolean_fields).include?(field.to_s)
5151
end
5252
end
53+
54+
def custom_text_fields
55+
return [] unless Decidim::ExtraUserFields.text_fields.is_a?(Hash)
56+
57+
Decidim::ExtraUserFields.text_fields.filter_map do |field, mandatory|
58+
current_organization.extra_user_field_configuration(:text_fields).include?(field.to_s)
59+
60+
[field, mandatory.present?]
61+
end.to_h
62+
end
5363
end
5464
end
5565
end

app/serializers/decidim/extra_user_fields/user_export_serializer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def extra_fields
3838
:underage,
3939
:select_fields,
4040
:boolean_fields,
41+
:text_fields,
4142
:statutory_representative_email
4243
]
4344
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 %>

app/views/decidim/extra_user_fields/_profile_form.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@
4141
<% if current_organization.activated_extra_field?(:boolean_fields) %>
4242
<%= render partial: "decidim/extra_user_fields/boolean_fields", locals: { f: f } %>
4343
<% end %>
44+
45+
<% if current_organization.activated_extra_field?(:text_fields) %>
46+
<%= render partial: "decidim/extra_user_fields/text_fields", locals: { f: f } %>
47+
<% end %>
4448
<% end %>

app/views/decidim/extra_user_fields/_registration_form.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@
5757
<% if current_organization.activated_extra_field?(:boolean_fields) %>
5858
<%= render partial: "decidim/extra_user_fields/boolean_fields", locals: { f: f } %>
5959
<% end %>
60+
61+
<% if current_organization.activated_extra_field?(:text_fields) %>
62+
<%= render partial: "decidim/extra_user_fields/text_fields", locals: { f: f } %>
63+
<% end %>
6064
</div>
65+
6166
<%= append_javascript_pack_tag "decidim_extra_user_fields.js" %>
6267
<%= append_stylesheet_pack_tag "decidim_extra_user_fields_css" %>
6368
<% 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<% custom_text_fields(form).each do |field, checked| %>
2+
<div class="card-section">
3+
<div class="row column">
4+
<%= label_tag "extra_user_fields_select_field_#{field}" do %>
5+
6+
<%= check_box_tag "extra_user_fields[text_fields][]",
7+
field,
8+
checked,
9+
id: "extra_user_fields_select_field_#{field}" %>
10+
<%= t("decidim.extra_user_fields.admin.extra_user_fields.text_fields.#{field}.label", default: field.to_s.humanize ) %>
11+
<span class="help-text"><%= t("decidim.extra_user_fields.admin.extra_user_fields.text_fields.#{field}.description", default: "") %></span>
12+
<% end %>
13+
</div>
14+
</div>
15+
<% end %>

config/locales/en.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ en:
102102
description: This field is a list of participant types. If checked,
103103
user will have to choose a participant type
104104
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
105110
update:
106111
failure: An error occurred on update
107112
success: Extra user fields correctly updated in organization
@@ -129,6 +134,9 @@ en:
129134
participant_type:
130135
label: Are you participating as an individual, or officially on behalf of
131136
an organization?
137+
text_fields:
138+
motto:
139+
label: What is your motto?
132140
statutory_representative:
133141
inform:
134142
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,18 @@ module ExtraUserFields
6363
config_accessor :boolean_fields do
6464
[:ngo]
6565
end
66+
67+
# If extra text fields are needed, they can be added as a Hash here (key is the field, value whether mandatory or not).
68+
# For the user interface, you can defined labels and descriptions for the fields (optionally):
69+
# decidim.extra_user_fields.text_fields.field_name.label
70+
# decidim.extra_user_fields.text_fields.field_name.description
71+
# For the admin interface, you can defined labels and descriptions for the fields (optionally):
72+
# decidim.extra_user_fields.admin.extra_user_fields.text_fields.field_name.label
73+
# decidim.extra_user_fields.admin.extra_user_fields.text_fields.field_name.description
74+
config_accessor :text_fields do
75+
{
76+
motto: false
77+
}
78+
end
6679
end
6780
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

0 commit comments

Comments
 (0)