<commit_before>require 'astrolabe/builder'
require 'parser/current'
require 'unparser'

class AttrAccessible2StrongParams::Converter
  def read_attr_accessible(filename)
    root_node, comments = parse_file_with_comments(filename)
    m = root_node.each_node(:send).select {|n| n.children[1] == :attr_accessible}.first
    @model_class_name = m.parent.parent.children[0].children[1]
    @aa_list = m.each_node(:sym).collect {|n| n.children[0]}
  end

  def write_controller_with_strong_params(filename)
    root_node, comments = parse_file_with_comments(filename)
    rewritten = Unparser.unparse(root_node, comments)
    temp_path = "#{filename}.rewritten"
    File.open(temp_path, 'w') do |temp_file|
      temp_file.write(rewritten)
      temp_file.puts unless rewritten.end_with?(?\n)
    end
    File.rename(temp_path, filename)
  end

private
  def parse_file_with_comments(filename)
    parser = Parser::CurrentRuby.new(Astrolabe::Builder.new)
    buffer = Parser::Source::Buffer.new(filename)
    buffer.source = File.read(filename)
    parser.parse_with_comments(buffer)
  end
end
<commit_msg>Support attr_accessible appears multi times.<commit_after>require 'astrolabe/builder'
require 'parser/current'
require 'unparser'

class AttrAccessible2StrongParams::Converter
  def read_attr_accessible(filename)
    root_node, comments = parse_file_with_comments(filename)
    aa_nodes = root_node.each_node(:send).select {|n| n.children[1] == :attr_accessible}
    aa_fields = []
    aa_nodes.each do |m|
      @model_class_name = m.parent.parent.children[0].children[1]
      aa_fields <<= m.each_node(:sym).collect {|n| n.children[0]}
    end
    @model_fields = aa_fields.flatten
  end

  def write_controller_with_strong_params(filename)
    root_node, comments = parse_file_with_comments(filename)
    rewritten = Unparser.unparse(root_node, comments)
    temp_path = "#{filename}.rewritten"
    File.open(temp_path, 'w') do |temp_file|
      temp_file.write(rewritten)
      temp_file.puts unless rewritten.end_with?(?\n)
    end
    File.rename(temp_path, filename)
  end

private
  def parse_file_with_comments(filename)
    parser = Parser::CurrentRuby.new(Astrolabe::Builder.new)
    buffer = Parser::Source::Buffer.new(filename)
    buffer.source = File.read(filename)
    parser.parse_with_comments(buffer)
  end
end

{'fraction_non_alphanumeric': 0.11315902271753107, 'fraction_numerical': 0.0042863266180882984, 'mean_word_length': 4.879093198992443, 'pattern_counts': {'":': 0, '<': 5, '<?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': '405888', 'n_tokens_mistral': 798, 'n_tokens_neox': 773, 'n_words': 129}