| 1 | class LyricsController < ApplicationController |
| 2 | def index |
| 3 | meta = Meta |
| 4 | .select(:title, :description) |
| 5 | .where(home: 'song_lyrics', locale: [I18n.locale, I18n.default_locale]) |
| 6 | .last |
| 7 | |
| 8 | @meta_title = meta.title if meta |
| 9 | @meta_description = meta.description if meta |
| 10 | @meta_url = request.url |
| 11 | |
| 12 | @page = Page |
| 13 | .select(:page_content, :title) |
| 14 | .where(page_slug: 'song_lyrics', locale: [I18n.locale, I18n.default_locale]) |
| 15 | .last |
| 16 | |
| 17 | @lyrics = Lyric |
| 18 | .select(:id, :views_count, :song_id) |
| 19 | .paginate(page: params[:page], per_page: 100) |
| 20 | |
| 21 | @trending_lyrics = @lyrics |
| 22 | .order(views_count: :desc) |
| 23 | .limit(10) |
| 24 | end |
| 25 | |
| 26 | def lyric |
| 27 | lyric_id = params[:slug].split('-')[-1] |
| 28 | @lyric = Lyric.find_by(id: lyric_id) |
| 29 | |
| 30 | redirect_to root_path and return unless @lyric.present? |
| 31 | |
| 32 | @lovers = @lyric.users |
| 33 | @author = @lyric.user |
| 34 | @song = @lyric.song |
| 35 | |
| 36 | unless user_signed_in? |
| 37 | flash[:warning] = t( |
| 38 | 'join_our_lyric_community', |
| 39 | song_name: @song&.name, |
| 40 | song_artist_name: @song.artists.first&.name || 'NaN' |
| 41 | ) |
| 42 | redirect_to signup_path(redirect_to: request.path) |
| 43 | end |
| 44 | |
| 45 | @page_title = t( |
| 46 | 'song_lyric_page_title', |
| 47 | song_name: @song&.name, |
| 48 | song_artist_name: @song.artists.first&.name || 'NaN' |
| 49 | ) |
| 50 | |
| 51 | @lyric_shown = @lyric.lyric.gsub('\\n', '<br>') |
| 52 | @author = @lyric.user |
| 53 | @meta_description = "#{@page_title}. #{@lyric.lyric.gsub('\\n', ' ').gsub(/\r\n?/, '')[0..300].strip}" |
| 54 | |
| 55 | @lyric.increment!(:views_count) unless browser.bot? |
| 56 | |
| 57 | @meta_title = @page_title |
| 58 | end |
| 59 | |
| 60 | def lyric_not_available |
| 61 | if @lyric.nil? |
| 62 | shown_text = t( |
| 63 | 'song_lyric_unavailable_content', |
| 64 | song_name: @song.name, |
| 65 | song_artist_name: @song.album.artists[0].name, |
| 66 | href: ActionController::Base.helpers.link_to( |
| 67 | t('song_lyric_unavailable_content_add_lyric'), |
| 68 | add_lyric_path(@song.id), |
| 69 | class: 'bb-button bb-green bb-large' |
| 70 | ) |
| 71 | ) |
| 72 | @lyric_shown = "<p class='lead'>#{shown_text}</p>" |
| 73 | @meta_description = t( |
| 74 | 'song_lyric_unavailable_meta_description', |
| 75 | song_name: @song.name, |
| 76 | song_artist_name: @song.album.artists[0].name, |
| 77 | song_album_name: @song.album.name |
| 78 | ) |
| 79 | |
| 80 | end |
| 81 | end |
| 82 | end |