diff --git a/app/admin/actors_admin.rb b/app/admin/actors_admin.rb index 8935be3..3d4e6e3 100644 --- a/app/admin/actors_admin.rb +++ b/app/admin/actors_admin.rb @@ -14,7 +14,7 @@ end decorate_collection do |collection| - collection.group(:id).left_outer_joins(:credits).select("actors.*, COUNT(credits.actor_id) AS credits_count") + collection.group(:id).left_outer_joins(:credits).select("people.*, COUNT(credits.person_id) AS credits_count") end sort_column(:credits_count) do |collection, order| diff --git a/app/admin/credits_admin.rb b/app/admin/credits_admin.rb index 642049a..2e20f22 100644 --- a/app/admin/credits_admin.rb +++ b/app/admin/credits_admin.rb @@ -8,7 +8,7 @@ end col(sm: 8) do - select :actor_id, Actor.alphabetical + select :person_id, Person.alphabetical row do col(sm: 9) { text_field :character } diff --git a/app/admin/movies_admin.rb b/app/admin/movies_admin.rb index 3613b8f..438bcee 100644 --- a/app/admin/movies_admin.rb +++ b/app/admin/movies_admin.rb @@ -20,7 +20,7 @@ column :title, link: true, sort: :title, class: "media-title-column" do |movie| safe_join([ content_tag(:strong, movie.title), - content_tag(:small, movie.credits.top_billing.map(&:name).compact.join(", "), class: "text-muted hidden-xs") + content_tag(:small, movie.acting_credits.top_billing.map(&:name).compact.join(", "), class: "text-muted hidden-xs") ], "
".html_safe) end column :genres, format: :tags, class: "hidden-xs" do |movie| @@ -54,8 +54,8 @@ end end - tab :credits, badge: movie.credits.count do - table CreditsAdmin.table, collection: movie.credits.includes(:actor) + tab :acting_credits, badge: movie.acting_credits.count do + table CreditsAdmin.table, collection: movie.acting_credits.includes(:person) end tab :media, partial: "admin/shared/media", badge: movie.media_count diff --git a/app/admin/tv_shows_admin.rb b/app/admin/tv_shows_admin.rb index f9132a7..6e404cc 100644 --- a/app/admin/tv_shows_admin.rb +++ b/app/admin/tv_shows_admin.rb @@ -20,7 +20,7 @@ column :title, link: true, sort: :name, class: "media-title-column" do |tv_show| safe_join([ content_tag(:strong, tv_show.name), - content_tag(:small, tv_show.credits.top_billing.map(&:name).join(", "), class: "text-muted hidden-xs") + content_tag(:small, tv_show.acting_credits.top_billing.map(&:name).join(", "), class: "text-muted hidden-xs") ], "
".html_safe) end column :genres, format: :tags, class: "hidden-xs" do |tv_show| @@ -53,8 +53,8 @@ end end - tab :credits, badge: tv_show.credits.count do - table CreditsAdmin.table, collection: tv_show.credits.includes(:actor) + tab :acting_credits, badge: tv_show.acting_credits.count do + table CreditsAdmin.table, collection: tv_show.acting_credits.includes(:person) end tab :seasons, badge: tv_show.seasons.count do diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 383def5..c820ad2 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -6,7 +6,7 @@ def index def show @movie = Movie.find(params[:id]) - @credits = @movie.credits.includes(:actor) + @credits = @movie.acting_credits.includes(:person) @videos = @movie.videos @posters = @movie.images.posters @backdrops = @movie.images.backdrops diff --git a/app/controllers/tv_shows_controller.rb b/app/controllers/tv_shows_controller.rb index 29bf26c..3cf34dc 100644 --- a/app/controllers/tv_shows_controller.rb +++ b/app/controllers/tv_shows_controller.rb @@ -6,7 +6,7 @@ def index def show @tv_show = TVShow.find(params[:id]) - @credits = @tv_show.credits.includes(:actor) + @credits = @tv_show.acting_credits.includes(:person) @seasons = @tv_show.seasons @videos = @tv_show.videos @posters = @tv_show.images.posters diff --git a/app/importers/tmdb_importer.rb b/app/importers/tmdb_importer.rb index 909e237..adf032b 100644 --- a/app/importers/tmdb_importer.rb +++ b/app/importers/tmdb_importer.rb @@ -27,7 +27,7 @@ def import } threads << Thread.new { - videos = throttle { @scope.videos( stub.id, language: "en") } + videos = throttle { @scope.videos(stub.id, language: "en") } VideosImporter.new(instance).import(videos) } diff --git a/app/models/acting_credit.rb b/app/models/acting_credit.rb new file mode 100644 index 0000000..2ab5cfe --- /dev/null +++ b/app/models/acting_credit.rb @@ -0,0 +1,7 @@ +class ActingCredit < Credit + alias_attribute :character, :role + + def self.top_billing + includes(:person).first(5) + end +end diff --git a/app/models/actor.rb b/app/models/actor.rb index 0fa82aa..db3792d 100644 --- a/app/models/actor.rb +++ b/app/models/actor.rb @@ -1,25 +1,2 @@ -class Actor < ApplicationRecord - include PgSearch::Model - pg_search_scope :pg_search, against: [:name], using: { tsearch: { prefix: true, tsvector_column: "tsv" } } - - has_many :credits - - scope :alphabetical, -> { order(name: :asc) } - - validates :name, presence: true - validates :tmdb_id, uniqueness: { allow_blank: true } - - enum gender: { "Not specified" => 0, "Female" => 1, "Male" => 2, "Non-Binary" => 3 } - - def initials - name.split.map(&:first).join - end - - def profile? - profile_path.present? - end - - def profile_url(version="original") - tmdb_image(profile_path, version) - end +class Actor < Person end diff --git a/app/models/concerns/media.rb b/app/models/concerns/media.rb index 1b65ff2..d9ecd7e 100644 --- a/app/models/concerns/media.rb +++ b/app/models/concerns/media.rb @@ -2,7 +2,7 @@ module Media extend ActiveSupport::Concern included do - has_many :credits, -> { ordered }, as: :media + has_many :acting_credits, -> { actors.ordered }, as: :media has_many :videos, as: :media has_many :images, as: :media diff --git a/app/models/credit.rb b/app/models/credit.rb index 64ffb09..c0fdf4c 100644 --- a/app/models/credit.rb +++ b/app/models/credit.rb @@ -1,12 +1,9 @@ class Credit < ApplicationRecord belongs_to :media, polymorphic: true - belongs_to :actor + belongs_to :person scope :ordered, -> { order(order: :asc) } + scope :actors, -> { where(type: "ActingCredit") } - delegate :name, :initials, :gender, :profile?, :profile_url, :profile_path, to: :actor, allow_nil: true - - def self.top_billing - includes(:actor).first(5) - end + delegate :name, :initials, :gender, :profile?, :profile_url, :profile_path, to: :person, allow_nil: true end diff --git a/app/models/person.rb b/app/models/person.rb new file mode 100644 index 0000000..05243a7 --- /dev/null +++ b/app/models/person.rb @@ -0,0 +1,25 @@ +class Person < ApplicationRecord + include PgSearch::Model + pg_search_scope :pg_search, against: [:name], using: { tsearch: { prefix: true, tsvector_column: "tsv" } } + + has_many :credits + + scope :alphabetical, -> { order(name: :asc) } + + validates :name, presence: true + validates :tmdb_id, uniqueness: { allow_blank: true } + + enum gender: { "Not specified" => 0, "Female" => 1, "Male" => 2, "Non-Binary" => 3 } + + def initials + name.split.map(&:first).join + end + + def profile? + profile_path.present? + end + + def profile_url(version="original") + tmdb_image(profile_path, version) + end +end diff --git a/db/migrate/20210514112735_convert_actors_to_people.rb b/db/migrate/20210514112735_convert_actors_to_people.rb new file mode 100644 index 0000000..99464ba --- /dev/null +++ b/db/migrate/20210514112735_convert_actors_to_people.rb @@ -0,0 +1,8 @@ +class ConvertActorsToPeople < ActiveRecord::Migration[5.2] + def change + rename_table :actors, :people + add_column :people, :type, :string + + Person.update_all(type: "Actor") + end +end diff --git a/db/migrate/20210514114729_convert_credits_to_people.rb b/db/migrate/20210514114729_convert_credits_to_people.rb new file mode 100644 index 0000000..2b92c90 --- /dev/null +++ b/db/migrate/20210514114729_convert_credits_to_people.rb @@ -0,0 +1,9 @@ +class ConvertCreditsToPeople < ActiveRecord::Migration[5.2] + def change + rename_column :credits, :actor_id, :person_id + rename_column :credits, :character, :role + add_column :credits, :type, :string + + Credit.update_all(type: "ActingCredit") + end +end diff --git a/db/schema.rb b/db/schema.rb index 20f6aae..dc92443 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,23 +10,11 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_02_22_031028) do +ActiveRecord::Schema.define(version: 2021_05_14_114729) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" - create_table "actors", force: :cascade do |t| - t.integer "tmdb_id" - t.string "name" - t.integer "gender", default: 0 - t.string "profile_path" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.tsvector "tsv" - t.index ["name"], name: "index_actors_on_name" - t.index ["tsv"], name: "index_actors_on_tsv", using: :gin - end - create_table "administrators", force: :cascade do |t| t.string "email" t.string "password_digest" @@ -41,13 +29,14 @@ create_table "credits", force: :cascade do |t| t.string "media_type" t.bigint "media_id" - t.bigint "actor_id" - t.string "character" + t.bigint "person_id" + t.string "role" t.integer "order" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.index ["actor_id"], name: "index_credits_on_actor_id" + t.string "type" t.index ["media_type", "media_id"], name: "index_credits_on_media_type_and_media_id" + t.index ["person_id"], name: "index_credits_on_person_id" end create_table "genres", force: :cascade do |t| @@ -108,6 +97,19 @@ t.index ["vote_average"], name: "index_movies_on_vote_average" end + create_table "people", force: :cascade do |t| + t.integer "tmdb_id" + t.string "name" + t.integer "gender", default: 0 + t.string "profile_path" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.tsvector "tsv" + t.string "type" + t.index ["name"], name: "index_people_on_name" + t.index ["tsv"], name: "index_people_on_tsv", using: :gin + end + create_table "tv_show_seasons", force: :cascade do |t| t.bigint "tv_show_id" t.integer "tmdb_id"