Skip to content

Commit

Permalink
✨ allows to config profile on sign up (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
karinevieira authored Feb 12, 2024
1 parent 5ec329e commit b17b16a
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 4 deletions.
2 changes: 2 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

class ApplicationController < ActionController::Base
before_action :authenticate_user!

include DeviseOverrides
end
16 changes: 16 additions & 0 deletions app/controllers/concerns/devise_overrides.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module DeviseOverrides
extend ActiveSupport::Concern

included do
before_action :configure_permitted_parameters, if: :devise_controller?
end

def configure_permitted_parameters
devise_parameter_sanitizer.permit(
:sign_up,
keys: [:email, :password, { profile_attributes: %i[display_name username] }]
)
end
end
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ class User < ApplicationRecord
has_one :profile, class_name: "UserProfile", dependent: :destroy

has_many :likes, dependent: :destroy

accepts_nested_attributes_for :profile, update_only: true

validates :profile, presence: true
end
17 changes: 16 additions & 1 deletion app/views/devise/registrations/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,22 @@
</div>

<div class="mb-6">
<%= f.text_field_component(:email, type: "email", id: "email") do |input| %>
<%= f.fields_for :profile, resource.build_profile do |profile_fields| %>
<div class="mb-6">
<%= profile_fields.text_field_component(:display_name, type: "display_name", id: "display_name", placeholder: "Jane Doe") do |input| %>
<% input.label { t(".display_name") } %>
<% end %>
<div>
<div class="mb-6">
<%= profile_fields.text_field_component(:username, type: "username", id: "username", placeholder: "janedoe") do |input| %>
<% input.label { t(".username") } %>
<% end %>
<div>
<% end %>
<div>

<div class="mb-6">
<%= f.text_field_component(:email, type: "email", id: "email", placeholder: "[email protected]") do |input| %>
<% input.label { t(".email") } %>
<% end %>
</div>
Expand Down
3 changes: 2 additions & 1 deletion config/locales/pt-BR/devise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ pt-BR:
new:
page_title: Crie sua conta
submit: Criar conta
name: Nome
display_name: Nome
username: Usuário
email: E-mail
password: Senha
password_confirmation: Confirmação da senha
Expand Down
8 changes: 8 additions & 0 deletions spec/factories/users_factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,13 @@
factory :user do
sequence(:email) { |n| "test#{n}@email.com" }
password { "password" }

transient do
sequence(:username) { |n| "janedoe#{n}" }
end

profile_attributes do
{ display_name: "Jane Crawford", username: username }
end
end
end
12 changes: 10 additions & 2 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
require "rails_helper"

RSpec.describe User do
it { is_expected.to have_many(:likes) }
describe "associations" do
it { is_expected.to have_many(:likes) }

it { is_expected.to have_one(:profile) }
it { is_expected.to have_one(:profile) }

it { is_expected.to accept_nested_attributes_for(:profile) }
end

describe "validations" do
it { is_expected.to validate_presence_of(:profile) }
end
end

0 comments on commit b17b16a

Please sign in to comment.