cirandas.net

ref: master

plugins/exchange/lib/ext/profile.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
require_dependency 'profile'

class Profile

  has_many :profile_exchanges, foreign_key: :profile_id, class_name: "ExchangePlugin::ProfileExchange"
  has_many :exchanges, through: :profile_exchanges

  def has_exchanged e
    found = false
    if (e != self)
      self.exchanges.each do |ex|
        if ex.profiles.first conditions: {id: e.id}
          found = true
          break
        end
      end
    end
    return found
  end

  def exchanges_count
    self.exchanges.count conditions: {state: "concluded"}
  end

  def exchanged_profile_count
    profiles = Array.new
    self.profile_exchanges.each do |e|
      if e.exchange.state == "concluded"
        profiles.push(e.the_other.id)
      end
    end

    return profiles.uniq.count
  end

end