Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: associations query limit on select options #3418

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@
<% else %>
<%= @form.select @field.id_input_foreign_key,
options_for_select(
# Options
@field.values_for_type(type),
# Selected
@field.value.is_a?(type) ? @field.value.to_param : nil
selected: @field.value.is_a?(type) ? @field.value.to_param : nil,
disabled: t("avo.more_records_available")
),
{
include_blank: @field.placeholder,
Expand Down Expand Up @@ -103,7 +102,7 @@
%>
<% else %>
<%= @form.select @field.id_input_foreign_key,
options_for_select(@field.options, @field.value.to_param),
options_for_select(@field.options, selected: @field.value.to_param, disabled: t("avo.more_records_available")),
{
include_blank: @field.placeholder,
},
Expand Down
12 changes: 9 additions & 3 deletions app/controllers/avo/associations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ def new
query = Avo::ExecutionContext.new(target: @field.attach_scope, query: query, parent: @record).handle
end

@options = query.all.map do |record|
[@attachment_resource.new(record: record).record_title, record.to_param]
end
@options = select_options(query)
end

@url = Avo::Services::URIService.parse(avo.root_url.to_s)
Expand Down Expand Up @@ -275,5 +273,13 @@ def destroy_success_action
end
end
end

def select_options(query)
query.all.limit(Avo.configuration.associations_lookup_list_limit).map do |record|
[@attachment_resource.new(record: record).record_title, record.to_param]
end.tap do |options|
options << t("avo.more_records_available") if options.size == Avo.configuration.associations_lookup_list_limit
end
end
end
end
3 changes: 1 addition & 2 deletions app/views/avo/associations/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
as: :select,
form: form,
name: @field.name.singularize,
options: options_for_select(@options,
nil),
options: options_for_select(@options, selected: nil, disabled: t("avo.more_records_available")),
include_blank: t('avo.choose_an_option'),
stacked: true,
classes: 'w-full'
Expand Down
2 changes: 2 additions & 0 deletions lib/avo/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Configuration
attr_accessor :is_developer_method
attr_accessor :search_results_count
attr_accessor :first_sorting_option
attr_accessor :associations_lookup_list_limit

def initialize
@root_path = "/avo"
Expand Down Expand Up @@ -119,6 +120,7 @@ def initialize
@is_developer_method = :is_developer?
@search_results_count = 8
@first_sorting_option = :desc # :desc or :asc
@associations_lookup_list_limit = 1000
end

def current_user_method(&block)
Expand Down
4 changes: 3 additions & 1 deletion lib/avo/fields/belongs_to_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ def values_for_type(model = nil)
query = Avo::ExecutionContext.new(target: attach_scope, query: query, parent: get_record).handle
end

query.all.map do |record|
query.all.limit(Avo.configuration.associations_lookup_list_limit).map do |record|
[resource.new(record: record).record_title, record.to_param]
end.tap do |options|
options << t("avo.more_records_available") if options.size == Avo.configuration.associations_lookup_list_limit
end
end

Expand Down
3 changes: 3 additions & 0 deletions lib/generators/avo/templates/initializer/avo.tt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ Avo.configure do |config|
## == Number of search results to display ==
# config.search_results_count = 8

## == Associations lookup list limit ==
# config.associations_lookup_list_limit = 1000

## == Cache options ==
## Provide a lambda to customize the cache store used by Avo.
## We compute the cache store by default, this is NOT the default, just an example.
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.ar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ ar:
loading: جاري التحميل
more: المزيد
more_content: المزيد من المحتوى
more_records_available: هناك المزيد من السجلات المتاحة.
new: جديد
next_page: الصفحة التالية
no_cancel: لا، إلغاء
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ de:
loading: Lade...
more: Mehr
more_content: Mehr Inhalt
more_records_available: Es sind weitere Datensätze verfügbar.
new: neu
next_page: Nächste Seite
no_cancel: Nein, abbrechen
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ en:
loading: Loading
more: More
more_content: More content
more_records_available: There are more records available.
new: new
next_page: Next page
no_cancel: No, cancel
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ es:
loading: Cargando
more: Más
more_content: Más contenido
more_records_available: Hay más registros disponibles.
new: nuevo/a
next_page: Página siguiente
no_cancel: No, cancelar
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ fr:
loading: Chargement
more: Plus
more_content: Plus de contenu
more_records_available: Il y a plus d'enregistrements disponibles.
new: Nouveau
next_page: Page suivante
no_cancel: Non, annuler
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ it:
loading: Caricamento in corso
more: Altro
more_content: Più contenuti
more_records_available: Sono disponibili più record.
new: nuovo
next_page: Pagina successiva
no_cancel: No, annulla
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ ja:
loading: 読み込み中
more: もっと
more_content: さらなるコンテンツ
more_records_available: さらに多くのレコードが利用可能です。
new: 新規
next_page: 次のページ
no_cancel: いいえ、キャンセル
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.nb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ nb:
loading: Laster
more: Mer
more_content: Mer innhold
more_records_available: Det er flere poster tilgjengelig.
new: ny
next_page: Neste side
no_cancel: Nei, avbryt
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ nl:
loading: Laden...
more: Meer
more_content: Meer inhoud
more_records_available: Er zijn meer records beschikbaar.
new: nieuw
next_page: Volgende pagina
no_cancel: Nee, annuleren
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.nn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ nn:
loading: Lastar
more: Meir
more_content: Mer innhold
more_records_available: Det finst fleire opptak tilgjengelege.
new: ny
next_page: Neste side
no_cancel: Nei, avbryt
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.pl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pl:
loading: Ładowanie
more: Więcej
more_content: Pokaż wiecej
more_records_available: Dostępnych jest więcej rekordów.
new: nowy
next_page: Następna strona
no_cancel: Nie, anuluj
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pt-BR:
loading: Carregando
more: Mais
more_content: Mais conteúdo
more_records_available: Existem mais registros disponíveis.
new: novo
next_page: Próxima página
no_cancel: Não, cancelar
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.pt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pt:
loading: A carregar
more: Mais
more_content: Mais conteúdo
more_records_available: Existem mais registos disponíveis.
new: novo
next_page: Próxima página
no_cancel: Não, cancelar
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.ro.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ ro:
loading: Se incarcă
more: Mai multe
more_content: Mai mult conținut
more_records_available: Sunt mai multe înregistrări disponibile.
new: nou
next_page: Pagina următoare
no_cancel: Nu, anulează
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ ru:
loading: Загрузка...
more: Ещё
more_content: Больше контента
more_records_available: Доступно больше записей.
new: новый
next_page: Следующая страница
no_cancel: Нет, отмена
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.tr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ tr:
loading: Yükleniyor
more: Daha fazla
more_content: Daha fazla içerik
more_records_available: Daha fazla kayıt mevcut.
new: yeni
next_page: Sonraki sayfa
no_cancel: Hayır, iptal et
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.uk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ uk:
loading: Завантаження
more: Ще
more_content: Більше вмісту
more_records_available: Є більше доступних записів.
new: новий
next_page: Наступна сторінка
no_cancel: Ні, скасувати
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.zh.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ zh:
loading: 加载中...
more: 更多
more_content: 更多内容
more_records_available: 还有更多记录可供参考。
new: 新建
next_page: 下一页
no_cancel: 不,取消
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/config/initializers/avo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@

config.alert_dismiss_time = 5000
config.search_results_count = 8
config.associations_lookup_list_limit = 1000

## == Menus ==
if Rails.env.test?
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/config/locales/avo.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ en:
x_records_selected_from_page_html: <span class="font-bold text-gray-700">%{selected}</span> records selected on this page
yes_confirm: Yes, I'm sure
you_missed_something_check_form: You might have missed something. Please check the form.
more_records_available: There are more records available.
filter_translations:
user_names_filter:
name: User names filter
Expand Down
18 changes: 18 additions & 0 deletions spec/system/avo/associations_using_fields_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,22 @@
end
}.to change(course.links, :count).by(-1)
end

