best_in_place
best_in_place copied to clipboard
Wrong order of collection items for select
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?
I have the same issue. Any suggestions?
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.
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
Just verified this is fixed on the master branch