<commit_before># == Schema Information
#
# Table name: planning_applications
#
#  id                      :integer          not null, primary key
#  address                 :text
#  postcode                :string(255)
#  description             :text
#  openlylocal_council_url :string(255)
#  url                     :text
#  uid                     :string(255)      not null
#  issue_id                :integer
#  created_at              :datetime         not null
#  updated_at              :datetime         not null
#  location                :spatial          geometry, 4326
#  authority_name          :string(255)
#  start_date              :date
#  hide_votes_count        :integer          default(0)
#
# Indexes
#
#  index_planning_applications_on_issue_id  (issue_id)
#  index_planning_applications_on_uid       (uid) UNIQUE
#

class PlanningApplication < ActiveRecord::Base
  NOS_HIDE_VOTES = 2

  include Locatable

  belongs_to :issue
  has_many :hide_votes
  has_many :users, through: :hide_votes
  scope :not_hidden, -> { where('hide_votes_count < ?', NOS_HIDE_VOTES) }
  scope :ordered, -> { order('start_date DESC') }
  scope :relevant, -> { where(relevant: true) }

  validates :uid, :url, presence: true
  validates :uid, uniqueness: true
  before_save :set_relevant

  class << self
    def remove_old
      where('created_at < ?', 8.months.ago).where(issue_id: nil).delete_all
    end
  end

  def has_issue?
    issue_id
  end

  def title
    if description.try(:present?)
      [uid, description].join(" ")
    else
      [uid, authority_name].join(" ")
    end
  end

  def part_hidden?
    hide_votes_count > 0 && hide_votes_count < NOS_HIDE_VOTES
  end

  def fully_hidden?
    hide_votes_count >= NOS_HIDE_VOTES
  end

  def populate_issue
    build_issue.tap do |issue|
      issue.title = "#{I18n.t("planning_application.issues.new.title_prefix")} : #{title}"
      issue.location = location
      issue.external_url = url
      issue.description = <<-EOS
        #{description}\n\n
        #{address}\n\n
        #{url}\n\n#{authority_name}\n
        #{I18n.t("planning_application.issues.new.application_reference")} : #{uid}
      EOS
      issue.tags_string = "planning"
    end
  end

  protected

  def set_relevant
    self.relevant = relevant?
  end

  def relevant?
    return true unless authority_name == 'Cambridge'
    case uid
    when /\/(TTCA|COND.*|CLUED|ADV)$/
      false
    else
      true
    end
  end
end<commit_msg>Fix planning app before save<commit_after># == Schema Information
#
# Table name: planning_applications
#
#  id                      :integer          not null, primary key
#  address                 :text
#  postcode                :string(255)
#  description             :text
#  openlylocal_council_url :string(255)
#  url                     :text
#  uid                     :string(255)      not null
#  issue_id                :integer
#  created_at              :datetime         not null
#  updated_at              :datetime         not null
#  location                :spatial          geometry, 4326
#  authority_name          :string(255)
#  start_date              :date
#  hide_votes_count        :integer          default(0)
#
# Indexes
#
#  index_planning_applications_on_issue_id  (issue_id)
#  index_planning_applications_on_uid       (uid) UNIQUE
#

class PlanningApplication < ActiveRecord::Base
  NOS_HIDE_VOTES = 2

  include Locatable

  belongs_to :issue
  has_many :hide_votes
  has_many :users, through: :hide_votes
  scope :not_hidden, -> { where('hide_votes_count < ?', NOS_HIDE_VOTES) }
  scope :ordered, -> { order('start_date DESC') }
  scope :relevant, -> { where(relevant: true) }

  validates :uid, :url, presence: true
  validates :uid, uniqueness: true
  before_save :set_relevant

  class << self
    def remove_old
      where('created_at < ?', 8.months.ago).where(issue_id: nil).delete_all
    end
  end

  def has_issue?
    issue_id
  end

  def title
    if description.try(:present?)
      [uid, description].join(" ")
    else
      [uid, authority_name].join(" ")
    end
  end

  def part_hidden?
    hide_votes_count > 0 && hide_votes_count < NOS_HIDE_VOTES
  end

  def fully_hidden?
    hide_votes_count >= NOS_HIDE_VOTES
  end

  def populate_issue
    build_issue.tap do |issue|
      issue.title = "#{I18n.t("planning_application.issues.new.title_prefix")} : #{title}"
      issue.location = location
      issue.external_url = url
      issue.description = <<-EOS
        #{description}\n\n
        #{address}\n\n
        #{url}\n\n#{authority_name}\n
        #{I18n.t("planning_application.issues.new.application_reference")} : #{uid}
      EOS
      issue.tags_string = "planning"
    end
  end

  protected

  def set_relevant
    self.relevant = relevant?
    true
  end

  def relevant?
    return true unless authority_name == 'Cambridge'
    case uid
    when /\/(TTCA|COND.*|CLUED|ADV)$/
      false
    else
      true
    end
  end
end
{'fraction_non_alphanumeric': 0.09899396378269618, 'fraction_numerical': 0.0096579476861167, 'mean_word_length': 2.2618110236220472, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 13, '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': '2956866', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1603, 'n_words': 352}