app/models/list.rb
# returns the unique varieties of an attribute across all items.
# sort_by: :frequency or :value
# convert: type conversion, such as :to_s
returns the unique varieties of an attribute across all items.
returns the unique varieties of an attribute across all items .
def uniq_of_attribute(attribute, items, sort_by:, convert: nil)
    all = items.flat_map do |item|
      item.send(attribute).presence
    end.uniq.compact
    if sort_by == :frequency
      all = all.group_by(&:itself)
        .sort_by { |value, duplicates| duplicates.count }
        .reverse.to_h.keys
    end
    if convert
      all = all.map do |value|
        value.send(convert)
      end
    end
    if sort_by == :value
      all = all.sort
    end
    all
  end
def uniq_of_attribute ( attribute , items , sort_by : , convert : nil ) all = items . flat_map do | item | item . send ( attribute ) . presence end . uniq . compact if sort_by == :frequency all = all . group_by ( & :itself ) . sort_by { | value , duplicates | duplicates . count } . reverse . to_h . keys end if convert all = all . map do | value | value . send ( convert ) end end if sort_by == :value all = all . sort end all end
returns the unique varieties of an attribute across all items.
returns the unique varieties of an attribute across all items .
attribute
<not_specific>
items
<not_specific>
sort_by
<not_specific>
convert
<not_specific>