Class: Merit::Badge

Inherits:
Object
  • Object
show all
Extended by:
Ambry::ActiveModel, Ambry::Model
Defined in:
app/models/merit/badge.rb

Class Method Summary collapse

Class Method Details

Defines Badge#meritable_models method, to get related entries with certain badge. For instance, Badge.find(3).users orm-specified


43
44
45
46
47
48
# File 'app/models/merit/badge.rb', line 43

def _define_related_entries_method(meritable_class_name)
  define_method(:"#{meritable_class_name.underscore.pluralize}") do
    sashes = BadgesSash.where(badge_id: id).pluck(:sash_id)
    meritable_class_name.constantize.where(sash_id: sashes)
  end
end

.find_by_name_and_level(name, level) ⇒ Object


30
31
32
33
34
35
36
37
38
# File 'app/models/merit/badge.rb', line 30

def find_by_name_and_level(name, level)
  badges = Merit::Badge.by_name(name)
  badges = badges.by_level(level) unless level.nil?
  if (badge = badges.first).nil?
    str = "No badge '#{name}' found. Define it in initializers/merit.rb"
    fail ::Merit::BadgeNotFound, str
  end
  badge
end