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