Skip to content
🛎 Notifications Center engine like GitHub or other application for any Rails applications.
Ruby HTML CSS Other
Branch: master
Clone or download
Latest commit 2ac29b4 Mar 28, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
app Bundle update and autocorrect Sep 19, 2018
bin Bundle update and autocorrect Sep 19, 2018
config Break changes: Feb 21, 2017
db/migrate Bundle update and autocorrect Sep 19, 2018
gemfiles Remove support for Rails 4.2 (#16) Mar 6, 2018
lib Dependecy Rails >= 5 Jan 19, 2019
test Bundle update and autocorrect Sep 19, 2018
.gitignore Remove support for Rails 4.2 (#16) Mar 6, 2018
.travis.yml Remove support for Rails 4.2 (#16) Mar 6, 2018
CHANGELOG.md Dependecy Rails >= 5 Jan 19, 2019
Gemfile Bundle update and autocorrect Sep 19, 2018
Gemfile-dev
Gemfile.lock Bundle update Mar 29, 2019
LICENSE Initial commit Mar 28, 2016
MIT-LICENSE First commit Mar 28, 2016
Makefile Remove support for Rails 4.2 (#16) Mar 6, 2018
README.md Bundle update Nov 16, 2018
Rakefile Bundle update and autocorrect Sep 19, 2018
notifications.gemspec Dependecy Rails >= 5 Jan 19, 2019

README.md

Notifications

Mountable notifications for any Rails applications.

Gem Version Build Status codecov.io

Example:

2016-03-29 10 48 16

Installation

# Gemfile Rails ~> 5
gem 'notifications', '~> 0.6.0'
# Gemfile for Rails ~> 4.2
gem 'notifications', '~> 0.5.0'

And then run bundle install.

You now have a notifications generator in your Rails application:

$ rails g notifications:install

You can generate views, controllers if you need to customize them:

$ rails g notifications:views
$ rails g notifications:controllers

Usage

Create a Notification

class User
  def follow(user)
    Notification.create(notify_type: 'follow', actor: self, user: user)
  end
end

class Comment
  belongs_to :post
  belongs_to :user

  after_commit :create_notifications, on: [:create]
  def create_notifications
    Notification.create(
      notify_type: 'comment',
      actor: self.user,
      user: self.post.user,
      target: self)
  end
end

Get unread notifications count for a user:

count = Notification.unread_count(current_user)

Write your custom Notification partial view for notify_types:

If you create a notify_type, you need to add a partial view in app/views/notifications/ path, for example:

# There have two notify_type
Notification.create(notify_type: 'follow' ....)
Notification.create(notify_type: 'mention', target: @reply, second_target: @topic, ....)

Your app must have:

  • app/views/notifications/_follow.html.erb
  • app/views/notifications/_mention.html.erb
# app/views/notifications/_follow.html.erb
<div class="media-heading">
  <%= link_to notification.actor.title, main_app.user_path(notification.actor) %> just followed you.
</div>
# app/views/notifications/_mention.html.erb
<div class="media-heading">
  <%= link_to notification.actor.title, main_app.user_path(notification.actor) %> has mentioned you in
  <%= link_to notification.second_target.title, main_app.topic_path(notification.second_target) %>
</div>
<div class="media-content">
  <%= notification.target.body %>
</div>

NOTE: When you want use Rails route path name in notification views, you must use main_app prefix. etc: main_app.user_path(user)

About Notification template N+1 performance

It is recommended that you use second_level_cache for solving N+1 performance issues.

Contributing

Testing for multiple Rails versions:

make test_51
# or test all
make test

Site Used

License

The gem is available as open source under the terms of the MIT License.

You can’t perform that action at this time.