Reputation: 5693
Are there any gems or libraries that can be used to show a message once to a user? I'm looking in particular for something that allows me to:
I'm a bit stuck with this, because I don't know what these kind of messages are commonly called. Do these kind of one-time messages have a name?
Upvotes: 1
Views: 2285
Reputation: 14635
there are many ways to implement such a notification system, your requirements are pretty basic so I'll stick with a basic approach using flash
Show Message on the first login:
You should keep track of ther user's last login and maybe count the logins, so what you could do:
Add a field "last_login_at" to your User Model which gets updated every time the user logs in, set it to NULL after registration. When the user logs in you check if the last_login_at
is NULL if yes you set a message to the flash like flash[:first_login] = true
then you can render a special welcome message in your template if this flag is set.
A better approach is to add a field like login_counter
which gets incremented every time the user logs in, so if it is zero the user is logging in for the first time. A login counter can also help you to identify "active" users who log in often etc.
There is a popular authentication system called devise which has these (and many more!) features built in: https://github.com/plataformatec/devise
Show maintenance warnings etc.
I'd create a new model called SystemMessage
which has fields like:
id | message_type | title | body | valid_from | valid_until
The message_type
attribute describes what kind of message it is like "maintenace notice", "new features" etc. dependent on the type of the message you could render different templates and adjust the look of the message.
The valid_from
and valid_until
attributes lets you control in which time frame the message should appear.
On every request you fetch these messages and show them to the user, you can also cache the messages on a regular basis to reduce the amount of queries. Have a look at the rails guide about caching: http://guides.rubyonrails.org/caching_with_rails.html
To inform users that are idle on a page you can implement an AJAX request which fires every five minutes or so and fetches the messages and checks if there are any messages newer than the ones already displayed.
If you need to show user-specific messages (like you got 5 new mails, someone commented on your post etc.) in real-time you could use a pub-sub messaging system like 'Faye' (tutorial: http://railscasts.com/episodes/260-messaging-with-faye or just google for "rails 3 faye")
I hope this is a starting point for you :)
Upvotes: 3