<commit_before>#
# Blogaze
# Copyright (C) 2011-2013 <NAME>
#
# Blogaze is released under the BSD 3-clause license.
# @license http://opensource.org/licenses/BSD-3-Clause
#

module Blogaze
  module Models
    class Post < Sequel::Model
      attr_accessor :post_tags
      plugin :validation_helpers

      many_to_one :user
      one_to_many :comments do |cmt|
        cmt.filter{in_moderation < 1}
      end

      def href
        return '/' + Time.at(self.published_at).year.to_s + '/' + Time.at(self.published_at).month.to_s + '/' + self.slug
      end

      def tags
        tags = []
        relationships = ::Blogaze::Models::TagsRelationship.where(:object_id => self.id, :object_type => 'post')
        relationships.each do |rel|
          tags.push rel.tag
        end

        return tags
      end

      def body_partial
        body = self.body.split('<!-- MORE -->')
        return body[0]
      end

      def validate
        validates_presence [:title, :body, :user_id]
        validates_integer :user_id
      end

      def before_create
        super
        self.slug = Innate::Helper::CGI.u(self.title.scan(/\w+/).join('-')).downcase
        self.created_at = Time.now.to_i
        self.published_at = Time.now.to_i
        self.updated_at = 0
      end

      def before_save
        self.slug = Innate::Helper::CGI.u(self.title.scan(/\w+/).join('-')).downcase
        self.updated_at = Time.now.to_i
      end

      def after_save
        process_tags
      end

      private

      def process_tags
        tag_names = []
        post_tags.each do |tag|
          t = Tag.find_or_create(:name => tag.strip)
          tag_names.push t.name
          if t and !::Blogaze::Models::TagsRelationship.where(:object_id => id, :object_type => 'post', :tag_id => t.id).first
            ::Blogaze::Models::TagsRelationship.new({ :object_id => id, :object_type => 'post', :tag_id => t.id }).save
          end
        end

        tags.each do |tag|<commit_msg>Added some code comments to the posts model.<commit_after>#
# Blogaze
# Copyright (C) 2011-2013 <NAME>
#
# Blogaze is released under the BSD 3-clause license.
# @license http://opensource.org/licenses/BSD-3-Clause
#

module Blogaze
  module Models
    class Post < Sequel::Model
      attr_accessor :post_tags
      plugin :validation_helpers

      many_to_one :user
      one_to_many :comments do |cmt|
        cmt.filter{in_moderation < 1}
      end

      ##
      # Returns the URI to the post.
      #
      def href
        return '/' + Time.at(self.published_at).year.to_s + '/' + Time.at(self.published_at).month.to_s + '/' + self.slug
      end

      ##
      # Returns an array of the posts tags.
      #
      def tags
        tags = []
        relationships = ::Blogaze::Models::TagsRelationship.where(:object_id => self.id, :object_type => 'post')
        relationships.each do |rel|
          tags.push rel.tag
        end

        return tags
      end

      ##
      # Returns the partial body of the post.
      #
      def body_partial
        body = self.body.split('<!-- MORE -->')
        return body[0]
      end

      def validate
        validates_presence [:title, :body, :user_id]
        validates_integer :user_id
      end

      def before_create
        super
        self.slug = Innate::Helper::CGI.u(self.title.scan(/\w+/).join('-')).downcase
        self.created_at = Time.now.to_i
        self.published_at = Time.now.to_i
        self.updated_at = 0
      end

      def before_save
        self.slug = Innate::Helper::CGI.u(self.title.scan(/\w+/).join('-')).downcase
        self.updated_at = Time.now.to_i
      end

      def after_save
        process_tags
      end

      private

      def process_tags
        # Names of tags, obviously
        tag_names = []

        # Loop over new tags and create them and the relationships, if needed
        post_tags.each do |tag|
          t = Tag.find_or_create(:name => tag.strip)
          tag_names.push t.name
          if t and !::Blogaze::Models::TagsRelationship.where(:object_id => id, :object_type => 'post', :tag_id => t.id).first
            ::Blogaze::Models::TagsRelationship.new({ :object_id => id, :object_type => 'post', :tag_id => t.id }).save
          end
        end

        # Loop over all tags and delete relationships
        # for removed tags.
        tags.each do |tag|
{'fraction_non_alphanumeric': 0.11976185024043966, 'fraction_numerical': 0.005953743989008472, 'mean_word_length': 2.0396659707724427, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2197447', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1373, 'n_words': 320}