| 1 | class WelcomeController < ApplicationController |
| 2 | QUERY_LIMIT_AUTOCOMPLETE = 5 |
| 3 | QUERY_LIMIT_SEARCH_PAGE = 10_000 |
| 4 | |
| 5 | def index |
| 6 | meta_tags 'home' |
| 7 | @template = 'main' |
| 8 | end |
| 9 | |
| 10 | def search |
| 11 | @keywords = params[:s] |
| 12 | return if @keywords.nil? |
| 13 | |
| 14 | meta_tags 'search' |
| 15 | @results = merge_search(QUERY_LIMIT_SEARCH_PAGE) |
| 16 | end |
| 17 | |
| 18 | private |
| 19 | |
| 20 | def meta_tags(slug) |
| 21 | meta = Meta |
| 22 | .select(:title, :description) |
| 23 | .where(home: slug, locale: [I18n.locale, I18n.default_locale]) |
| 24 | .last |
| 25 | |
| 26 | @meta_title = meta.title if meta |
| 27 | @meta_description = meta.description if meta |
| 28 | @meta_url = root_url |
| 29 | end |
| 30 | |
| 31 | def merge_search(limit) |
| 32 | keyword = params[:s].downcase |
| 33 | search_gending_notation(keyword, limit) + search_gending(keyword, limit) + |
| 34 | search_artist(keyword, limit) + search_song_provider(keyword, limit) + search_album(keyword, limit) |
| 35 | end |
| 36 | |
| 37 | def search_song(keywords, limit) |
| 38 | Song |
| 39 | .where('lower(name) LIKE ?', "%#{keywords}%") |
| 40 | .limit(limit) |
| 41 | .as_json(only: %i[id name]) + |
| 42 | search_song_provider(keywords, limit) |
| 43 | end |
| 44 | |
| 45 | def search_artist(keywords, limit) |
| 46 | Artist |
| 47 | .where('lower(name) LIKE ?', "%#{keywords}%") |
| 48 | .limit(limit) |
| 49 | .as_json(only: %i[id name]) + |
| 50 | search_artist_provider(keywords, limit) |
| 51 | end |
| 52 | |
| 53 | def search_album(keywords, limit) |
| 54 | Album |
| 55 | .where('lower(name) LIKE ?', "%#{keywords}%") |
| 56 | .limit(limit) |
| 57 | .as_json(only: %i[id name]) + |
| 58 | search_album_provider(keywords, limit) |
| 59 | end |
| 60 | |
| 61 | def search_album_provider(keywords, limit) |
| 62 | AlbumProvider |
| 63 | .where('lower(name) LIKE ?', "%#{keywords}%") |
| 64 | .limit(limit) |
| 65 | .as_json(only: %i[id name]) |
| 66 | end |
| 67 | |
| 68 | def search_artist_provider(keywords, limit) |
| 69 | ArtistProvider |
| 70 | .where('lower(name) LIKE ?', "%#{keywords}%") |
| 71 | .limit(limit) |
| 72 | .as_json(only: %i[id name]) |
| 73 | end |
| 74 | |
| 75 | def search_song_provider(keywords, limit) |
| 76 | SongProvider |
| 77 | .where('lower(name) LIKE ?', "%#{keywords}%") |
| 78 | .limit(limit) |
| 79 | .as_json(only: %i[id name]) |
| 80 | end |
| 81 | |
| 82 | def search_gending(keywords, limit) |
| 83 | JavaneseGending |
| 84 | .where('lower(name) LIKE ?', "%#{keywords}%") |
| 85 | .limit(limit) |
| 86 | .as_json(only: %i[id name]) |
| 87 | end |
| 88 | |
| 89 | def search_gending_notation(keywords, limit) |
| 90 | JavaneseGendingNotation |
| 91 | .where('lower(name) LIKE ?', "%#{keywords}%") |
| 92 | .limit(limit) |
| 93 | .as_json(only: %i[id name]) |
| 94 | end |
| 95 | end |