mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-19 13:19:39 +02:00
Opt-in to Current fully (#297)
This commit is contained in:
parent
23eaa3e066
commit
d61a22b070
8 changed files with 24 additions and 23 deletions
|
@ -17,7 +17,7 @@ class AccountsController < ApplicationController
|
|||
end
|
||||
|
||||
def create
|
||||
@account = Account.new(account_params.merge(family: current_family))
|
||||
@account = Account.new(account_params.merge(family: Current.family))
|
||||
@account.accountable = account_params[:accountable_type].constantize.new
|
||||
|
||||
if @account.save
|
||||
|
|
|
@ -2,31 +2,21 @@ module Authentication
|
|||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
helper_method :current_user
|
||||
helper_method :current_family
|
||||
helper_method :user_signed_in?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def authenticate_user!
|
||||
redirect_to new_session_path unless user_signed_in?
|
||||
if user = User.find_by(id: session[:user_id])
|
||||
Current.user = user
|
||||
else
|
||||
redirect_to new_session_url
|
||||
end
|
||||
|
||||
def current_user
|
||||
Current.user || authenticate_user_from_session
|
||||
end
|
||||
|
||||
def current_family
|
||||
current_user.family
|
||||
end
|
||||
|
||||
def authenticate_user_from_session
|
||||
User.find_by(id: session[:user_id])
|
||||
end
|
||||
|
||||
def user_signed_in?
|
||||
current_user.present?
|
||||
Current.user.present?
|
||||
end
|
||||
|
||||
def login(user)
|
||||
|
|
|
@ -5,7 +5,7 @@ class PasswordsController < ApplicationController
|
|||
end
|
||||
|
||||
def update
|
||||
if current_user.update(password_params)
|
||||
if Current.user.update(password_params)
|
||||
redirect_to root_path, notice: t(".success")
|
||||
else
|
||||
render :edit, status: :unprocessable_entity
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
class Current < ActiveSupport::CurrentAttributes
|
||||
attribute :user
|
||||
|
||||
delegate :family, to: :user
|
||||
end
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
|
||||
<h2 class="text-2xl font-semibold font-display">Cash</h2>
|
||||
<h3 class="mt-1 mb-4 text-sm text-gray-500"><%#= number_to_currency current_family.cash_balance %></h3>
|
||||
<h3 class="mt-1 mb-4 text-sm text-gray-500"><%#= number_to_currency Current.family.cash_balance %></h3>
|
||||
|
||||
<% current_family.accounts.each do |account| %>
|
||||
<% Current.family.accounts.each do |account| %>
|
||||
<div class="flex items-center justify-between px-3 py-3 mb-2 bg-white shadow-sm rounded-xl">
|
||||
<div class="flex items-center text-sm">
|
||||
<%= account.name %>
|
||||
|
|
|
@ -32,7 +32,7 @@
|
|||
<% end %>
|
||||
<div class="relative" data-controller="dropdown">
|
||||
<div class="flex" data-action="click->dropdown#toggleMenu">
|
||||
<div class="mr-1.5 text-white w-8 h-8 bg-gray-400 rounded-full flex items-center justify-center text-lg uppercase"><%= current_user.email.first %></div>
|
||||
<div class="mr-1.5 text-white w-8 h-8 bg-gray-400 rounded-full flex items-center justify-center text-lg uppercase"><%= Current.user.email.first %></div>
|
||||
</div>
|
||||
|
||||
<div class="absolute z-10 hidden w-screen px-2 mt-2 -translate-x-1/2 left-1/2 max-w-min" data-dropdown-target="menu">
|
||||
|
@ -63,7 +63,7 @@
|
|||
<div>
|
||||
<h2 class="text-sm font-semibold font-display">Cash</h2>
|
||||
|
||||
<% current_family.accounts.each do |account| %>
|
||||
<% Current.family.accounts.each do |account| %>
|
||||
<div class="flex items-center justify-between py-2">
|
||||
<div class="flex items-center text-sm">
|
||||
<%= account.name %>
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<h1>Update Password</h1>
|
||||
|
||||
<%= form_with model: current_user, url: password_path do |form| %>
|
||||
<%= form_with model: Current.user, url: password_path do |form| %>
|
||||
<%= auth_messages form %>
|
||||
|
||||
<div>
|
||||
|
|
9
test/models/current_test.rb
Normal file
9
test/models/current_test.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
require "test_helper"
|
||||
|
||||
class CurrentTest < ActiveSupport::TestCase
|
||||
test "family returns user family" do
|
||||
user = users(:bob)
|
||||
Current.user = user
|
||||
assert_equal user.family, Current.family
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue