Giter VIP home page Giter VIP logo

wupee's Introduction

Breaking changes

After using this gem in a few projects, I realized that all projects have different needs concerning the configuration of notifications (whether or not the user should received a notification or an email). We have apps which doesn't have the need for configuration so we are having a lots of Wupee::NotificationTypeConfiguration created and fetched from db for nothing. I really feel that the app should be responsible for addressing this. Also, in some apps, I had to reopen classes to override default behaviour of the gem or skip callbacks and adding others and I was feeling that something was wrong. See this issue for another example. Therefore, this branch removes all Wupee::NotificationTypeConfiguration but leaves a door open to customization, see Wupee initializer.

Wupee

Wupee is a simple gem which tries to fill the gap of lacking gems to manage notifications in Rails app. Wupee is an opinionated solution which assumes that users needs to:

  • be able to receive notifications in the app
  • be able to receive notifications by email

The main object of the solution is the Wupee::Notification which stores:

  • receiver (polymorphic): the recipient of the message
  • attached_object (polymorphic): the subject of the notification
  • notification_type_id: a reference to a Wupee::NotificationType object
  • is_read: boolean

Install:

To use it, add it to your Gemfile:

gem 'wupee'

and bundle:

$ bundle

Run the generator, install migrations and migrate:

$ rails g wupee:install
$ rake wupee:install:migrations
$ rake db:migrate

Running the generator will do a few things:

  1. create wupee initializer:
# config/initializers/wupee.rb
Wupee.mailer = NotificationsMailer # the class of the mailer you will use to send the emails
Wupee.deliver_when = :now # use :later if you already configured a queuing system

# uncomment and implement your logic here to avoid/permit email sending to your users
# leave it commented if you always want your users received emails
# Wupee.email_sending_rule = Proc.new do |receiver, notification_type| 
#   # logic goes here, returning a boolean
# end

# uncomment and implement your logic here to avoid/permit email sending to your users
# leave it commented if you always want your users received notifications
# Wupee.notification_sending_rule = Proc.new do |receiver, notification_type| 
#   # logic goes here, returning a boolean
# end
  1. create a mailer NotificationsMailer which inheritates from Wupee::NotificationsMailer
# app/mailers/notifications_mailer.rb
class NotificationsMailer < Wupee::NotificationsMailer
  default from: '[email protected]'
  layout false
end
  1. adds wupee to your locale yml file (for email subjects)
# config/locales/en.yml
en:
  wupee:
    email_subjects:

Getting started:

Generate a new notification type

rails g wupee:notification_type user_has_been_created

Will execute a few things:

  1. add an entry to your locale yml file :
en:
  wupee:
    email_subjects:
      user_has_been_created: "user_has_been_created"

Feel free to edit the subject, you can put variables, example

 ...
     user_has_been_created: "New user created: %{user_full_name}"
  1. create a json template for the notification:
json.subject ""
json.body ""
json.url ""
# none of this json attribute are mandatory!

In this template, you have access to the notification variable. You can customize it to fit your need, this is just an example.

  1. create an empty html template for the notification:
<!-- app/views/wupee/notifications/_user_has_been_created.html.erb -->
  1. You will have to create your email template as the generator doesn't create it. For example, if your mailer is named NotificationsMailer, your template will take place in app/views/notifications_mailer/user_has_been_created.html.erb

Use the concerns

Wupee::Receiver

Including the concern Wupee::Receiver in your receiver class (probably the User class) permits a few things:

  • get notifications of a user: @user.notifications
  • destroy Wupee::Notification associated to the receiver from db if it is destroyed

Wupee::AttachedObject

Including the concern Wupee::AttachedObject in your attached object classe(s) permits a few things:

  • get notifications associated to an attached object: @attached_object.notifications_as_attached_object
  • destroy Wupee::Notification associated to the attached object if it is destroyed

Use the DSL to send notifications

Imagine that you want to notify all admin that a new user signed up in your app and that you have a scope admin in your User class.

 Wupee.notify do |n|
   n.attached_object @the_new_user
   n.notif_type :user_has_been_created # you can also pass an instance of a Wupee::NotificationType class to this method
   n.subject_vars user_full_name: Proc.new { |notification| notification.attached_object.full_name } # variables to be interpolated the fill in the subject of the email (obviously optional)
   n.locals extra_data: "something" # extra_data will be accessible in template as @locals[:extra_data]
   n.receivers User.admin # you can use the method receiver instead of receivers for clarity if you pass only one instance of a receiver
   n.deliver :now # you can overwrite global configuration here, optional
 end

You can also use the method notify this way:

 Wupee.notify attached_object: @the_new_user, notif_type: :user_has_been_created, subject_vars: { user_full_name: Proc.new { |notification| notification.attached_object.full_name } }, locals: { extra_data: "Yeahhhhh" }, receivers: User.admin

Wupee::Api::NotificationsController

