<commit_before># -*- encoding : utf-8 -*-
# A physical commemorative plaque, which is either currently installed, or
# was once installed on a building, site or monument. Our definition of plaques is quite wide,
# encompassing 'traditional' blue plaques that commemorate a historic person's connection to a
# place, as well as plaques that commemorate buildings, events, and so on.
#
# === Attributes
# * +erected_at+ - The date on which the plaque was erected. Optional.
# * +latitude+ - location (as a decimal in WSG-84 projection). Optional.
# * +longitude+ - location (as a decimal in WSG-84 projection). Optional.
# * +created_at+
# * +updated_at+
# * +inscription+ - The text inscription on the self.
# * +reference+ - An official reference number or identifier for the self. Sometimes marked on the actual plaque itself, sometimes only in promotional material. Optional.
# * +notes+ - A general purpose notes field for internal admin and data-collection purposes.
# * +parsed_inscription+ - (not used?)
# * +photos_count+ -
# * +description+ -
# * +inscription_is_stub+ - The inscription is incomplete and needs entering.
# * +personal_connections_count+ -
# * +is_accurate_geolocation+ -
# * +is_current+ - Whether the plaque is currently on display (or has it been stolen!)
# * +inscription_in_english+ - Manual translation
# * +series_ref+ - if part of a series does it have a reference number/id?
# * +address+ - the physical street address
class Plaque < ApplicationRecord

  belongs_to :area, counter_cache: true, optional: true
  belongs_to :colour, counter_cache: true, optional: true
  belongs_to :language, counter_cache: true, optional: true
  belongs_to :series, counter_cache: true, optional: true
  has_many :personal_connections
  has_many :photos, -> { where(of_a_plaque: true).order('shot ASC')}, inverse_of: :plaque
  has_many :sponsorships
  has_many :organisations, through: :sponsorships
  has_one :pick

  attr_accessor :country, :other_colour_id, :force_us_state
  delegate :name, to: :colour, prefix: true, allow_nil: true
  delegate :name, :alpha2, to: :language, prefix: true, allow_nil: true

  before_save :use_other_colour_id
  before_save :usa_townify
  accepts_nested_attributes_for :photos, reject_if: proc { |attributes| attributes['photo_url'].blank? }
  scope :current, -> { where(is_current: true).order('id desc') }
  scope :geolocated, ->  { where(["plaques.latitude IS NOT NULL"]) }
  scope :ungeolocated, -> { where(latitude: nil).order('id DESC') }
  scope :photographed, -> { where("photos_count > 0") }
  scope :unphotographed, -> { where(photos_count: 0, is_current: true).order("id DESC") }
  scope :coloured, -> { where("colour_id IS NOT NULL") }
  scope :uncoloured, -> { where(colour_id: nil) }
  scope :photographed_not_coloured, -> { where(["photos_count > 0 AND colour_id IS NULL"]) }
  scope :geo_no_location, -> { where(["latitude IS NOT NULL AND address IS NULL"]) }
  scope :detailed_address_no_geo, -> { where(latitude: nil).where("address is not null") }  # TODO fix this
  scope :unconnected, -> { where(personal_connections_count: 0).order("id DESC") }
  scope :connected, -> { where("personal_connections_count > 0").order("id DESC") }
  scope :no_description, -> { where("description = '' OR description IS NULL") }
  scope :partial_inscription, -> { where(inscription_is_stub: true).order("id DESC") }
  scope :partial_inscription_photo, -> { where(photos_count: 1..99999, inscription_is_stub: true).order("id DESC") }
  scope :no_english_version, -> { where("language_id > 1").where(inscription_is_stub: false, inscription_in_english: nil) }
  scope :random, -> { order('random()') }

  include ApplicationHelper, ActionView::Helpers::TextHelper

  def coordinates
    geolocated? ? "#{latitude},#{longitude}" : ""
  end

  def full_address
    a = address ||+ ''
    puts "#{area.name}"
    a += ", " + area&.name if area
    a += ", " + area.country&.name if area
    a
  end

  def erected_at_string
    if erected_at?
      if erected_at.month == 1 && erected_at.day == 1
        erected_at.year.to_s
      else
        erected_at.to_s
      end
    else
      nil
    end
  end

  def erected_at_string=(date)
    if date.length == 4
      self.erected_at = Date.parse(date + "-01-01")
    else
      self.erected_at = date
    end
  end

  def geolocated?<commit_msg>left a print statement<commit_after># -*- encoding : utf-8 -*-
