koraktor/gallerist

View on GitHub
lib/gallerist/models/legacy_photos_extensions/person.rb

Summary

Maintainability
A
0 mins
Test Coverage
# This code is free software; you can redistribute it and/or modify it under
# the terms of the new BSD License.
#
# Copyright (c) 2015-2019, Sebastian Staudt

module Gallerist::PhotosExtensions::Person

  def __extend
    self.table_name = 'RKPerson'

    has_one :key_face, class_name: Gallerist::Face.to_s, primary_key: 'representativeFaceId', foreign_key: 'modelId'
    has_many :person_photos, primary_key: 'modelId', foreign_key: 'personId'

    alias_attribute :person_type, :personType
    alias_attribute :simple_name, :searchName

    default_scope do
      select(:modelId, :name, :representativeFaceId).
      where.not(name: nil).where.not(name: '').
      order(person_type: :desc, manual_order: :asc)
    end
  end

  # ActiveRecord does not support has_many-through associations across
  # different databases, so we have to query the photo manually
  def key_photo
    key_face.photo
  end

  # ActiveRecord does not support has_many-through associations across
  # different databases, so we have to query the photos manually
  def photos
    Gallerist::Photo.where modelId: person_photos.map(&:photo_id)
  end

end