Skip to content

Commit

Permalink
Merge pull request #90 from motohiro-mm/feature/add_family_page
Browse files Browse the repository at this point in the history
共有ページを作成
  • Loading branch information
motohiro-mm authored Oct 1, 2024
2 parents 4b7c780 + cb0ed1a commit 755e01f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
7 changes: 7 additions & 0 deletions app/controllers/families_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class FamiliesController < ApplicationController
def show
@family = current_user.family
end
end
4 changes: 4 additions & 0 deletions app/models/family.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ def destroy_family_having_no_user

Family.find(id).destroy!
end

def invitation_url(request)
"#{request.protocol}#{request.host_with_port}/welcome?invitation_token=#{invitation_token}"
end
end
38 changes: 38 additions & 0 deletions app/views/families/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<div>
<h1 class="font-bold text-4xl">献立表の共有</h1>

<div class="border border-stone-300 rounded-md p-4 my-5 text-stone-800">
<strong class="block font-bold text-lg">共有しているユーザー</strong>

<% @family.users.each do |user| %>
<p class="my-5 flex items-center">
<%= image_tag user.icon, class: 'icon' %>
<span class="ml-2"><%= user.name %></span>
</p>
<% end %>

<p class="my-5">
<input id="copyTarget" type="text" value="<%= @family.invitation_url(request) %>" readonly autocomplete="off">
<button class="rounded-lg py-3 px-5 bg-gray-600 text-white inline-block font-medium cursor-pointer" onclick="copyToClipboard()">共有URLをCopyする</button>
</p>
</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' %>

<script>
function copyToClipboard() {
// コピー対象をJavaScript上で変数として定義する
var copyTarget = document.getElementById("copyTarget");

// コピー対象のテキストを選択する
copyTarget.select();
copyTarget.setSelectionRange(0, 99999);

// 選択しているテキストをクリップボードにコピーする
document.execCommand("Copy");

// コピーをお知らせする
alert("コピーできました! : " + copyTarget.value);
}
</script>
</div>
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<% if current_user %>
<%= 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 '共有ページへ', family_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
5 changes: 3 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
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]

resource :user, only: %i[update destroy]
get 'user', to: 'users#edit', as: 'edit_user'
get 'family', to: 'families#show', as: 'family'
get 'meal_plans/calendar', to: 'meal_plans#calendar'
resources :meal_plans do
resources :meals, only: %i[new]
Expand Down

0 comments on commit 755e01f

Please sign in to comment.