Module: Mongoid::Utils::Listable

Extended by:
ActiveSupport::Concern
Defined in:
lib/mongoid/utils/listable.rb

Instance Method Summary collapse

Instance Method Details

#list_title(list) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/mongoid/utils/listable.rb', line 25

def list_title(list)
    #    lists_hash[list].is_a?(Array) ? lists_hash[list].collect(&:title).join(', ') : lists_hash[list].try(:title)
  if lists_hash[list].is_a?(Array)
    lists_hash[list].map { |l|
      l.is_a?(Array) ? l.collect(&:title).join(', ') : l.try(:title)
    }
  else
    lists_hash[list].try(:title)
  end
end

#lists_hashObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mongoid/utils/listable.rb', line 13

def lists_hash
    #    @hash ||= Hash[lists.map { |list| [list.model_name.param_key.to_sym, list] }]#.group_by(&:first).map { |k,v| {k => (el = v.map(&:last)).length > 1 ? el : el[0]} }.last || {}
  unless @hash
    hashes = lists.map{ |list| Hash[*[list.model_name.param_key.to_sym, list]] }
    @hash ||= hashes.flat_map(&:entries).group_by(&:first).map{|k,v| Hash[k, v.map(&:last)]}.reduce(Hash.new, :merge) || {}
    @hash.each {|k,v| if v.length == 1 then @hash[k] = v.first end}
              #hashes.inject{ |h1,h2| h1.merge(h2){ |*a| a[1,2] } } || {}
  end

  @hash
end

#update_lists(new_lists) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/mongoid/utils/listable.rb', line 36

def update_lists(new_lists)
  _lists = self.lists

  new_lists.each do |new_list|
    _lists = _lists.delete_if { |list| list.is_a? new_list.class }
  end

  self.lists = (_lists + new_lists)
end