Module: RailsAdminTagList::SuggestionsHelper

Defined in:
app/helpers/rails_admin_tag_list/suggestions_helper.rb

Instance Method Summary collapse

Instance Method Details

#tag_suggestions(field, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'app/helpers/rails_admin_tag_list/suggestions_helper.rb', line 3

def tag_suggestions(field, options = {})
  defaults = {
    :order => { :count => :desc },
    :length => 5
  }
  options = defaults.deep_merge(options)

  model = field.abstract_model.model_name.constantize
  tags_name = field.name.to_s.gsub(/_list/, '').pluralize.to_sym
  tags = model.tag_counts_on(tags_name)
  tags = sort_tags(tags, options[:order])
  tags[0..options[:length]].map(&:name)
end