Async::Messaging
The Async Messaging gem (Ruby on Rails) aims to simplify User notifications when handling asynchronous calls within your application, via messaging.
For example: you're using Delayed::Job (https://github.com/collectiveidea/delayed_job) and have completed a 'delayed' action, but want to notify the user about the status after it's completed. A simple flash[:notice] isn't going to work because you're outside User context.
NOTE: Currently this gem is under heavy development, function calls will change in the future
The capabilities of async-messaging are required for a private project, which is MongoDB/ Mongoid backed. Alternate messaging systems are not capable of handing these requirements (https://www.ruby-toolbox.com/categories/User_Messaging_Systems).
Roadmap
Version (0.0.1)
- Functional gem to push User notifications via flash messages
Future features (subject to change)
- Javascript flash message pull
- Activerecord support
- Email style user messaging
Installation
Add this line to your application's Gemfile:
gem 'async-messaging', github: "dblommesteijn/async-messaging"
And then execute:
$ bundle
Configuration
Generate initialize script for async-messaging
$ rails g asyncmessaging:install
Configure initializer script with your Models config/initializer/async-messaging.rb
# Async::Messaging initializer
# Use this hook to define your messaging setup
Async::Messaging.setup do |config|
# mongoid configuration
require 'devise/orm/mongoid'
# provide the root user instance (system messaging source)
# NOTE: this can/ should be a dummy account!
config.model_root = User.find("5333f81244656eccdc020000")
end
Add flash handler to your Controller app/controllers/application_controller.rb
# handle flash messaging from Async::Messaging helper
include Async::Messaging::Controller
NOTE: Async::Messaging::Controller adds before_filter :help_async_messaging that collects all user flash messages from the database and creates flashes.
Cancel flash message behaviour (skip before_filter)
skip_before_filter :help_async_messaging
Prepare your flash notification view
Below is an example of flash view (in slimlang) and Bootstrap. The example will not show any values other than Strings, now we need to expand on this by switching if key == :async_messaging
div.row
div.col-xs-12
- flash.each do |key, value|
- if key == :async_messaging
/ async_messaging flash
- value.each do |notification|
- next if notification.empty?
div{class="alert alert-info fade in"}
a.href{data-dismiss="alert" class="close"}
li.fa.fa-times
/ prettify the message
p
strong
= notification[:subject]
= " ".html_safe
= notification[:content]
- else
/ default flash
div{class="alert alert-info fade in"}
a.href{data-dismiss="alert" class="close"}
li.fa.fa-times
= value if value.is_a? String
Generating Messages
Notify a user, send a system message. NOTE: These messages are consumed upon retreival.
# sends a system message to a user as flash message
Async::Messaging::Notify.notify_user(User.first, subject: "System Notice", content: "How are you?", category: :flash, flags: [:notice])
NOTE: on page reload the flash message is collected in the ApplicationController via the before_filter in Async::Messaging::Controller.
Contributing?
- Fork it ( http://github.com/dblommesteijn/async-messaging/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request