Skip to content

Commit 39ae775

Browse files
committed
readme
1 parent b8e20b6 commit 39ae775

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ Currently, the following ENV variables are supported:
9696
| EXTRA_USER_FIELDS_GENDERS | Options for the gender field (you need to add the corresponding I18n keys, ie: `decidim.extra_user_fields.genders.prefer_not_to_say` ) | `female male other prefer_not_to_say` |
9797
| EXTRA_USER_FIELDS_AGE_RANGES | Options for the age range field (you need to add the corresponding I18n keys, e.g., `decidim.extra_user_fields.age_ranges.up_to_16`) | `up_to_16 17_to_30 31_to_60 61_or_more prefer_not_to_say` |
9898

99+
## Custom fields
100+
101+
If your use case include fields not defined in this module, it is possible to define custom fields of different types:
102+
103+
1. **Select fields** This configuration option allows you to define any number of extra user fields of the type "Select".
104+
105+
106+
See the next section "Configuration through an initializer" for more information.
107+
108+
99109
### Configuration through an initializer
100110

101111
It is also possible to configure the module using the an initializer:
@@ -110,6 +120,30 @@ Decidim::ExtraUserFields.configure do |config|
110120
config.age_ranges = ["30_or_younger", "31_or_older", "prefer_not_to_say"]
111121

112122
...
123+
124+
125+
# I extra select fields are needed, they can be added here.
126+
# The key is the field name and the value is a hash with the options.
127+
# You can (optionally) add I18n keys for the options (if not the text will be used as it is).
128+
# For the user interface, you can defined labels and descriptions for the fields (optionally):
129+
# decidim.extra_user_fields.select_fields.field_name.label
130+
# decidim.extra_user_fields.select_fields.field_name.description
131+
# For the admin interface, you can defined labels and descriptions for the fields (optionally):
132+
# decidim.extra_user_fields.admin.extra_user_fields.select_fields.field_name.label
133+
# decidim.extra_user_fields.admin.extra_user_fields.select_fields.field_name.description
134+
config_accessor :select_fields do
135+
{
136+
participant_type: {
137+
# "" => "",
138+
"individual" => "decidim.extra_user_fields.participant_types.individual",
139+
"organization" => "decidim.extra_user_fields.participant_types.organization"
140+
},
141+
favorite_pet: {
142+
"cat" => "my_app.favorite_pets.cat".
143+
"dog" => "my_app.favorite_pets.dog"
144+
}
145+
}
146+
end
113147
end
114148
```
115149

app/serializers/decidim/extra_user_fields/user_export_serializer.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def serialize
1313
def extra_user_fields
1414
extended_data = resource.extended_data.symbolize_keys
1515

16-
[:gender, :age_range, :country, :postal_code, :date_of_birth, :phone_number, :location, :underage, :statutory_representative_email].index_with do |key|
16+
extra_fields.index_with do |key|
1717
extended_data[key]
1818
end
1919
end
@@ -36,10 +36,8 @@ def extra_fields
3636
:phone_number,
3737
:location,
3838
:underage,
39+
:select_fields,
3940
:statutory_representative_email
40-
# Block ExtraUserFields AddExtraField
41-
42-
# EndBlock
4341
]
4442
end
4543
end

spec/serializers/decidim/extra_user_fields/user_export_serializer_spec.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
subject { described_class.new(resource) }
77

88
let(:resource) { create(:user, extended_data: registration_metadata) }
9-
# rubocop:disable Style/TrailingCommaInHashLiteral
109
let(:registration_metadata) do
1110
{
1211
gender:,
@@ -17,13 +16,10 @@
1716
phone_number:,
1817
location:,
1918
underage:,
20-
statutory_representative_email:,
21-
# Block ExtraUserFields ExtraUserFields
22-
23-
# EndBlock
19+
select_fields:,
20+
statutory_representative_email:
2421
}
2522
end
26-
# rubocop:enable Style/TrailingCommaInHashLiteral
2723

2824
let(:gender) { "other" }
2925
let(:age_range) { "17_to_30" }
@@ -35,9 +31,12 @@
3531
let(:underage) { true }
3632
let(:underage_limit) { 18 }
3733
let(:statutory_representative_email) { "parent@example.org" }
38-
# Block ExtraUserFields RspecVar
34+
let(:select_fields) do
35+
{
36+
"participant_type" => "individual"
37+
}
38+
end
3939

40-
# EndBlock
4140
let(:serialized) { subject.serialize }
4241

4342
describe "#serialize" do
@@ -73,6 +72,10 @@
7372
expect(serialized).to include(location: resource.extended_data["location"])
7473
end
7574

75+
it "includes the select fields" do
76+
expect(serialized).to include(select_fields: resource.extended_data["select_fields"])
77+
end
78+
7679
context "when users are blocked" do
7780
let(:resource) { create(:user, :blocked, extended_data: registration_metadata, blocked_at:) }
7881
let(:blocked_at) { Time.zone.now }

0 commit comments

Comments
 (0)