Skip to content

Commit

Permalink
Merge pull request boacausa#93 from kellynvd/add-user-validations
Browse files Browse the repository at this point in the history
Add presence validation to :name, :last_name, :email and :cpf on User
  • Loading branch information
carolinesalib authored Jan 11, 2020
2 parents 04f463f + 8373bef commit ade295b
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 16 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ end

gem 'coffee-rails', '~> 4.2'
gem 'cpf_cnpj'
gem 'validators'
gem 'faker'
gem 'jbuilder', '~> 2.5'
gem 'jquery-rails'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ GEM
unicorn (5.5.2)
kgio (~> 2.6)
raindrops (~> 0.7)
validators (3.0.3)
warden (1.2.8)
rack (>= 2.0.6)
web-console (3.7.0)
Expand Down Expand Up @@ -427,6 +428,7 @@ DEPENDENCIES
tzinfo-data
uglifier (>= 1.3.0)
unicorn
validators
web-console (>= 3.3.0)
webpacker (~> 4.x)

Expand Down
9 changes: 6 additions & 3 deletions app/controllers/ngo_area/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ def new
end

def create
@user = User.create(params_user)

redirect_to ngo_area_users_path
@user = User.new(params_user)
if @user.save
redirect_to ngo_area_users_path
else
render action: :new
end
end

def edit
Expand Down
27 changes: 27 additions & 0 deletions app/helpers/errors_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module ErrorsHelper
def form_error_tag(model)
return unless model.errors.any?

content_tag(
:div,
I18n.t(:form_error),
class: 'alert alert-danger'
)
end

def field_error_tag(model, attribute)
return if model.errors.keys.exclude?(attribute)

content_tag(
:span,
model.errors[attribute].first,
class: 'control-label'
)
end

def form_group_error_class(model, attribute)
return if model.errors.keys.exclude?(attribute)

'has-error'
end
end
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class User < ApplicationRecord
has_and_belongs_to_many :ngos
has_many :adoption_interests

validates :name, presence: true
validates :last_name, presence: true
validates :email, presence: true, email: true
validates :cpf, presence: true, cpf: true

def self.from_omniauth(auth)
user = User.find_by(email: auth.info.email)

Expand Down
43 changes: 30 additions & 13 deletions app/views/ngo_area/users/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,38 +1,54 @@
<div class="col-lg-12">
<div class="form-group">
<%= label_tag(:name, 'Nome') %>
<%= form_error_tag(@user) %>

<div class="form-group <%= form_group_error_class(@user, :name)%>">
<%= label_tag(:name, 'Nome', class: 'control-label') %>
<%= f.text_field :name, class: 'form-control' %>
<%= field_error_tag(@user, :name) %>
</div>
<div class="form-group">
<%= label_tag(:last_name, 'Sobrenome') %>

<div class="form-group <%= form_group_error_class(@user, :last_name)%>">
<%= label_tag(:last_name, 'Sobrenome', class: 'control-label') %>
<%= f.text_field :last_name, class: 'form-control' %>
<%= field_error_tag(@user, :last_name) %>
</div>
<div class="form-group">
<%= label_tag(:email, 'E-mail') %>

<div class="form-group <%= form_group_error_class(@user, :email)%>">
<%= label_tag(:email, 'E-mail', class: 'control-label') %>
<%= f.text_field :email, class: 'form-control', disabled: params[:action] == 'edit' %>
<%= field_error_tag(@user, :email) %>
</div>
<div class="form-group">
<%= label_tag(:cpf, 'CPF') %>

<div class="form-group <%= form_group_error_class(@user, :cpf)%>">
<%= label_tag(:cpf, 'CPF', class: 'control-label') %>
<%= f.text_field :cpf, class: 'form-control' %>
<%= field_error_tag(@user, :cpf) %>
</div>

<div class="form-group">
<%= label_tag(:phone, 'Telefone') %>
<%= f.text_field :phone, class: 'form-control', maxlength: 15 %>
</div>

<div class="form-group">
<%= label_tag(:group, 'Grupo') %>
<%= f.select :group, user_groups_for_select, { include_blank: 'Nenhum' } , class: 'form-control' %>
</div>
<% if params[:action] == 'new' %>
<div class="form-group">
<%= label_tag(:password) %>

<% if params[:action] == 'new' || params[:action] == 'create' %>
<div class="form-group <%= form_group_error_class(@user, :password) %>">
<%= label_tag(:password,'Senha', class: 'control-label') %>
<%= f.password_field :password, autocomplete: "off", class: 'form-control' %>
<%= field_error_tag(@user, :password) %>
</div>
<div class="form-group">
<%= label_tag(:password_confirmation) %>

<div class="form-group <%= form_group_error_class(@user, :password_confirmation) %>">
<%= label_tag(:password_confirmation,'Confirmar Senha', class: 'control-label') %>
<%= f.password_field :password_confirmation, autocomplete: "off", class: 'form-control' %>
<%= field_error_tag(@user, :password_confirmation) %>
</div>
<% end %>

<div class="form-group">
<label>ONGs</label>
<% Ngo.active.each do |ngo| %>
Expand All @@ -45,6 +61,7 @@
<% end %>
</div>
</div>

<div class="col-lg-12">
<div class="form-group">
<%= f.submit 'Salvar', class: 'btn btn-success' %>
Expand Down
9 changes: 9 additions & 0 deletions config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pt-BR:
new: novo
adoption_interests: interessados_adocao

form_error: 'Há erros no formulário, por favor verifique'

activerecord:
attributes:
user:
Expand All @@ -17,6 +19,13 @@ pt-BR:
restrict_dependent_destroy:
has_one: Não é possível excluir o registro pois existe um %{record} dependente
has_many: Não é possível excluir o registro pois existem %{record} dependentes
models:
user:
attributes:
email:
invalid_email: 'não é válido'
cpf:
invalid_cpf: 'não é válido'

date:
abbr_day_names:
Expand Down
1 change: 1 addition & 0 deletions spec/factories/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
factory :user do
name { Faker::Name.name }
last_name { Faker::Name.last_name }
cpf { CPF.generate }
email { Faker::Internet.email }
password { Faker::Internet.password }
group { nil }
Expand Down

0 comments on commit ade295b

Please sign in to comment.