<commit_before>module ShopProductSink
  module ApiCreatable
    extend ActiveSupport::Concern

    module ClassMethods
      def initialize_from_resource(resource)
        attributes = usable_keys.reduce({}) do |result, key|
          result[key] = resource.public_send(key) if resource.respond_to?(key)
          result
        end
        record = self.new(attributes)
        record.extract_relations!(resource)
        record
      end

      def initialize_from_resources(resources)
        Array[*resources].flatten.map { |resource| initialize_from_resource(resource) }
      end

      def create_from_resource(resource)
        object = initialize_from_resource(resource)
        object.save
        object
      end

      def create_from_resources(resources)
        Array[*resources].flatten.map { |resource| create_from_resource(resource) }
      end

      def usable_keys
        columns.map(&:name)
      end
    end

    def extract_relations!(resource)
      self.class.reflect_on_all_associations.each do |association|
        if resource.respond_to?(association.name)
          resource_or_resources = resource.public_send(association.name)
          records = association.klass.initialize_from_resources(resource_or_resources)
          public_send("#{association.name}=", association.collection? ? records : records.first)
        end
      end
    end
  end
end<commit_msg>workaround for variants association name in extract_relations<commit_after>module ShopProductSink
  module ApiCreatable
    extend ActiveSupport::Concern

    module ClassMethods
      def initialize_from_resource(resource)
        attributes = usable_keys.reduce({}) do |result, key|
          result[key] = resource.public_send(key) if resource.respond_to?(key)
          result
        end
        record = self.new(attributes)
        record.extract_relations!(resource)
        record
      end

      def initialize_from_resources(resources)
        Array[*resources].flatten.map { |resource| initialize_from_resource(resource) }
      end

      def create_from_resource(resource)
        object = initialize_from_resource(resource)
        object.save
        object
      end

      def create_from_resources(resources)
        Array[*resources].flatten.map { |resource| create_from_resource(resource) }
      end

      def usable_keys
        columns.map(&:name)
      end
    end

    def extract_relations!(resource)
      self.class.reflect_on_all_associations.each do |association|
      	# workaround for variants association name
      	association_name = association.name == :product_variants ? :variants : association.name

        if resource.respond_to?(association_name)
          resource_or_resources = resource.public_send(association_name)
          records = association.klass.initialize_from_resources(resource_or_resources)
          public_send("#{association.name}=", association.collection? ? records : records.first)
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.07573726541554959, 'fraction_numerical': 0.0, 'mean_word_length': 2.9431968295904887, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1552130', 'n_tokens_mistral': 805, 'n_tokens_neox': 785, 'n_words': 155}