<commit_before>require 'faker'

module Forgeable
  def forge(association, attributes={}, foreign_key=nil)
    foreign_key ||= self.class.name.downcase + '_id'
    attributes.merge!(foreign_key => self.id)
    eval(association.to_s.classify).forge(attributes)
  end
  
  def forge_photo(photo=true)
    if photo.is_a?(String)
      self.photo = File.open(photo)
    else
      self.photo = File.open(RAILS_ROOT + '/test/fixtures/files/image.jpg')
    end
  end
  
  def forge_file(file=true)
    if file.is_a?(String)
      self.file = File.open(file)
    else
      self.file = ActionController::TestUploadedFile.new(File.dirname(__FILE__) + '/fixtures/files/attachment.pdf', nil, false)
    end
  end
  
  def self.included(mod)
    mod.extend(ClassMethods)
    mod.class_eval <<-END
      @@forgery_defaults = {}
      
      def self.forgery_defaults=(defaults)
        class_eval do
          @@forgery_defaults = defaults
        end
      end

      def self.forgery_defaults
        defaults = {}
        @@forgery_defaults.each do |key, val|
          if val.respond_to? :call
            defaults[key] = val.call
          elsif val.is_a? Symbol
            defaults[key] = self.fake(val)
          else
            defaults[key] = val
          end
        end
        defaults
      end
    END
  end
  
  module ClassMethods
    
    def forge(attributes={})
      attributes.symbolize_keys!
      attributes = forgery_defaults.merge(attributes)
      photo = attributes.delete(:photo)
      file = attributes.delete(:file)
      obj = new(attributes)
      unless obj.save
        unless obj.errors.all? { |a, e| e =~ /(another|already).+with this/ }
          raise ActiveRecord::RecordInvalid, obj
        end
        obj.errors.each do |a, e|
          obj.attributes[a.to_s] << rand(1000).to_s
        end
        obj.save!
      end
      obj.forge_photo(photo) if photo
      obj.forge_file(file)   if file
      obj<commit_msg>Set attributes individually in forgeries to avoid attr_accessible lockout.<commit_after>require 'faker'

module Forgeable
  def forge(association, attributes={}, foreign_key=nil)
    foreign_key ||= self.class.name.downcase + '_id'
    attributes.merge!(foreign_key => self.id)
    eval(association.to_s.classify).forge(attributes)
  end
  
  def forge_photo(photo=true)
    if photo.is_a?(String)
      self.photo = File.open(photo)
    else
      self.photo = File.open(RAILS_ROOT + '/test/fixtures/files/image.jpg')
    end
  end
  
  def forge_file(file=true)
    if file.is_a?(String)
      self.file = File.open(file)
    else
      self.file = ActionController::TestUploadedFile.new(File.dirname(__FILE__) + '/fixtures/files/attachment.pdf', nil, false)
    end
  end
  
  def self.included(mod)
    mod.extend(ClassMethods)
    mod.class_eval <<-END
      @@forgery_defaults = {}
      
      def self.forgery_defaults=(defaults)
        class_eval do
          @@forgery_defaults = defaults
        end
      end

      def self.forgery_defaults
        defaults = {}
        @@forgery_defaults.each do |key, val|
          if val.respond_to? :call
            defaults[key] = val.call
          elsif val.is_a? Symbol
            defaults[key] = self.fake(val)
          else
            defaults[key] = val
          end
        end
        defaults
      end
    END
  end
  
  module ClassMethods
    
    def forge(attributes={})
      attributes.symbolize_keys!
      attributes = forgery_defaults.merge(attributes)
      photo = attributes.delete(:photo)
      file = attributes.delete(:file)
      obj = new
      attributes.each do |attribute, value|
        obj.send("#{attribute}=", value)
      end
      unless obj.save
        unless obj.errors.all? { |a, e| e =~ /(another|already).+with this/ }
          raise ActiveRecord::RecordInvalid, obj
        end
        obj.errors.each do |a, e|
          obj.attributes[a.to_s] << rand(1000).to_s
        end
        obj.save!
      end
      obj.forge_photo(photo) if photo
      obj.forge_file(file)   if file
      obj
{'fraction_non_alphanumeric': 0.1067360079247152, 'fraction_numerical': 0.001981178801386825, 'mean_word_length': 2.208101667990469, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, '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': '1155238', 'n_tokens_mistral': 1294, 'n_tokens_neox': 1268, 'n_words': 260}