Parent

Files

SimpleForm::Inputs::CollectionInput

Public Class Methods

boolean_collection() click to toggle source

Default boolean collection for use with selects/radios when no collection is given. Always fallback to this boolean collection. Texts can be translated using i18n in "simple_form.yes" and "simple_form.no" keys. See the example locale file.

# File lib/simple_form/inputs/collection_input.rb, line 8
def self.boolean_collection
  i18n_cache :boolean_collection do
    [ [I18n.t(:"simple_form.yes", :default => 'Yes'), true],
      [I18n.t(:"simple_form.no", :default => 'No'), false] ]
  end
end

Public Instance Methods

input() click to toggle source
# File lib/simple_form/inputs/collection_input.rb, line 15
def input
  collection = (options[:collection] || self.class.boolean_collection).to_a
  detect_collection_methods(collection, options)
  @builder.send(:"collection_#{input_type}", attribute_name, collection, options[:value_method],
                options[:label_method], input_options, input_html_options)
end
input_options() click to toggle source
# File lib/simple_form/inputs/collection_input.rb, line 22
def input_options
  options = super
  options[:include_blank] = true unless skip_include_blank?
  options
end

Protected Instance Methods

detect_collection_methods(collection, options) click to toggle source

Detect the right method to find the label and value for a collection. If no label or value method are defined, will attempt to find them based on default label and value methods that can be configured through SimpleForm.collection_label_methods and SimpleForm.collection_value_methods.

# File lib/simple_form/inputs/collection_input.rb, line 41
def detect_collection_methods(collection, options)
  sample = collection.first || collection.last

  case sample
  when Array
    label, value = :first, :last
  when Integer
    label, value = :to_s, :to_i
  when String, NilClass
    label, value = :to_s, :to_s
  end

  options[:label_method] ||= label || SimpleForm.collection_label_methods.find { |m| sample.respond_to?(m) }
  options[:value_method] ||= value || SimpleForm.collection_value_methods.find { |m| sample.respond_to?(m) }
end
skip_include_blank?() click to toggle source

Check if :include_blank must be included by default.

# File lib/simple_form/inputs/collection_input.rb, line 31
def skip_include_blank?
  (options.keys & [:prompt, :include_blank, :default, :selected]).any? ||
    options[:input_html].try(:[], :multiple)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.