-
Notifications
You must be signed in to change notification settings - Fork 569
Conversation
<span class="text-muted css-truncate css-truncate-target">Joined <%= local_time_ago(user.created_at) %></span><br> | ||
<span class="text-muted css-truncate css-truncate-target"> | ||
Joined <%= local_time_ago(user.created_at) %> / | ||
Last active: <%= user.last_active_at.present? ? local_time_ago(user.last_active_at) : 'Unknown' %> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't we set the last_active_at
to created_at
when the user is first created? That way we never have the case where the last_active_at
is unknown.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be null in our main Postgres database.
I was wondering if there is a case that the indexes on Elasticsearch is corrupted/outdated, which might cause this value to be null.
@cyhsutw thanks for doing this. What do you think about writing a data transition script so that we can set these timestamps for everyone? The process I see here is:
Does that sound alright? |
@tarebyte thanks for the feedback. I'm totally 🆒 with the process you suggested.
class SetUserLastActiveAtValue < ActiveRecord::Migration
def change
User.update_all('last_active_at = updated_at')
end
end
|
@tarebyte I added some migration files, if there's no problem with those, I'll go ahead and open separate PRs for each of them. Also, I am working on adding few more test for this change, so I'm marking this as WIP for now. |
I added a test to the Should we add another test to check if the cc/ @tarebyte |
I don't think that's necessary |
become_active | ||
else | ||
auth_redirect | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cyhsutw What do you think about something like this?
return become_active if logged_in? && adequate_scopes?
auth_redirect
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🆒 That's much more elegant!
update_columns(last_active_at: Time.zone.now) | ||
# but we still want indexes to work | ||
self.class.update_index('stafftools#user') { self } | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these comments are useful, but would be better placed at the top.
# This updates the `last_active_at` column without
# updating the model, but keeps the index updated.
def become_active
update_columns(last_active_at: Time.zone.now)
self.class.update_index('stafftools#user') { self }
end
@@ -12,6 +12,12 @@ | |||
end | |||
end | |||
|
|||
describe '#become_active' do | |||
it 'updates the last_active_at attribute' do | |||
expect { user.become_active }.to change { user.last_active_at } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also check here that the updated_at
column remains the same before and after.
it 'does not change the updated_at column' do
updated_at = user.updated_at
user.become_active
expect(updated_at).to eql(user.updated_at)
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in #b3ed41b.
I left a few minor comments, @cyhsutw. After that this is good to go! |
@tarebyte Thanks for reviewing the changes, just updated the code based on your comments. |
Confirmed in production! |
Hello guys,
This PR is related to #356, it tracks the timestamp of a user's last access to the app.
Staffs can view the timestamp using stafftools, as shown below:
The difference between #571 and this pull request is that whether the
updated_at
ofUser
will be updated or not.