Skip to content

Instantly share code, notes, and snippets.

@et
Created May 2, 2017 15:47
Show Gist options
  • Save et/e2a5442a6c6e7ac00ad8126f211330c3 to your computer and use it in GitHub Desktop.
Save et/e2a5442a6c6e7ac00ad8126f211330c3 to your computer and use it in GitHub Desktop.
Adding errors to all active admin forms
Dir.glob('app/admin/*.rb') do |file_name|
File.open(file_name, "r") do |f|
text = File.read(f)
form_matches = text.match(/form do \|(?<block_param>\w+)\|/)
if form_matches && form_matches[:block_param]
semantic_errors_matches = text.match(/#{form_matches[:block_param]}.semantic_errors\(\*f\.object\.errors\.keys\)/)
if !semantic_errors_matches
text.gsub!(form_matches.to_s, form_matches.to_s + "\n" + " " + "#{form_matches[:block_param]}.semantic_errors(*#{form_matches[:block_param]}.object.errors.keys)")
File.open(file_name, "w") {|file| file.puts text }
end
else
# puts "#{file} has no form"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment