hummingbird-me/hummingbird

View on GitHub
app/serializers/anime_serializer.rb

Summary

Maintainability
A
0 mins
Test Coverage
class AnimeSerializer < ActiveModel::Serializer
  embed :ids

  attributes :id, :canonical_title, :english_title, :romaji_title, :synopsis,
    :poster_image, :poster_image_thumb, :show_type, :age_rating,
    :age_rating_guide, :episode_count, :episode_length, :started_airing,
    :started_airing_date_known, :finished_airing, :genres, :updated_at

  def id
    object.slug
  end

  def canonical_title
    object.canonical_title(scope.try(:title_language_preference) || 'canonical')
  end

  def english_title
    object.alt_title
  end

  def romaji_title
    object.title
  end

  def poster_image
    if !object.sfw? && (scope.nil? || scope.try(:sfw_filter))
      "/assets/missing-anime-cover.jpg"
    else
      object.poster_image.url(:large)
    end
  end

  def poster_image_thumb
    if !object.sfw? && (scope.nil? || scope.try(:sfw_filter))
      "/assets/missing-anime-cover.jpg"
    else
      object.poster_image.url(:medium)
    end
  end

  def episode_count
    (object.episode_count && object.episode_count > 0) ? object.episode_count : nil
  end

  def episode_length
    (object.episode_length && object.episode_length > 0) ? object.episode_length : nil
  end

  def started_airing
    object.started_airing_date
  end

  def finished_airing
    object.finished_airing_date
  end

  def genres
    object.genres.map(&:name).sort
  end
end