<commit_before>
class Article < ActiveRecord::Base

  attr_accessible :name, :body, :abstract, :profile, :tag_list, :parent,
                  :allow_members_to_edit, :translation_of_id, :language,
                  :license_id, :parent_id, :display_posts_in_current_language,
                  :category_ids, :posts_per_page, :moderate_comments,
                  :accept_comments, :feed, :published, :source, :source_name,
                  :highlighted, :notify_comments, :display_hits, :slug,
                  :external_feed_builder, :display_versions, :external_link,
                  :image_builder, :show_to_followers,
                  :author, :display_preview

  acts_as_having_image

  SEARCHABLE_FIELDS = {
    :name => {:label => _('Name'), :weight => 10},
    :abstract => {:label => _('Abstract'), :weight => 3},
    :body => {:label => _('Content'), :weight => 2},
    :slug => {:label => _('Slug'), :weight => 1},
    :filename => {:label => _('Filename'), :weight => 1},
  }

  SEARCH_FILTERS = {
    :order => %w[more_recent more_popular more_comments],
    :display => %w[full]
  }

  def initialize(*params)
    super

    if !params.blank?
      if params.first.has_key?(:profile) && !params.first[:profile].blank?
        profile = params.first[:profile]
        self.published = false unless profile.public_profile
      end

      self.published = params.first["published"] if params.first.has_key?("published")
      self.published = params.first[:published] if params.first.has_key?(:published)
    end

  end

  def self.default_search_display
    'full'
  end

  #FIXME This is necessary because html is being generated on the model...
  include ActionView::Helpers::TagHelper

  # use for internationalizable human type names in search facets
  # reimplement on subclasses
  def self.type_name
    _('Content')
  end

  track_actions :create_article, :after_create, :keep_params => [:name, :url, :lead, :first_image], :if => Proc.new { |a| a.is_trackable? && !a.image? }

  # xss_terminate plugin can't sanitize array fields
  before_save :sanitize_tag_list

  before_create do |article|
    if article.author
      article.author_name = article.author.name
    end
  end

  belongs_to :profile
  validates_presence_of :profile_id, :name
  validates_presence_of :slug, :path, :if => lambda { |article| !article.name.blank? }

  validates_length_of :name, :maximum => 150

  validates_uniqueness_of :slug, :scope => ['profile_id', 'parent_id'], :message => N_('The title (article name) is already being used by another article, please use another title.'), :if => lambda { |article| !article.slug.blank? }

  belongs_to :author, :class_name => 'Person'
  belongs_to :last_changed_by, :class_name => 'Person', :foreign_key => 'last_changed_by_id'
  belongs_to :created_by, :class_name => 'Person', :foreign_key => 'created_by_id'

  has_many :comments, :class_name => 'Comment', :foreign_key => 'source_id', :dependent => :destroy, :order => 'created_at asc'

  has_many :article_categorizations, -> { where 'articles_categories.virtual = ?', false }
  has_many :categories, :through => :article_categorizations

  has_many :article_categorizations_including_virtual, :class_name => 'ArticleCategorization'
  has_many :categories_including_virtual, :through => :article_categorizations_including_virtual, :source => :category

  acts_as_having_settings :field => :setting

  settings_items :display_hits, :type => :boolean, :default => true
  settings_items :author_name, :type => :string, :default => ""
  settings_items :allow_members_to_edit, :type => :boolean, :default => false
  settings_items :moderate_comments, :type => :boolean, :default => false
  settings_items :followers, :type => Array, :default => []
  has_and_belongs_to_many :article_privacy_exceptions, :class_name => 'Person', :join_table => 'article_privacy_exceptions'

  belongs_to :reference_article, :class_name => "Article", :foreign_key => 'reference_article_id'

  belongs_to :license

  has_many :translations, :class_name => 'Article', :foreign_key => :translation_of_id
  belongs_to :translation_of, :class_name => 'Article', :foreign_key => :translation_of_id
  before_destroy :rotate_translations

  acts_as_voteable<commit_msg>article: ensure comments fetch considers polymorphic relation<commit_after>
