<commit_before># -*- 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 = "<i class='fi-plus'></i>".html_safe if attribute_value.nil? || attribute_value.empty?
  else
    attribute_value = object.send(attribute)._presentation rescue  "<i class='fi-plus'></i>".html_safe
  end
  link_to_inline_edit object, attribute, attribute_value
end

def dropdown_with_other_edit(object, attribute)
  x ||= 1

  puts "XXXXXXXXXXXXXXXXXX" + x.to_s
  x += 1

  attribute = attribute.to_s

  puts "XXXXXXXXXXXXXXXXXX" + x.to_s
  x += 1
  
  foreign_key = object.class.reflect_on_association(attribute.to_sym).options[:foreign_key] || attribute.foreign_key.to_sym

  puts "XXXXXXXXXXXXXXXXXX" + x.to_s
  x += 1

  #  object.send('build_' + attribute) unless object.send(attribute)
  o = attribute.capitalize.constantize

  puts "XXXXXXXXXXXXXXXXXX" + x.to_s
  x += 1

  if cancan_enabled?
    values = o.accessible_by(current_ability).order(o.order_by_clause)
  else
    values = o.order(o.order_by_clause)
  end
  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 = '<div class="ui-widget">'
  out << select('_' + object.class.to_s.underscore, foreign_key.to_sym, collection, {selected: object[foreign_key.to_sym], prompt: 'maake een keuze'}, {id: '_' + object.class.to_s.underscore + '_' + object.id.to_s + '_'  + foreign_key.to_s})
  #  collection_select( ('_' + object.class.to_s.underscore).to_sym, attribute.to_s.foreign_key.to_sym, values, 'id', 'name', :selected => object.send(attribute).id)
  out << '</div>
  <script>
    (function( $ ) {
    $.widget( "custom.combobox", {
      _create: function() {
        this.wrapper = $( "<span>" )
          .addClass( "custom-combobox" )
          .insertAfter( this.element );
 
        this.element.hide();
        this._createAutocomplete();
        this._createShowAllButton();
      },
 
      _createAutocomplete: function() {
        var selected = this.element.children( ":selected" ),
          value = selected.val() ? selected.text() : "";
 
        this.input = $( "<input name=\''
        
    out << '_' + object.class.to_s.underscore + '[' + attribute + '_other]' 
        
        out << '\'>" )
          .appendTo( this.wrapper )
          .val( value )
          .attr( "title", "" )<commit_msg>remove debugging and refactor dropdown_with_other_edit<commit_after># -*- 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 = "<i class='fi-plus'></i>".html_safe if attribute_value.nil? || attribute_value.empty?
  else
    attribute_value = object.send(attribute)._presentation rescue  "<i class='fi-plus'></i>".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.capitalize.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 = '<div class="ui-widget">'
  out << select('_' + object.class.to_s.underscore, foreign_key.to_sym, collection, {selected: object[foreign_key.to_sym]}, {id: '_' + object.class.to_s.underscore + '_' + object.id.to_s + '_'  + foreign_key.to_s})
  out << '</div>
  <script>
    (function( $ ) {
    $.widget( "custom.combobox", {
      _create: function() {
        this.wrapper = $( "<span>" )
          .addClass( "custom-combobox" )
          .insertAfter( this.element );
 
        this.element.hide();
        this._createAutocomplete();
        this._createShowAllButton();
      },
 
      _createAutocomplete: function() {
        var selected = this.element.children( ":selected" ),
          value = selected.val() ? selected.text() : "";
 
        this.input = $( "<input name=\''
        
    out << '_' + object.class.to_s.underscore + '[' + attribute + '_other]' 
        
        out << '\'>" )
          .appendTo( this.wrapper )
          .val( value )
          .attr( "title", "" )
{'fraction_non_alphanumeric': 0.15080441946113587, 'fraction_numerical': 0.0021321961620469083, 'mean_word_length': 3.4714038128249567, 'pattern_counts': {'":': 2, '<': 41, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5212040', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1689, 'n_words': 304}