| 1 | # frozen_string_literal: true |
| 2 | |
| 3 | require 'action_view' |
| 4 | require 'action_view/helpers' |
| 5 | |
| 6 | # Notation of a Javanese gending |
| 7 | # Not to be confused with JavaneseGending itself |
| 8 | |
| 9 | class JavaneseGendingNotation < ApplicationRecord |
| 10 | include ActionView::Helpers::DateHelper |
| 11 | include Rails.application.routes.url_helpers |
| 12 | |
| 13 | belongs_to :user |
| 14 | belongs_to :javanese_gending |
| 15 | has_many :comments, as: :commentable |
| 16 | has_many :gending_notation_vote, dependent: :destroy |
| 17 | has_many :users, through: :gending_notation_vote |
| 18 | |
| 19 | def link_title |
| 20 | name.to_s.downcase |
| 21 | end |
| 22 | |
| 23 | def slug |
| 24 | "#{name}-#{id}".downcase.parameterize |
| 25 | end |
| 26 | |
| 27 | def user_or_nowhereman |
| 28 | user || User.find_by(username: 'nowhereman') |
| 29 | end |
| 30 | |
| 31 | def as_json(options = {}) |
| 32 | options[:methods] = %i[path type created] |
| 33 | super |
| 34 | end |
| 35 | |
| 36 | def path |
| 37 | javanese_gending_gamelan_notation_detail_path(slug) |
| 38 | end |
| 39 | |
| 40 | def created |
| 41 | "#{time_ago_in_words(created_at)} #{I18n.t('ago')}" |
| 42 | end |
| 43 | |
| 44 | def as_json_packed |
| 45 | as_json(only: %i[id name rich_format]) |
| 46 | end |
| 47 | |
| 48 | private |
| 49 | |
| 50 | def type |
| 51 | I18n.t(self.class.name) |
| 52 | end |
| 53 | end |