decidim-user_fields 0.0.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +308 -49
  3. data/Rakefile +253 -0
  4. data/app/forms/decidim/custom_user_fields/admin/customizations_config_form.rb +67 -0
  5. data/app/forms/decidim/custom_user_fields/verifications/verification_form.rb +85 -0
  6. data/app/overrides/decidim/devise/invitations/edit/extra_user_fields_form.html.erb.deface +2 -2
  7. data/app/overrides/decidim/devise/omniauth_registrations/new/extra_user_fields.html.erb.deface +3 -0
  8. data/app/overrides/invitation_override.rb +31 -0
  9. data/app/packs/entrypoints/decidim_custom_user_fields.js +0 -0
  10. data/app/packs/stylesheets/decidim/custom_user_fields/_decidim_custom_user_fields.scss +48 -0
  11. data/app/views/decidim/custom_user_fields/_profile_form.html.erb +2 -2
  12. data/app/views/decidim/custom_user_fields/_registration_form.html.erb +2 -2
  13. data/app/views/decidim/custom_user_fields/_verification_form.erb +4 -0
  14. data/app/views/decidim/custom_user_fields/system/_customizations_tab.html.erb +15 -0
  15. data/config/assets.rb +9 -0
  16. data/config/i18n-tasks.yml +22 -0
  17. data/config/locales/decidim_custom_user_fields_scenarios.en.yml +50 -0
  18. data/config/locales/decidim_custom_user_fields_scenarios.fr.yml +48 -0
  19. data/config/locales/decidim_toggle.en.yml +13 -0
  20. data/config/locales/decidim_toggle.fr.yml +13 -0
  21. data/config/locales/decidim_user_fields.en.yml +25 -0
  22. data/config/locales/decidim_user_fields.fr.yml +25 -0
  23. data/lib/decidim/custom_user_fields/custom_user_fields.rb +10 -14
  24. data/lib/decidim/custom_user_fields/customization_field_naming.rb +17 -0
  25. data/lib/decidim/custom_user_fields/customizations/builder.rb +48 -0
  26. data/lib/decidim/custom_user_fields/customizations/customization.rb +29 -0
  27. data/lib/decidim/custom_user_fields/customizations.rb +42 -0
  28. data/lib/decidim/custom_user_fields/decidim_integrations.rb +52 -0
  29. data/lib/decidim/custom_user_fields/definition_guidance.rb +109 -0
  30. data/lib/decidim/custom_user_fields/dev/bootstrap.rb +102 -0
  31. data/lib/decidim/custom_user_fields/dev/local_oidc_seeder.rb +150 -0
  32. data/lib/decidim/custom_user_fields/dev/omniauth_providers_patcher.rb +68 -0
  33. data/lib/decidim/custom_user_fields/dev/openid_connect_setup.rb +110 -0
  34. data/lib/decidim/custom_user_fields/dev/scenario_customizations.rb +72 -0
  35. data/lib/decidim/custom_user_fields/engine.rb +59 -26
  36. data/lib/decidim/custom_user_fields/extended_data.rb +52 -0
  37. data/lib/decidim/custom_user_fields/field_definition.rb +28 -6
  38. data/lib/decidim/custom_user_fields/fields/boolean_field.rb +51 -0
  39. data/lib/decidim/custom_user_fields/fields/date_field.rb +80 -10
  40. data/lib/decidim/custom_user_fields/fields/dummy_field.rb +40 -0
  41. data/lib/decidim/custom_user_fields/fields/extra_field_ref_field.rb +118 -0
  42. data/lib/decidim/custom_user_fields/fields/generic_field.rb +99 -6
  43. data/lib/decidim/custom_user_fields/fields/text_area_field.rb +34 -9
  44. data/lib/decidim/custom_user_fields/fields/text_field.rb +47 -6
  45. data/lib/decidim/custom_user_fields/i18n/customization_keys.rb +55 -0
  46. data/lib/decidim/custom_user_fields/i18n/customization_keys_scanner.rb +80 -0
  47. data/lib/decidim/custom_user_fields/i18n/tasks.rb +7 -0
  48. data/lib/decidim/custom_user_fields/overrides/authorizations_controller.rb +18 -0
  49. data/lib/decidim/custom_user_fields/overrides/command.rb +23 -21
  50. data/lib/decidim/custom_user_fields/overrides/confirmations_controller.rb +15 -0
  51. data/lib/decidim/custom_user_fields/overrides/devise_authentication_methods.rb +68 -0
  52. data/lib/decidim/custom_user_fields/overrides/form_definition.rb +25 -8
  53. data/lib/decidim/custom_user_fields/overrides/invitations_controller.rb +109 -0
  54. data/lib/decidim/custom_user_fields/overrides/omniauth_command.rb +40 -0
  55. data/lib/decidim/custom_user_fields/overrides/omniauth_registrations_controller.rb +93 -0
  56. data/lib/decidim/custom_user_fields/overrides/user.rb +16 -0
  57. data/lib/decidim/custom_user_fields/registration_fields.rb +99 -0
  58. data/lib/decidim/custom_user_fields/update_customizations_config_command.rb +39 -0
  59. data/lib/decidim/custom_user_fields/upgrade/migrate_toggle_config.rb +74 -0
  60. data/lib/decidim/custom_user_fields/verifications/builder.rb +100 -0
  61. data/lib/decidim/custom_user_fields/verifications/verifications.rb +82 -0
  62. data/lib/decidim/custom_user_fields/version.rb +4 -2
  63. data/lib/decidim/user_fields.rb +46 -13
  64. data/lib/tasks/decidim/custom_user_fields/custom_user_fields_upgrade.rake +16 -0
  65. data/lib/tasks/decidim/custom_user_fields/custom_user_fields_webpacker.rake +59 -0
  66. data/lib/tasks/decidim/custom_user_fields/decidim_custom_user_fields_upgrade_tasks.rake +16 -0
  67. data/lib/tasks/decidim/custom_user_fields/dev/local_setup.rake +16 -0
  68. metadata +80 -15
  69. data/app/overrides/decidim/devise/invitations/edit/extra_user_fields.html.erb.deface +0 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c4b8731649562fb4f77d5683c0fedb90ec23a7d188b27ecca0ed23eb6b6ad6ce
