Module: Kudos::Achievementable

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/kudos/achievementable.rb

Instance Method Summary collapse

Instance Method Details

#award(achievement:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/models/concerns/kudos/achievementable.rb', line 15

def award(achievement:)
  object = kudos_users_achievements.find_or_initialize_by(kudos_achievement_id: achievement.id)
  return if object.rank.to_i > achievement.rank.to_i

  object.update!(
    {
      rank: achievement.rank,
      points: object.points.to_i + achievement.points,
      title: achievement.title,
      description: achievement.description
    }.compact
  )
end

#awarded?(achievement:) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'app/models/concerns/kudos/achievementable.rb', line 29

def awarded?(achievement:)
  kudos_users_achievements.find_by({ kudos_achievement_id: achievement.id, rank: achievement.rank }.compact)
end