1

I'm creating a rails application that is a backend for a mobile application. The backend is implemented with a RESTful web API. Currently I am trying to add gamification to the platform through the use of badges that can be earned by the user.

Right now the badges are tied to the user through a one-to-many relationship, i.e. the user can have many badges. The badges are rewarded to a user after they have completed various tasks. The completion of tasks are based off of the change of a number of different variables. These variables are incremented/decremented by calling various API endpoints defined within the REST API. Every time these endpoints are called I check to see if the user's conditions have changed and if they qualify for any new badges or if the no longer meet the criteria for certain badges.

Currently the logic that checks to see if any new badges have been added or removed is contained within the view controllers themselves. This is unnecessary and creates a duplicated code in multiple areas.

I am fairly new to RoR and was wondering what is the best practice for implementing this code inside a class that is visible to all of my controllers. I looked into putting it into a helper module but realized this was not the correct solution. What and how should I go about doing this?

ScottOBot
  • 829
  • 3
  • 16
  • 35
  • You can try putting the badge code in a [concern](http://stackoverflow.com/questions/14541823/how-to-use-concerns-in-rails-4) – p4sh4 Jun 22 '15 at 02:52
  • Is that better or worse then just adding a utility class inside the lib folder? @p4sh4 – ScottOBot Jun 22 '15 at 04:11
  • [Read up](http://www.monkeyandcrow.com/blog/reading_rails_concern/) on concerns to know more, it really depends on the situation – p4sh4 Jun 22 '15 at 08:40

1 Answers1

3

Take a look at merit gem. Even if you want to develop your own badge system from scratch this gem contains plenty of nice solutions you can use. https://github.com/merit-gem/merit

shlajin
  • 1,308
  • 8
  • 22