The controller have various actions all scoped for the current user:

  • wupee/api/notifications#index : fetch notifications, takes an optional get parameter scopes (example: scopes=read,ordered)
  • wupee/api/notifications#show : fetch a notification
  • wupee/api/notifications#mark_as_read : mark a notification as read
  • wupee/api/notifications#mark_all_as_read : mark all notifications as read

To use this controller, define a controller inheriting from Wupee::Api::NotificationsController, set the routes in your config/routes.rb and define a method current_user which returns the user signed in.

Example:

  # config/routes.rb
  namespace :api, defaults: { format: :json } do
    resources :notifications, only: [:index, :show] do
      patch :mark_as_read, on: :member
      patch :mark_all_as_read, on: :collection
    end
  end

  # app/controllers/api/notifications_controller.rb
  class Api::NotificationsController < Wupee::Api::NotificationsController 
    before_action :authenticate_user! # if you are using devise
  end

Why WUPEE ?

What's UP SlEEde

License

This project rocks and uses MIT-LICENSE.

wupee's People

Contributors

gnepud avatar islamazab avatar nflorentin avatar sairam avatar sylvainbx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wupee's Issues

current_user

Hi, Grate awesome here! I'm just having an issue with the current_user method. I defined it on my main app application controller ....but it just keep ignoring me :( . I'm using rails 5.0.1 and devise. My current_user method is aliased as current_usuario by devise.

Could you please update the readme on how or where you expect to define it?

Thanks!

allow multiple header values in Wupee

Hi,

I am trying to send multiple header values. In a regular actionMailer I can use

headers['X-Header-Tag'] = ['tag1']
headers['X-Header-Tag'] = ['tag2']

Since wupee is doing a merge of headers as a hash, this is not possible.

Mail::Header can be passed as an object from wupee, but there the serialization is not allowing that.

also, the mailer method is expecting a hash
https://github.com/sleede/wupee/blob/master/app/mailers/wupee/notifications_mailer.rb#L23

Prevent email notification based on attached_object_type

current configuration allow to prevent email only on basis of receiver and notification_type.

Wupee.email_sending_rule = Proc.new do |receiver, notification_type| 
end

In my case I need to prevent email notification based on attached_object type. How should I go about it ?

Notification defaults

Hi Sleede team, I've been working these days with wupee on a project, and ended up with a special need:
When a new notification_type is generated, I think it's useful to be able to assign a default_behavior to this one (corresponding with the enum value, on the notification_type_configuration).
I've solved it on my project by adding the column default_behavior, which has the default value of 0 on the database. Then, modifying the after_create callback of the receiver, you can customize it very easily, because it is very handy to be able to configure notifications defaults. Maybe you want a notification to be initialy just via notification only, or just via email, and leave then the freedom to the user to reconfigure it.

So now my notification_types has the column default_behavior, and the callback I'm using is the following one:

Wupee::NotificationType.find_each do |notification_type|
  Wupee::NotificationTypeConfiguration.create! do |config|
    config.notification_type_id = notification_type.id
    config.receiver = self
    config.value = notification_type.default_behavior
  end
end

Now the generator should accept an option like

rails g wupee:notification_type user_has_been_created DEFAULT=email

This is just an idea, of course, feel free to reject it :)
Thanks!

P.S.: Are you going to update the version?

Missing actor of the notification

Hi!

I would like to have an in-app notification like the following:
User A mentioned you on Video A.

"you" would be my receiver
"Video A" or "User A" would be attached objects... It is possible to attach multiple objects?

I think "Video A" should be an attached object and "User A" should fall in another abstraction, for example an Actor

ActionView::MissingTemplate

Hello! I am getting ActionView::MissingTemplate error .-.
Instead of searching on views/wupee/notifications/:notification_type.html.erb, rails is searching only on views/notifications_mailer

How do I print a notification in my template?

I have your gem all configured and working, the notifications are being created as they should, and I have access to current_user.notifications. I'd like to display the notifications in my main layout, in FB style with a bell and dropdown menu.

However when I iterate through the notifications, I don't see any methods for accessing the notification's subject, body or URL as they are set up in the json.jbuilder file. Can you please document this with an example?

Merci beaucoup!

[1.1.4] Migrations - Directly inheriting from ActiveRecord::Migration is not supported and Index name length

Update wupee generator to support Rails 5. Currently the generator creates migration files like this:

class CreateWupeeNotificationTypes < ActiveRecord::Migration
  def change
    create_table :wupee_notification_types do |t|
      t.string :name
      t.timestamps null: false
    end

    add_index :wupee_notification_types, :name, unique: true
  end
end

When they should be like this (note the [5.1]):

class CreateWupeeNotificationTypes < ActiveRecord::Migration[5.1]
  def change
    create_table :wupee_notification_types do |t|
      t.string :name
      t.timestamps null: false
    end

    add_index :wupee_notification_types, :name, unique: true
  end
end

Also check index names length. They are being generated with a length > 63 which breaks on regular gem code base.

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.