| 1 | # Be sure to restart your server when you modify this file. |
| 2 | # |
| 3 | # +grant_on+ accepts: |
| 4 | # * Nothing (always grants) |
| 5 | # * A block which evaluates to boolean (recieves the object as parameter) |
| 6 | # * A block with a hash composed of methods to run on the target object with |
| 7 | # expected values (+votes: 5+ for instance). |
| 8 | # |
| 9 | # +grant_on+ can have a +:to+ method name, which called over the target object |
| 10 | # should retrieve the object to badge (could be +:user+, +:self+, +:follower+, |
| 11 | # etc). If it's not defined merit will apply the badge to the user who |
| 12 | # triggered the action (:action_user by default). If it's :itself, it badges |
| 13 | # the created object (new user for instance). |
| 14 | # |
| 15 | # The :temporary option indicates that if the condition doesn't hold but the |
| 16 | # badge is granted, then it's removed. It's false by default (badges are kept |
| 17 | # forever). |
| 18 | |
| 19 | module Merit |
| 20 | class BadgeRules |
| 21 | include Merit::BadgeRulesMethods |
| 22 | |
| 23 | def initialize |
| 24 | # If it creates user, grant badge |
| 25 | # Should be "current_user" after registration for badge to be granted. |
| 26 | # Find badge by badge_id, badge_id takes presidence over badge |
| 27 | # grant_on 'users#create', badge_id: 7, badge: 'just-registered', to: :itself |
| 28 | |
| 29 | # If it has 10 comments, grant commenter-10 badge |
| 30 | # grant_on 'comments#create', badge: 'commenter', level: 10 do |comment| |
| 31 | # comment.user.comments.count == 10 |
| 32 | # end |
| 33 | |
| 34 | # If it has 5 votes, grant relevant-commenter badge |
| 35 | # grant_on 'comments#vote', badge: 'relevant-commenter', |
| 36 | # to: :user do |comment| |
| 37 | # |
| 38 | # comment.votes.count == 5 |
| 39 | # end |
| 40 | |
| 41 | # Changes his name by one wider than 4 chars (arbitrary ruby code case) |
| 42 | # grant_on 'registrations#update', badge: 'autobiographer', |
| 43 | # temporary: true, model_name: 'User' do |user| |
| 44 | # |
| 45 | # user.name.length > 4 |
| 46 | # end |
| 47 | grant_on 'users#show', badge_id: 1, temporary: true do |user| |
| 48 | !user.username.nil? |
| 49 | end |
| 50 | |
| 51 | # Probaby not the best part to grant the badge. |
| 52 | grant_on 'users#show', badge_id: 2, temporary: true do |user| |
| 53 | user.spotify&.onboarded? |
| 54 | end |
| 55 | |
| 56 | # Probaby not the best part to grant the badge. |
| 57 | grant_on 'users#show', badge_id: 3, temporary: true do |user| |
| 58 | user.google&.onboarded? |
| 59 | end |
| 60 | end |
| 61 | end |
| 62 | end |