<commit_before># Upgrades simple_form_for to effective_form_with
module Effective
  class FormUpgrader

    def initialize(folder: 'app/views/')
      @folders = Array(folder)
    end

    def upgrade!
      @folders.each do |folder|
        Dir.glob(folder + '**/*').each do |path|
          next if File.directory?(path)
          next unless path.include?('.html')

          writer = Effective::CodeWriter.new(path)

          name = path.split('/')[0...-1] - ['app', 'views']
          resource = Effective::Resource.new(name)

          if writer.find { |line| line.include?('simple_form_for') }
            upgrade_simple_form(writer, resource)
          elsif writer.find { |line| line.include?('semantic_form_for') }
            upgrade_formtastic(writer, resource)
          elsif writer.find { |line| line.include?('form_for') }
            upgrade_form_for(writer, resource)
          else
            next # Nothign to do
          end

          writer.write!
        end
      end

      puts 'All Done. Have a great day.'
      true
    end

    private

    SIMPLE_FORM_FOR_REGEX = /simple_form_for( |\()(\[:[^,]+, ?[^,]+\])?(([^,]+),)?.+do \|(\w+)\|/
    SIMPLE_FORM_INPUT_ATTRIBUTE = /\.input( |\():(\w+)(,|$)/
    SIMPLE_FORM_INPUT_AS_ONE = /(as: :(\w+))/
    SIMPLE_FORM_INPUT_AS_TWO = /(:as => :(\w+))/
    SIMPLE_FORM_INPUT_COLLECTION_ONE = /(collection: ([^,]+?))(,|$)/
    SIMPLE_FORM_INPUT_COLLECTION_TWO = /(:collection => :([^,]+?))(,|$)/

    def upgrade_simple_form(writer, resource)
      puts "Upgrading simple form: #{writer.filename}"

      letter = nil

      # Replace simple_form_for
      writer.all { |line| line.include?('simple_form_for') }.each do |line|
        content = writer.lines[line]
        matched = content.match(SIMPLE_FORM_FOR_REGEX)
        raise("unable to match simple_form_for from:\n#{content}") unless matched.present?

        original = matched[0]
        model = matched[2] || matched[4]
        letter = matched[5]

        raise("unable to determine simple_form_for subject from:\n#{content}") unless original && model && letter

        content.sub!(original, "effective_form_with(model: #{model}) do |#{letter}|")
        writer.replace(line, content)
      end

      # Replace .input
      writer.all { |line| line.include?('.input :') }.each do |line|
        content = writer.lines[line]
        attribute = content.match(SIMPLE_FORM_INPUT_ATTRIBUTE)
        raise("unable to match simple_form_for input attribute from\n#{content}") unless attribute.present?

        as = content.match(SIMPLE_FORM_INPUT_AS_ONE) || content.match(SIMPLE_FORM_INPUT_AS_TWO)
        collection = content.match(SIMPLE_FORM_INPUT_COLLECTION_ONE) || content.match(SIMPLE_FORM_INPUT_COLLECTION_TWO)

        if as.present?
          content.sub!(",#{as[0]}", '')
          content.sub!(", #{as[0]}", '')
        end

        if collection.present?
          content.sub!(",#{collection[0]}", ',')
          content.sub!(", #{collection[0]}", ',')
          content.sub!(attribute[0], "#{attribute[0]} #{collection[2]},")
        end

        input_type = find_input_type(resource: resource, attribute: attribute[2], as: (as[2] if as))

        content.sub!('input', input_type)
        writer.replace(line, content)
      end

      # Replace f.submit with f.save
      writer.all { |line| line.include?("#{letter}.submit") }.each do |line|<commit_msg>update comments<commit_after># Upgrades simple_form_for to effective_form_with
module Effective
  class FormUpgrader

    def initialize(folder: 'app/views/')
      @folders = Array(folder)
    end

    def upgrade!
      @folders.each do |folder|
        Dir.glob(folder + '**/*').each do |path|
          next if File.directory?(path)
          next unless path.include?('.html')

          writer = Effective::CodeWriter.new(path)

          name = path.split('/')[0...-1] - ['app', 'views']
          resource = Effective::Resource.new(name)

          if writer.find { |line| line.include?('simple_form_for') }
            upgrade_simple_form(writer, resource)
          elsif writer.find { |line| line.include?('semantic_form_for') }
            upgrade_formtastic(writer, resource)
          elsif writer.find { |line| line.include?('form_for') }
            upgrade_form_for(writer, resource)
          else
            next # Nothing to do
          end

          writer.write!
        end
      end

      puts 'All Done. Have a great day.'
      true
    end

    private

    SIMPLE_FORM_FOR_REGEX = /simple_form_for( |\()(\[:[^,]+, ?[^,]+\])?(([^,]+),)?.+do \|(\w+)\|/
    SIMPLE_FORM_INPUT_ATTRIBUTE = /\.input( |\():(\w+)(,|$)/
    SIMPLE_FORM_INPUT_AS_ONE = /(as: :(\w+))/
    SIMPLE_FORM_INPUT_AS_TWO = /(:as => :(\w+))/
    SIMPLE_FORM_INPUT_COLLECTION_ONE = /(collection: ([^,]+?))(,|$)/
    SIMPLE_FORM_INPUT_COLLECTION_TWO = /(:collection => :([^,]+?))(,|$)/

    def upgrade_simple_form(writer, resource)
      puts "Upgrading simple form: #{writer.filename}"

      letter = nil

      # Replace simple_form_for
      writer.all { |line| line.include?('simple_form_for') }.each do |line|
        content = writer.lines[line]
        matched = content.match(SIMPLE_FORM_FOR_REGEX)
        raise("unable to match simple_form_for from:\n#{content}") unless matched.present?

        original = matched[0]
        model = matched[2] || matched[4]
        letter = matched[5]

        raise("unable to determine simple_form_for subject from:\n#{content}") unless original && model && letter

        content.sub!(original, "effective_form_with(model: #{model}) do |#{letter}|")
        writer.replace(line, content)
      end

      # Replace .input
      writer.all { |line| line.include?('.input :') }.each do |line|
        content = writer.lines[line]
        attribute = content.match(SIMPLE_FORM_INPUT_ATTRIBUTE)
        raise("unable to match simple_form_for input attribute from\n#{content}") unless attribute.present?

        as = content.match(SIMPLE_FORM_INPUT_AS_ONE) || content.match(SIMPLE_FORM_INPUT_AS_TWO)
        collection = content.match(SIMPLE_FORM_INPUT_COLLECTION_ONE) || content.match(SIMPLE_FORM_INPUT_COLLECTION_TWO)

        if as.present?
          content.sub!(",#{as[0]}", '')
          content.sub!(", #{as[0]}", '')
        end

        if collection.present?
          content.sub!(",#{collection[0]}", ',')
          content.sub!(", #{collection[0]}", ',')
          content.sub!(attribute[0], "#{attribute[0]} #{collection[2]},")
        end

        input_type = find_input_type(resource: resource, attribute: attribute[2], as: (as[2] if as))

        content.sub!('input', input_type)
        writer.replace(line, content)
      end

      # Replace f.submit
      writer.all { |line| line.include?("#{letter}.submit") }.each do |line|
{'fraction_non_alphanumeric': 0.15852222549308212, 'fraction_numerical': 0.004415660877244628, 'mean_word_length': 2.7479316050744624, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '113358', 'n_tokens_mistral': 2220, 'n_tokens_neox': 2136, 'n_words': 422}