Module: Hashdown::SelectOptions::ClassMethods

Defined in:
lib/hashdown/select_options.rb

Instance Method Summary collapse

Instance Method Details

#select_options(*args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/hashdown/select_options.rb', line 26

def select_options(*args)
  options = generate_options_for_cache_key(args)

  scope = scoped
  options[:is_sorted] = scope.arel.orders.any?
  scope = select_options_scope_with_order(scope, options)

  cache_key = Hashdown.cache_key(:select_options, self.to_s, select_options_cache_key(options, scope))

  Hashdown.cached(cache_key) do
    if grouping = options[:group]
      scope.all.group_by {|record| grouping.call(record) }.map do |group, records|
        [group, map_records_to_select_options(records, options)]
      end
    else
      map_records_to_select_options(scope.all, options)
    end
  end
end