#53 Gem Public Activity - add an Activity Feed to your app

07/09/2021
Transcript
Hello. So in this episode we are going to implement the gem named public activity. The gem lets us have total surveillance of everything that happens inside our application. So imagine we're going to have a separate database table inside our application where we're going to keep track of everything that happened to our post our users or to whatever we have all the great, uh, update and, uh, distractions that happen inside our applications can be tracked, uh, by public activity. And we can have an activity feed where we will see who did vote inside our application and not miss anything. So here's the gem and here's an example feed. So we can see who created the post, who removed, who edited the post, and so on. And we can have this not only for our post, but for users and for any models inside the application. So let's implement the gem and see how it works. Now we see it's again, a wildly popular gem, really used by, by a lot of, uh, repositories. So let's install it and uh, try to see it in action. I'm going to add this gem to our gem file. Okay. And now we are going to run bundle and next we're going to run a migration rails, generate public activity migration. Now again, I run bundle and run rails, generate public activity migration. Okay, let's have a look at this migration. I'm opening DB migrate Great activities and what do we have here? So you have end new table named activities and it has trackable, well, they're going to track either a post or a user or whatever. So it's going to be polymorphic, meaning these are going to be two attributes. It's going to be either user or post. And it's going to be the idea of the user or post. Then they're going to have an owner. This can for example, be their user that, uh, did this activity. So it can be, usually the current user will be saved here. Then they're going to have the key. It'll be something like post update or user dot create. Then parameters. It can be anything you that you can want to add on your own. So let's try to see it in action. I will run rails to be migrate. And uh, next we are going to need to add this to lines inside our model. So we are going to go to our app model mod post RB and we will add, include public activity model and tracked. And now we can actually see if it is working. I will start the Rails console and I'm going to check if we have any activities. Now how can I check if we have any activities? If I just try activity dot count, we get nothing because we have no, uh, constant as activity. So we need to kind of initialize the activities and uh, uh, have a way to view the activities. Now to do this, we are going to run public activity Activity dot count. Okay? And now we have zero activities. So now we are going to create a new post. I will say post create, uh, title, uh, with an activity. So we have created a post and let's count the public activities. You see we have one activity. Let's say we want to have a look at this activity. I'll say public activity lost. And we see, so the checkable type is post the ID is 13. Uh, we don't have any owner sign. The key is post do grade, and we have the time when the post was created. So it was say inside the activities, uh, database table. And uh, that's basically the basic usage of public activity. Now, let's actually have a place where they're going to display all the activities inside our application. So, uh, let's, uh, maybe create a new view for this or it can be just inside our home view. So I'm going to go to the controllers to application controller, no actual to dashboard controller. Yeah, and say public activity activity. And we are going to say add activities equals public activity. Activity Do order. We'll order them by the creation date created at the send in. Okay. And let's display these activities. So I'm going to go to views to our dashboard and inside index I'll try to display the activities. I will say, uh, at activities each do activity And I'll try to display something from the activity. I will say equals activity. Do what do we have here, do to trackable? Okay, let's see if this works. So here we have, uh, well the trackable is the post. That's why we have uh, the name, that title of the post. Let's just display some additional information. Let's see when the activity has been created. So activity do created at then, uh, they're going to have some additional information. What else would we want to have? So can actually be a link to the trackable. So, uh, link two, activity trackable in the title and activity, trackable in the description. And you see it now it is a link to the post. Now why do we have it as a link in the PO to the post? We didn't define that. We want to have the post title. It is because here in post, uh, I defined that two string would be titled If I remove this two string, then you see we just have the post object. 'cause the application doesn't know that we want to get the post, uh, title. So, uh, let's actually remove the two string from here and say a checkable title. And now we declare that we want to see the post title. Even this is a post. But what if you don't want to have, uh, activities only for post? What if you want to have activities for users too? Well, we would do also include this public activity into our user model. Let's do this. I'm going to just include public activity into our user model and let's, uh, actually destroy the current user. I will cancel the account and now log in as another user. Okay, I will sign up and you see we get this error. So undefining method, method, title for user. Well, because the user doesn't have a title. So, uh, if the activity is the post, then uh, if the checkable is the post, then we want to have the title. And if the checkable is not the post, but the user, we don't have maybe the user email address. So we would basically say if activity trackable type is a post, let's see if this works. And if the activity trackable type is the post, then we have this link. And let's also add some kind of row, some kind of break. And if the type, uh, is not post, but uh, user, we would have a link to the user's email. Or actually we don't have a user part at all. So we don't need any link, uh, if it is the user part. Anyway, let's have a look now. So we already have three different activities. Some of for posts, some are for user. Let's just declare that we have, uh, let's just declare what kind of activities these are. Let's say Activ key. And here we see user create, user destroy and post create. Now they are not in the best order. Let's order them by ascending. So looks fine. And let's just, uh, go back to the styling and say that we want to see the key before we see what kind of activity it was. So we have the time, we see what has happened post Great User Destroy. And uh, in the cases of post, we're also going to display the title of the post. Looks good. Now what if we try to update the current user? I will go and update my password. So I'm going to input a new password and update and see the user update has also been saved. And what if you don't want to save the user update? Also, let's go and change the order. Have you done it? Now? It is ascending. Was should be descended maybe. Let's see. Yeah, descendant looks better. So the new activities are on top. Yeah, so what if you don't want to save then a user has been updated. Well, we can save only particular activities in trackable if we want. So going to our user rb, we can say track and we would say that we want to track, uh, only rate and uh, destroy. So now we're not going to track when a user updated his password or updated his account in general. Let's try once again. So now we have just one user update. Uh, okay, it gives us an error. So a syntax error. So there shouldn't be a coma here. Okay, I will go and update my account once again and you see no additional activity has been saved. And also what if you want to see which specific user updated or created a post. Now I go to posts, I go to create a new post and uh, a new activity has been saved. Here's the new activity, but we didn't save any owner for it. So let's try to find the owner activity owner and well, there is no owner. So let's assign the current user as the owner of this activity. How do we do it? Well, we would need to add to lines to our application, uh, controller and to our post rb. If we go to the wiki of this public activity, we can seek quite a few useful, uh, options. If we find user, yeah, let's set the activity owner. So we would include this, include public activity, toe control inside our application controller. And it would let us kind of get the current user information inside our model for the public activity. So let's go to our application controller and I'm going to add include public activity store controller. And now I'm going to add this track to owner inside our post rb. So we'll have not just checked, but we're also going to assign the owner. And by this line they're just saying that we are going to save the current user as the owner of this activity. Let's try once again. So I'm going to, let's say edit the same post update. I go to home and we see that the post has been edited and the user has been saved as the owner. Now we can also have a link to the user's account. So let's say link to activity owner, activity owner and undefined method, user path. Ah, yeah, of course we don't have, uh, a defined user. So let's say just, uh, activity owner do email And uh, some of the activities don't have an owner and don't have an email. So we will say activity owner. And if you have an owner and the owner has an email, then they're going to display the email. So looks quite good. Okay, and what if we want to track the owner for, uh, the user? So here they're going to also assign the owner. It would say, uh, owner, it'll be itself. Now with this public activity, we need to write not just sell, but itself in this case. So let me, let's say, uh, delete my account. I cancel my account and I'm going to create a new account, Okay? And go here this see, uh, have user destroy and user create. And we have the email of the user that, uh, has been created. Save the, the, is it save the, yeah, it is saved as the owner. So looks good. This way we can, uh, actually, uh, save the record itself as the owner of this activity, but not, uh, the user. So not all this the user, the current user has to be an owner of a specific, uh, activity. And what if you want to, let's say run something in our console now. So let's say Rails console and I will say post great, uh, title, uh, with activity tool. Okay? And here we get a rollback, it doesn't work. And define method, current user. Why doesn't it work? Because here when creating this activity for the post, we try to find the current user and we don't have a current user. So we would like to disable public activity when we are running, uh, seeds, uh, or when we're working in the console. So we would say something like Public activity dot enabled equals false. And now we can run this command and it works and it is not saved in public activity. Now this way we can, uh, disable public activity then enabled through enable public activity totally globally around our application. But we can also do it, uh, on uh, Uh, lower level. So we can do it uh, just for a class like post, uh, public activity off or post public activity do on. So looks, uh, quite fine. And what if we want to get, uh, all the activities for a specific post, for example. So what, uh, if we go to our posts index and we want to see how many activities and uh, get some information about all the activities for a specific post. So the easiest way and the most beautiful one would be to have a relationship. We would add a relationship. So we would say that uh, a post has many activities as trackable because we are going to see the activities that disposed is trackable. Then uh, the clause name will be public activity activity like this clause name. Okay? And actually if just in case you want to destroy activities associated with the post, when a post is deleted, you can also add dependent Destroy right here. So let's see if it works. I will uh, try to list how many activities each of the posts has by just getting post.activities.com. So going into our post index, I will, uh, add an activities column And I would say post activities dot count and we see how many activities are associated with the specific post and we can get their IDs. So block id, so like this we can, uh, access the associated, uh, public activities to the specific post. So looks quite nice. Okay, and one more thing that we can do is run and add some pub, some public activities that are totally custom that I associated not just with uh, uh, create destroy or update. So we can say something like, uh, post dot, uh, has been updated or post not status updated or whatever. So let's try adding a custom activity and we would do it somewhere in our post controller. So let's imagine that uh, when we have update, we are going to create a custom activity. So let's, uh, if post has been updated, add a custom activity for this. We would say add post grade activity. Then we would say the activity name. It would be, uh, so like title updated or whatever. And uh, we can actually add some kind of parameters. So parameters and inside the parameters we would say something like, well, we can add anything. We can say, uh, time zone. Now let's see if this works. So I'm going to update a post. Okay, synex error, let's say, uh, time zone. Okay, yeah. So we have to have a key and a value in this parameters and let's try to update some kind of post. So I'm going to go to edit, add some content Post was successfully updated. I go home and have a, see two activities have been created. So one has the key title updated and the other one has just the key update. And we've also saved parameters. So let's add this parameters into our view. We would say, uh, activity and dot parameters. So here you see for the other activities, we don't have any parameters, but here we've, uh, well we've decided to save the time zone, so looks good. Let's, uh, go back and actually disable this post update so that we have only this post of title updated activity when run. The update action to do this would go back to our post controller and here would say post, uh, this, uh, public activity off. Then we would run update, and then again we would say post public activity on, let's see if it works. I will, uh, go and edit the post. Once again, you see just two activities were added at the same time. We don't need that. I update the post And you see the post update activity doesn't work anymore. It is overwritten. We disabled it and instead we run this activity post title updated. Now again, you can, you don't have to do it here, you can just disable it inside our post rb, the same way we did it inside our user rb, they said that we don't want to track anything except Create and Destroy. And here we can also say check only, uh, create and Destroy. So that's basically it. You see, we can have, uh, a database table with all the activities then going to our posts. We can get all the activities for a specific post. We can get all activities that were done by a specific user. And, uh, looks nice. Yeah, and that's basically it. So thanks for bringing me and have fun implementing this feature in your next application.
0
Join the conversation
Sign in to access PRO lessons, access private repos, leave comments, create watch lists.
We collect your email address, name and username to create your account. We do not share your email address with anyone else.