Last active
May 13, 2022 02:42
-
-
Save willnet/b4b5d80704fab02a5d1867687f6b1724 to your computer and use it in GitHub Desktop.
string型もしくはtext型なのに最大長のバリデーションをかけていないものを出力するスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ActiveRecord::Base.descendants.reject(&:abstract_class?).each do |model| | |
string_or_text_columns = model.columns.select { |column| column.type == :string || column.type == :text } | |
columns_with_maximum_length_validation = model.validators.select {|v| v.is_a? ActiveRecord::Validations::LengthValidator }.select {|v| v.options[:maximum] || v.options[:within] }.map(&:attributes).flatten.uniq | |
string_or_text_columns.each do |column| | |
if columns_with_maximum_length_validation.include?(column.name.to_sym) | |
puts "#{model.name}##{column.name} is OK" | |
else | |
puts "#{model.name}##{column.name} is NG" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment