| 1 | require 'action_view' |
| 2 | require 'action_view/helpers' |
| 3 | |
| 4 | class ArtistProvider < ApplicationRecord |
| 5 | include ActionView::Helpers::DateHelper |
| 6 | include Rails.application.routes.url_helpers |
| 7 | |
| 8 | belongs_to :user |
| 9 | belongs_to :artist, optional: true |
| 10 | |
| 11 | enum provider_type: %i[ |
| 12 | spotify |
| 13 | ] |
| 14 | |
| 15 | def as_json(options = {}) |
| 16 | options[:methods] = %i[path type created] |
| 17 | super |
| 18 | end |
| 19 | |
| 20 | def type |
| 21 | I18n.t(self.class.name) |
| 22 | end |
| 23 | |
| 24 | def slug |
| 25 | "#{name}-#{id}".downcase.parameterize |
| 26 | end |
| 27 | |
| 28 | def path |
| 29 | artist_bridge_path(slug) |
| 30 | end |
| 31 | |
| 32 | def provider_url |
| 33 | "https://open.spotify.com/artist/#{provider_id}" |
| 34 | end |
| 35 | |
| 36 | def created |
| 37 | "#{time_ago_in_words(created_at)} #{I18n.t('ago')}" |
| 38 | end |
| 39 | end |