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