Skip to content

Commit

Permalink
Merge pull request #88 from motohiro-mm/feature/add_user_page
Browse files Browse the repository at this point in the history
ユーザー設定ページを追加
  • Loading branch information
motohiro-mm authored Oct 1, 2024
2 parents 7091493 + 3a0954a commit c448da2
Show file tree
Hide file tree
Showing 18 changed files with 110 additions and 8 deletions.
Binary file added app/assets/images/apple.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/chimpanzee.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/clown.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/eagle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/fighter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/fish.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/girl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/king.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/man.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@
*= require_tree .
*= require_self
*/

.icon{
width: 30px;
height: 30px;
}
31 changes: 31 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

class UsersController < ApplicationController
before_action :set_user, only: %i[edit update destroy]
def edit; end

def update
if @user.update(user_params)
redirect_to meal_plans_path, notice: 'User was successfully updated.', status: :see_other
else
render :edit, status: :unprocessable_entity
end
end

def destroy
current_user.destroy!
current_user.family.destroy_family_having_no_user
reset_session
redirect_to root_path, notice: 'User was successfully deleted.'
end

private

def set_user
@user = current_user
end

def user_params
params.require(:user).permit(:name, :icon)
end
end
6 changes: 6 additions & 0 deletions app/models/family.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ class Family < ApplicationRecord
has_many :meeting_rooms, dependent: :destroy

validates :invitation_token, presence: true

def destroy_family_having_no_user
return if User.exists?(family_id: id)

Family.find(id).destroy!
end
end
24 changes: 24 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@
class User < ApplicationRecord
belongs_to :family
has_many :remarks, dependent: :nullify

enum :icon, {
'man.png': 0,
'king.png': 1,
'girl.png': 2,
'clown.png': 3,
'fighter.png': 4,
'fish.png': 5,
'eagle.png': 6,
'apple.png': 7,
'chimpanzee.png': 8
}

ICON = {
man: 'man.png',
king: 'king.png',
girl: 'girl.png',
clown: 'clown.png',
fighter: 'fighter.png',
eagle: 'eagle.png',
apple: 'apple.png',
chimpanzee: 'chimpanzee.png'
}.freeze

def self.find_or_new_from_auth_hash(auth_hash)
user_params = user_params_from_auth_hash(auth_hash)
find_or_initialize_by(uid: user_params[:uid], provider: user_params[:provider]) do |user|
Expand Down
3 changes: 2 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
<header class="bg-stone-700 text-white py-4 px-5 flex justify-between items-center">
<div class="flex items-center">
<% if current_user %>
<span><%= current_user.name %>さん</span>
<%= image_tag current_user.icon, class: 'icon invert brightness-0' %>
<%= link_to "#{current_user.name}さん", edit_user_path, class: 'ml-4 text-stone-100 hover:underline' %>
<%= link_to 'ログアウト', log_out_path, class: 'ml-4 text-stone-300 hover:underline' %>
<% else %>
<%= button_to 'Googleでログイン', '/auth/google_oauth2', method: :post, data: { turbo: false },
Expand Down
2 changes: 0 additions & 2 deletions app/views/meal_plans/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<%#= link_to "ユーザー設定" , edit_user_path %>
<% content_for :title, 'MealPlans' %>
<div class="flex justify-between items-center">
<h1 class="font-bold text-4xl text-stone-800">MealPlans</h1>
Expand Down
7 changes: 2 additions & 5 deletions app/views/remarks/_remark.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<div>

<div id="<%= dom_id remark %>">
<p class="my-5">
<%= image_tag remark.user.icon, class: 'icon' %>
<%= remark.content %>
<%# if remark.proposal? %>
<%#= link_to "登録へ", remark_timing_path(remark), class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%# end %>
</p>
</div>
39 changes: 39 additions & 0 deletions app/views/users/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

<div>
<h1 class="font-bold text-4xl">Userページ</h1>
<%= form_with(model: @user, class: 'contents') do |form| %>
<% if @user.errors.any? %>
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
<h2><%= pluralize(@user.errors.count, 'error') %> prohibited this user from being saved:</h2>

<ul>
<% @user.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="my-5">
<%= form.label :name %>
<%= form.text_field :name,
class: 'block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full' %>
</div>

<div class="my-5">
<%= form.label :icon %>
<%= form.select :icon, User::ICON.keys.zip(User::ICON.values),
class: 'block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full' %>
</div>

<div class="inline">
<%= form.submit class: 'rounded-lg py-3 px-5 bg-gray-600 text-white inline-block font-medium cursor-pointer' %>
</div>
<% end %>
<%= button_to 'アカウント削除', user_path, method: :delete, data: { turbo_confirm: '本当に削除して良いですか?' } %>
</div>

<div>
<%= link_to '献立表に戻る', meal_plans_path,
class: 'ml-2 rounded-lg py-3 px-5 text-stone-500 bg-stone-100 inline-block font-medium' %>
</div>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
get 'auth/failure', to: redirect('/')
get 'log_out', to: 'sessions#destroy', as: 'log_out'
resources :sessions, only: %i[create destroy]
resource :user, only: %i[edit update destroy]

get 'meal_plans/calendar', to: 'meal_plans#calendar'
resources :meal_plans do
Expand Down

0 comments on commit c448da2

Please sign in to comment.