Fix search via Ransack association

This commit is contained in:
Alex Sherman 2020-10-29 17:08:34 +05:00
parent 44637b08cf
commit 358a39e0fd
2 changed files with 16 additions and 11 deletions

View file

@ -11,27 +11,27 @@ class Registrar
search_params[:name_matches].present? search_params[:name_matches].present?
domain = Domain.find_by(name: search_params[:name_matches]) domain = Domain.find_by(name: search_params[:name_matches])
if domain redirect_to info_registrar_domains_url(domain_name: domain.name) and return if domain
redirect_to info_registrar_domains_url(domain_name: domain.name) and return
end
end end
domains = if params[:statuses_contains] domains = if params[:statuses_contains]
current_registrar_user.registrar.domains current_domain_scope.where('domains.statuses @> ?::varchar[]',
.includes(:registrar, :registrant) "{#{params[:statuses_contains].join(',')}}")
.where('domains.statuses @> ?::varchar[]',
"{#{params[:statuses_contains].join(',')}}")
else else
current_registrar_user.registrar.domains.includes(:registrar, :registrant) current_domain_scope
end end
if params[:contacts_ident_eq]
domains = domains.where(contacts: { ident: params[:contacts_ident_eq] })
end
normalize_search_parameters do normalize_search_parameters do
@q = domains.search(search_params) @q = domains.search(search_params.except(:contacts_ident_eq))
@domains = @q.result.page(params[:page]) @domains = @q.result.page(params[:page])
# if we do not get any results, add wildcards to the name field and search again # if we do not get any results, add wildcards to the name field and search again
if @domains.count == 0 && search_params[:name_matches] !~ /^%.+%$/ if @domains.count == 0 && search_params[:name_matches] !~ /^%.+%$/
new_search_params = search_params.to_h new_search_params = search_params.to_h.except(:contacts_ident_eq)
new_search_params[:name_matches] = "%#{new_search_params[:name_matches]}%" new_search_params[:name_matches] = "%#{new_search_params[:name_matches]}%"
@q = domains.search(new_search_params) @q = domains.search(new_search_params)
@domains = @q.result.page(params[:page]) @domains = @q.result.page(params[:page])
@ -57,6 +57,10 @@ class Registrar
end end
end end
def current_domain_scope
current_registrar_user.registrar.domains.includes(:registrar, :registrant)
end
def info def info
authorize! :info, Depp::Domain authorize! :info, Depp::Domain
@data = @domain.info(params[:domain_name]) if params[:domain_name] @data = @domain.info(params[:domain_name]) if params[:domain_name]

View file

@ -18,7 +18,8 @@
<div class="col-md-3"> <div class="col-md-3">
<div class="form-group"> <div class="form-group">
<%= f.label :contact_ident, for: nil %> <%= f.label :contact_ident, for: nil %>
<%= f.search_field :contacts_ident_eq, class: 'form-control', placeholder: t(:contact_ident) %> <%= f.search_field :contacts_ident_eq, value: search_params[:contacts_ident_eq],
class: 'form-control', placeholder: t(:contact_ident) %>
</div> </div>
</div> </div>