| 1 | class AlbumsController < ApplicationController |
| 2 | def index |
| 3 | meta_tags 'albums' |
| 4 | |
| 5 | @page = Page |
| 6 | .select(:page_content, :title) |
| 7 | .where(page_slug: 'albums', locale: [I18n.locale, I18n.default_locale]) |
| 8 | .last |
| 9 | |
| 10 | @albums = Album |
| 11 | .includes(:artists) |
| 12 | .select(:id, :name, :views_count, :description, :updated_at) |
| 13 | .where(album_artists: { primary: true }) |
| 14 | .references(:artists) |
| 15 | .paginate(page: params[:page], per_page: 10) |
| 16 | |
| 17 | @trending_albums = Album |
| 18 | .includes(:artists) |
| 19 | .select(:id, :name, :views_count, :description, :updated_at) |
| 20 | .where(album_artists: { primary: true }) |
| 21 | .references(:artists) |
| 22 | .order(views_count: :desc) |
| 23 | .limit(10) |
| 24 | end |
| 25 | |
| 26 | def detail |
| 27 | @page_title_artist = t('album_artists') |
| 28 | @page_title_songs = t('album_song_list') |
| 29 | |
| 30 | album_id = params[:slug].split('-')[-1] |
| 31 | @album = Album |
| 32 | .select('albums.id, albums.name, albums.user_id, albums.description, albums.views_count') |
| 33 | .find_by id: album_id |
| 34 | |
| 35 | return redirect_to album_path unless @album |
| 36 | |
| 37 | @artists = Artist |
| 38 | .select('artists.id, artists.name') |
| 39 | .joins(:album_artists) |
| 40 | .where(album_artists: { album_id: @album.id }) |
| 41 | .order('album_artists.primary DESC') |
| 42 | |
| 43 | @page_title = @album.name |
| 44 | |
| 45 | meta_tags 'album_detail' |
| 46 | @album.increment!(:views_count) unless browser.bot? |
| 47 | end |
| 48 | |
| 49 | def bridge |
| 50 | @page_title_artist = t('album_artists') |
| 51 | @page_title_songs = t('album_song_list') |
| 52 | |
| 53 | album_id = params[:slug].split('-')[-1] |
| 54 | @album = AlbumProvider |
| 55 | .select(:id, :name, :user_id, :provider_id, :provider_type) |
| 56 | .find_by(id: album_id) |
| 57 | |
| 58 | return redirect_to album_path unless @album |
| 59 | |
| 60 | @page_title = @album.name |
| 61 | |
| 62 | meta_tags 'album_detail' |
| 63 | end |
| 64 | |
| 65 | def add |
| 66 | if request.post? |
| 67 | album = Album.new(album_params) |
| 68 | unless album.save |
| 69 | flash[:warning] = "We need at the album's title and description." |
| 70 | redirect_to album_add_path |
| 71 | return |
| 72 | end |
| 73 | redirect_to album_detail_path(album.slug) |
| 74 | end |
| 75 | |
| 76 | @album = Album.new(name: params[:name], description: params[:description]) |
| 77 | @user = user_signed_in? ? current_user : User.find_by(username: 'nowhereman') |
| 78 | @page_title = 'Add Album' |
| 79 | |
| 80 | @meta_title = @page_title |
| 81 | @meta_description = @page_title |
| 82 | end |
| 83 | |
| 84 | def vote |
| 85 | return redirect_to login_path unless user_signed_in? |
| 86 | |
| 87 | vote = AlbumVote.new(album_id: params[:id], user_id: current_user.id) |
| 88 | vote.save |
| 89 | |
| 90 | redirect_back(fallback_location: root_path) |
| 91 | end |
| 92 | |
| 93 | def unvote |
| 94 | return redirect_to login_path unless user_signed_in? |
| 95 | |
| 96 | vote = AlbumVote.where(album_id: params[:id], user_id: current_user.id) |
| 97 | vote.destroy_all |
| 98 | |
| 99 | redirect_back(fallback_location: root_path) |
| 100 | end |
| 101 | |
| 102 | private |
| 103 | |
| 104 | def album_params |
| 105 | params |
| 106 | .require(:album) |
| 107 | .permit(:name, :description, :user_id) |
| 108 | end |
| 109 | |
| 110 | def meta_tags(slug) |
| 111 | meta = Meta |
| 112 | .select(:title, :description) |
| 113 | .where(home: slug, locale: [I18n.locale, I18n.default_locale]) |
| 114 | .last |
| 115 | @meta_title = meta.title if meta |
| 116 | @meta_description = meta.description if meta |
| 117 | end |
| 118 | end |