Skip to content

Commit

Permalink
Merge pull request #19 from dalvarez2596/lesson-58
Browse files Browse the repository at this point in the history
Add online function for users
  • Loading branch information
dalvarez2596 authored Oct 26, 2024
2 parents 5bab99f + c5864f6 commit f76d65a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
6 changes: 6 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class ApplicationController < ActionController::Base
before_action :authenticate_user!
protect_from_forgery

after_action :user_activity
allow_browser versions: :modern

include Pundit::Authorization
Expand All @@ -16,6 +17,11 @@ def set_global_variables
end

private

def user_activity
current_user.try :touch
end

def user_not_authorized # pundit
flash[:alert] = "You are not authorized to perform this action."
redirect_back_or_to(root_path)
Expand Down
5 changes: 4 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ def assign_default_role
end
end

private
def online?
updated_at > 2.minutes.ago
end

private
validate :must_have_a_role, on: :update
def must_have_a_role
errors.add(:role_ids, "Must have a at least one role.") unless roles.any?
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/_user.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.card.my-2
.card-header
.fa.fa-user
%span{ class: user.online? ? "text-success fa fa-user" : "text-danger fa fa-user" }
= link_to user.email, user_path(user)
.card-footer
Signed up
Expand Down

0 comments on commit f76d65a

Please sign in to comment.