Sha256: bc0d2b206f5d1f7fc0a9aeceb025fab8252d4d30fdd37076e37e67f4ab5ae17b

Contents?: true

Size: 971 Bytes

Versions: 1

Compression:

Stored size: 971 Bytes

Contents

module SelectionSelect
  def selections field, options = {}, html_options = {}
    options ||= {}
    html_options ||= {}
    system_code = options[:system_code] || field
    system_code = Selections::Selection.find_by_system_code(system_code.to_s)
    if system_code
      items = system_code.children
      field_id = (field.to_s + "_id").to_sym
      if object.new_record? && object.send(field_id).nil?
        default = items.find_by_is_default(true)
        object.send(field_id.to_s+"=", default.id) if default && !default.archived
      end
      options[:include_blank] = true if object.send(field_id).blank? && options[:include_blank].nil?
      #TODO add default style
      #html_options[:style] ||=
      select field_id, items.filter_archived_except_selected(object.send(field_id)).map {|item| [item.name, item.id]}, options, html_options
    else
      "Invalid system_code"
    end
  end
end

ActionView::Helpers::FormBuilder.send :include, SelectionSelect

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
selections-0.0.1 lib/selections/selection_select.rb