Class: SorbetRails::ModelPlugins::ActiveRecordAttribute
- Inherits:
-
Base
- Object
- Parlour::Plugin
- Base
- SorbetRails::ModelPlugins::ActiveRecordAttribute
- Defined in:
- lib/sorbet-rails/model_plugins/active_record_attribute.rb
Constant Summary
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
#available_classes, #model_class
Instance Method Summary collapse
- #active_record_type_to_sorbet_type(klass) ⇒ Object
- #generate(root) ⇒ Object
- #type_for_column_def(column_def) ⇒ Object
Methods inherited from Base
Methods included from SorbetRails::ModelUtils
#exists_class_method?, #exists_instance_method?, #model_assoc_proxy_class_name, #model_class, #model_class_name, #model_module_name, #model_relation_class_name, #model_relation_shared_module_name
Constructor Details
This class inherits a constructor from SorbetRails::ModelPlugins::Base
Instance Method Details
#active_record_type_to_sorbet_type(klass) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/sorbet-rails/model_plugins/active_record_attribute.rb', line 79 def active_record_type_to_sorbet_type(klass) case klass when ActiveRecord::Type::Boolean "T::Boolean" when ActiveRecord::Type::DateTime DateTime when ActiveRecord::Type::Date Date when ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter klass.klass when ActiveRecord::Type::Decimal BigDecimal when ActiveRecord::Type::Float Float when ActiveRecord::Type::Time Time when ActiveRecord::Type::BigInteger, ActiveRecord::Type::Integer, ActiveRecord::Type::DecimalWithoutScale, ActiveRecord::Type::UnsignedInteger Integer when ActiveRecord::Type::Binary, ActiveRecord::Type::String, ActiveRecord::Type::Text String else # Json type is only supported in Rails 5.2 and above case when Object.const_defined?('ActiveRecord::Type::Json') && klass.is_a?(ActiveRecord::Type::Json) "T.any(T::Array[T.untyped], T::Boolean, Float, T::Hash[T.untyped, T.untyped], Integer, String)" when Object.const_defined?('ActiveRecord::Enum::EnumType') && klass.is_a?(ActiveRecord::Enum::EnumType) String else "T.untyped" end end end |
#generate(root) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/sorbet-rails/model_plugins/active_record_attribute.rb', line 6 def generate(root) columns_hash = @model_class.table_exists? ? @model_class.columns_hash : {} return unless columns_hash.size > 0 attribute_module_name = self.model_module_name("GeneratedAttributeMethods") attribute_module_rbi = root.create_module(attribute_module_name) attribute_module_rbi.create_extend("T::Sig") model_class_rbi = root.create_class(self.model_class_name) model_class_rbi.create_include(attribute_module_name) columns_hash.sort.each do |column_name, column_def| if @model_class.defined_enums.has_key?(column_name) # enum attribute is treated differently assignable_type = "T.any(Integer, String, Symbol)" assignable_type = "T.nilable(#{assignable_type})" if column_def.null return_type = "String" return_type = "T.nilable(#{return_type})" if column_def.null attribute_module_rbi.create_method( column_name.to_s, return_type: return_type, ) attribute_module_rbi.create_method( "#{column_name}=", parameters: [ Parameter.new("value", type: assignable_type) ], return_type: nil, ) else column_type = type_for_column_def(column_def) attribute_module_rbi.create_method( column_name.to_s, return_type: column_type.to_s, ) attribute_module_rbi.create_method( "#{column_name}=", parameters: [ Parameter.new("value", type: column_type.to_s) ], return_type: nil, ) end attribute_module_rbi.create_method( "#{column_name}?", return_type: "T::Boolean", ) end end |
#type_for_column_def(column_def) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/sorbet-rails/model_plugins/active_record_attribute.rb', line 59 def type_for_column_def(column_def) cast_type = ActiveRecord::Base.connection.respond_to?(:lookup_cast_type_from_column) ? ActiveRecord::Base.connection.lookup_cast_type_from_column(column_def) : column_def.cast_type strict_type = active_record_type_to_sorbet_type(cast_type) if column_def.respond_to?(:array?) && column_def.array? strict_type = "T::Array[#{strict_type}]" end column_def.null ? "T.nilable(#{strict_type})" : strict_type end |