Skip to content

Commit

Permalink
Consolidate Album and Artist image logic into concern
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredmoody committed Jun 3, 2022
1 parent 282ac1f commit db31db9
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 76 deletions.
2 changes: 1 addition & 1 deletion app/controllers/albums_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def index

def show
@songs = @album.songs.includes(:artist)
AttachAlbumImageFromDiscogsJob.perform_later(@album) if @album.need_attach_from_discogs?
@album.attach_image_from_discogs
end

def edit
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/artists_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def show
@albums = @artist.albums.load_async
@appears_on_albums = @artist.appears_on_albums.load_async

AttachArtistImageFromDiscogsJob.perform_later(@artist) if @artist.need_attach_from_discogs?
@artist.attach_image_from_discogs
end

def edit
Expand Down
14 changes: 0 additions & 14 deletions app/jobs/attach_album_image_from_discogs_job.rb

This file was deleted.

14 changes: 0 additions & 14 deletions app/jobs/attach_artist_image_from_discogs_job.rb

This file was deleted.

10 changes: 10 additions & 0 deletions app/jobs/attach_image_from_discogs_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class AttachImageFromDiscogsJob < ApplicationJob
queue_as :default

def perform(discogs_imageable)
image_url = DiscogsApi.image(discogs_imageable)
discogs_imageable.update(remote_image_url: image_url) if image_url.present?
end
end
11 changes: 1 addition & 10 deletions app/models/album.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,20 @@

class Album < ApplicationRecord
include Searchable
include Imageable

validates :name, uniqueness: {scope: :artist}

has_many :songs, -> { order(:tracknum) }, inverse_of: :album, dependent: :destroy
belongs_to :artist, touch: true

mount_uploader :image, ImageUploader

search_by :name, associations: :artist

def title
is_unknown? ? I18n.t("text.unknown_album") : name
end

def has_image?
image.file.present?
end

def is_unknown?
name.blank?
end

def need_attach_from_discogs?
Setting.discogs_token.present? && !has_image? && !is_unknown?
end
end
11 changes: 1 addition & 10 deletions app/models/artist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

class Artist < ApplicationRecord
include Searchable
include Imageable

has_many :albums, dependent: :destroy
has_many :songs

mount_uploader :image, ImageUploader

search_by :name

def title
Expand All @@ -17,10 +16,6 @@ def title
name
end

def has_image?
image.file.present?
end

def is_unknown?
name.blank?
end
Expand All @@ -32,8 +27,4 @@ def all_albums
def appears_on_albums
Album.joins(:songs).where("albums.artist_id != ? AND songs.artist_id = ?", id, id).distinct
end

def need_attach_from_discogs?
Setting.discogs_token.present? && !has_image? && !is_unknown?
end
end
23 changes: 23 additions & 0 deletions app/models/concerns/imageable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Imageable
extend ActiveSupport::Concern

included do
mount_uploader :image, ImageUploader

after_commit :attach_image_from_discogs, on: :create
end

def has_image?
image.file.present?
end

def attach_image_from_discogs
AttachImageFromDiscogsJob.perform_later(self) if needs_image_from_discogs?
end

private

def needs_image_from_discogs?
Setting.discogs_token.present? && !has_image? && !is_unknown?
end
end
4 changes: 4 additions & 0 deletions app/models/discogs_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def search(options)
JSON.parse response, symbolize_names: true if response.success?
end

def image(imageable)
imageable.is_a?(Artist) ? artist_image(imageable) : album_image(imageable)
end

def artist_image(artist)
raise TypeError, "Expect Artist instance" unless artist.is_a? Artist

Expand Down
2 changes: 1 addition & 1 deletion test/controllers/albums_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class AlbumsControllerTest < ActionDispatch::IntegrationTest
assert_not album.has_image?
assert_not album.is_unknown?

assert_enqueued_with(job: AttachAlbumImageFromDiscogsJob) do
assert_enqueued_with(job: AttachImageFromDiscogsJob) do
assert_login_access(url: album_url(album)) do
assert_response :success
end
Expand Down
2 changes: 1 addition & 1 deletion test/controllers/artists_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ArtistsControllerTest < ActionDispatch::IntegrationTest
assert_not artist.has_image?
assert_not artist.is_unknown?

assert_enqueued_with(job: AttachArtistImageFromDiscogsJob) do
assert_enqueued_with(job: AttachImageFromDiscogsJob) do
assert_login_access(url: artist_url(artist)) do
assert_response :success
end
Expand Down
23 changes: 0 additions & 23 deletions test/jobs/attach_artist_image_from_discogs_job_test.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,22 @@ class AttachAlbumImageFromDiscogsJobTest < ActiveJob::TestCase
DiscogsApi.stub(:album_image, "http://example.com/cover.jpg") do
assert_not album.has_image?

AttachAlbumImageFromDiscogsJob.perform_now(album)
AttachImageFromDiscogsJob.perform_now(album)
assert album.reload.has_image?
end
end

test "should attach image to artist" do
artist = artists(:artist1)

stub_request(:get, "http://example.com/cover.jpg")
.to_return(body: file_fixture("cover_image.jpg").read, status: 200, headers: {"Content-Type" => "image/jpeg"})

DiscogsApi.stub(:artist_image, "http://example.com/cover.jpg") do
assert_not artist.has_image?

AttachImageFromDiscogsJob.perform_now(artist)
assert artist.reload.has_image?
end
end
end

0 comments on commit db31db9

Please sign in to comment.