# == Schema Information
#
# Table name: pictures
#
#  id             :integer          not null, primary key
#  features       :text
#  imageable_type :string
#  imageable_id   :integer
#  created_at     :datetime         not null
#  updated_at     :datetime         not null
#  image          :binary
#  features_json  :text
#
# Indexes
#
#  index_pictures_on_imageable_type_and_imageable_id  (imageable_type,imageable_id)
#

class Picture < ApplicationRecord
  belongs_to :imageable, polymorphic: true, optional: true

  validates :image, presence: true
  validates :features, presence: true

  attr_accessor :image_filename

  before_validation do
    if image_filename.present?
      self.features, self.features_json = Array(Python.new.face_encodings(image_filename))
      MiniMagick::Image.new(image_filename) do |image|
        image.resize '100x100'
      end
      self.image = File.binread(image_filename)
    end
  end
end