# A physical commemorative plaque, which is either currently installed, or
# was once installed on a building, site or monument. Our definition of plaques is quite wide,
# encompassing 'traditional' blue plaques that commemorate a historic person's connection to a
# place, as well as plaques that commemorate buildings, events, and so on.
#
# === Attributes
# * +erected_at+ - The date on which the plaque was erected. Optional.
# * +latitude+ - location (as a decimal in WSG-84 projection). Optional.
# * +longitude+ - location (as a decimal in WSG-84 projection). Optional.
# * +created_at+
# * +updated_at+
# * +inscription+ - The text inscription on the self.
# * +reference+ - An official reference number or identifier for the self. Sometimes marked on the actual plaque itself, sometimes only in promotional material. Optional.
# * +notes+ - A general purpose notes field for internal admin and data-collection purposes.
# * +parsed_inscription+ - (not used?)
# * +photos_count+ -
# * +description+ -
# * +inscription_is_stub+ - The inscription is incomplete and needs entering.
# * +personal_connections_count+ -
# * +is_accurate_geolocation+ -
# * +is_current+ - Whether the plaque is currently on display (or has it been stolen!)
# * +inscription_in_english+ - Manual translation
# * +series_ref+ - if part of a series does it have a reference number/id?
# * +address+ - the physical street address
class Plaque < ApplicationRecord

  belongs_to :area, counter_cache: true, optional: true
  belongs_to :colour, counter_cache: true, optional: true
  belongs_to :language, counter_cache: true, optional: true
  belongs_to :series, counter_cache: true, optional: true
  has_many :personal_connections
  has_many :photos, -> { where(of_a_plaque: true).order('shot ASC')}, inverse_of: :plaque
  has_many :sponsorships
  has_many :organisations, through: :sponsorships
  has_one :pick

  attr_accessor :country, :other_colour_id, :force_us_state
  delegate :name, to: :colour, prefix: true, allow_nil: true
  delegate :name, :alpha2, to: :language, prefix: true, allow_nil: true

  before_save :use_other_colour_id
  before_save :usa_townify
  accepts_nested_attributes_for :photos, reject_if: proc { |attributes| attributes['photo_url'].blank? }
  scope :current, -> { where(is_current: true).order('id desc') }
  scope :geolocated, ->  { where(["plaques.latitude IS NOT NULL"]) }
  scope :ungeolocated, -> { where(latitude: nil).order('id DESC') }
  scope :photographed, -> { where("photos_count > 0") }
  scope :unphotographed, -> { where(photos_count: 0, is_current: true).order("id DESC") }
  scope :coloured, -> { where("colour_id IS NOT NULL") }
  scope :uncoloured, -> { where(colour_id: nil) }
  scope :photographed_not_coloured, -> { where(["photos_count > 0 AND colour_id IS NULL"]) }
  scope :geo_no_location, -> { where(["latitude IS NOT NULL AND address IS NULL"]) }
  scope :detailed_address_no_geo, -> { where(latitude: nil).where("address is not null") }  # TODO fix this
  scope :unconnected, -> { where(personal_connections_count: 0).order("id DESC") }
  scope :connected, -> { where("personal_connections_count > 0").order("id DESC") }
  scope :no_description, -> { where("description = '' OR description IS NULL") }
  scope :partial_inscription, -> { where(inscription_is_stub: true).order("id DESC") }
  scope :partial_inscription_photo, -> { where(photos_count: 1..99999, inscription_is_stub: true).order("id DESC") }
  scope :no_english_version, -> { where("language_id > 1").where(inscription_is_stub: false, inscription_in_english: nil) }
  scope :random, -> { order('random()') }

  include ApplicationHelper, ActionView::Helpers::TextHelper

  def coordinates
    geolocated? ? "#{latitude},#{longitude}" : ""
  end

  def full_address
    a = address ||+ ''
    a += ", " + area&.name if area
    a += ", " + area.country&.name if area
    a
  end

  def erected_at_string
    if erected_at?
      if erected_at.month == 1 && erected_at.day == 1
        erected_at.year.to_s
      else
        erected_at.to_s
      end
    else
      nil
    end
  end

  def erected_at_string=(date)
    if date.length == 4
      self.erected_at = Date.parse(date + "-01-01")
    else
      self.erected_at = date
    end
  end

  def geolocated?
{'fraction_non_alphanumeric': 0.12432245415753662, 'fraction_numerical': 0.0057663475954330525, 'mean_word_length': 4.202159568086382, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, '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': '726678', 'n_tokens_mistral': 2849, 'n_tokens_neox': 2703, 'n_words': 818}