<commit_before>class Element < ActiveRecord::Base
  
  # All Elements inside a cell are a list. All Elements not in cell are in the cell_id.nil list.
  acts_as_list :scope => [:page_id, :cell_id]
  stampable :stamper_class_name => :user
  
  has_many :contents, :order => :position, :dependent => :destroy
  belongs_to :cell
  belongs_to :page
  has_and_belongs_to_many :to_be_sweeped_pages, :class_name => 'Page', :uniq => true
  
  validates_uniqueness_of :position, :scope => [:page_id, :cell_id]
  validates_presence_of :name, :on => :create, :message => N_("Please choose an element.")
  
  attr_accessor :create_contents_after_create
  after_create :create_contents, :unless => Proc.new { |m| m.create_contents_after_create == false }
  
  # TODO: add a trashed column to elements table
  scope :trashed, where(:page_id => nil).order('updated_at DESC')
  
  # Returns next Element on self.page or nil. Pass a Element.name to get next of this kind.
  def next(name = nil)
    if name.nil?
      find_conditions = ["public = 1 AND page_id = ? AND position > ?", self.page.id, self.position]
    else
      find_conditions = ["public = 1 AND page_id = ? AND name = ? AND position > ?", self.page.id, name, self.position]
    end
    self.class.where(find_conditions).order("position ASC").limit(1)
  end
  
  # Returns previous Element on self.page or nil. Pass a Element.name to get previous of this kind.
  def prev(name = nil)
    if name.nil?
      find_conditions = ["public = 1 AND page_id = ? AND position < ?", self.page.id, self.position]
    else
      find_conditions = ["public = 1 AND page_id = ? AND name = ? AND position < ?", self.page.id, name, self.position]
    end
    self.class.where(find_conditions).order("position DESC").limit(1)
  end

  def store_page page
    unless self.to_be_sweeped_pages.include? page
      self.to_be_sweeped_pages << page
      self.save
    end
  end
  
  # nullifies the page_id aka. trashs it.
  def trash
    self.page_id = nil
    self.folded = true
    self.save(false)
  end
  
  def trashed?
    page_id.nil?
  end
  
  def content_by_name(name)
    self.contents.find_by_name(name)
  end

  def content_by_type(essence_type)
    self.contents.find_by_essence_type(essence_type)
  end

  def all_contents_by_name(name)
    self.contents.find_all_by_name(name)
  end

  def all_contents_by_type(essence_type)
    self.contents.find_all_by_essence_type(essence_type)
  end
<commit_msg>Return true if element#store_page gets nil object<commit_after>class Element < ActiveRecord::Base
  
  # All Elements inside a cell are a list. All Elements not in cell are in the cell_id.nil list.
  acts_as_list :scope => [:page_id, :cell_id]
  stampable :stamper_class_name => :user
  
  has_many :contents, :order => :position, :dependent => :destroy
  belongs_to :cell
  belongs_to :page
  has_and_belongs_to_many :to_be_sweeped_pages, :class_name => 'Page', :uniq => true
  
  validates_uniqueness_of :position, :scope => [:page_id, :cell_id]
  validates_presence_of :name, :on => :create, :message => N_("Please choose an element.")
  
  attr_accessor :create_contents_after_create
  after_create :create_contents, :unless => Proc.new { |m| m.create_contents_after_create == false }
  
  # TODO: add a trashed column to elements table
  scope :trashed, where(:page_id => nil).order('updated_at DESC')
  
  # Returns next Element on self.page or nil. Pass a Element.name to get next of this kind.
  def next(name = nil)
    if name.nil?
      find_conditions = ["public = 1 AND page_id = ? AND position > ?", self.page.id, self.position]
    else
      find_conditions = ["public = 1 AND page_id = ? AND name = ? AND position > ?", self.page.id, name, self.position]
    end
    self.class.where(find_conditions).order("position ASC").limit(1)
  end
  
  # Returns previous Element on self.page or nil. Pass a Element.name to get previous of this kind.
  def prev(name = nil)
    if name.nil?
      find_conditions = ["public = 1 AND page_id = ? AND position < ?", self.page.id, self.position]
    else
      find_conditions = ["public = 1 AND page_id = ? AND name = ? AND position < ?", self.page.id, name, self.position]
    end
    self.class.where(find_conditions).order("position DESC").limit(1)
  end

  # Stores the page into `to_be_sweeped_pages` (Pages that have to be sweeped after updating element).
  def store_page(page)
    return true if page.nil?
    unless self.to_be_sweeped_pages.include? page
      self.to_be_sweeped_pages << page
      self.save
    end
  end

  # nullifies the page_id aka. trashs it.
  def trash
    self.page_id = nil
    self.folded = true
    self.save(false)
  end
  
  def trashed?
    page_id.nil?
  end
  
  def content_by_name(name)
    self.contents.find_by_name(name)
  end

  def content_by_type(essence_type)
    self.contents.find_by_essence_type(essence_type)
  end

  def all_contents_by_name(name)
    self.contents.find_all_by_name(name)
  end

  def all_contents_by_type(essence_type)
    self.contents.find_all_by_essence_type(essence_type)
  end

{'fraction_non_alphanumeric': 0.10223452639905083, 'fraction_numerical': 0.002372948388372553, 'mean_word_length': 3.57737556561086, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 29, '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': '7619871', 'n_tokens_mistral': 1763, 'n_tokens_neox': 1731, 'n_words': 461}