diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index 1554ab84..814a8854 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -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 diff --git a/app/controllers/concerns/authentication.rb b/app/controllers/concerns/authentication.rb index e568a56f..3fddee29 100644 --- a/app/controllers/concerns/authentication.rb +++ b/app/controllers/concerns/authentication.rb @@ -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? - 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]) + if user = User.find_by(id: session[:user_id]) + Current.user = user + else + redirect_to new_session_url + end end def user_signed_in? - current_user.present? + Current.user.present? end def login(user) diff --git a/app/controllers/passwords_controller.rb b/app/controllers/passwords_controller.rb index b1e859c6..56b24b60 100644 --- a/app/controllers/passwords_controller.rb +++ b/app/controllers/passwords_controller.rb @@ -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 diff --git a/app/models/current.rb b/app/models/current.rb index 73a9744b..fd51b793 100644 --- a/app/models/current.rb +++ b/app/models/current.rb @@ -1,3 +1,5 @@ class Current < ActiveSupport::CurrentAttributes attribute :user + + delegate :family, to: :user end diff --git a/app/views/accounts/index.html.erb b/app/views/accounts/index.html.erb index 8e067a3c..39bc4efe 100644 --- a/app/views/accounts/index.html.erb +++ b/app/views/accounts/index.html.erb @@ -1,8 +1,8 @@

Cash

-

<%#= number_to_currency current_family.cash_balance %>

+

<%#= number_to_currency Current.family.cash_balance %>

-<% current_family.accounts.each do |account| %> +<% Current.family.accounts.each do |account| %>
<%= account.name %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 59d4379d..bcfed54a 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -32,7 +32,7 @@ <% end %>
-
<%= current_user.email.first %>
+
<%= Current.user.email.first %>