# -*- encoding : utf-8 -*- InlineForms::SPECIAL_COLUMN_TYPES[:dropdown_with_other]=:belongs_to # dropdown def dropdown_with_other_show(object, attribute) attribute = attribute.to_s foreign_key = object.class.reflect_on_association(attribute.to_sym).options[:foreign_key] || attribute.foreign_key.to_sym id = object[foreign_key] if id == 0 attribute_value = object[attribute + '_other'] attribute_value = "".html_safe if attribute_value.nil? || attribute_value.empty? else attribute_value = object.send(attribute)._presentation rescue "".html_safe end link_to_inline_edit object, attribute, attribute_value end def dropdown_with_other_edit(object, attribute) attribute = attribute.to_s foreign_key = object.class.reflect_on_association(attribute.to_sym).options[:foreign_key] || attribute.foreign_key.to_sym o = attribute.camelcase.constantize values = o.all values = o.accessible_by(current_ability) if cancan_enabled? values.each do |v| v.name = v._presentation end # values.sort_by! &:name collection = values.map {|v|[v.name, v.id]} collection << [object[attribute + '_other'], 0] unless object[attribute + '_other'].nil? || object[attribute + '_other'].empty? out = '
' out.html_safe end def dropdown_with_other_update(object, attribute) attribute = attribute.to_s foreign_key = object.class.reflect_on_association(attribute.to_sym).options[:foreign_key] || attribute.foreign_key.to_sym # if there is an attribute attr, then there must be an attribute attr_other other = params[('_' + object.class.to_s.underscore).to_sym][(attribute + "_other").to_sym] # see if it matches anything (but we need to look at I18n too! lookup_model = attribute.camelcase.constantize name_field = 'name_' + I18n.locale.to_s name_field = 'name' unless lookup_model.new.respond_to? name_field puts 'XXXXXXXXXXXXXXXXXXXX ' + name_field.inspect match = lookup_model.where(name_field.to_sym => other).first # problem if there are dupes! puts 'XXXXXXXXXXXXXXXXXXXX ' + match.inspect match.nil? ? object[foreign_key] = 0 : object[foreign_key] = match.id # problem if there is a record with id: 0 ! match.nil? ? object[attribute + '_other'] = other : object[attribute + '_other'] = nil end