<commit_before>module Tabloids
  class Post
    include Mongoid::Document
    include Mongoid::Document::Taggable
    include Mongoid::Timestamps
  
    field :published,     :type => Boolean, :default => false
    
    field :url_friendly_token, :type => String
    field :content,           :type => String
    field :title,             :type => String

    field :published_at,  :type => Time
    
    scope :for_category, ->(category){ where(:category_id => category.id) }
    scope :published, where(:published => true)
    
    belongs_to :category
  
    validates_presence_of :title
    validates_presence_of :content
    
    before_save :update_published_at
    before_save :generate_url_friendly_token
    
    def self.for_token(token)
      self.where(:url_friendly_token => token).first
    end
    
    protected
    
    def generate_url_friendly_token
      if self.published && self.url_friendly_token.blank?
        self.url_friendly_token = self.title.gsub(' ', '-').gsub(/\.|!|,|:/, "")
      end
    end
    
    def update_published_at
      self.published_at = Time.now if self.published && self.published_changed?
    end
  end
end<commit_msg>Newest scope and validations<commit_after>module Tabloids
  class Post
    include Mongoid::Document
    include Mongoid::Document::Taggable
    include Mongoid::Timestamps
  
    field :published,     :type => Boolean, :default => false
    
    field :url_friendly_token, :type => String
    field :content,           :type => String
    field :title,             :type => String

    field :published_at,  :type => Time
    
    scope :for_category, ->(category){ where(:category_id => category.id) }
    scope :newest_first, desc(:published_on)
    scope :published, where(:published => true)
    
    belongs_to :category
  
    validates_presence_of :title
    validates_presence_of :content
    validates_uniqueness_of :title
    
    before_save :update_published_at
    before_save :generate_url_friendly_token
    
    def self.for_token(token)
      self.where(:url_friendly_token => token).first
    end
    
    protected
    
    def generate_url_friendly_token
      if self.published && self.url_friendly_token.blank?
        self.url_friendly_token = self.title.gsub(' ', '-').gsub(/\.|!|,|:/, "")
      end
    end
    
    def update_published_at
      self.published_at = Time.now if self.published && self.published_changed?
    end
  end
end
{'fraction_non_alphanumeric': 0.09954751131221719, 'fraction_numerical': 0.0, 'mean_word_length': 2.8059467918622847, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '5600486', 'n_tokens_mistral': 739, 'n_tokens_neox': 736, 'n_words': 161}