context "when associations options exceeds associations_lookup_list_limit" do
let!(:link) { Course::Link.first }

before { Avo.configuration.associations_lookup_list_limit = 1 }
after { Avo.configuration.associations_lookup_list_limit = 1000 }

it "limits select options" do
visit avo.resources_course_path(course)

scroll_to find('turbo-frame[id="has_many_field_show_links"]')

click_on "Attach link"

expect(page).to have_select "fields_related_id", options: ["Choose an option", link.link, "There are more records available."]
expect(page).to have_selector 'option[disabled="disabled"][value="There are more records available."]'
end
end
end
30 changes: 29 additions & 1 deletion spec/system/avo/create_via_belongs_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,25 @@

expect(Fish.last.user).to eq User.last
end

context "when belongs_to record options exceeds associations_lookup_list_limit" do
let!(:course) { create :course }
let!(:exceeded_course) { create :course }

before { Avo.configuration.associations_lookup_list_limit = 1 }
after { Avo.configuration.associations_lookup_list_limit = 1000 }

it "limits select options" do
visit "/admin/resources/course_links/new"
expect(page).to have_select "course_link_course_id", options: ["Choose an option", course.name, "There are more records available."]
expect(page).to have_selector 'option[disabled="disabled"][value="There are more records available."]'
end
end
end

context 'with polymorphic belongs_to' do
it 'successfully creates a new commentable and assigns it to the comment', :aggregate_failures do
visit '/admin/resources/comments/new'
visit "/admin/resources/comments/new"

fill_in 'comment_body', with: 'Test comment'

Expand Down Expand Up @@ -124,6 +138,20 @@
commentable: Post.last
)
end

context "when belongs_to record options exceeds associations_lookup_list_limit" do
let!(:user) { User.first }
let!(:exceeded_user) { create :user }

before { Avo.configuration.associations_lookup_list_limit = 1 }
after { Avo.configuration.associations_lookup_list_limit = 1000 }

it "limits select options" do
visit "/admin/resources/comments/new"
expect(page).to have_select "comment_user_id", options: ["Choose an option", user.name, "There are more records available."]
expect(page).to have_selector 'option[disabled="disabled"][value="There are more records available."]'
end
end
end

context 'with models that uses prefix_id' do
Expand Down
Loading