| 1 | require 'action_view' |
| 2 | require 'action_view/helpers' |
| 3 | |
| 4 | class JavaneseGending < ApplicationRecord |
| 5 | include ActionView::Helpers::DateHelper |
| 6 | include Rails.application.routes.url_helpers |
| 7 | |
| 8 | belongs_to :user |
| 9 | has_many :javanese_gending_notation |
| 10 | has_many :comments, as: :commentable |
| 11 | has_many :gending_vote, dependent: :destroy |
| 12 | has_many :users, through: :gending_vote |
| 13 | |
| 14 | def user_or_nowhereman |
| 15 | user || User.find_by(username: 'nowhereman') |
| 16 | end |
| 17 | |
| 18 | def slug |
| 19 | "#{name.downcase.parameterize}-#{id.to_s}" |
| 20 | end |
| 21 | |
| 22 | def as_json(options = {}) |
| 23 | options[:methods] = %i[path type created] |
| 24 | super |
| 25 | end |
| 26 | |
| 27 | private |
| 28 | |
| 29 | def created |
| 30 | "#{time_ago_in_words(created_at)} #{I18n.t('ago')}" |
| 31 | end |
| 32 | |
| 33 | def path |
| 34 | javanese_gending_detail_path(slug) |
| 35 | end |
| 36 | |
| 37 | def type |
| 38 | I18n.t(self.class.name) |
| 39 | end |
| 40 | end |