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