2015-04-25 13:17:47 -04:00
|
|
|
class Leader < ActiveRecord::Base
|
2015-04-26 11:47:59 -04:00
|
|
|
has_one :last_attack, -> { order created_at: :desc }, foreign_key: :attacker, primary_key: :leader, class_name: 'Raid'
|
|
|
|
has_one :last_defense, -> { order created_at: :desc }, foreign_key: :defender, primary_key: :leader, class_name: 'Raid'
|
2015-04-25 13:17:47 -04:00
|
|
|
|
2015-04-26 11:42:19 -04:00
|
|
|
def last_action
|
|
|
|
if last_attack == nil && last_defense == nil
|
|
|
|
@last_action = nil
|
|
|
|
elsif last_defense == nil
|
|
|
|
@last_action = last_attack.created_at
|
|
|
|
elsif last_attack == nil
|
|
|
|
@last_action = last_defense.created_at
|
|
|
|
else
|
|
|
|
@last_action = (last_attack.created_at > last_defense.created_at) ? last_attack.created_at : last_defense.created_at
|
|
|
|
end
|
|
|
|
|
|
|
|
return @last_action
|
|
|
|
end
|
2015-04-25 13:17:47 -04:00
|
|
|
end
|