lib/rails/generators/generated_attribute.rb in railties-7.2.2 vs lib/rails/generators/generated_attribute.rb in railties-8.0.0.beta1
- old
+ new
@@ -1,8 +1,9 @@
# frozen_string_literal: true
require "active_support/time"
+require "active_support/core_ext/string/starts_ends_with"
module Rails
module Generators
class GeneratedAttribute # :nodoc:
INDEX_OPTIONS = %w(index uniq)
@@ -87,24 +88,28 @@
# parse possible attribute options like :limit for string/text/binary/integer, :precision/:scale for decimals or :polymorphic for references/belongs_to
# when declaring options curly brackets should be used
def parse_type_and_options(type)
case type
when /(text|binary)\{([a-z]+)\}/
- return $1, size: $2.to_sym
+ parsed_type, parsed_options = $1, { size: $2.to_sym }
when /(string|text|binary|integer)\{(\d+)\}/
- return $1, limit: $2.to_i
+ parsed_type, parsed_options = $1, { limit: $2.to_i }
when /decimal\{(\d+)[,.-](\d+)\}/
- return :decimal, precision: $1.to_i, scale: $2.to_i
+ parsed_type, parsed_options = :decimal, { precision: $1.to_i, scale: $2.to_i }
when /(references|belongs_to)\{(.+)\}/
- type = $1
+ parsed_type = $1
provided_options = $2.split(/[,.-]/)
- options = Hash[provided_options.map { |opt| [opt.to_sym, true] }]
-
- return type, options
+ parsed_options = Hash[provided_options.map { |opt| [opt.to_sym, true] }]
else
- return type, {}
+ parsed_type, parsed_options = type&.remove("!"), {}
end
+
+ if type&.ends_with?("!")
+ parsed_options[:null] = false
+ end
+
+ return parsed_type, parsed_options
end
end
def initialize(name, type = nil, index_type = false, attr_options = {})
@name = name
@@ -119,12 +124,12 @@
when :integer then :number_field
when :float, :decimal then :text_field
when :time then :time_field
when :datetime, :timestamp then :datetime_field
when :date then :date_field
- when :text then :text_area
- when :rich_text then :rich_text_area
- when :boolean then :check_box
+ when :text then :textarea
+ when :rich_text then :rich_textarea
+ when :boolean then :checkbox
when :attachment, :attachments then :file_field
else
:text_field
end
end