# File lib/best_in_place/helper.rb, line 3
    def best_in_place(object, field, opts = {})

      best_in_place_assert_arguments(opts)
      type = opts[:as] || :input
      field = field.to_s

      options = {}
      options[:data] = HashWithIndifferentAccess.new(opts[:data])
      options[:data]['bip-type'] = type
      options[:data]['bip-attribute'] = field

      real_object = best_in_place_real_object_for object

      display_value = best_in_place_build_value_for(real_object, field, opts)

      value = real_object.send(field)

      if opts[:collection] or type == :checkbox
        collection = opts[:collection]
        value = value.to_s
        collection = best_in_place_default_collection if collection.blank?
        collection = best_in_place_collection_builder(type, collection)
        display_value = collection.flat_map{|a| a[0].to_s == value ? a[1] : nil }.compact[0]
        collection = collection.to_json
        options[:data]['bip-collection'] = html_escape(collection)
      end

      options[:class] = ['best_in_place'] + Array(opts[:class] || opts[:classes])
      options[:id] = opts[:id] || BestInPlace::Utils.build_best_in_place_id(real_object, field)

      pass_through_html_options(opts, options)

      options[:data]['bip-activator'] = opts[:activator].presence

      options[:data]['bip-html-attrs'] = opts[:html_attrs].to_json unless opts[:html_attrs].blank?
      options[:data]['bip-inner-class'] = opts[:inner_class].presence

      options[:data]['bip-placeholder'] = html_escape(opts[:place_holder]).presence

      options[:data]['bip-object'] = opts[:param] || BestInPlace::Utils.object_to_key(real_object)
      options[:data]['bip-ok-button'] = opts[:ok_button].presence
      options[:data]['bip-ok-button-class'] = opts[:ok_button_class].presence
      options[:data]['bip-cancel-button'] = opts[:cancel_button].presence
      options[:data]['bip-cancel-button-class'] = opts[:cancel_button_class].presence
      options[:data]['bip-original-content'] = html_escape(opts[:value] || value).presence

      options[:data]['bip-skip-blur'] = opts.has_key?(:skip_blur) ? opts[:skip_blur].presence : BestInPlace.skip_blur

      options[:data]['bip-url'] = url_for(opts[:url] || object)

      if real_object.respond_to?(:new_record?) and real_object.new_record?
        options[:data]['bip-new-object'] = true
        # collect name => value map of unsaved attributes to be serialized
        options[:data]['bip-extra-payload'] = Hash[real_object.changes.map { |k,v| [k, v[1]] }]
      end

      options[:data]['bip-confirm'] = opts[:confirm].presence
      options[:data]['bip-value'] = html_escape(value).presence

      if opts[:raw]
        options[:data]['bip-raw'] = 'true'
      end

      # delete nil keys only
      options[:data].delete_if { |_, v| v.nil? }
      container = opts[:container] || BestInPlace.container
      content_tag(container, display_value, options, opts[:raw].blank?)
    end