feature/share-libs
rb 60 lines 1.22 KB
Raw
1 # frozen_string_literal: true
2
3 require 'action_view'
4 require 'action_view/helpers'
5
6 class SongProvider < ApplicationRecord
7 include ActionView::Helpers::DateHelper
8 include Rails.application.routes.url_helpers
9
10 belongs_to :user
11 belongs_to :song
12 belongs_to :album_provider, optional: true
13 belongs_to :artist_provider, optional: true
14 # Should only has one chord. All contribution should be done through one chord.
15 has_one :chord
16 has_one :audio_file
17
18 has_many :song_provider_vote, dependent: :destroy
19 has_many :voters, through: :song_provider_vote, source: :user
20
21 enum provider_type: %i[
22 spotify
23 youtube
24 ]
25
26 enum status: %i[
27 published
28 unpublished
29 ]
30
31 def as_json(options = {})
32 options[:methods] = %i[audio_file path type provider_id provider_type created]
33 super
34 end
35
36 def type
37 I18n.t(self.class.name)
38 end
39
40 def slug
41 "#{name}-#{id}".downcase.parameterize
42 end
43
44 def path
45 song_bridge_path(slug)
46 end
47
48 def provider_url
49 case provider_type
50 when 'spotify'
51 "https://open.spotify.com/track/#{provider_id}"
52 when 'youtube'
53 "https://www.youtube.com/watch?v=#{provider_id}"
54 end
55 end
56
57 def created
58 "#{time_ago_in_words(created_at)} #{I18n.t('ago')}"
59 end
60 end