48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/validates_lengths_from_database/core.rb', line 48
def validate_lengths_from_database
options = self.class.validate_lengths_from_database_options
if options[:only]
columns_to_validate = options[:only].map(&:to_s)
else
columns_to_validate = self.class.column_names.map(&:to_s)
columns_to_validate -= options[:except].map(&:to_s) if options[:except]
end
columns_to_validate.each do |column|
column_schema = self.class.columns.find {|c| c.name == column }
next if column_schema.nil?
next if column_schema.respond_to?(:array) && column_schema.array
next unless [:string, :text, :decimal].include?(column_schema.type)
case column_schema.type
when :string
if column_limit = options[:limit][column_schema.type] || column_schema.limit
validate_length_by_characters(column, column_limit)
end
when :text
if column_limit = options[:limit][column_schema.type] || column_schema.limit
validate_length_by_bytesize(column, column_limit)
end
when :decimal
if column_schema.precision && column_schema.scale
validate_numericality_with_precision(column, column_schema)
end
end
end
end
|