Code Monkey home page Code Monkey logo

notifications's Introduction

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:

# unread count
unread_count = Notification.unread_count(current_user)

# read count
read_count = Notification.read_count(current_user)
# for non-user class
Notifications.config.user_class = 'Member'

#or change 

Notifications.configure do
  # Class name of you User model, default: 'User'
  self.user_class = 'User'

  # Method of user name in User model, default: 'name'
  # self.user_name_method = 'name'

  # Method of user avatar in User model, default: nil
  # self.user_avatar_url_method = nil

  # Method name of user profile page path, in User model, default: nil
  # self.user_profile_url_method = 'profile_url'

  # authenticate_user method in your Controller, default: nil
  # If you use Devise, authenticate_user! is correct
  # self.authenticate_user_method = 'authenticate_user!'

  # current_user method name in your Controller, default: 'current_user'
  # If you use Devise, current_user is correct
  # self.current_user_method = 'current_user'
end

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.

notifications's People

Contributors

huacnlee avatar eric-guo avatar gagangupta19 avatar joaumg avatar davidzhu001 avatar asiansteev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.