bernat / best_in_place

A RESTful unobtrusive jQuery Inplace-Editor and a helper as a Rails Gem
http://blog.bernatfarrero.com/in-place-editing-with-javascript-jquery-and-rails-3/
1.2k stars 574 forks source link

Wrong order of collection items for select #593

Open rivella50 opened 6 years ago

rivella50 commented 6 years ago

Hi, i know this issue has been addressed many times, but i still do not see a solution: When using the following column code in ActiveAdmin the order of the items does not represent my order given in the select statement (it is reordered later after the id keys):

column 'sector' do |i|
    best_in_place i, :sector_id, as: :select, url: admin_sector_path(i), :collection => Sector.order('name asc').map { |f| [f.id, f.name] }
end

Is there a possibility not to allow a reordering for the array given to the collection?

gmhawash commented 6 years ago

I have the same issue. Any suggestions?

kmayer commented 6 years ago

BIP (https://github.com/bernat/best_in_place#usage-of-rails-3-gem) wants the collection as a hash

:collection: If you are using the :select type then you must specify the collection of values it takes as a hash where values represent the display text and keys are the option's value when selected.

e.g. { value => display }

But rails collection_for_select methods want an Array with [[display, value], ...], which just happens to become { display => value } when you call .to_h on the array.

We sort of need a bip helper that will take rails collection and change it to the expected Hash class. Not hard, but annoying.

kmayer commented 6 years ago

Here's my terrible hack until a PR

module BestInPlace
  module_function
  def bippify(collection)
    if collection.is_a?(ActiveRecord::Associations::CollectionProxy)
      collection.map{|elt| [elt.id, elt.name]}.to_h
    else
      collection
    end
  end
end
sqlninja commented 5 years ago

Just verified this is fixed on the master branch