ScottOBot
ScottOBot

Reputation: 849

Ruby on Rails - Help Adding Badges to Application

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?

Upvotes: 1

Views: 880

Answers (1)

shlajin
shlajin

Reputation: 1576

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

Upvotes: 3

Related Questions