lib/madmin/fields/has_many.rb in madmin-1.2.0 vs lib/madmin/fields/has_many.rb in madmin-1.2.1
- old
+ new
@@ -1,19 +1,23 @@
module Madmin
module Fields
class HasMany < Field
def options_for_select(record)
- association = record.class.reflect_on_association(attribute_name)
- klass = association.klass
- resource = nil
- klass.all.map do |r|
- resource ||= Madmin.resource_for(r)
- [resource.display_name(r), r.id]
+ if (records = record.send(attribute_name))
+ return [] unless records.first
+ resource = Madmin.resource_for(records.first)
+ records.map { |record| [resource.display_name(record), record.id] }
+ else
+ []
end
end
def to_param
{"#{attribute_name.to_s.singularize}_ids".to_sym => []}
+ end
+
+ def index_path
+ Madmin.resource_by_name(model.reflect_on_association(attribute_name).klass).index_path(format: :json)
end
end
end
end