Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation for creating a new field #10

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ def update_personal_data
@user.extended_data = extended_data
end

# rubocop:disable Style/TrailingCommaInArguments
def extended_data
@extended_data ||= (@user&.extended_data || {}).merge(
country: @form.country,
postal_code: @form.postal_code,
date_of_birth: @form.date_of_birth,
gender: @form.gender
gender: @form.gender,
# Block ExtraUserFields SaveInExtendedData
# EndBlock
)
end
# rubocop:enable Style/TrailingCommaInArguments
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,19 @@ def create_or_find_user
@user.save!
end

# rubocop:disable Style/TrailingCommaInArguments
def extended_data
@extended_data ||= (@user&.extended_data || {}).merge(
country: form.country,
postal_code: form.postal_code,
date_of_birth: form.date_of_birth,
gender: form.gender
country: @form.country,
postal_code: @form.postal_code,
date_of_birth: @form.date_of_birth,
gender: @form.gender,
# Block ExtraUserFields SaveInExtendedData

# EndBlock
)
end
# rubocop:enable Style/TrailingCommaInArguments
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,20 @@ def update_extra_user_fields!
)
end

# rubocop:disable Style/TrailingCommaInHashLiteral
def extra_user_fields
{
"enabled" => form.enabled.presence || false,
"date_of_birth" => { "enabled" => form.date_of_birth.presence || false },
"country" => { "enabled" => form.country.presence || false },
"postal_code" => { "enabled" => form.postal_code.presence || false },
"gender" => { "enabled" => form.gender.presence || false }
"gender" => { "enabled" => form.gender.presence || false },
# Block ExtraUserFields SaveFieldInConfig

# EndBlock
}
end
# rubocop:enable Style/TrailingCommaInHashLiteral
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions app/forms/concerns/decidim/extra_user_fields/forms_definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@ module FormsDefinitions
attribute :date_of_birth, Decidim::Attributes::LocalizedDate
attribute :gender, String

# Block ExtraUserFields Attributes

# EndBlock

validates :country, presence: true, if: :country?
validates :postal_code, presence: true, if: :postal_code?
validates :date_of_birth, presence: true, if: :date_of_birth?
validates :gender, presence: true, inclusion: { in: Decidim::ExtraUserFields::Engine::DEFAULT_GENDER_OPTIONS.map(&:to_s) }, if: :gender?

# Block ExtraUserFields Validations

# EndBlock
end

def map_model(model)
Expand All @@ -29,6 +37,10 @@ def map_model(model)
self.postal_code = extended_data[:postal_code]
self.date_of_birth = Date.parse(extended_data[:date_of_birth]) if extended_data[:date_of_birth].present?
self.gender = extended_data[:gender]

# Block ExtraUserFields MapModel

# EndBlock
end

private
Expand All @@ -49,6 +61,10 @@ def postal_code?
extra_user_fields_enabled && current_organization.activated_extra_field?(:postal_code)
end

# Block ExtraUserFields EnableFieldMethod

# EndBlock

def extra_user_fields_enabled
@extra_user_fields_enabled ||= current_organization.extra_user_fields_enabled?
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ class ExtraUserFieldsForm < Decidim::Form
attribute :postal_code, Virtus::Attribute::Boolean
attribute :date_of_birth, Virtus::Attribute::Boolean
attribute :gender, Virtus::Attribute::Boolean
# Block ExtraUserFields Attributes

# EndBlock

def map_model(model)
self.enabled = model.extra_user_fields["enabled"]
self.country = model.extra_user_fields.dig("country", "enabled")
self.postal_code = model.extra_user_fields.dig("postal_code", "enabled")
self.date_of_birth = model.extra_user_fields.dig("date_of_birth", "enabled")
self.gender = model.extra_user_fields.dig("gender", "enabled")
# Block ExtraUserFields MapModel

# EndBlock
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,24 @@ def serialize
def extra_user_fields
extended_data = resource.extended_data.symbolize_keys

[:gender, :country, :postal_code, :date_of_birth].index_with do |key|
extra_fields.index_with do |key|
extended_data[key]
end
end

# rubocop:disable Style/TrailingCommaInArrayLiteral
def extra_fields
[
:gender,
:country,
:postal_code,
:date_of_birth,
# Block ExtraUserFields AddExtraField

# EndBlock
]
end
# rubocop:enable Style/TrailingCommaInArrayLiteral
end
end
end
4 changes: 4 additions & 0 deletions app/views/decidim/extra_user_fields/_profile_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@
<% if current_organization.activated_extra_field?(:postal_code) %>
<%= f.text_field :postal_code %>
<% end %>

<%# Block ExtraUserFields AccountFormFields %>

<%# EndBlock %>
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
<%= f.text_field :postal_code %>
</div>
<% end %>

<%# Block ExtraUserFields SignUpFormFields %>

<%# EndBlock %>
</div>
</div>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
<%= render partial: "decidim/extra_user_fields/admin/extra_user_fields/fields/country", locals: { form: form } %>
<%= render partial: "decidim/extra_user_fields/admin/extra_user_fields/fields/postal_code", locals: { form: form } %>
<%= render partial: "decidim/extra_user_fields/admin/extra_user_fields/fields/gender", locals: { form: form } %>
<%# Block ExtraUserFields ExtraFields %>

<%# EndBlock %>
</div>
</div>
Loading