Module: Badginator::Nominee

Defined in:
lib/badginator/nominee.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
# File 'lib/badginator/nominee.rb', line 4

def self.included(base)
  base.class_eval {
    has_many :badges, class_name: "AwardedBadge", as: :awardee
  }
end

Instance Method Details

#has_badge?(badge_code) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/badginator/nominee.rb', line 29

def has_badge?(badge_code)
  AwardedBadge.where(badge_code: badge_code, awardee: self).first
end

#try_award_badge(badge_name, context = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/badginator/nominee.rb', line 10

def try_award_badge(badge_name, context = {})
  badge = Badginator.get_badge(badge_name)

  success =  badge.condition.call(self, context)

  if success
    if (self.has_badge?(badge_name))
      status = Badginator::Status(Badginator::ALREADY_WON)
    else
      awarded_badge = AwardedBadge.create! awardee: self, badge_code: badge.code
      status = Badginator::Status(Badginator::WON, awarded_badge)
    end
  else
    status = Badginator::Status(Badginator::DID_NOT_WIN)
  end

  status
end