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

Instance 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


60
61
62
63
64
65
# File 'app/models/merit/badge.rb', line 60

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(send "_#{Merit.orm}_sash_in", sashes)
  end
end

.find_by_name_and_level(name, level) ⇒ Object


38
39
40
41
42
43
44
45
46
# File 'app/models/merit/badge.rb', line 38

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

.last_granted(options = {}) ⇒ Object

DEPRECATED: `last_granted` will be removed from merit, please refer to: github.com/tute/merit/wiki/How-to-show-last-granted-badges


50
51
52
53
54
55
# File 'app/models/merit/badge.rb', line 50

def last_granted(options = {})
  warn '[merit] [DEPRECATION] `last_granted` will be removed from merit, please refer to: https://github.com/tute/merit/wiki/How-to-show-last-granted-badges'
  options[:since_date] ||= 1.month.ago
  options[:limit]      ||= 10
  BadgesSash.last_granted(options)
end

Instance Method Details

#_active_record_sash_in(sashes) ⇒ Object


33
34
35
# File 'app/models/merit/badge.rb', line 33

def _active_record_sash_in(sashes)
  {sash_id: sashes}
end

#_mongoid_sash_in(sashes) ⇒ Object


29
30
31
# File 'app/models/merit/badge.rb', line 29

def _mongoid_sash_in(sashes)
  {:sash_id.in => sashes}
end