<commit_before>require 'roo'
#
# == Schema Information
#
# Table name: images
#
#  id                 :integer          not null, primary key
#  path               :string(255)
#  website_id         :integer
#  group_id           :integer
#  created_at         :datetime
#  updated_at         :datetime
#  canonical_id       :string(255)
#  assignments_count  :integer          default(0)
#  descriptions_count :integer          default(0)
#  title              :text(65535)
#  priority           :boolean          default(FALSE)
#  status_code        :integer          default(0)
#  page_urls          :text(65535)
#
# Indexes
#
#  index_images_on_group_id    (group_id)
#  index_images_on_website_id  (website_id)
#

class Image < ActiveRecord::Base
  serialize :page_urls, JSON

  acts_as_taggable_on :tags
  before_validation :update_status_code

  belongs_to :website, touch: true
  belongs_to :group, touch: true

  has_many :descriptions, dependent: :destroy
  has_associated_audits
  has_many :assignments, dependent: :destroy
  has_many :users, through: :assignments

  validates :path, :presence => true, :uniqueness => {:scope => :website_id}
  validates :canonical_id, :presence => true, :uniqueness => {:scope => :website_id}
  validates_associated :website, :group
  validates_presence_of :website, :group

  default_scope {order('priority DESC, created_at DESC')}

  scope :unassigned, -> (n = 0) { select { |i| i.assignments_count == n } }
  scope :undescribed, -> (n = 0) { select { |i| i.descriptions_count == n } }
  scope :assigned_undescribed, -> (n = 0) { select { |i| i.descriptions_count == n && i.assignments_count > n} }
  scope :unassigned_undescribed, -> (n = 0) { select { |i| i.descriptions_count == n && i.assignments_count == n} }
  scope :described, -> (n = 0) { select { |i| i.descriptions_count > n } }
  scope :assigned, -> (n = 0) { select { |i| i.assignments_count > n } }
  scope :prioritized, -> { order('priority DESC')}

  paginates_per 50

  def to_s
    path
  end

  def alt(status_ids=[2])
    d = descriptions.where(metum_id: 1, status_id: status_ids, locale: "en").first
    if d
      d.text
    else
      ""
    end
  end

  def long(status_ids=[2])
    d = descriptions.where(metum_id: 3, status_id: status_ids, locale: "en").first
    if d
      d.text 
    else
      ""
    end
  end

  def is_mca?
    if website.url.include?("mcachicago")
      true
    else 
      false
    end
  end


  def url
    if website
      website.url + path
    end
  end

  #TODO per locale
  #has no descriptions by this user 
  def undescribed_by?(user)
    !descriptions.collect{ |d| d.user_id}.compact.include?(user.id)
  end

  #has 1 of description by this user
  def described_by?(user)
   descriptions.collect{ |d| d.user_id}.compact.include?(user.id)
  end

  #TODO consider locale
  #has 1 of each metum by this user  ? in any combo of locales and status'es
  def completed_by?(user)
    meta_ids = Metum.all.map{|m| m.id}
    described_meta_ids = descriptions.collect{ |d| d.metum_id if d.user_id == user.id}.compact 
    (meta_ids - described_meta_ids).empty?
  end

  def status
    case status_code
    when 0
      "Not Described"
    when 1
      "Partially Completed"
    when 2<commit_msg>// image absolute path with default https ref #37<commit_after>require 'roo'
#
# == Schema Information
#
# Table name: images
#
#  id                 :integer          not null, primary key
#  path               :string(255)
#  website_id         :integer
#  group_id           :integer
#  created_at         :datetime
#  updated_at         :datetime
#  canonical_id       :string(255)
#  assignments_count  :integer          default(0)
#  descriptions_count :integer          default(0)
#  title              :text(65535)
#  priority           :boolean          default(FALSE)
#  status_code        :integer          default(0)
#  page_urls          :text(65535)
#
# Indexes
#
#  index_images_on_group_id    (group_id)
#  index_images_on_website_id  (website_id)
#

class Image < ActiveRecord::Base
  serialize :page_urls, JSON

  acts_as_taggable_on :tags
  before_validation :update_status_code

  belongs_to :website, touch: true
  belongs_to :group, touch: true

  has_many :descriptions, dependent: :destroy
  has_associated_audits
  has_many :assignments, dependent: :destroy
  has_many :users, through: :assignments

  validates :path, :presence => true, :uniqueness => {:scope => :website_id}
  validates :canonical_id, :presence => true, :uniqueness => {:scope => :website_id}
  validates_associated :website, :group
  validates_presence_of :website, :group

  default_scope {order('priority DESC, created_at DESC')}

  scope :unassigned, -> (n = 0) { select { |i| i.assignments_count == n } }
  scope :undescribed, -> (n = 0) { select { |i| i.descriptions_count == n } }
  scope :assigned_undescribed, -> (n = 0) { select { |i| i.descriptions_count == n && i.assignments_count > n} }
  scope :unassigned_undescribed, -> (n = 0) { select { |i| i.descriptions_count == n && i.assignments_count == n} }
  scope :described, -> (n = 0) { select { |i| i.descriptions_count > n } }
  scope :assigned, -> (n = 0) { select { |i| i.assignments_count > n } }
  scope :prioritized, -> { order('priority DESC')}

  paginates_per 50

  def to_s
    path
  end

  def alt(status_ids=[2])
    d = descriptions.where(metum_id: 1, status_id: status_ids, locale: "en").first
    if d
      d.text
    else
      ""
    end
  end

  def long(status_ids=[2])
    d = descriptions.where(metum_id: 3, status_id: status_ids, locale: "en").first
    if d
      d.text 
    else
      ""
    end
  end

  def is_mca?
    if website.url.include?("mcachicago")
      true
    else 
      false
    end
  end


  def url(protocol="https")
    if path.starts_with?("//")
      protocol + path
    elsif website
      website.url + path
    else 
      path
    end
  end

  #TODO per locale
  #has no descriptions by this user 
  def undescribed_by?(user)
    !descriptions.collect{ |d| d.user_id}.compact.include?(user.id)
  end

  #has 1 of description by this user
  def described_by?(user)
   descriptions.collect{ |d| d.user_id}.compact.include?(user.id)
  end

  #TODO consider locale
  #has 1 of each metum by this user  ? in any combo of locales and status'es
  def completed_by?(user)
    meta_ids = Metum.all.map{|m| m.id}
    described_meta_ids = descriptions.collect{ |d| d.metum_id if d.user_id == user.id}.compact 
    (meta_ids - described_meta_ids).empty?
  end

  def status
    case status_code
    when 0
      "Not Described"
    when 1
      "Partially Completed"
    when 2
{'fraction_non_alphanumeric': 0.11342697477722398, 'fraction_numerical': 0.011176559432109953, 'mean_word_length': 2.814516129032258, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2927566', 'n_tokens_mistral': 2370, 'n_tokens_neox': 2257, 'n_words': 585}