Module: ActsAsCaesar::ActAsCandidate

Defined in:
lib/acts_as_caesar/modules/act_as_candidate.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.handle_candidate_update_notification(base, candidate) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/acts_as_caesar/modules/act_as_candidate.rb', line 14

def self.handle_candidate_update_notification(base, candidate)
  # ensure we have an id
  unless candidate.key =~ /\_([\da-f]+)$/ && id = $1
    raise ArgumentError, "#{candidate.key} does not contain a valid key"
  end

  if ActiveRecordPersistence.can_handle?(base)
    ActiveRecordPersistence.new(base, id).update_score
  elsif MongoidPersistence.can_handle?(base)
    MongoidPersistence.new(base, id).update_score
  elsif base.respond_to?(:acts_as_caesar_score_persistence)
    base.acts_as_caesar_score_persistence.new(base, id).update_score
  else
    raise StandardError, %Q(
      Incompatible database adapter, try implementing the following method:
      def self.acts_as_caesar_score_persistence
        # returns a class name like lib/acts_as_caesar/services/mongoid_persistence.rb
        # e.g. MongoidPersistence
      end
    )
  end
end

.included(base) ⇒ Object



8
9
10
11
12
# File 'lib/acts_as_caesar/modules/act_as_candidate.rb', line 8

def self.included(base)
  Notification.subscribe(key: /^#{base.name.downcase.gsub('::', '_')}\_[\da-f]+$/) do |candidate|
    handle_candidate_update_notification(base, candidate)
  end
end

Instance Method Details

#total_candidate_scoreObject



37
38
39
# File 'lib/acts_as_caesar/modules/act_as_candidate.rb', line 37

def total_candidate_score
  ActsAsCaesar::Candidate.new(self).total
end