Native system notifications in rails applications (development mode)

Let's just say that from time to time you would like to display a notification on your screen of some event that occurred. As an example let's display a request time. The problem occurs when you work in heterogeneous environment and your friends use either linux or mac. In that case you might be tempted to define Gemfile as:

That is not going to work correctly and will lead to problems. Every time you run bundle install on different platform it will resolve dependencies differently and cause problems for your coworkers that are in the second operating system camp. And in worst scenario you will be hit when last Gemfile.lock is committed from Mac but you try to deploy to linux production server using bundle --deployment which is not allowed to recalculate dependencies. You can recognize it by the message "You have modified your Gemfile in development but did not check the resulting snapshot (Gemfile.lock) into version control".

What we basically want to achive is described in Gemfile manual:

"If a gem should only be used in a particular platform or set of platforms, you can specify them. Platforms are essentially identical to groups, except that you do not need to use the --without install-time flag to exclude groups of gems for other platforms."

Except that we want to load different gems depending on ruby platform (that is not the same thing as platform). In other words we want bundler to resolve dependencies considering both libnotify and growl but install only these which are going to work in current environment.

Run once bundle install --without darwin on linux or bundle install --without linux on mac and bundler will store these setings in .bundle/config

Next time you can just use bundle install on your dev machine.

For your production server configure capistrano to skip both of them:

Now we can try to use them in our development mode. We need a way to detect current operating system. There are 2 easy ones that I am familiar with:

I am aware that RUBY_PLATFORM might return "java" but don't care since probably both of them won't work in Java anyway. Finally the small code for displaying a notification on your system which goes into the bottom of config/environments/development.rb so that it is only loaded in development environment (obviously):

Did you like it? Here is what you can do now:

Popularne posty z tego bloga

Ruby vs Kotlin: Custom Comparable classes

re-enable tray icons in ubuntu 13.10?