feature/share-libs
rb 44 lines 911 Bytes
Raw
1 require 'action_view'
2 require 'action_view/helpers'
3
4 class Artist < ApplicationRecord
5 include ActionView::Helpers::DateHelper
6 include Rails.application.routes.url_helpers
7
8 has_many :album_artists
9 has_many :album, through: :album_artists
10 accepts_nested_attributes_for :album_artists
11
12 belongs_to :user
13
14 has_many :comments, as: :commentable
15
16 has_many :artist_vote, dependent: :destroy
17 has_many :users, through: :artist_vote
18
19 validates_length_of :name, presence: true, allow_blank: false
20 validates_length_of :description, presence: true, allow_blank: false
21
22 def as_json(options = {})
23 options[:methods] = %i[path type created]
24 super
25 end
26
27 def slug
28 "#{name}-#{id}".downcase.parameterize
29 end
30
31 private
32
33 def type
34 I18n.t(self.class.name)
35 end
36
37 def path
38 artist_detail_path(slug)
39 end
40
41 def created
42 "#{time_ago_in_words(created_at)} #{I18n.t('ago')}"
43 end
44 end