diff --git a/app/decorators/anime_decorator.rb b/app/decorators/anime_decorator.rb new file mode 100644 index 0000000..2632073 --- /dev/null +++ b/app/decorators/anime_decorator.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class AnimeDecorator < Draper::Decorator + delegate_all + + def human_status_name + I18n.t("labels.status.#{status}") + end +end diff --git a/app/models/anime.rb b/app/models/anime.rb index 58078e3..3b5df8b 100644 --- a/app/models/anime.rb +++ b/app/models/anime.rb @@ -16,6 +16,8 @@ class Anime < ApplicationRecord validates :wiki_url, length: { maximum: Settings.anime.wiki_url.maximum_length } + enum status: { unpublished: 0, prepared: 1, published: 2 } + mount_uploader :picture, PictureUploader def airing? diff --git a/app/models/season.rb b/app/models/season.rb index 0a26cf0..a34d161 100644 --- a/app/models/season.rb +++ b/app/models/season.rb @@ -18,6 +18,8 @@ class Season < ApplicationRecord validates :previous_name, :behind_name, length: { maximum: Settings.season.name.maximum_length } + enum status: { unpublished: 0, prepared: 1, published: 2 } + def self.airing(date) where('seasons.start_on <= ?', date) .where('seasons.end_on >= ? or seasons.end_on is null', date) diff --git a/app/views/api/admin/animes/index.json.jbuilder b/app/views/api/admin/animes/index.json.jbuilder index 58bcc3e..18979af 100644 --- a/app/views/api/admin/animes/index.json.jbuilder +++ b/app/views/api/admin/animes/index.json.jbuilder @@ -3,6 +3,7 @@ json.animes do json.array! @animes do |anime| json.id anime.id + json.status_name anime.decorate.human_status_name json.title anime.title json.picture anime.picture.url json.airing anime.airing? diff --git a/config/locales/ja.yml b/config/locales/ja.yml index 0541a12..ba037be 100644 --- a/config/locales/ja.yml +++ b/config/locales/ja.yml @@ -4,3 +4,8 @@ ja: extension_whitelist_error: にアップロードできる拡張子は、jpg/jpeg/png/gifのみです sessions: invalid_parameters: メールアドレスまたはパスワードが正しくありません + labels: + status: + unpublished: 非公開 + prepared: 準備中 + published: 公開 diff --git a/db/migrate/20170913092757_add_status_to_animes.rb b/db/migrate/20170913092757_add_status_to_animes.rb new file mode 100644 index 0000000..ec47f1a --- /dev/null +++ b/db/migrate/20170913092757_add_status_to_animes.rb @@ -0,0 +1,6 @@ +class AddStatusToAnimes < ActiveRecord::Migration[5.1] + def change + # NOTE: default 0 is :unpublished + add_column :animes, :status, :integer, null: false, default: 0 + end +end diff --git a/db/migrate/20170914085437_add_status_to_seasons.rb b/db/migrate/20170914085437_add_status_to_seasons.rb new file mode 100644 index 0000000..c0f7614 --- /dev/null +++ b/db/migrate/20170914085437_add_status_to_seasons.rb @@ -0,0 +1,6 @@ +class AddStatusToSeasons < ActiveRecord::Migration[5.1] + def change + # NOTE: default 0 is :unpublished + add_column :seasons, :status, :integer, null: false, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index 97421af..363e50c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170910082027) do +ActiveRecord::Schema.define(version: 20170914085437) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -54,6 +54,7 @@ t.datetime "updated_at", null: false t.integer "created_by" t.integer "updated_by" + t.integer "status", default: 0, null: false end create_table "appearances", id: :serial, force: :cascade do |t| @@ -111,6 +112,7 @@ t.string "previous_name" t.integer "created_by" t.integer "updated_by" + t.integer "status", default: 0, null: false end create_table "singers", id: :serial, force: :cascade do |t| diff --git a/spec/factories/animes.rb b/spec/factories/animes.rb index 1b43be6..6eeac80 100644 --- a/spec/factories/animes.rb +++ b/spec/factories/animes.rb @@ -6,5 +6,16 @@ sequence(:summary) { |n| "あらすじ#{n}" } wiki_url Faker::Internet.url picture { fixture_file_upload('spec/fixtures/clover.gif', 'image/gif') } + status { Anime.statuses.keys.sample } + + trait :unpublished do + status :unpublished + end + trait :prepared do + status :prepared + end + trait :published do + status :published + end end end diff --git a/spec/requests/admin/animes_spec.rb b/spec/requests/admin/animes_spec.rb index 188c052..6f22184 100644 --- a/spec/requests/admin/animes_spec.rb +++ b/spec/requests/admin/animes_spec.rb @@ -3,8 +3,8 @@ require 'rails_helper' describe 'GET /api/admin/animes', autodoc: true do - let!(:anime1) { create(:anime, created_at: 2.minutes.ago) } - let!(:anime2) { create(:anime) } + let!(:anime1) { create(:anime, :published, created_at: 2.minutes.ago) } + let!(:anime2) { create(:anime, :prepared) } let!(:season1) { create(:season, anime: anime1, phase: 1).decorate } let!(:season2) { create(:season, anime: anime1, phase: 2).decorate } let!(:melody1) { create(:melody, :op, season: season1) } @@ -28,6 +28,7 @@ animes: [ { id: anime2.id, + status_name: '準備中', title: anime2.title, picture: anime2.picture.url, airing: false, @@ -35,6 +36,7 @@ }, { id: anime1.id, + status_name: '公開', title: anime1.title, picture: anime1.picture.url, airing: true,