Files
grecia/app/controllers/management/document_verifications_controller.rb
Javi Martín 11832cc07d Make it easier to customize allowed parameters
When customizing CONSUL, one of the most common actions is adding a new
field to a form.

This requires modifying the permitted/allowed parameters. However, in
most cases, the method returning these parameters returned an instance
of `ActionController::Parameters`, so adding more parameters to it
wasn't easy.

So customizing the code required copying the method returning those
parameters and adding the new ones. For example:

```
def something_params
  params.require(:something).permit(
    :one_consul_attribute,
    :another_consul_attribute,
    :my_custom_attribute
  )
end
```

This meant that, if the `something_params` method changed in CONSUL, the
customization of this method had to be updated as well.

So we're extracting the logic returning the parameters to a method which
returns an array. Now this code can be customized without copying the
original method:

```
alias_method :consul_allowed_params, :allowed_params

def allowed_params
  consul_allowed_params + [:my_custom_attribute]
end
```
2022-04-07 19:35:40 +02:00

55 lines
1.6 KiB
Ruby

class Management::DocumentVerificationsController < Management::BaseController
before_action :clean_document_number, only: :check
before_action :set_document, only: :check
def index
@document_verification = Verification::Management::Document.new
end
def check
@document_verification = Verification::Management::Document.new(document_verification_params)
if @document_verification.valid?
if @document_verification.verified?
render :verified
elsif @document_verification.user?
render :new
elsif @document_verification.in_census?
redirect_to new_management_email_verification_path(email_verification: document_verification_params.to_h)
else
render :invalid_document
end
else
render :index
end
end
def create
@document_verification = Verification::Management::Document.new(document_verification_params)
@document_verification.verify
render :verified
end
private
def document_verification_params
params.require(:document_verification).permit(allowed_params)
end
def allowed_params
[:document_type, :document_number, :date_of_birth, :postal_code]
end
def set_document
session[:document_type] = params[:document_verification][:document_type]
session[:document_number] = params[:document_verification][:document_number]
clear_password
end
def clean_document_number
return if params[:document_verification][:document_number].blank?
params[:document_verification][:document_number] = params[:document_verification][:document_number].gsub(/[^a-z0-9]+/i, "").upcase
end
end