add ppl to accounts

This commit is contained in:
Celina Lopez
2024-10-25 14:10:39 -07:00
parent a2bfec6da9
commit 24c52d958b
10 changed files with 105 additions and 58 deletions

View File

@@ -0,0 +1,34 @@
class Accounts::AccountUsersController < ApplicationController
before_action :set_account
def create
user = User.find_or_initialize_by(email: user_params[:email]) do |user|
user.first_name = email.split("@").first
user.password = Devise.friendly_token[0, 20]
user.save!
end
AccountUser.create!(account: @account, user: user)
redirect_to account_account_users_path(@account), notice: "User was successfully added."
end
def destroy
@account.account_users.find(params[:id]).destroy
redirect_to account_account_users_path(@account), notice: "User was successfully destroyed."
end
def index
@pagy, @account_users = pagy(@account.account_users)
end
private
def user_params
params.require(:user).permit(:email)
end
def set_account
@account = Account.find(params[:account_id])
end
end

View File

@@ -4,8 +4,4 @@ class AccountsController < ApplicationController
session[:account_id] = @account.id
redirect_to root_path
end
def index
@pagy, @accounts = pagy(current_user.accounts)
end
end

View File

@@ -79,16 +79,20 @@ module Users
def create_user
ActiveRecord::Base.transaction do
user = User.create!(
email: auth.info.email,
# name: auth.info.name,
password: Devise.friendly_token[0, 20]
)
account = Account.create!(
owner: user,
name: "#{auth.info.name || auth.info.email.split("@").first}'s Account"
)
AccountUser.create!(account: account, user: user)
user = User.find_or_initialize_by(email: auth.info.email) do |user|
user.first_name = auth.info.name
user.password = Devise.friendly_token[0, 20]
user.save!
end
if user.owned_accounts.zero?
account = Account.create!(
owner: user,
name: "#{auth.info.name || auth.info.email.split("@").first}'s Account"
)
AccountUser.create!(account: account, user: user)
end
user
end
end

View File

@@ -30,6 +30,7 @@ class User < ApplicationRecord
has_many :account_users, dependent: :destroy
has_many :accounts, through: :account_users
has_many :owned_accounts, class_name: "Account", foreign_key: "owner_id"
has_many :providers, dependent: :destroy
has_many :clusters, through: :accounts
@@ -39,5 +40,4 @@ class User < ApplicationRecord
# has_many :notifications, as: :recipient, dependent: :destroy, class_name: "Noticed::Notification"
# has_many :notification_mentions, as: :record, dependent: :destroy, class_name: "Noticed::Event"
end

View File

@@ -1,7 +0,0 @@
<%= tag.div id: ("accounts" if @pagy.page == 1) do %>
<% @accounts.each do |account| %>
<h3><%= account.name %></h3>
<%= render "account_users/index", account_users: account.account_users %>
<% end %>
<% end %>

View File

@@ -0,0 +1,54 @@
<%= content_for :title, "Users" %>
<%= turbo_stream_from :account_users %>
<div>
<div class="flex items-center justify-between">
<h3 class="text-lg font-medium">Users</h3>
</div>
<div class="mt-5">
<div aria-label="Card" class="card card-bordered bg-base-100">
<div class="card-body">
<div class="text-right px-5 pt-5">
<!-- Start: User Action -->
<button class="btn btn-primary btn-sm" onclick="click_outside_modal.showModal()">
<iconify-icon icon="lucide:plus" height="16"></iconify-icon>
<span class="hidden sm:inline">New User</span>
</button>
<!-- End: User Action -->
</div>
<div>
<%= tag.div id: ("account_users" if @pagy.page == 1) do %>
<%= render "accounts/account_users/index", account_users: @account_users, cached: true %>
<% end %>
</div>
</div>
</div>
</div>
<% if @pagy.pages > 1 %>
<div class="flex items-center justify-end px-5 pb-5 pt-3">
<%== custom_pagy_nav(@pagy) %>
</div>
<% end %>
</div>
<!-- Start: Click Outside Modal -->
<dialog aria-label="Modal" class="modal" id="click_outside_modal">
<div class="modal-box">
<form method="dialog">
<button aria-label="Close modal" class="btn btn-circle btn-ghost btn-sm absolute right-2 top-2">
<iconify-icon icon="lucide:x" height="16"></iconify-icon>
</button>
</form>
<div class="mb-8 w-full text-xl font-bold">Hello!</div>
<div>Press ESC key or click on X button to close</div>
</div>
<form method="dialog" class="modal-backdrop">
<button>close</button>
</form>
</dialog>
<!-- End: Click Outside Modal -->

View File

@@ -1,27 +0,0 @@
<%= content_for :title, "Accounts" %>
<%= turbo_stream_from :accounts %>
<div>
<div class="flex items-center justify-between">
<h3 class="text-lg font-medium">Accounts</h3>
</div>
<div class="mt-5">
<div aria-label="Card" class="card card-bordered bg-base-100">
<div class="card-body">
<div>
<%= tag.div id: ("accounts" if @pagy.page == 1) do %>
<%= render "accounts/index", accounts: @accounts, cached: true %>
<% end %>
</div>
</div>
</div>
</div>
<% if @pagy.pages > 1 %>
<div class="flex items-center justify-end px-5 pb-5 pt-3">
<%== custom_pagy_nav(@pagy) %>
</div>
<% end %>
</div>

View File

@@ -20,14 +20,6 @@
</div>
</li>
<li>
<div>
<%= link_to accounts_path do %>
<iconify-icon icon="lucide:users" height="16"></iconify-icon>
Accounts
<% end %>
</div>
</li>
<% if current_user.admin? && respond_to?(:madmin_root_path) %>
<li>
<div>

View File

@@ -2,7 +2,8 @@ require "sidekiq/web"
Rails.application.routes.draw do
draw :madmin
resources :accounts, only: [ :index ] do
resources :accounts, only: [] do
resources :account_users, only: %i[create index destroy], module: :accounts
member do
get :switch
end