Skip to content

Commit

Permalink
Extract generic Person and Credit base classes
Browse files Browse the repository at this point in the history
  • Loading branch information
spohlenz committed May 14, 2021
1 parent 6708f35 commit a7d8b9d
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 58 deletions.
2 changes: 1 addition & 1 deletion app/admin/actors_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
2 changes: 1 addition & 1 deletion app/admin/credits_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
6 changes: 3 additions & 3 deletions app/admin/movies_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
], "<br />".html_safe)
end
column :genres, format: :tags, class: "hidden-xs" do |movie|
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions app/admin/tv_shows_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
], "<br />".html_safe)
end
column :genres, format: :tags, class: "hidden-xs" do |tv_show|
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/tv_shows_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/importers/tmdb_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
7 changes: 7 additions & 0 deletions app/models/acting_credit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ActingCredit < Credit
alias_attribute :character, :role

def self.top_billing
includes(:person).first(5)
end
end
25 changes: 1 addition & 24 deletions app/models/actor.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/models/concerns/media.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions app/models/credit.rb
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions app/models/person.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions db/migrate/20210514112735_convert_actors_to_people.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions db/migrate/20210514114729_convert_credits_to_people.rb
Original file line number Diff line number Diff line change
@@ -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
34 changes: 18 additions & 16 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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|
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit a7d8b9d

Please sign in to comment.