#136 User Notifications
6-3-2018

Summary

Using the Notifications Rails Engine, we are able to add notifications to our applications based on certain events. The engine provides the schema and necessary controls and associations to seamlessly integrate into your app.
8
rails notifications engine 13:08

Summary

Terminalrails g notifications:install
rails db:migrate

rails g notifications:controllers
rails g notifications:views
Gemfilegem 'notifications'
layouts/application.html.erb<div class='container'>
  <ul class='navigation'>
  <% if user_signed_in? %>
    <li><%= link_to pluralize(Notification.unread_count(current_user), 'Notification'), notifications_path %></li>
    <li><%= link_to current_user.email, main_app.edit_user_registration_path %></li>
    <li><%= link_to 'Logout', main_app.destroy_user_session_path, method: :delete %></li>
  <% else %>
    <li><%= link_to 'Login', main_app.new_user_session_path %></li>
  <% end %>
  </ul>
  <%= yield %>
</div>
models/comment.rbclass Comment < ApplicationRecord
  belongs_to :post
  belongs_to :user

  after_commit :create_notifications, on: :create

  private

  def create_notifications
    Notification.create do |notification|
      notification.notify_type = 'post'
      notification.actor = self.user
      notification.user = self.post.user
      notification.target = self
      notification.second_target = self.post
    end
  end
end
views/notifications/_post.html.erb<div class=''>
  <%= link_to notification.actor.email, main_app.user_path(notification.actor) %> has commented in
  <%= link_to notification.second_target.content, main_app.user_post_path(notification.user, notification.second_target) %>
</div>

<div class=''>
  <%= notification.target.content %>
</div>
db/routes.rb  mount Notifications::Engine => "/notifications"


RailsCod3rFuture said 4 months ago:

If its not too much to ask. Can you please make a similar video for activity_notification gem? Its almost identical to this one, but it may be more optimized. https://github.com/simukappu/activity_notification

kobaltz PRO said 4 months ago:

I've had a chance to look at this gem. Its documentation is pretty extensive which is always nice to see. I'll consider covering this gem in the future. One thing that I highly dislike about this gem is that it seems a lot more invasive in the models. It also has a lot of added complexity based on the documentation and I'm not convinced that it's necessary to accomplish what they're doing. Overall the gem does look pretty cool and for a large complex app, this may be a good fit.

blimey85 said 4 months ago:

Second vote for the activity_notification. It looks very slick and something I'm gonna add in to my project soon.

Login to Comment