mirror of
https://github.com/internetee/registry.git
synced 2025-07-27 21:16:12 +02:00
Add missing partial and wildcard search on /admin/contact_versions
This commit is contained in:
parent
2cff7d93fd
commit
7b47eab0d8
3 changed files with 15 additions and 24 deletions
|
@ -7,23 +7,9 @@ module Admin
|
|||
def index
|
||||
params[:q] ||= {}
|
||||
|
||||
search_params = params[:q].deep_dup.except(:created_at_gteq, :created_at_lteq)
|
||||
|
||||
where_s = '1=1'
|
||||
|
||||
search_params.each do |key, value|
|
||||
next if value.empty?
|
||||
|
||||
where_s += case key
|
||||
when 'event'
|
||||
" AND event = '#{value}'"
|
||||
else
|
||||
create_where_string(key, value)
|
||||
end
|
||||
end
|
||||
|
||||
versions = Version::ContactVersion.includes(:item).where(where_s).order(created_at: :desc, id: :desc)
|
||||
@q = versions.ransack(params[:q])
|
||||
search_params = PartialSearchFormatter.format(params[:q])
|
||||
versions = Version::ContactVersion.includes(:item).order(created_at: :desc, id: :desc)
|
||||
@q = versions.ransack(polymorphic_association(search_params))
|
||||
|
||||
@versions = @q.result.page(params[:page])
|
||||
@versions = @versions.per(params[:results_per_page]) if params[:results_per_page].to_i.positive?
|
||||
|
@ -53,8 +39,14 @@ module Admin
|
|||
render json: Version::ContactVersion.search_by_query(params[:q])
|
||||
end
|
||||
|
||||
def create_where_string(key, value)
|
||||
" AND object->>'#{key}' ~* '#{value}'"
|
||||
private
|
||||
|
||||
def polymorphic_association(search_params)
|
||||
record_type = {}
|
||||
fields = %w[name code ident]
|
||||
fields.each { |field| record_type[:"item_of_Contact_type_#{field}_matches"] = search_params[:"#{field}_matches"] }
|
||||
|
||||
record_type
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
class PartialSearchFormatter
|
||||
def self.format(params)
|
||||
percentage = '%'
|
||||
search_params = params.deep_dup
|
||||
|
||||
search_params.each do |key, value|
|
||||
next unless key.include?('matches') && value.present?
|
||||
|
||||
value << percentage
|
||||
value << '%'
|
||||
end
|
||||
|
||||
search_params
|
||||
|
|
|
@ -7,15 +7,15 @@
|
|||
.col-md-3
|
||||
.form-group
|
||||
= f.label :name
|
||||
= f.search_field :name, value: params[:q][:name], class: 'form-control', placeholder: t(:name)
|
||||
= f.search_field :name_matches, value: params[:q][:name_matches], class: 'form-control', placeholder: t(:name)
|
||||
.col-md-3
|
||||
.form-group
|
||||
= f.label :id
|
||||
= f.search_field :code, value: params[:q][:code], class: 'form-control', placeholder: t(:id)
|
||||
= f.search_field :code_matches, value: params[:q][:code_matches], class: 'form-control', placeholder: t(:id)
|
||||
.col-md-3
|
||||
.form-group
|
||||
= f.label :ident
|
||||
= f.search_field :ident, value: params[:q][:ident], class: 'form-control', placeholder: t(:ident)
|
||||
= f.search_field :ident_matches, value: params[:q][:ident_matches], class: 'form-control', placeholder: t(:ident)
|
||||
.col-md-3
|
||||
.form-group
|
||||
= label_tag :action
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue