Email Subscription Workflow - Part 6 - Unsubscribing from Emails - Ruby on Rails

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] continuing on we need to add the concept of unsubscribing from these these updates to a user model and the idea here is to add a new field to the user model that will essentially generate a vein a random hex number that we can add to the link in the email so it's somewhat secure I like more secure than just having the user log in every time and no one wants to do that when they unsubscribe from something so this kind of takes that and runs with it wouldn't say it's the most perfect feature or way it to implement it but it's the way I found to be the the best for me so what I want to do is first generate a new column that we add to the user model it's going to be called unsubscribe hash so rails generate migration add unsubscribe hash to users and I'll say unsubscribe hash strength rails we'll just go ahead and say rails DB migrate I do the same thing in my hello rails course if you've ever checked that out we do kind of a similar concept it's a reddit clone we build where we have unsubscribing mechanisms from emails and whatnot so it's kind of anything that I'm just including for free here so hope you dig it on the user model we need to add this unsubscribe method so the idea here is every time a user is created we're gonna generate a secure token and save it to their actual user model in the database and the idea here is when they do create this or click this link in the email that number is gonna match up and then it'll be them only them who can actually do this thing and perform this method so what that looks like is before create we can do these callback functions on models just like you would an in controller so it's kind of nice we'll say add unsubscribe - and we'll create a private method block so everything below here is gonna be private and in a little bit and say add unsubscribe hash and this will essentially call the user class and set it to what's baked into Ruby so secure random hex that just generates a new token a new number and sets it to that column right before the users created once it's saved now at this point we don't actually have those in our existing users so this would only pertain to new users you create on the fly you could go through in your console and update those so I'll do that so what I want to do is take all the users and map through each so we'll just say user user update and then pass in that unsubscribe unsubscribe hash column and within it we'll just do that secure random dot hex call if you've run this it should be all set so if we say user all oh I don't think I'd close that that's what's wrong let me back out and try this again I made a type of there copy this line paste I need to close this boom there we go so notice that each user got updated that's just two users in my database if you're doing this on a bigger app that might take a while so beef beef or wormed that might be a thing to deal with next with that in mind we can just say I mean if you really want to verify with your just Rails console user not last it's gonna be J Smitty notice the hash it created so it's the scrambled number that we're gonna access later so to use this in practice we need some new routing so I'll just add a new path for this particular unsubscribe and it can be anywhere but we're gonna say a match and we're gonna say users unsubscribe this can be like anything really if you want it but then we'll pass in the unsubscribe hash and let's get a map to our controller we're gonna create called email and then an unsubscribe method on it or action and then we'll say as unsubscribe and then via all routes in our in our project here we can see there's a new for just a unsubscribe the fact that we call it as unsubscribe gives us this shorthand for this giant URL otherwise it's going to point to an emails controller called unsubscribe so we can create that controller next and I'll just generate it we can just say rails generate controller emails you just say emails yeah and then in it we could just say unsubscribe okay in that controller it should just have that one method now nothing in it but it will create a view which is what we want the view is gonna be where you end up when you do click that link in the email it's going to say like successfully unsubscribe so we can add that next I'll actually kind of pull that from my other project just to save some time just some generic markup but we do add a little bit of of a reason of what you've opted out from that all happens in the controller which will account for next and then in the actual email we need to render the unsubscribe link now so what we can do is create a shared folder I'm just gonna call it shared inside I'll just call it unsubscribe link or something so unsubscribe so in there will be some general markup for this this class I'll just set a style of font size 14 pixels margin top 16 and Outlook is weird so you can do this other margin top 16 pixels so it renders in certain mail clients that are annoying then we'll just say link to and we'll pass unsubscribe URL that's the one we just generated in the routes inside will say that user unsubscribe hash so the at user again if you recall our mailers correlates to the user getting the email so and all these mailers we have this user instance so you need to have that and that's where that's coming from what we can do though with its URL we can pass the pass in like a parameter through the URL saying okay what's what is it subscribing from so that's what this is going to be I'm going to call it subscription and then a subscription type will be what we render through the partial as a like an instance or ID so that'll make more sense here in a second so this will render in each mailer now let me double check everything's typed too right I think so so in all of our mailers we just render that partial now we can go back to our views task mailer let me shared unsubscribe link and then we'll say subscription tight like should be a key like a key we give it so for this one task created will just say task created fast completed and you'll see why we do this in a second okay now if we want to go check out those those emails just to see what the link looks like we can do that so we're ELLs mailers just check one out it just looks like this but look at the URL there if you look at the lower left of my screen has that secure indem hash and then our routing and then notice the subscription parameter is passing in what we identified it as adding to project so this relates to that specific email so the task completed will have a similar thing so that way in the controller we can kind of do some logic with it knowing which one we want to update so in the controller layer this is where the magic really happens we want to go back to that email controller in the unsubscribe link we're gonna do first find the user based on that hash we passed so in active record depending on the column on your model you can just do a fine by whatever the model name is it's pretty handy so in this case that's what we're doing so params and then we're gonna pass in the one that comes through the on the link so unsubscribe hash which is what we called it that gives us access to the user model which we're gonna update based on the parameters set so case statement will say whatever the subscription comes in as we're gonna do some logic to round subscript shown so we'll say when added to product you do it in block here so well first set a reason instance just to say update the view being added you'll see what this looks like in a second projects and user dot update notify when added to project just be setting these to false if your unsubscribing so that make sense that we could do a win tasks created kind of a similar thing here copy these but just change a few things okay so that should go ahead and toggle that boolean in the background when we do click that link in the email so what that ends up doing if you go and just click one just for grins and you got a typo emails controller in the output you have one too many in blocks here yeah so we get this view if you go to your profile settings you'll see it there it didn't do it because I think I am NOT the original sender of that email yeah this is going to Andy so that wouldn't count so if I if I sign out and go in as my other account and go to that email again I'll say turn this off okay notice the URL go to my settings that should be turned off so pretty cool it works now one thing we could do is make sure that when that happens if I do turn this off we shouldn't be sending that email you know so one thing I want to do first though is get a mail catcher set up so if you've never heard of mail catcher it's a nice little tool to add to your rails app it's another gem third-party gem so you'll need to install it as an actual just gem not in your gem file and then we need to add this line right here to our development environment so I'll just go do that real quick just at the ends fine and once saved you'll need to reboot your server when I open up a new tab and I'll run mail catcher I already have this installed so you might need to install it on your own first and when you fire it up you can hit this second link and in little go ahead and run these emails so a few are already sent I think I was demoing this the other day so I'll clear those out and right now if we were to go create some sort of task or a project let's say a task at this point so hello world task is created I don't see an email that's because we aren't running sidekick in the background so one other thing we need to do on top of running mail catcher and our rails server is running sidekick so you can do that assuming you have sidekick in your project bundle exact sidekick this is what I typically run you need to queue up the default and then mailers you can see the documentation on that if you're not sure what that means should kick up this Jim know which is the queuing service and then you might see an email come through at that point because it was queued up that deliver later method we called earlier that's what's going on and it won't send until the sidekicks like running so this ended up working we did get an email saying this project a new task was created I could go ahead and do the same for it was checked or completed see if that works maybe we have an issue there double checks so if we get any logs that should be sending an email but it's not a perimeter ID it is completed see her controller real quick so the tasks troller create projects a project users current user tasks me over tasks okay it looks good one thing I gotcha is first need to do this I need to check first if the task is updated before running this will say that save that there and try that again there we go now I'll get an email the first thing is to verify that was saved so that's good to go and then what was the other one completed created and then added to a product so maybe if we make a new project let's go to the product new product screw second got an email for that and 11 said you second project cool so if I toggle that off okay that should work hmm oh this is this is from this is to me I'm logged in so it needs to be the other user in this case so that's a little hiccup that's kind of confusing of sending these emails locally but other than that this seems to be working so everything's going great what we need to do now is if that person is going to toggle off that preference we should probably not email them as a result so what we can do is go into our controllers where we added the logic to loop through each user and just add the correlating boolean property and that's basically wrapping things up so here we added that comment early on if we just do a if user dot notify when added to project just a question mark basically if that's true we'll send that email otherwise skip it this is kind of performance heavy though you might want to extract this into some other maybe background job on its own you might even do this stuff after a model like we did on our unsubscribe hash like you know that after create concept you can do it even on the model there the concept that's tough with that is getting access to the current user you can't have that as easily in your in your models it is possible it's just not so much of a convention so in our task controller we'll do the same thing so instead of this mailer will say if user dot notify when tasks created send that and then this one will be the same of completed okay so those essentially should check first if those emails are sent so say I am unsubscribed let's let's check all my settings first let's say I'll unsubscribe from everything and just update that there we go so if I were to you know make it to do tasks that goes to Jay Smitty but if I was kind of confusing if I log out and go into Jay Smitty and go into my profile I'll uncomment everything oops I need to pass him at password update and then logout go back into my other accountant so we can see if I make changes John Smith should get notified but since he hasn't subscribed to get notified we shouldn't see any email so I just completed that task created a task no email sent if I create a new project and add John to it he shouldn't get a project notification so and him cool notice no emails came in so this this mail catcher service it delivers any email for any user so you gotta think about it that way that's what's might be confusing but in this essence those subscriptions aren't coming in so everything's working great and our unsubscribe links are working too as well so if I go to my subscriptions that none or set here if I update that save go to logout to go to John Smith and I'll add a new task I'll complete a task I do get an email okay so I'm gonna go log back out now log in is Andy I know this is probably confusing his old he'll go back to that email this is the one sent to me directly because I have been subscribed I want to unsubscribe go to my profile settings I should not see that now when a task is completed so that's working well great so everything seems to be working guys so that's that's the gist of how I'm adding email subscriptions is this day and age it's not perfect because it could be cleaned up there's a lot of a repetitive code here a repeating code these things could probably be extracted to your modeler if you really wanted to and maybe even accept two concerns that are included so you don't have so much logic going on for each action but I wanted to give you at least the foundation of something like this I know it's something that honestly I had trouble finding anywhere on the Internet of like a good tactic or a way to do this without you know ripping my hair out so hopefully that's been helpful you could take this a step further and just render like way better UI around like the user logic when you add a person to a project or you know any of that stuff but just taking into consideration this is how that stuff works hopefully you enjoyed the Ajax portion as well so that's it for now if you have any questions or feedback feel free to leave it in the comments this will be a blog post as well on my blog so go check that out for the written version and until next time thanks again for watching and a channel actually just passed 10,000 subscribers so I can't thank you all enough for the support really helps me keep going and I really appreciate it alright guys peace hello rails is my new course on Ruby on Rails I'll teach you Ruby on Rails from the ground up visit hello rails IO for more information [Music] you
Info
Channel: Web-Crunch
Views: 916
Rating: undefined out of 5
Keywords: ruby on rails, email subscriptions, ruby, rails, ruby on rails 2020, ruby on rails tutorials, web crunch, web-crunch, rails mailers, rails action mailer, rails background jobs, sidkiq, action mailer, rails unsubscribe from email, rails email notifications, email notifications, automated emails rails, rails tutorial, hello rails, rails email subscription workflow, email, emails ruby on rails, sending emails ruby on rails
Id: UujxXDwr1XA
Channel Id: undefined
Length: 25min 7sec (1507 seconds)
Published: Mon Jun 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.