<commit_before>require 'active_support'

module AirBlade
  module ActsAsParentOf

    def self.included(base)
      base.extend ActMethods
    end

    module ActMethods
      # Each child in the array should be the plural form,
      # i.e. matching its has_many declaration.
      def acts_as_parent_of(*children)
        unless included_modules.include? InstanceMethods
          extend ClassMethods
          include InstanceMethods

          children.each do |child|
            self.send :after_update, "save_#{child}"
            self.send :validates_associated, "#{child}"
          end

          children.each do |child|
            singular_child = ActiveSupport::Inflector.singularize child
            code = <<-END
              def new_#{singular_child}_attributes=(#{singular_child}_attributes)
                #{singular_child}_attributes.each do |attributes|
                  #{child}.build attributes
                end
              end

              def existing_#{singular_child}_attributes=(#{singular_child}_attributes)
                #{child}.reject(&:new_record?).each do |#{singular_child}|
                  attributes = #{singular_child}_attributes[#{singular_child}.id.to_s]
                  if attributes
                    #{singular_child}.attributes = attributes
                  else
                    #{child}.delete #{singular_child}
                  end
                end
              end

              def save_#{child}
                #{child}.each do |#{singular_child}|
                  #{singular_child}.save(false)
                end
              end
            END
            module_eval code, __FILE__, __LINE__
          end

        end
      end
    end

    module ClassMethods
    end

    module InstanceMethods
    end

  end
end
<commit_msg>Allow multiple declarations of acts_as_parent_of.<commit_after>require 'active_support'

module AirBlade
  module ActsAsParentOf

    def self.included(base)
      base.extend ActMethods
    end

    module ActMethods

      # Each child in the array should be the plural form,
      # i.e. matching its has_many declaration.
      def acts_as_parent_of(*children)
        children.each do |child|
          self.send :after_update, "save_#{child}"
          self.send :validates_associated, "#{child}"
        end

        children.each do |child|
          singular_child = ActiveSupport::Inflector.singularize child
          code = <<-END
            def new_#{singular_child}_attributes=(#{singular_child}_attributes)
              #{singular_child}_attributes.each do |attributes|
                #{child}.build attributes
              end
            end

            def existing_#{singular_child}_attributes=(#{singular_child}_attributes)
              #{child}.reject(&:new_record?).each do |#{singular_child}|
                attributes = #{singular_child}_attributes[#{singular_child}.id.to_s]
                if attributes
                  #{singular_child}.attributes = attributes
                else
                  #{child}.delete #{singular_child}
                end
              end
            end

            def save_#{child}
              #{child}.each do |#{singular_child}|
                #{singular_child}.save(false)
              end
            end
          END
          module_eval code, __FILE__, __LINE__
        end
      end

    end

  end
end

{'fraction_non_alphanumeric': 0.07961222091656875, 'fraction_numerical': 0.0, 'mean_word_length': 1.7218225419664268, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1346012', 'n_tokens_mistral': 884, 'n_tokens_neox': 845, 'n_words': 202}