Method: ProjectFile.add_line

Defined in:
lib/project_file.rb

.add_line(name, end_model, line_content) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/project_file.rb', line 50

def self.add_line(name, end_model, line_content)
  open_close(name, 'model') do |file, tempfile|
    line_found = false
    file.each do |line|
      if (line.include?('end') || line.include?("through: :#{end_model}")) && !line_found
        line_found = true
        line_association = ''
        line_content.each do |key, value|
          line_association << if %w[has_many has_one].include?(key)
                                "  #{key} #{value}"
                              else
                                ", #{key}: #{value}"
                              end
        end

        line_association << "\n"
        tempfile << line_association
      end
      tempfile << line
    end
  end
end