<commit_before>class Comfy::Cms::Block < ActiveRecord::Base
  self.table_name = 'comfy_cms_blocks'
  
  # -- Relationships --------------------------------------------------------
  belongs_to :blockable,
    :polymorphic  => true
  has_many :files,
    :autosave     => true,
    :dependent    => :destroy
  
  # -- Callbacks ------------------------------------------------------------
  before_save :prepare_files
  
  # -- Validations ----------------------------------------------------------
  validates :identifier,
    :presence   => true,
    :uniqueness => { :scope => [:blockable_type, :blockable_id] }
    
  # -- Instance Methods -----------------------------------------------------
  # Tag object that is using this block
  def tag
    @tag ||= blockable.tags(:reload).detect{|t| t.is_cms_block? && t.identifier == identifier}
  end
  
protected
  
  def prepare_files
    temp_files = [self.content].flatten.select do |f|
      %w(ActionDispatch::Http::UploadedFile Rack::Test::UploadedFile File).member?(f.class.name)
    end
    
    return if temp_files.blank?
    
    # only accepting one file if it's PageFile. PageFiles will take all
    single_file = self.tag.instance_of?(ComfortableMexicanSofa::Tag::PageFile)
    temp_files = [temp_files.first].compact if single_file
    
    temp_files.each do |file|
      self.files.collect{|f| f.mark_for_destruction } if single_file
      self.files.build(:site => self.blockable.site, :dimensions => self.tag.try(:dimensions), :file => file)
    end
    
    self.content = nil unless self.content.is_a?(String)
  end
end<commit_msg>Block can render itself<commit_after>class Comfy::Cms::Block < ActiveRecord::Base
  self.table_name = 'comfy_cms_blocks'
  
  # -- Relationships --------------------------------------------------------
  belongs_to :blockable,
    :polymorphic  => true
  has_many :files,
    :autosave     => true,
    :dependent    => :destroy
  
  # -- Callbacks ------------------------------------------------------------
  before_save :prepare_files
  
  # -- Validations ----------------------------------------------------------
  validates :identifier,
    :presence   => true,
    :uniqueness => { :scope => [:blockable_type, :blockable_id] }
    
  # -- Instance Methods -----------------------------------------------------
  # Tag object that is using this block
  def tag
    @tag ||= blockable.tags(:reload).detect{|t| t.is_cms_block? && t.identifier == identifier}
  end
  

  def render
    tag.try(:render)
  end

protected
  
  def prepare_files
    temp_files = [self.content].flatten.select do |f|
      %w(ActionDispatch::Http::UploadedFile Rack::Test::UploadedFile File).member?(f.class.name)
    end
    
    return if temp_files.blank?
    
    # only accepting one file if it's PageFile. PageFiles will take all
    single_file = self.tag.instance_of?(ComfortableMexicanSofa::Tag::PageFile)
    temp_files = [temp_files.first].compact if single_file
    
    temp_files.each do |file|
      self.files.collect{|f| f.mark_for_destruction } if single_file
      self.files.build(:site => self.blockable.site, :dimensions => self.tag.try(:dimensions), :file => file)
    end
    
    self.content = nil unless self.content.is_a?(String)
  end
end
{'fraction_non_alphanumeric': 0.24292742927429276, 'fraction_numerical': 0.0, 'mean_word_length': 1.9018733273862622, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '1657734', 'n_tokens_mistral': 957, 'n_tokens_neox': 945, 'n_words': 198}