4
- data.tar.gz: f7c168dcdcaca0f1b194123b882b9da9f163d19ca5de0157372586b9813debb9
3
+ metadata.gz: 24006b3a04eeb2d3236a31ed29ccbd834fc0cb2f99794c4d38dcba978c1d75c1
4
+ data.tar.gz: 94c451d2d5ff60e16cf14afb8f0efaed037e763f19b305170415b0fd5e496845
5
5
  SHA512:
6
- metadata.gz: 7d080a57c352167b864f51b0d9c44071ce993b34458df073a3e1f1eb49972fdc0695bb3339fa06985d7b407ba2e5731064d6b6fdd2229cf7020f6a7eb3b4e555
7
- data.tar.gz: 67644476c30f84a6de18b720a050c73ab117b744159ab05e3c43be9bb3434866b3ab6fb512080f106b9182801673e7fc04b7bc0896779bf706a680c1a774ee55
6
+ metadata.gz: 7e833154273a50cfb23d7b3529b04d9241750199be3d2095c564c1ae31974c38c60c4894f4610afd3ccf095e5dce1da0de1475b19f91120a1ba55348a25adbf6
7
+ data.tar.gz: 75d66d356e14ffd771423d3b4dd7bcac4b3f843e7c7c7fcff0ee1da3666af3015f9e696f5a0f50df375bf976535583a9894d54cfa468ab2e1f15382762d0aa44
data/README.md CHANGED
@@ -22,106 +22,365 @@
22
22
  </a>
23
23
  </p>
24
24
 
25
-
26
25
  # Decidim User Fields
27
- Add custom user fields through a configuration file and without migration.
28
- This module aims to configure in a blast new fields for subscription and profile editing. It supports:
26
+ This Decidim module adds custom user fields through a configuration file and without migration. This module aims to configure in a blast new fields for subscription and profile editing. It supports:
29
27
 
30
28
  - User registration
31
29
  - User profiles
32
30
  - User invitations
31
+ - Omniauth registration (OIDC profile completion after IdP sign-in)
33
32
 
