Module: OneTouch::HostRelation::ClassMethods

Defined in:
lib/one_touch/models/host_relation.rb

Instance Method Summary collapse

Instance Method Details

#host_relations(to_media, media_to_host) ⇒ Object

results like

{ [:focus, @tagA] => [ User1, User2, User3] , [:dislike, @tagB] => [ User2, User5 ] ... }
or { [ nil, @tagA] => [ Article1, Post2, User3] , [:nil, @tagB] => [ User2, User5 ] ... }
or { [[:Tag, 1], :focus] => [ [:User, 1], [:User, 2] ... ] } or
simply is { [:Tag, :focus] => [ [:User, 1], [:User, 2] ... ] } or
just as { 1 => [ [:User, 1], [:User, 2] ... ] }, 1 is the relation point
{ TagA => [ :focus, User1, User2, User3] , TagB => [ :like, User2, User5 ] }


17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/one_touch/models/host_relation.rb', line 17

def host_relations(to_media, media_to_host)
  includes(to_media.to_sym => media_to_host.to_sym).inject({}) do |memo,favorable|
    favorable.send(to_media).each do |favor|
      favor.respond_to?(:context) ? (context = favor.context) : (context = nil)
      combine_key = [context, favorable]

      host = favor.send media_to_host
      memo.keys.include?(combine_key) ? memo[combine_key] << host : memo[combine_key] = [host]
    end
    memo
  end
end