class Article < ActiveRecord::Base

  attr_accessible :name, :body, :abstract, :profile, :tag_list, :parent,
                  :allow_members_to_edit, :translation_of_id, :language,
                  :license_id, :parent_id, :display_posts_in_current_language,
                  :category_ids, :posts_per_page, :moderate_comments,
                  :accept_comments, :feed, :published, :source, :source_name,
                  :highlighted, :notify_comments, :display_hits, :slug,
                  :external_feed_builder, :display_versions, :external_link,
                  :image_builder, :show_to_followers,
                  :author, :display_preview

  acts_as_having_image

  SEARCHABLE_FIELDS = {
    :name => {:label => _('Name'), :weight => 10},
    :abstract => {:label => _('Abstract'), :weight => 3},
    :body => {:label => _('Content'), :weight => 2},
    :slug => {:label => _('Slug'), :weight => 1},
    :filename => {:label => _('Filename'), :weight => 1},
  }

  SEARCH_FILTERS = {
    :order => %w[more_recent more_popular more_comments],
    :display => %w[full]
  }

  def initialize(*params)
    super

    if !params.blank?
      if params.first.has_key?(:profile) && !params.first[:profile].blank?
        profile = params.first[:profile]
        self.published = false unless profile.public_profile
      end

      self.published = params.first["published"] if params.first.has_key?("published")
      self.published = params.first[:published] if params.first.has_key?(:published)
    end

  end

  def self.default_search_display
    'full'
  end

  #FIXME This is necessary because html is being generated on the model...
  include ActionView::Helpers::TagHelper

  # use for internationalizable human type names in search facets
  # reimplement on subclasses
  def self.type_name
    _('Content')
  end

  track_actions :create_article, :after_create, :keep_params => [:name, :url, :lead, :first_image], :if => Proc.new { |a| a.is_trackable? && !a.image? }

  # xss_terminate plugin can't sanitize array fields
  before_save :sanitize_tag_list

  before_create do |article|
    if article.author
      article.author_name = article.author.name
    end
  end

  belongs_to :profile
  validates_presence_of :profile_id, :name
  validates_presence_of :slug, :path, :if => lambda { |article| !article.name.blank? }

  validates_length_of :name, :maximum => 150

  validates_uniqueness_of :slug, :scope => ['profile_id', 'parent_id'], :message => N_('The title (article name) is already being used by another article, please use another title.'), :if => lambda { |article| !article.slug.blank? }

  belongs_to :author, :class_name => 'Person'
  belongs_to :last_changed_by, :class_name => 'Person', :foreign_key => 'last_changed_by_id'
  belongs_to :created_by, :class_name => 'Person', :foreign_key => 'created_by_id'

  has_many :comments, :class_name => 'Comment', :as => 'source', :dependent => :destroy, :order => 'created_at asc'

  has_many :article_categorizations, -> { where 'articles_categories.virtual = ?', false }
  has_many :categories, :through => :article_categorizations

  has_many :article_categorizations_including_virtual, :class_name => 'ArticleCategorization'
  has_many :categories_including_virtual, :through => :article_categorizations_including_virtual, :source => :category

  acts_as_having_settings :field => :setting

  settings_items :display_hits, :type => :boolean, :default => true
  settings_items :author_name, :type => :string, :default => ""
  settings_items :allow_members_to_edit, :type => :boolean, :default => false
  settings_items :moderate_comments, :type => :boolean, :default => false
  settings_items :followers, :type => Array, :default => []
  has_and_belongs_to_many :article_privacy_exceptions, :class_name => 'Person', :join_table => 'article_privacy_exceptions'

  belongs_to :reference_article, :class_name => "Article", :foreign_key => 'reference_article_id'

  belongs_to :license

  has_many :translations, :class_name => 'Article', :foreign_key => :translation_of_id
  belongs_to :translation_of, :class_name => 'Article', :foreign_key => :translation_of_id
  before_destroy :rotate_translations

  acts_as_voteable
{'fraction_non_alphanumeric': 0.12878610002347968, 'fraction_numerical': 0.002113172106128199, 'mean_word_length': 4.347771500313873, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 117, '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': '4472488', 'n_tokens_mistral': 2740, 'n_tokens_neox': 2629, 'n_words': 617}