34
- ⚠️ It **does not support**:
33
+ > Are you on GitHub ? Please use the reference repository on [GitLab for issues and pull requests](https://git.octree.ch/decidim/vocacity/decidim-modules/decidim-user_fields).
35
34
 
36
- - Omniauth registrations
35
+ All the information to use the gem is on our [documentation website](https://octree-gva.github.io/decidim-user_fields)
37
36
 
38
- ## Set up the module
39
- Add the gem to your Gemfile
40
- ```
37
+ ## Install the module
38
+ Add the gems to your Gemfile
39
+ ```ruby
41
40
  gem "decidim-user_fields"
41
+ gem "decidim-toggle",
42
+ git: "https://git.octree.ch/decidim/vocacity/decidim-modules/decidim-toggle",
43
+ branch: "main"
42
44
  ```
43
45
 
44
- Run bundle
46
+ Run bundle and install toggle migrations:
45
47
  ```bash
46
48
  bundle install
49
+ rails decidim_toggle:install:migrations
50
+ rails db:migrate
47
51
  ```
48
52
 
49
- Create an initializer `config/initializers/custom_user_fields.rb`
53
+ ## Customizations (per organization)
54
+
55
+ Register named customizations in an initializer. Each customization bundles registration/profile fields and authorization workflows. Sysadmins enable them per organization in **System → Organizations → User field customizations** (via decidim-toggle).
56
+
57
+ ```ruby
58
+ Decidim::CustomUserFields.register_customization :community do |customization|
59
+ customization.registration_fields do |set|
60
+ set.add_field :social_media_url, type: :text, required: false
61
+ end
62
+ end
63
+
64
+ Decidim::CustomUserFields.register_customization :ngos do |customization|
65
+ customization.registration_fields do |set|
66
+ set.add_field :organization_name, type: :text, required: true
67
+ set.add_field :organization_email, type: :text, required: true
68
+ end
69
+
70
+ customization.authorization "NgoVerify" do |config|
71
+ config.add_field :organization_name, type: :extra_field_ref, ref: :organization_name, required: true
72
+ end
73
+ end
50
74
  ```
51
- Decidim::CustomUserFields.configure do |config|
52
- config.add_field :birthdate, type: :date, required: true
53
- config.add_field :province, type: :select, options: [:vd, :vs, :ar], default: :vd, required: true
54
- config.add_field :address, type: :textarea, required: false, rows: 10
55
- config.add_field :purpose, type: :text, required: false
75
+
76
+ Admin toggle radio labels: `decidim_toggle.system.custom_user_fields.<name>_enabled`. The none option uses `decidim_toggle.system.custom_user_fields.none`.
77
+ Customization display labels (`Customization#label`): `decidim.custom_user_fields.customizations.<name>`.
78
+
79
+ Full key list and how to run `i18n-tasks missing` (customization registry scanner): see the [Translate](https://octree-gva.github.io/decidim-user_fields/dev_documentation/locales) docs page.
80
+
81
+ Sysadmins enable at most one customization per organization (radio). Bundled authorization workflows appear under **System → Authorizations** only while that customization is enabled; switching or choosing none also unselects those handlers.
82
+
83
+ ## Upgrading from field sets to customizations
84
+
85
+ User values in `extended_data` and existing authorization grants are unchanged when field and handler names stay the same.
86
+
87
+ Toggle config migrates from `{ "active_field_set": "default" }` to `{ "enabled_customization": "default" }`.
88
+
89
+ On deploy, `bin/rails decidim:upgrade` (or `decidim:update`) runs `decidim_custom_user_fields:upgrade:migrate_toggle_config` automatically.
90
+
91
+ Until that runs, the module still reads `active_field_set` and legacy `{name}_enabled` flags when `enabled_customization` is absent.
92
+
93
+ Manual run:
94
+
95
+ ```bash
96
+ bin/rails decidim_custom_user_fields:upgrade:migrate_toggle_config
97
+ DRY_RUN=1 bin/rails decidim_custom_user_fields:upgrade:migrate_toggle_config
98
+ ```
99
+
100
+ Refactor your initializer from `register_field_set` to `register_customization`, keeping the same customization name as the former field set (e.g. `:default`). Unknown or removed DSL calls (`custom_fields`, `register_field_set`, `configure { add_field }`, typos) raise `Decidim::CustomUserFields::Error` with a migration or DidYouMean hint.
101
+
102
+ ## Try Omniauth locally (Zitadel + Docker)
103
+
104
+ Local review of this gem (clone this repo). Integrators adding the gem to an existing Decidim app: see **Install the module** above.
105
+
106
+ **Prerequisite:** Docker and Docker Compose.
107
+
108
+ ### Quick start
109
+
110
+ 1. Clone this repository.
111
+ 2. `./bin/dev-oidc-up`
112
+ 3. Wait (~5–10 min the first time).
113
+ 4. Open http://localhost:3000.
114
+
115
+ Unlike `spam_signal` / `voca`, default `docker compose up` does **not** start a Decidim process; do not use `docker compose up -d decidim` for first success.
116
+
117
+ Idempotent: safe to re-run when the stack is already up.
118
+
119
+ ### What `./bin/dev-oidc-up` does
120
+
121
+ | Step | Action | Output |
122
+ |------|--------|--------|
123
+ | 1 | Start Zitadel stack | Zitadel on :8080, admin PAT in `docker/zitadel/bootstrap/admin.pat` |
124
+ | 2 | Configure Zitadel SMTP + OIDC app | `docker/zitadel/oidc.env` (gitignored) |
125
+ | 3 | `rake test_app` (first time only) | Dummy app in `spec/decidim_dummy_app` |
126
+ | 4 | `dev:prepare_secrets`, `db:drop db:create db:migrate` | Empty Decidim schema |
127
+ | 5 | `dev:seed` | Minimal org + admin + Zitadel provider + **association** scenario |
128
+ | 6 | `docker compose --profile dev run --rm --service-ports decidim … rails s` | Rails on http://localhost:3000 (foreground) |
129
+
130
+ ### URLs and credentials
131
+
132
+ | URL | Purpose |
133
+ |-----|---------|
134
+ | http://localhost:3000 | Decidim dummy app |
135
+ | http://localhost:8080 | Zitadel console |
136
+ | http://localhost:1080 | MailCatcher (emails) |
137
+
138
+ | Account | Login |
139
+ |---------|-------|
140
+ | Decidim admin | `admin@example.org` / `decidim123456789` |
141
+ | Zitadel admin | `admin@zitadel.localhost` / `Password1!` |
142
+
143
+ ### Experiment: OIDC sign-in → custom fields
144
+
145
+ 1. Open http://localhost:8080, sign in as Zitadel admin.
146
+ 2. **Users → New** — create a test user (email + password).
147
+ 3. Open http://localhost:3000/users/sign_in, click **Zitadel**, sign in with that user.
148
+ 4. Complete the profile form — the association demo shows the “I represent an association” checkbox.
149
+ 5. Optional: verify authorization at http://localhost:3000/account/authorizations/new?handler=association_only (after checking the boolean on registration).
150
+
151
+ Change the seeded scenario (e.g. optional birthdate + age authorizations):
152
+
153
+ ```bash
154
+ DEV_CUSTOMIZATION=birthdate_age_gates ./bin/dev-oidc-up
155
+ ```
156
+
157
+ ### Environment variables (local OIDC)
158
+
159
+ | Variable | Default | Required | Purpose |
160
+ |----------|---------|----------|---------|
161
+ | `DEV_CUSTOMIZATION` | `association` | no | Scenario customization enabled by `dev:seed` (`association`, `birthdate_age_gates`, `location_validation`) |
162
+ | `DECIDIM_HOST` | `localhost` | no | Organization host matched by `dev:seed` |
163
+ | `ZITADEL_OIDC_ENABLED` | set in compose | no | Loads dev OIDC middleware + scenario customizations |
164
+ | `ZITADEL_URL` | `http://localhost:8080` | no | Zitadel API base URL used by `dev-oidc-up` |
165
+ | `OIDC_ISSUER` | `http://localhost:8080` | no | Public issuer written to `oidc.env` |
166
+ | `OIDC_REDIRECT_URI` | `http://localhost:3000/users/auth/openid_connect/callback` | no | OIDC redirect URI for the Zitadel app |
167
+
168
+ Generated at setup time (do not commit): `docker/zitadel/oidc.env` — see `docker/zitadel/oidc.env.example`.
169
+
170
+ ### Troubleshooting
171
+
172
+ | Symptom | Fix |
173
+ |---------|-----|
174
+ | No **Zitadel** button on sign-in | Re-run `./bin/dev-oidc-up` |
175
+ | `Missing docker/zitadel/oidc.env` | Re-run `./bin/dev-oidc-up` (Zitadel must be ready first) |
176
+ | `Missing PAT at docker/zitadel/bootstrap/admin.pat` | `docker compose down -v` then `./bin/dev-oidc-up` |
177
+ | Zitadel not ready yet | Wait 1–2 min on first boot; check http://localhost:8080/debug/ready |
178
+ | `connection refused` to `localhost:8080` on Zitadel login | Re-run `./bin/dev-oidc-up` — token/userinfo must use `zitadel:8080` inside Docker |
179
+ | Zitadel login fails with `"Unknown"` / `Instance not found` | Ensure `ZITADEL_PUBLIC_HOST=localhost:8080` on the Decidim container (Host header for internal calls) |
180
+ | OIDC completes custom fields but asks for email confirmation | Re-run `./bin/dev-oidc-up` after updating the gem — OpenID Connect verified emails should sign you in directly |
181
+ | Port 3000 empty | Do not use plain `docker compose up` or `docker compose up -d decidim` — use `./bin/dev-oidc-up` |
182
+ | `cd: spec/decidim_dummy_app: No such file or directory` (or missing `config/application.rb`) | Optional `docker compose down -v`, then `./bin/dev-oidc-up` (script runs `rake test_app` before any dummy `cd`) |
183
+ | `network … not found` when starting Rails | Stale compose network after one-shots — re-run `./bin/dev-oidc-up` (script no longer uses `profile web up`) |
184
+ | `decidim_users already exists` on setup | Re-run `docker compose down -v && ./bin/dev-oidc-up` (script runs `db:drop db:create db:migrate`) |
185
+ | `A server is already running` / stale `server.pid` | `rm -f spec/decidim_dummy_app/tmp/pids/server.pid` then re-run `./bin/dev-oidc-up` |
186
+
187
+ Reset all local data: `docker compose down -v` then `./bin/dev-oidc-up`.
188
+
189
+ ## How to add a custom user field
190
+
191
+ Create an initializer `config/initializers/custom_user_fields.rb`:
192
+
193
+ ```ruby
194
+ Decidim::CustomUserFields.register_customization :default do |customization|
195
+ customization.registration_fields do |set|
196
+ set.add_field :birthdate, type: :date, required: true
197
+ set.add_field :address, type: :textarea, required: false, rows: 10
198
+ set.add_field :purpose, type: :text, required: false
199
+ end
200
+ end
201
+ ```
202
+ ## Renewable verifications
203
+ To set an authorization as renewable, register it inside a customization and use `renewable!(time_between_renew)`:
204
+ ```
205
+ Decidim::CustomUserFields.register_customization :pb2024 do |customization|
206
+ customization.registration_fields { |set| set.add_field :foo, type: :text }
207
+ customization.authorization "PB2024" do |config|
208
+ config.renewable!(2.days) # Will need to renew authorization after 2 days.
209
+ end
56
210
  end
57
211
  ```
212
+ This will have no effect under < 30, or without the `decidim-ephemerable` gem.
58
213
 
59
214
 
215
+
216
+ ## Ephemerable verifications
217
+ To set an authorization as ephemerable, register it inside a customization and use `ephemerable!`:
218
+ ```
219
+ Decidim::CustomUserFields.register_customization :pb2024 do |customization|
220
+ customization.registration_fields { |set| set.add_field :foo, type: :text }
221
+ customization.authorization "PB2024" do |config|
222
+ config.ephemerable!
223
+ config.renewable!(1.day) # The ephemerable will be valid for a day
224
+ end
225
+ end
226
+ ```
227
+ This will have no effect under < 30, or without the `decidim-ephemerable` gem.
228
+
60
229
  ### Available field types
61
230
 
62
231
  **`:date`**
63
232
 
64
233
  parameters:
65
234
  * `required`: must choose a date
66
- * `min`: date in ISO8601 where the user cannot select before
67
- * `max`: date in ISO8601 where the user cannot select after
235
+ * `not_before`: date in ISO8601 where the user cannot select before
236
+ * `not_after`: date in ISO8601 where the user cannot select after
237
+ * `skip_hashing`: Do not hash the field result (watch out privacy concerns)
68
238
 
69
239
  **`:textarea`**
240
+ a textarea field, which the content will be trimmed (no whitespaces before and after).
70
241
 
71
242
  parameters:
72
243
  * `required`: if the field is required
73
- * `min`: minimal length
74
- * `max`: maximal length
75
- * `rows`: how many rows the field should display
244
+ * `min`: minimal text length
245
+ * `max`: maximal text length
246
+ * `skip_hashing`: Do not hash the field result (watch out privacy concerns)
247
+ * `ui.rows`: how many rows the field should display
76
248
 
77
249
  **`:text`**
250
+ a text field, which the content will be trimmed (no whitespaces before and after).
78
251
 
79
252
  parameters:
80
253
  * `required`: if the field is required
254
+ * `values_in`: restrict the values accepted for the field
255
+ * `format`: a regex (like `format: /\A[A-z0-9]*\z/)
256
+ * `skip_hashing`: Do not hash the field result (watch out privacy concerns)
257
+
258
+ **`:extra_field_ref` (Authorization only)**
259
+ > This field is a quiet special one, it allows workflows where
260
+ > the extra_field on registration is optional, but get required on authorization.
261
+ > Warning: this field can be use only in authorization configuration
262
+
263
+ parameters:
264
+ * `ref`: the extra_field reference name
265
+ * `hide_if_value`: hide the extra field if there is already a value.
266
+ * `skip_hashing`: Do not hash the field result (watch out privacy concerns)
267
+ * `skip_update_on_verified`: Do not update the extra_field reference when verified
268
+ * `...`: any other parameters to override the reference option
269
+
81
270
 
82
271
  ### Labels
83
272
  Labels are translated and are under the translation scope `decidim.custom_user_fields`.
84
- Example of a `fr.yml` file:
273
+ Example of a `config/locales/fr.yml` file:
274
+
275
+ ```yml
276
+ fr:
277
+ decidim:
278
+ authorization_handlers:
279
+ pb2024:
280
+ name: "Données de rescencement pour le Budget Participatif 2024"
281
+ explanation: "Donnée récoltée pour participer au BP 2024"
282
+ custom_user_fields:
283
+ extended_data:
284
+ first_name:
285
+ label: "Prénoms"
286
+ last_name:
287
+ label: "Nom de famille"
288
+ pb2024:
289
+ birthdate:
290
+ label: "Date de naissance"
291
+ help_text: "La participation est réservée aux personnes de plus de 18ans"
292
+ postal_code:
293
+ label: "Code postal"
294
+ help_text: "La participation est réservée aux habitants de MaCommune.
295
+ ```
296
+
297
+ # Create an authorization with custom fields
298
+
299
+ ```ruby
300
+ Decidim::CustomUserFields.register_customization :pb2024 do |customization|
301
+ customization.registration_fields do |set|
302
+ set.add_field :first_name, type: :text, required: false
303
+ set.add_field :last_name, type: :text, required: false
304
+ end
305
+
306
+ customization.authorization "PB2024" do |config|
307
+ config.ephemerable!
308
+ config.add_field :first_name, type: :extra_field_ref, required: true, skip_hashing: true, hide_if_value: true
309
+ config.add_field :last_name, type: :extra_field_ref, required: true, skip_hashing: true, hide_if_value: true
310
+ config.add_field :birthdate, type: :date, required: true, not_after: 18.years.ago.to_date.iso8601
311
+ config.add_field :postal_code, type: :text, required: true, format: /\A[\-0-9]*\z/, values_in: ["2000", "2001", "2002"]
312
+ end
313
+ end
314
+ ```
85
315
 
316
+ Then, add locales for this flow:
86
317
  ```yml
87
318
  fr:
88
- activemodel:
89
- attributes:
90
- user:
91
- birthday: "Birthday"
92
- address: "Address"
93
- purpose: "Reason for Being Here"
94
- province: "Canton"
95
319
  decidim:
320
+ authorization_handlers:
321
+ pb2024:
322
+ name: "Participer au BP 2024"
323
+ explanation: "Valider votre compte"
96
324
  custom_user_fields:
97
- province:
98
- options:
99
- vd: "Vaud"
100
- vs: "Valais"
101
- ar: "Aarau"
325
+ first_name:
326
+ label: Prénoms
327
+ help_text: Ce champs est requis pour participer aux budgets participatif.
328
+ last_name:
329
+ label: Nom de famille
330
+ pb2024:
331
+ birthdate:
332
+ label: Date de naissance
333
+ bad_not_after: Seul les > 18 peuvent participer
334
+ postal_code:
335
+ label: Code postal
336
+ bad_values: Ce code postal est inconu dans maCommune
337
+ help_text: Seul les communies prêt de maCommune peux être acceptée
102
338
  ```
103
339
 
104
- ## Run locally
105
- To run this module locally, we use Docker-compose:
340
+ ## Module development (specs, shell)
341
+
342
+ For **OIDC experimentation**, use [Try Omniauth locally](#try-omniauth-locally-zitadel--docker) above.
343
+
344
+ The `decidim` compose service is the OIDC/dev container (shell or Rails). `./bin/dev-oidc-up` starts Rails with `compose run --service-ports`.
345
+
346
+ ```bash
347
+ # Interactive shell in the dev container
348
+ docker compose --profile dev run --rm decidim bash
349
+
350
+ # CI-shaped specs (default Gemfile, no ephemeral gem)
351
+ docker compose -f docker-compose.ci.yml run --rm rspec
352
+
353
+ # Same specs with decidim-ephemeral_participation (Appraisal `with_ephemeral`)
354
+ docker compose -f docker-compose.ci.yml run --rm -e BUNDLE_GEMFILE=/app/gemfiles/with_ephemeral.gemfile rspec
355
+ ```
356
+
357
+ ### Advanced — dummy already generated
358
+
359
+ Use these only after `spec/decidim_dummy_app/config/application.rb` exists (`./bin/dev-oidc-up` or `rake test_app`).
106
360
 
107
361
  ```bash
108
- docker-compose up
362
+ # Rails only (infra must already be up — e.g. after a partial setup)
363
+ docker compose --profile dev run --rm --service-ports decidim bash -c \
364
+ "cd /home/module/spec/decidim_dummy_app && rm -f tmp/pids/server.pid && bundle exec rails server -b 0.0.0.0 -p 3000"
365
+
366
+ # Specs (from repo root, dummy app Gemfile)
367
+ docker compose --profile dev run --rm decidim bash -c \
368
+ "cd /home/module && BUNDLE_GEMFILE=spec/decidim_dummy_app/Gemfile bundle exec rspec"
109
369
  ```
110
- This will run a decidim-user_fields container, which sleeps.
111
- You can run any command you want in the running container, like:
112
370
 
113
- | Command | Description |
114
- |--------------------------------------------------------------------------------------------------|-----------------------------------------------------|
115
- | `docker-compose exec -it decidim-user_fields bundle exec rails db:seed` | Seed the database (run on localhost:3000) |
116
- | `docker-compose exec -it decidim-user_fields bin/webpack-dev-server` | Compile assets and watch for changes |
117
- | `docker-compose exec -it decidim-user_fields bin/rails s -b 0.0.0.0` | Run the Rails server in development |
118
- | `docker-compose exec -it decidim-user_fields bundle exec rspec /home/decidim/module/spec` | Run tests for the module |
119
- | `docker-compose exec -it decidim-user_fields bundle exec rubocop -a /home/decidim/module` | Correct lint errors with RuboCop |
120
- | `docker-compose exec -it decidim-user_fields bash` | Navigate your container in bash |
371
+ Regenerate the dummy app when switching `BUNDLE_GEMFILE` (`rake test_app` in that bundle). Default CI has no ephemeral gem; `ruby::rspec-ephemeral` installs `decidim-ephemeral_participation` `v0.0.9`.
121
372
 
373
+ Useful paths inside the container:
122
374
 
375
+ | Path | Purpose |
376
+ |------|---------|
377
+ | `/home/module` | This gem (mounted from the repo) |
378
+ | `/home/module/spec/decidim_dummy_app` | Dummy Decidim app used for local OIDC + specs |
123
379
 
124
- While developing locally, you have two environment variables that can help you:
380
+ Environment variables in the dev image:
125
381
 
126
- * `ROOT`: the root of the application using the module
127
- * `MODULE_ROOT`: the place where your gem code is.
382
+ | Variable | Purpose |
383
+ |----------|---------|
384
+ | `DEV_MODULE` | Gem under development (`decidim-user_fields`) |
385
+ | `DECIDIM_VERSION` | Decidim release used by the dummy app |
386
+ | `BUNDLE_GEMFILE` | Bundler gemfile. Empty uses `./Gemfile` (no ephemeral). `gemfiles/with_ephemeral.gemfile` adds `decidim-ephemeral_participation`. |
data/Rakefile ADDED
@@ -0,0 +1,253 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "decidim/dev/common_rake"
5
+
6
+ # Dummy generation boots Rails (`app:template`) before this task can patch files.
7
+ def dummy_shakapacker_yml_source
8
+ core = Gem.loaded_specs["decidim-core"]&.full_gem_path
9
+ from_core = core && File.join(core, "lib/decidim/shakapacker/shakapacker.yml")
10
+ return from_core if from_core && File.exist?(from_core)
11
+
12
+ File.join(Gem.loaded_specs.fetch("shakapacker").full_gem_path, "lib/install/config/shakapacker.yml")
13
+ end
14
+
15
+ def copy_dummy_shakapacker_yml!(dummy_root)
16
+ dest = File.join(dummy_root, "config/shakapacker.yml")
17
+ return if File.exist?(dest) && !File.empty?(dest)
18
+ return unless File.directory?(File.join(dummy_root, "config"))
19
+
20
+ FileUtils.cp(dummy_shakapacker_yml_source, dest)
21
+ end
22
+
23
+ def poll_dummy_shakapacker_yml(dummy_root, stop)
24
+ thread = Thread.new do
25
+ until stop.call
26
+ copy_dummy_shakapacker_yml!(dummy_root)
27
+ sleep 0.1
28
+ end
29
+ end
30
+ thread.abort_on_exception = true
31
+ thread
32
+ end
33
+
34
+ def with_dummy_shakapacker_yml(dummy_root)
35
+ stop = false
36
+ thread = poll_dummy_shakapacker_yml(dummy_root, -> { stop })
37
+ yield
38
+ ensure
39
+ stop = true
40
+ thread&.join
41
+ copy_dummy_shakapacker_yml!(dummy_root)
42
+ end
43
+
44
+ def inject_toggle_into_dummy_gemfile!(dummy_root)
45
+ gemfile = File.join(dummy_root, "Gemfile")
46
+ return unless File.exist?(gemfile)
47
+
48
+ contents = File.read(gemfile).sub(dummy_toggle_gem_pattern, "")
49
+ File.write(gemfile, "#{contents.rstrip}\n\n#{dummy_toggle_gem_declaration}\n")
50
+ end
51
+
52
+ def dummy_toggle_gem_pattern
53
+ /^[ \t]*gem ["']decidim-toggle["'][^\n]*(?:\n[ \t]+(?:git:|github:|branch:|tag:|path:|ref:)[^\n]*)*\n?/
54
+ end
55
+
56
+ def dummy_toggle_gem_declaration
57
+ <<~RUBY.strip
58
+ gem "decidim-toggle",
59
+ git: "https://gitlab.com/octree_gva/decidim/vocacity/decidim-modules/decidim-toggle.git",
60
+ branch: "main"
61
+ RUBY
62
+ end
63
+
64
+ def install_module(path)
65
+ Dir.chdir(path) do
66
+ Bundler.with_unbundled_env do
67
+ sh "bundle exec rails decidim_toggle:install:migrations"
68
+ next unless File.read("Gemfile").include?("decidim-ephemeral_participation")
69
+
70
+ sh "bundle exec rails decidim_ephemeral_participation:install:migrations"
71
+ end
72
+ end
73
+ end
74
+
75
+ def ephemeral_gem_in_bundle?
76
+ Bundler.definition.dependencies.any? { |dep| dep.name == "decidim-ephemeral_participation" }
77
+ end
78
+
79
+ def inject_ephemeral_into_dummy_gemfile!(dummy_root, enabled: ephemeral_gem_in_bundle?)
80
+ return unless enabled
81
+
82
+ gemfile = File.join(dummy_root, "Gemfile")
83
+ contents = File.read(gemfile)
84
+ return if contents.include?("decidim-ephemeral_participation")
85
+
86
+ File.open(gemfile, "a") do |file|
87
+ file.puts <<~RUBY
88
+
89
+ gem "decidim-ephemeral_participation",
90
+ git: "https://gitlab.com/octree_gva/decidim/vocacity/decidim-modules/decidim-ephemeral_participation.git",
91
+ tag: "v0.0.9"
92
+ RUBY
93
+ end
94
+ end
95
+
96
+ def seed_db(path)
97
+ Dir.chdir(path) do
98
+ raise "db:seed failed" unless system("bundle exec rails db:seed")
99
+ end
100
+ end
101
+
102
+ desc "Prepare for testing"
103
+ task :prepare_tests do
104
+ # Remove previous existing db, and recreate one.
105
+ disable_docker_compose = ENV.fetch("DISABLED_DOCKER_COMPOSE", "false") == "true"
106
+ unless disable_docker_compose
107
+ sh "docker compose -f docker-compose.yml down -v"
108
+ sh "docker compose -f docker-compose.yml up -d --remove-orphans"
109
+ end
110
+ ENV["RAILS_ENV"] = "test"
111
+ test_db = {
112
+ "adapter" => "postgresql",
113
+ "encoding" => "unicode",
114
+ "host" => ENV.fetch("DATABASE_HOST", "localhost"),
115
+ "port" => ENV.fetch("DATABASE_PORT", "5432").to_i,
116
+ "username" => ENV.fetch("DATABASE_USERNAME", "decidim"),
117
+ "password" => ENV.fetch("DATABASE_PASSWORD", "TEST-baeGhi4Ohtahcee5eejoaxaiwaezaiGo"),
118
+ "database" => "decidim_test",
119
+ # GitLab/docker Postgres services rarely offer TLS on the internal hostname
120
+ "sslmode" => ENV.fetch("DATABASE_SSLMODE", "disable"),
121
+ # Ephemeral migration changes available_authorizations array → jsonb then updates rows.
122
+ "prepared_statements" => false
123
+ }
124
+ # Dummy app is CI-only; Spring / bin/rails often boot `development` even when we intend `test`.
125
+ # Mirror `test` so `db:migrate` never dies on missing `development` (see ActiveRecord::AdapterNotSpecified).
126
+ database_yml = {
127
+ "development" => test_db.dup,
128
+ "test" => test_db.dup
129
+ }
130
+
131
+ config_file = File.expand_path("spec/decidim_dummy_app/config/database.yml", __dir__)
132
+ File.open(config_file, "w") { |f| YAML.dump(database_yml, f) }
133
+ Dir.chdir("spec/decidim_dummy_app") do
134
+ # Strip Bundler’s parent-project env (`BUNDLE_GEMFILE`, etc. from `bundle exec rake` at engine root)
135
+ # so `bundle exec` in the dummy app resolves that Gemfile. Use `with_unbundled_env`, not only
136
+ # `with_original_env` (Bundler docs: subcommands in another directory).
137
+ Bundler.with_unbundled_env do
138
+ # Fresh DB every run: regenerating the dummy app rewrites migration timestamps, so an
139
+ # existing schema (local compose volume / re-run) would hit PG::DuplicateTable.
140
+ # Use Rake `sh` so a failed migrate aborts; `env` sets vars in the shell (reliable vs Kernel#system env quirks).
141
+ sh "env RAILS_ENV=test DISABLE_SPRING=1 DISABLE_DATABASE_ENVIRONMENT_CHECK=1 bundle exec rails db:drop db:create db:migrate"
142
+ end
143
+ end
144
+ end
145
+
146
+ def root_gemfile_path
147
+ File.expand_path("Gemfile", __dir__)
148
+ end
149
+
150
+ # Appraisal jobs set BUNDLE_GEMFILE. AppGenerator copies the engine-root Gemfile, not that path.
151
+ def alternate_bundle_gemfile
152
+ gemfile = ENV.fetch("BUNDLE_GEMFILE", nil)
153
+ return if gemfile.blank?
154
+
155
+ expanded = File.expand_path(gemfile)
156
+ return if expanded == root_gemfile_path
157
+ return unless File.exist?(expanded) && File.exist?("#{expanded}.lock")
158
+
159
+ expanded
160
+ end
161
+
162
+ # Dummy app lives in spec/decidim_dummy_app; the engine is ../.. from there.
163
+ # The ephemeral lock records PATH remote: /app (compose mount), which is wrong in CI.
164
+ def lockfile_for_dummy_app(contents)
165
+ contents.sub(/^PATH\n remote: .+$/, "PATH\n remote: ../..")
166
+ end
167
+
168
+ def with_active_gemfile_staged
169
+ backup_gemfile = nil
170
+ backup_lock = nil
171
+ root_gemfile = root_gemfile_path
172
+ root_lock = "#{root_gemfile}.lock"
173
+ gemfile = alternate_bundle_gemfile
174
+ if gemfile
175
+ backup_gemfile = File.read(root_gemfile)
176
+ backup_lock = File.binread(root_lock) if File.exist?(root_lock)
177
+ File.write(root_gemfile, File.read(gemfile))
178
+ File.binwrite(root_lock, lockfile_for_dummy_app(File.binread("#{gemfile}.lock")))
179
+ end
180
+ yield
181
+ ensure
182
+ if backup_gemfile
183
+ File.write(root_gemfile, backup_gemfile)
184
+ if backup_lock
185
+ File.binwrite(root_lock, backup_lock)
186
+ else
187
+ FileUtils.rm_f(root_lock)
188
+ end
189
+ end
190
+ end
191
+
192
+ # `--demo` copies initiative handlers that eager-load Decidim::Initiatives.
193
+ # This Gemfile does not load that component.
194
+ def strip_optional_initiatives_stubs!(dummy_root)
195
+ %w(
196
+ app/services/dummy_signature_handler.rb
197
+ app/services/dummy_sms_mobile_phone_validator.rb
198
+ config/initializers/decidim_initiatives.rb
199
+ app/views/decidim/initiatives
200
+ ).each { |relative| FileUtils.rm_rf(File.join(dummy_root, relative)) }
201
+ end
202
+
203
+ desc "Generates a dummy app for testing"
204
+ task :test_app do
205
+ dummy_root = File.expand_path("spec/decidim_dummy_app", __dir__)
206
+ needs_ephemeral = ephemeral_gem_in_bundle?
207
+ with_dummy_shakapacker_yml(dummy_root) do
208
+ with_active_gemfile_staged do
209
+ Bundler.with_original_env do
210
+ ENV.delete("BUNDLE_GEMFILE")
211
+ generate_decidim_app(
212
+ "spec/decidim_dummy_app",
213
+ "--app_name",
214
+ "decidim_test",
215
+ "--path",
216
+ "../..",
217
+ "--skip_spring",
218
+ "--demo",
219
+ "--force_ssl",
220
+ "false",
221
+ "--locales",
222
+ "en,ca,es,fr"
223
+ )
224
+ end
225
+ end
226
+ end
227
+ strip_optional_initiatives_stubs!(dummy_root)
228
+ inject_ephemeral_into_dummy_gemfile!(dummy_root, enabled: needs_ephemeral)
229
+ inject_toggle_into_dummy_gemfile!(dummy_root)
230
+ # Install under with_unbundled_env before install_module (needs `bundle exec rails`).
231
+ Dir.chdir(File.expand_path("spec/decidim_dummy_app", __dir__)) do
232
+ Bundler.with_unbundled_env do
233
+ sh "bundle install -j $(nproc) --retry 3"
234
+ end
235
+ end
236
+ install_module("spec/decidim_dummy_app")
237
+ Rake::Task["prepare_tests"].invoke
238
+ end
239
+
240
+ desc "Generates a development app"
241
+ task :development_app do
242
+ Bundler.with_original_env do
243
+ generate_decidim_app(
244
+ "development_app",
245
+ "--app_name",
246
+ "#{base_app_name}_development_app",
247
+ "--path",
248
+ "..",
249
+ "--recreate_db",
250
+ "--demo"
251
+ )
252
+ end
253
+ end