lib/rails/generators/generated_attribute.rb in railties-7.1.0.beta1 vs lib/rails/generators/generated_attribute.rb in railties-7.1.0.rc1
- old
+ new
@@ -41,10 +41,14 @@
index_type, type = type, nil if valid_index_type?(type)
type, attr_options = *parse_type_and_options(type)
type = type.to_sym if type
+ if dangerous_name?(name)
+ raise Error, "Could not generate field '#{name}', as it is already defined by Active Record."
+ end
+
if type && !valid_type?(type)
raise Error, "Could not generate field '#{name}' with unknown type '#{type}'."
end
if index_type && !valid_index_type?(index_type)
@@ -58,11 +62,17 @@
end
new(name, type, index_type, attr_options)
end
+ def dangerous_name?(name)
+ defined?(ActiveRecord::Base) &&
+ ActiveRecord::Base.dangerous_attribute_method?(name)
+ end
+
def valid_type?(type)
DEFAULT_TYPES.include?(type.to_s) ||
+ !defined?(ActiveRecord::Base) ||
ActiveRecord::Base.connection.valid_type?(type)
end
def valid_index_type?(index_type)
INDEX_OPTIONS.include?(index_type.to_s)