adds residence verification

This commit is contained in:
rgarcia
2015-08-27 23:53:49 +02:00
parent 61b7ff5e21
commit 07180bcd04
5 changed files with 113 additions and 0 deletions

View File

@@ -51,4 +51,10 @@ class ApplicationController < ActionController::Base
redirect_to finish_signup_path redirect_to finish_signup_path
end end
end end
def verify_resident!
unless current_user.residence_verified?
redirect_to new_residence_path, alert: t('verification.residence.alert.unconfirmed_residency')
end
end
end end

View File

@@ -0,0 +1,31 @@
class Verification::ResidenceController < ApplicationController
before_action :authenticate_user!
before_action :verify_attemps_left!, only: [:new, :create]
skip_authorization_check
def new
@residence = Residence.new
end
def create
@residence = Residence.new(residence_params.merge(user: current_user))
if @residence.save
redirect_to verified_user_path, notice: t('verification.residence.create.flash.success')
else
current_user.update(residence_verification_tries: current_user.residence_verification_tries += 1)
render :new
end
end
private
def residence_params
params.require(:residence).permit(:document_number, :document_type, :date_of_birth, :postal_code)
end
def verify_attemps_left!
if current_user.residence_verification_tries >= 5
redirect_to account_path, alert: t('verification.residence.alert.verify_attemps_left')
end
end
end

45
app/models/residence.rb Normal file
View File

@@ -0,0 +1,45 @@
class Residence
include ActiveModel::Model
include ActiveModel::Dates
attr_accessor :user, :document_number, :document_type, :date_of_birth, :postal_code
validates_presence_of :document_number
validates_presence_of :document_type
validates_presence_of :date_of_birth
validates_presence_of :postal_code
validates :postal_code, length: { is: 5 }
validate :residence_in_madrid
validate :document_number_uniqueness
def initialize(attrs={})
self.date_of_birth = parse_date('date_of_birth', attrs)
attrs = remove_date('date_of_birth', attrs)
super
end
def save
return false unless valid?
user.update(document_number: document_number,
document_type: document_type,
residence_verified_at: Time.now)
end
def document_number_uniqueness
errors.add(:document_number, "Already in use") if User.where(document_number: document_number).any?
end
def residence_in_madrid
return if errors.any?
self.date_of_birth = date_to_string(date_of_birth)
residency = UserApi.new(self)
errors.add(:residence_in_madrid, false) unless residency.valid?
self.date_of_birth = string_to_date(date_of_birth)
end
end

View File

@@ -0,0 +1,12 @@
<% if @residence.errors[:residence_in_madrid].present? %>
<div id="error_explanation" class="alert-box alert radius">
<%= t('verification.residence.new.error_verifying_census') %>
<%= mail_to "tec.gobiernoabierto@madrid.es" %>
</div>
<% else %>
<%= render 'shared/errors',
resource: @residence,
message: t('verification.residence.new.form_errors') %>
<% end %>

View File

@@ -0,0 +1,19 @@
<div class="row account">
<div class="small-12 column">
<h1 class="inline-block"><%= t('verification.residence.new.title') %></h1>
<%= form_for @residence, url: residence_path do |f| %>
<%= render 'errors' %>
<%= f.select :document_type, document_types, prompt: "" %>
<%= f.text_field :document_number %>
<%= f.date_select :date_of_birth,
prompt: true,
start_year: 1900, end_year: 16.years.ago.year %>
<%= f.text_field :postal_code %>
<%= f.submit "Verify" %>
<% end %>
</div>
</div>