Methods
I
Attributes
[R] select_field
[R] select_options
[R] value_field
Instance Public methods
items_for_select_menu(options = {})

Provides the Model content in a ready to use <select> options array

Params

  • :options

    • :prompt [String/Boolean] => The text shown on the <select> field in the browser. (Defaults to “Select NameOfYourModel”)

    • :divider [Boolean] => Whether to add a divider/separator between the prompt and the main options. (Defaults to true)

    • :order [Array] => A normal DM order declaration. (Defaults to [:name] or the name of the #select_field declared)

    • :show_root [Boolean] => Whether to add the Top Level Parent in the choices. (Defaults to true)

    • :root_text [String] => The text to show as the Parent item in select list. (Defaults to “Top Level NameOfYourModel”)

Examples

Category.items_for_select_menu  
  => [ [nil, 'Select Category'], [nil, '---'], [1, 'Category 1'] ,....]

Category.items_for_select_menu(:prompt => "Custom Prompt")  
  => [ [nil, 'Custom Prompt'],...]

Category.items_for_select_menu(:prompt => false)  
  => [ [1, 'Category 1'] ,...]

Category.items_for_select_menu(:divider => false )  
  => array without the [nil, '---'] node

Category.items_for_select_menu(:order => [ :id.desc ] )  
  => array with the order reversed. (Prompts & divider always comes first)

Category.items_for_select_menu(:publish_status => "on", :order => [ :id.desc ] )  
  => returns only those items that matches the query params or just an empty Select Menu

If your model is a Tree:

Category.items_for_select_menu(:root_text => "Custom Root Text")  # sets the text for the Top Level (root) Parent
  => [ ..., [0, 'Custom Root Text'],...]

Category.items_for_select_menu(:show_root => false)  # removes the Top Level (root) Parent from the

@api public

# File lib/is/select.rb, line 102
def items_for_select_menu(options = {}) 
  # clean out the various parts
  html_options = options.slice(:prompt, :divider, :show_root, :root_text)
  sql_options  = options.except(:prompt, :divider, :show_root, :root_text)
  
  # defaults
  html_options = {
    :prompt => "Select #{self.name}",
    :divider => true,
    :show_root => true,
    :root_text => "Top Level #{self.name}",
  }.merge(html_options)
  
  sql_options = {
    :order => [self.select_field.to_sym],
  }.merge(sql_options)
  
  mi = self.select_options[:is_tree] ?  all({ :parent_id => nil }.merge(sql_options) ) :  all(sql_options)
  
  res = []
  if html_options[:prompt]
    res << [nil, html_options[:prompt]] 
    res << ['nil', "  ------  "] if html_options[:divider]
    if self.select_options[:is_tree]
      if html_options[:show_root]
        res << [0, html_options[:root_text]] 
        res << ['nil',"  ------  "] if html_options[:divider]
      end
    end
  end
  
  if self.select_options[:is_tree]
    mi.each do |x|
      res << [x.send(self.value_field), x.send(self.select_field)]
      unless x.children.blank?
        x.children.each do |child|
          res << [child.send(self.value_field), "-- #{child.send(self.select_field)}"]
          child.children.each do |grand_child| 
            res << [ grand_child.send(self.value_field), "-- -- #{grand_child.send(self.select_field)}"] 
          end unless child.children.blank?
        end
      end
    end
  else
    mi.each do |x|
      res << [x.send(self.value_field), x.send(self.select_field)]
    end
  end
  res
end