<commit_before>require 'simple_object'

module Migrant
  # Converts the following DSL:
  #
  # class MyModel < ActiveRecord::Base
  #   structure do
  #     my_field "some string"
  #    end
  # end
  # into a schema on that model class by calling method_missing(my_field)
  # and deciding what the best schema type is for the user's requiredments
  class Schema
    attr_accessor :indexes, :columns, :validations, :methods_to_alias

    def initialize
      @proxy = SchemaProxy.new(self)
      @columns = Hash.new
      @indexes = Array.new
      @validations = Hash.new
      @methods_to_alias = Array.new
    end

    def define_structure(&block)
      # Runs method_missing on columns given in the model "structure" DSL
      @proxy.translate_fancy_dsl(&block) if block_given?
    end

    def add_associations(associations)
      associations.each do |association|
        field = association.association_foreign_key.to_sym
        case association.macro
          when :belongs_to
            if association.options[:polymorphic]
              @columns[(association.name.to_s+'_type').to_sym] = DataType::Polymorphic.new(:field => field)
              @indexes << [(association.name.to_s+'_type').to_sym, field]
            end<commit_msg>Fixes Issue #4 - belongs_to association does not infer correct foreign key when overridden using :foreign_key<commit_after>require 'simple_object'

module Migrant
  # Converts the following DSL:
  #
  # class MyModel < ActiveRecord::Base
  #   structure do
  #     my_field "some string"
  #    end
  # end
  # into a schema on that model class by calling method_missing(my_field)
  # and deciding what the best schema type is for the user's requiredments
  class Schema
    attr_accessor :indexes, :columns, :validations, :methods_to_alias

    def initialize
      @proxy = SchemaProxy.new(self)
      @columns = Hash.new
      @indexes = Array.new
      @validations = Hash.new
      @methods_to_alias = Array.new
    end

    def define_structure(&block)
      # Runs method_missing on columns given in the model "structure" DSL
      @proxy.translate_fancy_dsl(&block) if block_given?
    end

    def add_associations(associations)
      associations.each do |association|
        # Rails 3.1 changes primary_key_name to foreign_key (correct behaviour), so this is essentially backwards compatibility for Rails 3.0
        field = (association.respond_to?(:foreign_key))? association.foreign_key.to_sym : association.primary_key_name.to_sym
        case association.macro
          when :belongs_to
            if association.options[:polymorphic]
              @columns[(association.name.to_s+'_type').to_sym] = DataType::Polymorphic.new(:field => field)
              @indexes << [(association.name.to_s+'_type').to_sym, field]
            end
{'fraction_non_alphanumeric': 0.08366106542724347, 'fraction_numerical': 0.001787629603146228, 'mean_word_length': 3.096632503660322, 'pattern_counts': {'":': 0, '<': 9, '<?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': '1962893', 'n_tokens_mistral': 840, 'n_tokens_neox': 807, 'n_words': 224}