| 1 | require 'action_view' |
| 2 | require 'action_view/helpers' |
| 3 | |
| 4 | class Chord < ApplicationRecord |
| 5 | include Rails.application.routes.url_helpers |
| 6 | |
| 7 | belongs_to :user, optional: true |
| 8 | belongs_to :song_provider, optional: true |
| 9 | validates_length_of :chord, minimum: 10, presence: true, allow_blank: false |
| 10 | validates_length_of :title, presence: true, allow_blank: false |
| 11 | |
| 12 | has_many :chord_vote, dependent: :destroy |
| 13 | has_many :users, through: :chord_vote |
| 14 | has_many :chord_histories, dependent: :destroy |
| 15 | |
| 16 | has_many :comments, as: :commentable |
| 17 | |
| 18 | # Enum, enum everywhere... |
| 19 | enum status: [ |
| 20 | :pending, # Freshly submitted. |
| 21 | :approved, # Approved by the community. |
| 22 | :deleted # To be deleted. |
| 23 | ] |
| 24 | |
| 25 | def status_symbol |
| 26 | pending? ? 'bi-clock' : 'bi-check2-circle' |
| 27 | end |
| 28 | |
| 29 | def status_color |
| 30 | pending? ? 'text-warning' : 'text-success' |
| 31 | end |
| 32 | |
| 33 | def user_or_nowhereman |
| 34 | user || User.find_by(username: 'nowhereman') |
| 35 | end |
| 36 | |
| 37 | def link_title |
| 38 | if song_provider.present? |
| 39 | song_provider.name.to_s |
| 40 | else |
| 41 | title |
| 42 | end |
| 43 | end |
| 44 | |
| 45 | def slug |
| 46 | slug = if song_provider.present? |
| 47 | "#{song_provider.name}-#{id}" |
| 48 | else |
| 49 | "#{title}-#{id}" |
| 50 | end |
| 51 | slug.downcase.parameterize |
| 52 | end |
| 53 | |
| 54 | def path |
| 55 | guitar_chord_detail_path(slug) |
| 56 | end |
| 57 | |
| 58 | # ActiveAdmin title. See: https://stackoverflow.com/a/8429960/1137814 |
| 59 | def name |
| 60 | song_provider.present? ? song_provider.name : title |
| 61 | end |
| 62 | end |