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

[Autocomplete] Make a search across multiple models #530

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion app/components/search_bar/component.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div id="search-bar" class="sticky inset-x-0 z-20 flex items-center justify-center px-6 py-4
bg-gradient-to-r from-blue-gradient-2 to-blue-gradient-1 top-20 md:top-22.75 md:gap-5">
<%# Keyword input %>
<div class="relative w-full max-w-xl" data-controller="autocomplete" data-autocomplete-url-value="<%= autocomplete_index_path %>" role="combobox">
<div class="relative w-full max-w-xl" data-controller="autocomplete" data-autocomplete-url-value="<%= autocomplete_index_path %>" role="combobox" data-autocomplete-min-length-value="2">
<span class="absolute inset-y-0 left-0 flex items-center px-3 pointer-events-none">
<%= inline_svg_tag "solid_search.svg", class: 'h-4 w-4 fill-current text-gray-2' %>
</span>
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/autocomplete_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ class AutocompleteController < ApplicationController
skip_after_action :verify_authorized

def index
@suggestions = Tag.suggestions(params[:q])
@suggestions = PgSearch.multisearch(params[:q]).map do |record|
record.searchable.name
end.uniq.sort

render layout: false
end
end
2 changes: 2 additions & 0 deletions app/models/beneficiary_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@
# updated_at :datetime not null
#
class BeneficiaryGroup < ApplicationRecord
include PgSearch::Model
multisearchable against: :name
has_many :beneficiary_subcategories, dependent: :destroy
end
3 changes: 3 additions & 0 deletions app/models/beneficiary_subcategory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
# updated_at :datetime not null
#
class BeneficiarySubcategory < ApplicationRecord
include PgSearch::Model
multisearchable against: :name

belongs_to :beneficiary_group

def self.top(limit: 10)
Expand Down
2 changes: 2 additions & 0 deletions app/models/cause.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# updated_at :datetime not null
#
class Cause < ApplicationRecord
include PgSearch::Model
multisearchable against: :name
has_many :organization_causes, dependent: :destroy
has_many :organizations, through: :organization_causes
has_many :services
Expand Down
10 changes: 6 additions & 4 deletions app/models/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
class Location < ActiveRecord::Base
include Locations::Searchable
include Locations::Officeable
include PgSearch::Model
multisearchable against: [:name]

enum non_standard_office_hours: { appointment_only: 1, always_open: 2, no_set_business_hours: 3 }

belongs_to :organization, optional: true

scope :active, -> { joins(:organization).where(organization: { active: true }) }
scope :public_address, -> {where(public_address: true)}
scope :public_address, -> { where(public_address: true) }
scope :besides_po_boxes, -> { where(po_box: false) }
# scope :in_nashville, -> { where("ST_DWithin(lonlat, ST_GeographyFromText('SRID=4326;POINT(-86.78125827725053 36.16404968727089)'), 1000000) = true") }
scope :locations_with_, ->(cause) { group(:id).joins(:causes).where(causes: { id: cause.id }) }
Expand All @@ -51,8 +53,8 @@ class Location < ActiveRecord::Base
validates :longitude, presence: true
validates :lonlat, presence: true
validates :main, inclusion: { in: [true, false] }
validates :offer_services, inclusion: { in: [ true, false ] }
validates :non_standard_office_hours, inclusion: { in: non_standard_office_hours.keys}, allow_blank: true
validates :offer_services, inclusion: { in: [true, false] }
validates :non_standard_office_hours, inclusion: { in: non_standard_office_hours.keys }, allow_blank: true

scope :additional, -> { where(main: false) }
scope :main, -> { where(main: true) }
Expand Down Expand Up @@ -84,7 +86,7 @@ def formatted_address
end

def address_with_suite_number
address.split(",").insert(1, suite).join(", ")
address.split(',').insert(1, suite).join(', ')
end

def link_to_google_maps
Expand Down
2 changes: 2 additions & 0 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
class Organization < ApplicationRecord
include Organizations::Constants
validates_with OrganizationValidator
include PgSearch::Model
multisearchable against: [:name]

scope :active, -> { where(active: true) }

Expand Down
3 changes: 3 additions & 0 deletions app/models/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
# updated_at :datetime not null
#
class Service < ApplicationRecord
include PgSearch::Model
multisearchable against: :name

belongs_to :cause
has_many :location_services, dependent: :destroy
has_many :locations, through: :location_services
Expand Down
11 changes: 1 addition & 10 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,5 @@
class Tag < ApplicationRecord
include PgSearch::Model
belongs_to :organization

pg_search_scope :search_suggestions, against: :name,
using: {
tsearch: { prefix: true },
trigram: { threshold: 0.2 }
}

def self.suggestions(term)
search_suggestions(term).pluck(:name).uniq.map(&:downcase)
end
multisearchable against: :name
end
2 changes: 1 addition & 1 deletion app/views/home/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ main
| Search for listings of nonprofit organizations based on your needs.
= form_with model: @search, url: search_path, method: :get do |f|
.c-form class="py-0 mx-auto sm:px-3 sm:flex-col sm:justify-center sm:max-w-4xl"
div class="relative w-full sm:max-w-xl mb-7 sm:mb-0" data-controller="autocomplete" data-autocomplete-url-value=autocomplete_index_path role="combobox"
div class="relative w-full sm:max-w-xl mb-7 sm:mb-0" data-controller="autocomplete" data-autocomplete-url-value=autocomplete_index_path role="combobox" data-autocomplete-min-length-value="2"
= f.text_field :keyword, autocomplete: "search", class:"c-input pl-10 m-0 w-full", placeholder: "Try \"Mental Health Nonprofits\"", data: { "autocomplete-target": "input" }
ul class="absolute w-full px-1 bg-white rounded-sm" data-autocomplete-target="results" role="listbox"
= inline_svg_tag 'search-icon.svg', class:"absolute top-1/3 left-4"
Expand Down
3 changes: 3 additions & 0 deletions config/initializers/pg_search.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PgSearch.multisearch_options = { using: { tsearch: { prefix: true },
trigram: { threshold: 0.1 },
dmetaphone: {} } }
Loading