<commit_before>=begin
  Camaleon CMS is a content management system
  Copyright (C) 2015 by <NAME>
  Email: <EMAIL>
  This program is free software: you can redistribute it and/or modify   it under the terms of the GNU Affero General Public License as  published by the Free Software Foundation, either version 3 of the  License, or (at your option) any later version.
  This program is distributed in the hope that it will be useful,  but WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the  GNU Affero General Public License (GPLv3) for more details.
=end
class CamaleonCms::PostDefault < ActiveRecord::Base
  include CamaleonCms::Metas
  include CamaleonCms::CustomFieldsRead
  self.table_name = "#{PluginRoutes.static_system_info["db_prefix"]}posts"

  attr_accessible :user_id, :title, :slug, :content, :content_filtered, :status,  :visibility, :visibility_value, :post_order, :post_type_key, :taxonomy_id, :published_at, :post_parent, :post_order
  attr_accessor :draft_id
  attr_accessible :data_options
  attr_accessible :data_metas
  cattr_accessor :current_user
  cattr_accessor :current_site

  has_many :term_relationships, class_name: "CamaleonCms::TermRelationship", foreign_key: :objectid, dependent: :destroy, primary_key: :id
  has_many :children, ->{ where(post_class: "PostDefault") }, class_name: "CamaleonCms::PostDefault", foreign_key: :post_parent, dependent: :destroy, primary_key: :id

  validates :title, :slug, presence: true

  # callbacks
  before_validation :before_validating
  before_save :before_saved
  before_destroy :destroy_dependencies

  # find a content by slug (support multi language)
  def self.find_by_slug(slug)
    if self.class.current_site.present? && self.class.current_site.get_meta("languages_site", []).count <= 1
      res = self.where(slug: slug)
    else
      res = self.where("#{CamaleonCms::Post.table_name}.slug = ? OR #{CamaleonCms::Post.table_name}.slug LIKE ? ", slug, "%-->#{slug}<!--%")
    end
    res.reorder("").first
  end

  # return the parent of a post (support for sub contents or tree of posts)
  def parent
    CamaleonCms::Post.where(id: self.post_parent).first
  end

  # return the author of this Content
  def author
    begin
      CamaleonCms::User.find(self.user_id)
    rescue
      CamaleonCms::User.admin_scope.first
    end
  end

  # save meta values passed from form
  def set_meta_from_form(data_metas)
    data_metas.each do |key, value|
      self.set_meta(key, value)
    end
  end<commit_msg>fixed error for find_by_slug post find<commit_after>=begin
  Camaleon CMS is a content management system
  Copyright (C) 2015 by <NAME>
  Email: <EMAIL>
  This program is free software: you can redistribute it and/or modify   it under the terms of the GNU Affero General Public License as  published by the Free Software Foundation, either version 3 of the  License, or (at your option) any later version.
  This program is distributed in the hope that it will be useful,  but WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the  GNU Affero General Public License (GPLv3) for more details.
=end
class CamaleonCms::PostDefault < ActiveRecord::Base
  include CamaleonCms::Metas
  include CamaleonCms::CustomFieldsRead
  self.table_name = "#{PluginRoutes.static_system_info["db_prefix"]}posts"

  attr_accessible :user_id, :title, :slug, :content, :content_filtered, :status,  :visibility, :visibility_value, :post_order, :post_type_key, :taxonomy_id, :published_at, :post_parent, :post_order
  attr_accessor :draft_id
  attr_accessible :data_options
  attr_accessible :data_metas
  cattr_accessor :current_user
  cattr_accessor :current_site

  has_many :term_relationships, class_name: "CamaleonCms::TermRelationship", foreign_key: :objectid, dependent: :destroy, primary_key: :id
  has_many :children, ->{ where(post_class: "PostDefault") }, class_name: "CamaleonCms::PostDefault", foreign_key: :post_parent, dependent: :destroy, primary_key: :id

  validates :title, :slug, presence: true

  # callbacks
  before_validation :before_validating
  before_save :before_saved
  before_destroy :destroy_dependencies

  # find a content by slug (support multi language)
  def self.find_by_slug(slug)
    if current_site.present? && current_site.get_meta("languages_site", []).count <= 1
      res = self.where(slug: slug)
    else
      res = self.where("#{CamaleonCms::Post.table_name}.slug = ? OR #{CamaleonCms::Post.table_name}.slug LIKE ? ", slug, "%-->#{slug}<!--%")
    end
    res.reorder("").first
  end

  # return the parent of a post (support for sub contents or tree of posts)
  def parent
    CamaleonCms::Post.where(id: self.post_parent).first
  end

  # return the author of this Content
  def author
    begin
      CamaleonCms::User.find(self.user_id)
    rescue
      CamaleonCms::User.admin_scope.first
    end
  end

  # save meta values passed from form
  def set_meta_from_form(data_metas)
    data_metas.each do |key, value|
      self.set_meta(key, value)
    end
  end
{'fraction_non_alphanumeric': 0.0919225503618228, 'fraction_numerical': 0.0027381185214159984, 'mean_word_length': 4.669623059866963, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, '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': '2219342', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1604, 'n_words': 492}