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.
- checksums.yaml +4 -4
- data/README.md +308 -49
- data/Rakefile +253 -0
- data/app/forms/decidim/custom_user_fields/admin/customizations_config_form.rb +67 -0
- data/app/forms/decidim/custom_user_fields/verifications/verification_form.rb +85 -0
- data/app/overrides/decidim/devise/invitations/edit/extra_user_fields_form.html.erb.deface +2 -2
- data/app/overrides/decidim/devise/omniauth_registrations/new/extra_user_fields.html.erb.deface +3 -0
- data/app/overrides/invitation_override.rb +31 -0
- data/app/packs/entrypoints/decidim_custom_user_fields.js +0 -0
- data/app/packs/stylesheets/decidim/custom_user_fields/_decidim_custom_user_fields.scss +48 -0
- data/app/views/decidim/custom_user_fields/_profile_form.html.erb +2 -2
- data/app/views/decidim/custom_user_fields/_registration_form.html.erb +2 -2
- data/app/views/decidim/custom_user_fields/_verification_form.erb +4 -0
- data/app/views/decidim/custom_user_fields/system/_customizations_tab.html.erb +15 -0
- data/config/assets.rb +9 -0
- data/config/i18n-tasks.yml +22 -0
- data/config/locales/decidim_custom_user_fields_scenarios.en.yml +50 -0
- data/config/locales/decidim_custom_user_fields_scenarios.fr.yml +48 -0
- data/config/locales/decidim_toggle.en.yml +13 -0
- data/config/locales/decidim_toggle.fr.yml +13 -0
- data/config/locales/decidim_user_fields.en.yml +25 -0
- data/config/locales/decidim_user_fields.fr.yml +25 -0
- data/lib/decidim/custom_user_fields/custom_user_fields.rb +10 -14
- data/lib/decidim/custom_user_fields/customization_field_naming.rb +17 -0
- data/lib/decidim/custom_user_fields/customizations/builder.rb +48 -0
- data/lib/decidim/custom_user_fields/customizations/customization.rb +29 -0
- data/lib/decidim/custom_user_fields/customizations.rb +42 -0
- data/lib/decidim/custom_user_fields/decidim_integrations.rb +52 -0
- data/lib/decidim/custom_user_fields/definition_guidance.rb +109 -0
- data/lib/decidim/custom_user_fields/dev/bootstrap.rb +102 -0
- data/lib/decidim/custom_user_fields/dev/local_oidc_seeder.rb +150 -0
- data/lib/decidim/custom_user_fields/dev/omniauth_providers_patcher.rb +68 -0
- data/lib/decidim/custom_user_fields/dev/openid_connect_setup.rb +110 -0
- data/lib/decidim/custom_user_fields/dev/scenario_customizations.rb +72 -0
- data/lib/decidim/custom_user_fields/engine.rb +59 -26
- data/lib/decidim/custom_user_fields/extended_data.rb +52 -0
- data/lib/decidim/custom_user_fields/field_definition.rb +28 -6
- data/lib/decidim/custom_user_fields/fields/boolean_field.rb +51 -0
- data/lib/decidim/custom_user_fields/fields/date_field.rb +80 -10
- data/lib/decidim/custom_user_fields/fields/dummy_field.rb +40 -0
- data/lib/decidim/custom_user_fields/fields/extra_field_ref_field.rb +118 -0
- data/lib/decidim/custom_user_fields/fields/generic_field.rb +99 -6
- data/lib/decidim/custom_user_fields/fields/text_area_field.rb +34 -9
- data/lib/decidim/custom_user_fields/fields/text_field.rb +47 -6
- data/lib/decidim/custom_user_fields/i18n/customization_keys.rb +55 -0
- data/lib/decidim/custom_user_fields/i18n/customization_keys_scanner.rb +80 -0
- data/lib/decidim/custom_user_fields/i18n/tasks.rb +7 -0
- data/lib/decidim/custom_user_fields/overrides/authorizations_controller.rb +18 -0
- data/lib/decidim/custom_user_fields/overrides/command.rb +23 -21
- data/lib/decidim/custom_user_fields/overrides/confirmations_controller.rb +15 -0
- data/lib/decidim/custom_user_fields/overrides/devise_authentication_methods.rb +68 -0
- data/lib/decidim/custom_user_fields/overrides/form_definition.rb +25 -8
- data/lib/decidim/custom_user_fields/overrides/invitations_controller.rb +109 -0
- data/lib/decidim/custom_user_fields/overrides/omniauth_command.rb +40 -0
- data/lib/decidim/custom_user_fields/overrides/omniauth_registrations_controller.rb +93 -0
- data/lib/decidim/custom_user_fields/overrides/user.rb +16 -0
- data/lib/decidim/custom_user_fields/registration_fields.rb +99 -0
- data/lib/decidim/custom_user_fields/update_customizations_config_command.rb +39 -0
- data/lib/decidim/custom_user_fields/upgrade/migrate_toggle_config.rb +74 -0
- data/lib/decidim/custom_user_fields/verifications/builder.rb +100 -0
- data/lib/decidim/custom_user_fields/verifications/verifications.rb +82 -0
- data/lib/decidim/custom_user_fields/version.rb +4 -2
- data/lib/decidim/user_fields.rb +46 -13
- data/lib/tasks/decidim/custom_user_fields/custom_user_fields_upgrade.rake +16 -0
- data/lib/tasks/decidim/custom_user_fields/custom_user_fields_webpacker.rake +59 -0
- data/lib/tasks/decidim/custom_user_fields/decidim_custom_user_fields_upgrade_tasks.rake +16 -0
- data/lib/tasks/decidim/custom_user_fields/dev/local_setup.rake +16 -0
- metadata +80 -15
- data/app/overrides/decidim/devise/invitations/edit/extra_user_fields.html.erb.deface +0 -3
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module CustomUserFields
|
|
5
|
+
module ScenarioCustomizations
|
|
6
|
+
class << self
|
|
7
|
+
def register!
|
|
8
|
+
return if Customizations.find(:birthdate_age_gates)
|
|
9
|
+
|
|
10
|
+
register_birthdate_age_gates!
|
|
11
|
+
register_location_validation!
|
|
12
|
+
register_association!
|
|
13
|
+
refresh_form_definitions!
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def refresh_form_definitions!
|
|
17
|
+
[
|
|
18
|
+
Decidim::RegistrationForm,
|
|
19
|
+
Decidim::OmniauthRegistrationForm,
|
|
20
|
+
Decidim::AccountForm
|
|
21
|
+
].each { |form_class| FormDefinition.setup_form_class(form_class) }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def register_birthdate_age_gates!
|
|
27
|
+
Decidim::CustomUserFields.register_customization(:birthdate_age_gates) do |customization|
|
|
28
|
+
customization.registration_fields do |set|
|
|
29
|
+
set.add_field :birthdate, type: :date, required: false
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
{ "TwelvePlus" => 12, "SixteenPlus" => 16, "EighteenPlus" => 18 }.each do |authorization_name, years|
|
|
33
|
+
customization.authorization authorization_name do |config|
|
|
34
|
+
config.add_field :birthdate,
|
|
35
|
+
type: :extra_field_ref,
|
|
36
|
+
ref: :birthdate,
|
|
37
|
+
required: true,
|
|
38
|
+
not_after: years.years.ago.to_date.iso8601
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def register_location_validation!
|
|
45
|
+
Decidim::CustomUserFields.register_customization(:location_validation) do |customization|
|
|
46
|
+
customization.authorization "LocationValidated" do |config|
|
|
47
|
+
config.add_field :birthdate, type: :text, required: true, format: /\A\d{2}\.\d{2}\.\d{4}\z/
|
|
48
|
+
config.add_field :postal_code, type: :text, required: true
|
|
49
|
+
config.add_field :full_name, type: :text, required: true
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def register_association!
|
|
55
|
+
Decidim::CustomUserFields.register_customization(:association) do |customization|
|
|
56
|
+
customization.registration_fields do |set|
|
|
57
|
+
set.add_field :represent_association, type: :boolean, required: true
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
customization.authorization "AssociationOnly" do |config|
|
|
61
|
+
config.add_field :represent_association,
|
|
62
|
+
type: :extra_field_ref,
|
|
63
|
+
ref: :represent_association,
|
|
64
|
+
required: true,
|
|
65
|
+
must_be_true: true
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
require
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails"
|
|
4
|
+
require "decidim/core"
|
|
5
|
+
require "decidim/toggle"
|
|
6
|
+
require "deface"
|
|
4
7
|
|
|
5
8
|
module Decidim
|
|
6
9
|
module CustomUserFields
|
|
@@ -12,30 +15,60 @@ module Decidim
|
|
|
12
15
|
# root to: "custom_user_fields#index"
|
|
13
16
|
end
|
|
14
17
|
|
|
15
|
-
|
|
18
|
+
# Avoid Zeitwerk::NameError by copying this override from Deface (no longer works after Rails 7.1 because of API changes)
|
|
19
|
+
initializer "decidim_custom_user_fields.zeitwerk_ignore_overrides",
|
|
20
|
+
before: :set_load_path do |app|
|
|
21
|
+
next if Rails.configuration.respond_to?(:autoloader) && Rails.configuration.autoloader != :zeitwerk
|
|
22
|
+
|
|
23
|
+
overrides_path = Engine.root.join("app/overrides")
|
|
24
|
+
app.config.eager_load_paths.delete(overrides_path)
|
|
25
|
+
Rails.autoloaders.each { |autoloader| autoloader.ignore(overrides_path) }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
initializer "decidim_custom_user_fields.registration_additions" do
|
|
16
29
|
config.to_prepare do
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
30
|
+
DecidimIntegrations.apply!
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
initializer "decidim_custom_user_fields.dev_openid_connect",
|
|
35
|
+
after: :load_config_initializers do
|
|
36
|
+
next unless defined?(Decidim::CustomUserFields::Dev::OpenidConnectSetup)
|
|
37
|
+
|
|
38
|
+
Decidim::CustomUserFields::Dev::OpenidConnectSetup.register_middleware!
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
initializer "decidim_custom_user_fields.dev_scenarios" do
|
|
42
|
+
next unless Rails.env.development?
|
|
43
|
+
|
|
44
|
+
require "decidim/custom_user_fields/dev/scenario_customizations"
|
|
45
|
+
Rails.application.config.to_prepare do
|
|
46
|
+
Decidim::CustomUserFields::ScenarioCustomizations.register!
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
initializer "decidim_custom_user_fields.authorization_workflow_filter" do
|
|
51
|
+
next unless Decidim::Toggle.respond_to?(:filter_authorization_workflows)
|
|
52
|
+
|
|
53
|
+
Decidim::Toggle.filter_authorization_workflows do |workflow, organization|
|
|
54
|
+
Verifications.selectable_workflow?(
|
|
55
|
+
workflow,
|
|
56
|
+
Verifications.enabled_customization_names(organization)
|
|
57
|
+
)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
initializer "decidim_custom_user_fields.organization_settings_tab",
|
|
62
|
+
after: "decidim_toggle.organization_settings_tabs" do
|
|
63
|
+
Decidim::Toggle.settings_tabs :organization_settings do |tabs|
|
|
64
|
+
next unless Customizations.any?
|
|
65
|
+
|
|
66
|
+
tabs.add_tab :custom_user_fields,
|
|
67
|
+
::I18n.t("decidim.custom_user_fields.system.customizations_tab"),
|
|
68
|
+
form: Admin::CustomizationsConfigForm,
|
|
69
|
+
command: UpdateCustomizationsConfigCommand,
|
|
70
|
+
module_name: :custom_user_fields,
|
|
71
|
+
form_layout_partial: "decidim/custom_user_fields/system/customizations_tab"
|
|
39
72
|
end
|
|
40
73
|
end
|
|
41
74
|
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module CustomUserFields
|
|
5
|
+
module ExtendedData
|
|
6
|
+
module_function
|
|
7
|
+
|
|
8
|
+
def validate_params(organization, params, errors: ActiveModel::Errors.new(Object.new))
|
|
9
|
+
params = params.to_h.with_indifferent_access
|
|
10
|
+
active_registration_fields(organization).each do |field_def|
|
|
11
|
+
key = field_def.name
|
|
12
|
+
next unless params.has_key?(key.to_s) || params.has_key?(key)
|
|
13
|
+
|
|
14
|
+
raw = params[key.to_s] || params[key]
|
|
15
|
+
value = field_def.sanitized_value(raw)
|
|
16
|
+
field_def.validate(value, params, errors)
|
|
17
|
+
end
|
|
18
|
+
errors
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def build_from_params(organization, params)
|
|
22
|
+
params = params.to_h.with_indifferent_access
|
|
23
|
+
data = {}
|
|
24
|
+
active_registration_fields(organization).each do |field_def|
|
|
25
|
+
key = field_def.name
|
|
26
|
+
next unless params.has_key?(key.to_s) || params.has_key?(key)
|
|
27
|
+
|
|
28
|
+
raw = params[key.to_s] || params[key]
|
|
29
|
+
data[key] = field_def.sanitized_value(raw)
|
|
30
|
+
end
|
|
31
|
+
data
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def merge_into(user, organization, params)
|
|
35
|
+
errors = ActiveModel::Errors.new(user)
|
|
36
|
+
validate_params(organization, params, errors:)
|
|
37
|
+
return [false, errors] if errors.any?
|
|
38
|
+
|
|
39
|
+
extended_data = (user.extended_data || {}).merge(build_from_params(organization, params))
|
|
40
|
+
if user.update(extended_data:)
|
|
41
|
+
[true, errors]
|
|
42
|
+
else
|
|
43
|
+
[false, user.errors]
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def active_registration_fields(organization)
|
|
48
|
+
RegistrationFields.active_registration_fields(organization)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "forwardable"
|
|
2
4
|
|
|
3
5
|
module Decidim
|
|
4
6
|
module CustomUserFields
|
|
@@ -7,28 +9,48 @@ module Decidim
|
|
|
7
9
|
|
|
8
10
|
attr_reader :type,
|
|
9
11
|
:name,
|
|
10
|
-
:field
|
|
12
|
+
:field,
|
|
13
|
+
:handler_name
|
|
11
14
|
|
|
12
15
|
def_delegators :@field,
|
|
13
16
|
:configure_form,
|
|
14
17
|
:map_model,
|
|
15
|
-
:form_tag
|
|
18
|
+
:form_tag,
|
|
19
|
+
:options,
|
|
20
|
+
:options=,
|
|
21
|
+
:i18n_context,
|
|
22
|
+
:i18n_context=,
|
|
23
|
+
:validate,
|
|
24
|
+
:skip_hashing?,
|
|
25
|
+
:sanitized_value,
|
|
26
|
+
:storage_name
|
|
16
27
|
|
|
17
|
-
def initialize(name, kwargs)
|
|
28
|
+
def initialize(name, kwargs, handler_name)
|
|
29
|
+
@handler_name = handler_name
|
|
18
30
|
@name = name.to_s.to_sym
|
|
19
31
|
@type = kwargs[:type]
|
|
20
|
-
# TODO: parse kwargs
|
|
21
32
|
case type
|
|
33
|
+
when :dummy
|
|
34
|
+
@field = Fields::DummyField.new(self, kwargs)
|
|
22
35
|
when :text
|
|
23
36
|
@field = Fields::TextField.new(self, kwargs)
|
|
24
37
|
when :textarea
|
|
25
38
|
@field = Fields::TextAreaField.new(self, kwargs)
|
|
26
39
|
when :date
|
|
27
40
|
@field = Fields::DateField.new(self, kwargs)
|
|
41
|
+
when :extra_field_ref
|
|
42
|
+
@field = Fields::ExtraFieldRefField.new(self, kwargs)
|
|
43
|
+
when :boolean
|
|
44
|
+
@field = Fields::BooleanField.new(self, kwargs)
|
|
28
45
|
else
|
|
29
|
-
raise Error,
|
|
46
|
+
raise Error, DefinitionGuidance.unsupported_type_message(type)
|
|
30
47
|
end
|
|
31
48
|
end
|
|
49
|
+
|
|
50
|
+
def deep_dup
|
|
51
|
+
opts = field.options.except(:type, "type").merge(type:)
|
|
52
|
+
self.class.new(name, opts, handler_name)
|
|
53
|
+
end
|
|
32
54
|
end
|
|
33
55
|
end
|
|
34
56
|
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module CustomUserFields
|
|
5
|
+
module Fields
|
|
6
|
+
class BooleanField < GenericField
|
|
7
|
+
def configure_form(form)
|
|
8
|
+
form.attribute(name, :boolean)
|
|
9
|
+
validations = {}
|
|
10
|
+
if options[:must_be_true] || required?
|
|
11
|
+
validations[:inclusion] = {
|
|
12
|
+
in: [true, "1", 1, "true"],
|
|
13
|
+
message: proc { label(:required) }
|
|
14
|
+
}
|
|
15
|
+
end
|
|
16
|
+
apply_form_validations(form, validations)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def skip_hashing?
|
|
20
|
+
true
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def validate(value, _data, errors)
|
|
24
|
+
return unless options[:must_be_true] || required?
|
|
25
|
+
|
|
26
|
+
errors.add(name, label(:required)) unless ActiveModel::Type::Boolean.new.cast(value)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def map_model(form, data)
|
|
30
|
+
return if data[name].nil?
|
|
31
|
+
|
|
32
|
+
form[name] = ActiveModel::Type::Boolean.new.cast(data[name])
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def sanitized_value(value)
|
|
36
|
+
ActiveModel::Type::Boolean.new.cast(value)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def form_tag(form_tag)
|
|
40
|
+
form_tag.check_box(
|
|
41
|
+
name,
|
|
42
|
+
label: label(:label),
|
|
43
|
+
help_text: label_exists?(:help_text) && label(:help_text),
|
|
44
|
+
class: class_name,
|
|
45
|
+
label_options: { class: label_class_name }
|
|
46
|
+
)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -1,29 +1,99 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Decidim
|
|
2
4
|
module CustomUserFields
|
|
3
5
|
module Fields
|
|
4
6
|
class DateField < GenericField
|
|
5
7
|
def configure_form(form)
|
|
6
8
|
form.attribute(name, String)
|
|
7
|
-
|
|
9
|
+
validations = {
|
|
10
|
+
format: {
|
|
11
|
+
with: /\A\d{4}-\d{2}-\d{2}\z/,
|
|
12
|
+
allow_blank: !required?,
|
|
13
|
+
message: proc do
|
|
14
|
+
label(:bad_format)
|
|
15
|
+
end
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
if required?
|
|
19
|
+
validations[:presence] = {
|
|
20
|
+
message: proc do |_object, _data|
|
|
21
|
+
label(:required)
|
|
22
|
+
end
|
|
23
|
+
}
|
|
24
|
+
end
|
|
25
|
+
apply_form_validations(form, validations)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def validate(value, _data, errors)
|
|
29
|
+
return if value.blank? && !required?
|
|
30
|
+
|
|
31
|
+
if value.blank?
|
|
32
|
+
errors.add(name, label(:required))
|
|
33
|
+
return
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
date_value = nil
|
|
37
|
+
begin
|
|
38
|
+
date_value = Date.strptime(value, "%Y-%m-%d")
|
|
39
|
+
rescue TypeError, ::Date::Error
|
|
40
|
+
errors.add(name, label(:bad_date))
|
|
41
|
+
return
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
validate_not_before(date_value, errors) if options[:not_before].present?
|
|
45
|
+
validate_not_after(date_value, errors) if options[:not_after].present?
|
|
8
46
|
end
|
|
9
47
|
|
|
10
48
|
def map_model(form, data)
|
|
11
49
|
form[name] = data[name] if data[name].present?
|
|
12
50
|
end
|
|
13
51
|
|
|
14
|
-
def
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
end
|
|
52
|
+
def sanitized_value(raw_value)
|
|
53
|
+
return nil if raw_value.blank?
|
|
54
|
+
|
|
55
|
+
raw_value
|
|
56
|
+
end
|
|
20
57
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
58
|
+
def form_tag(form_tag)
|
|
59
|
+
options = {
|
|
60
|
+
label: label(:label),
|
|
61
|
+
help_text: label_exists?(:help_text) && label(:help_text),
|
|
62
|
+
label_options: { class: label_class_name },
|
|
24
63
|
class: class_name
|
|
64
|
+
}
|
|
65
|
+
raw = form_tag.object[name]
|
|
66
|
+
form_tag.object.public_send("#{name}=", nil) if raw.blank?
|
|
67
|
+
if raw.present?
|
|
68
|
+
begin
|
|
69
|
+
options[:value] = Date.strptime(raw.to_s, "%Y-%m-%d")
|
|
70
|
+
rescue Date::Error
|
|
71
|
+
# ignore invalid stored values when re-rendering the form
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
form_tag.date_field(
|
|
75
|
+
name,
|
|
76
|
+
options
|
|
25
77
|
)
|
|
26
78
|
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
def not_before_date
|
|
83
|
+
Date.strptime(options[:not_before], "%Y-%m-%d")
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def not_after_date
|
|
87
|
+
Date.strptime(options[:not_after], "%Y-%m-%d")
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def validate_not_before(value, errors)
|
|
91
|
+
errors.add(name, label(:bad_not_before)) if value < not_before_date
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def validate_not_after(value, errors)
|
|
95
|
+
errors.add(name, label(:bad_not_after)) if value > not_after_date
|
|
96
|
+
end
|
|
27
97
|
end
|
|
28
98
|
end
|
|
29
99
|
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module CustomUserFields
|
|
5
|
+
module Fields
|
|
6
|
+
class DummyField < GenericField
|
|
7
|
+
def configure_form(form)
|
|
8
|
+
form.attribute(name, String)
|
|
9
|
+
validation_hash
|
|
10
|
+
|
|
11
|
+
apply_form_validations(form, validation_hash)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def validation_hash
|
|
15
|
+
{}
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def map_model(form, data)
|
|
19
|
+
form[name] = data[name].strip if data[name].present?
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def sanitized_value(value)
|
|
23
|
+
stripped_value = (value || "").strip
|
|
24
|
+
return stripped_value if stripped_value.present?
|
|
25
|
+
|
|
26
|
+
nil
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def form_tag(_form_tag)
|
|
30
|
+
{
|
|
31
|
+
name:,
|
|
32
|
+
label: label(:label),
|
|
33
|
+
help_text: label_exists?(:help_text) && label(:help_text),
|
|
34
|
+
class_name:
|
|
35
|
+
}.to_json
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module CustomUserFields
|
|
5
|
+
module Fields
|
|
6
|
+
class ExtraFieldRefField < GenericField
|
|
7
|
+
attr_accessor :reference, :customization_name
|
|
8
|
+
|
|
9
|
+
delegate :configure_form, :validate, :sanitized_value, :skip_hashing?, to: :peer_field
|
|
10
|
+
|
|
11
|
+
def validate_customization_reference!
|
|
12
|
+
resolve_reference_definition
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def map_model(_form, _data)
|
|
16
|
+
raise Error, "Can't use Extra Field Ref for registration"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def form_tag(form)
|
|
20
|
+
user = current_user(form)
|
|
21
|
+
extended_data = (user.extended_data || {}).with_indifferent_access
|
|
22
|
+
ref = resolve_reference_definition
|
|
23
|
+
stored = extended_data[ref.name]
|
|
24
|
+
assign_form_value(form.object, stored) if stored.present?
|
|
25
|
+
|
|
26
|
+
if stored.present? && options[:hide_if_value]
|
|
27
|
+
content_tag(
|
|
28
|
+
:span,
|
|
29
|
+
form.hidden_field(name),
|
|
30
|
+
class: "#{class_name} #{class_modifer("hidden")}"
|
|
31
|
+
)
|
|
32
|
+
else
|
|
33
|
+
peer = peer_field
|
|
34
|
+
old_context = peer.i18n_context
|
|
35
|
+
peer.i18n_context = i18n_context
|
|
36
|
+
field_tag = peer.form_tag(form)
|
|
37
|
+
peer.i18n_context = old_context
|
|
38
|
+
field_tag
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def i18n_context
|
|
43
|
+
"decidim.custom_user_fields.extended_data"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
def peer_field
|
|
49
|
+
@peer_field ||= build_peer_field
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def build_peer_field
|
|
53
|
+
ref = resolve_reference_definition
|
|
54
|
+
peer_type = ref.type
|
|
55
|
+
if peer_type == :extra_field_ref
|
|
56
|
+
raise Decidim::CustomUserFields::Error,
|
|
57
|
+
"extra_field_ref #{name} resolved to another extra_field_ref (#{reference_field_name})"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Force :type last so neither ref.options nor overlays can recreate ExtraFieldRefField.
|
|
61
|
+
kwargs = ref.options.except(:type, "type", :ref, "ref").merge(overlay_options).merge(type: peer_type)
|
|
62
|
+
FieldDefinition.new(name, kwargs, handler_name).field
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def reference_field_name
|
|
66
|
+
CustomizationFieldNaming.prefixed(customization_name, options[:ref] || name)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def storage_name
|
|
70
|
+
reference_field_name
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Extra-field-ref metadata must not leak into the peer field kwargs
|
|
74
|
+
# (especially :type), or FieldDefinition rebuilds another ExtraFieldRefField.
|
|
75
|
+
def overlay_options
|
|
76
|
+
options.except(:ref, :type, :hide_if_value, :skip_update_on_verified, "ref", "type")
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def resolve_reference_definition
|
|
80
|
+
@reference ||= begin
|
|
81
|
+
if customization_name.blank?
|
|
82
|
+
raise Decidim::CustomUserFields::Error,
|
|
83
|
+
"customization must be set for extra_field_ref field #{name}"
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
customization = Customizations.find(customization_name)
|
|
87
|
+
raise Decidim::CustomUserFields::Error, "Customization #{customization_name} not found" unless customization
|
|
88
|
+
|
|
89
|
+
match = customization.fields.find { |field| field.name == reference_field_name }
|
|
90
|
+
unless match
|
|
91
|
+
raise Decidim::CustomUserFields::Error,
|
|
92
|
+
"Field #{reference_field_name} not found in customization #{customization_name}"
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
match.deep_dup.tap do |reference|
|
|
96
|
+
overlays = overlay_options
|
|
97
|
+
next if overlays.empty?
|
|
98
|
+
|
|
99
|
+
reference.options = reference.options.except(:type, "type").merge(overlays)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def assign_form_value(form_object, value)
|
|
105
|
+
if form_object.respond_to?("#{name}=")
|
|
106
|
+
form_object.public_send("#{name}=", value)
|
|
107
|
+
else
|
|
108
|
+
form_object[name] = value
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def current_user(form)
|
|
113
|
+
form.object.user
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|