Class: AdHonorem::Progress

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/adhonorem/models/progress.rb

Overview

Store users’ progress in database for each badge triggered at least once

Constant Summary collapse

PROGRESS_TYPES =
{
  numeric: :capped_numeric_progress,
  percentage: :percentage_progress,
  stringified: :stringified_progress
}.freeze

Instance Method Summary collapse

Instance Method Details

#advance(amount = 1) ⇒ Object



14
15
16
17
18
# File 'lib/adhonorem/models/progress.rb', line 14

def advance(amount = 1)
  return if done?
  self.numeric_progress += amount
  save
end

#done?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/adhonorem/models/progress.rb', line 24

def done?
  numeric_progress >= objective.amount_needed
end

#objectiveObject



20
21
22
# File 'lib/adhonorem/models/progress.rb', line 20

def objective
  badge.objectives[objective_slug.to_sym]
end

#progress(progress_type = :numeric) ⇒ Object



28
29
30
31
# File 'lib/adhonorem/models/progress.rb', line 28

def progress(progress_type = :numeric)
  raise AdHonorem::NotSuchProgressType unless PROGRESS_TYPES.keys.include?(progress_type)
  send(PROGRESS_TYPES[progress_type])
end