<commit_before># Model to keep track of discussion postings

class Posting < ActiveRecord::Base
  attr_accessible :title, :body, :node_id, :is_question, :person_id, :deleted_at

  belongs_to :node
  belongs_to :person

  validates :title, :presence => true
  validates :body, :presence => true
  validates :node_id, :presence => true

  scope :questions, :conditions => { :is_question => true }, :order => "created_at DESC"
  scope :unanswered_questions, :conditions => { :is_question => true, :answered_at => nil }, :order => "created_at DESC"

  scope :created_since, lambda { |since| { :conditions => ['created_at >= ? ', since] }}

  before_validation :strip_bad_tags
  after_create :notify_people
  after_create :tweet_posting

  def was_answered?
    !!self.answered_at
  end

  def first_posting?
    node.postings.count == 1
  end

  def new_thing
    {:id => id,
      :node_id => node_id,
      :node_name => node.name,
      :date => created_at,
      :description => "Posting by '#{person.fullname}' about the '#{node.name}' model",
      :title => title,
      :contents => body}
  end

  def body_urlified
    body_urlified = body.gsub /(https?:\/\/[-\/_.~%?='\w:]+\w)/ do
      "<a target=\"_blank\" href=\"#{$1}\">#{$1}</a>"
    end

    # body_urlified = body.gsub(/(https?:\/\/[\w.\/_-]+[\w\/]+)/, "<a href='#{$1}' target='_blank'>#{$1}</a>")
    logger.warn "[Posting#body_urlified] body input is '#{body}'"
    logger.warn "[Posting#body_urlified] body output is '#{body_urlified}'"
    body_urlified
  end

  def notify_people
    Notifications.updated_discussion(node, person).deliver
  end

  def strip_bad_tags
    attributes.each do |name, value|
      %w(link script style form input textarea button img).each do |bad_word|
        if value.to_s =~ /(<\s*#{bad_word})/i
          STDERR.puts "'#{bad_word}' found in '#{name}' = '#{value}'"
          self.send("#{name}=".to_sym, attributes[name].gsub($1, "&amp;#{bad_word}"))
        end
      end
    end
  end

  def tweet_posting
    return unless node.world_visible?
    Twitter.update("#{person.fullname} added #{ActiveSupport::Inflector::ordinalize(node.postings.count)} post about #NetLogo model #{node.name}, at #{node.url}")
  end

end
<commit_msg>Improve posting URL<commit_after># Model to keep track of discussion postings

class Posting < ActiveRecord::Base
  attr_accessible :title, :body, :node_id, :is_question, :person_id, :deleted_at

  belongs_to :node
  belongs_to :person

  validates :title, :presence => true
  validates :body, :presence => true
  validates :node_id, :presence => true

  scope :questions, :conditions => { :is_question => true }, :order => "created_at DESC"
  scope :unanswered_questions, :conditions => { :is_question => true, :answered_at => nil }, :order => "created_at DESC"

  scope :created_since, lambda { |since| { :conditions => ['created_at >= ? ', since] }}

  before_validation :strip_bad_tags
  after_create :notify_people
  after_create :tweet_posting

  def was_answered?
    !!self.answered_at
  end

  def first_posting?
    node.postings.count == 1
  end

  def new_thing
    {:id => id,
      :node_id => node_id,
      :node_name => node.name,
      :date => created_at,
      :description => "Posting by '#{person.fullname}' about the '#{node.name}' model",
      :title => title,
      :contents => body}
  end

  def body_urlified
    body_urlified = body.gsub /(https?:\/\/[-\/_.~%?='\w:]+\w)/ do
      "<a target=\"_blank\" href=\"#{$1}\">#{$1}</a>"
    end

    logger.warn "[Posting#body_urlified] body input is '#{body}'"
    logger.warn "[Posting#body_urlified] body output is '#{body_urlified}'"
    body_urlified
  end

  def notify_people
    Notifications.updated_discussion(node, person).deliver
  end

  def strip_bad_tags
    attributes.each do |name, value|
      %w(link script style form input textarea button img).each do |bad_word|
        if value.to_s =~ /(<\s*#{bad_word})/i
          STDERR.puts "'#{bad_word}' found in '#{name}' = '#{value}'"
          self.send("#{name}=".to_sym, attributes[name].gsub($1, "&amp;#{bad_word}"))
        end
      end
    end
  end

  def tweet_posting
    return unless node.world_visible?
    Twitter.update("#{person.fullname} added #{ActiveSupport::Inflector::ordinalize(node.postings.count)} post about #NetLogo model #{node.name}, at #{node.url}")
  end

end

{'fraction_non_alphanumeric': 0.16028433845448292, 'fraction_numerical': 0.002293052052281587, 'mean_word_length': 3.5723270440251573, 'pattern_counts': {'":': 0, '<': 13, '<?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': '7421542', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1482, 'n_words': 357}