sorbet-rails 0.5.5.1 → 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -0
  3. data/lib/sorbet-rails/config.rb +1 -0
  4. data/lib/sorbet-rails/helper_rbi_formatter.rb +12 -0
  5. data/lib/sorbet-rails/model_plugins/active_record_attribute.rb +51 -9
  6. data/lib/sorbet-rails/model_plugins/active_record_factory_methods.rb +28 -0
  7. data/lib/sorbet-rails/model_plugins/enumerable_collections.rb +1 -0
  8. data/lib/sorbet-rails/model_plugins/plugins.rb +3 -0
  9. data/sorbet-rails.gemspec +1 -1
  10. data/spec/generators/rails-template.rb +14 -1
  11. data/spec/generators/sorbet_test_cases.rb +17 -0
  12. data/spec/sorbet_spec.rb +4 -0
  13. data/spec/support/v4.2/Gemfile.lock +5 -5
  14. data/spec/support/v4.2/app/models/wand.rb +2 -0
  15. data/spec/support/v4.2/app/models/wizard.rb +1 -1
  16. data/spec/support/v4.2/config/initializers/sorbet_rails.rb +1 -1
  17. data/spec/support/v4.2/db/migrate/20190620000001_create_wizards.rb +1 -1
  18. data/spec/support/v4.2/db/migrate/20190620000004_add_more_column_types_to_wands.rb +1 -0
  19. data/spec/support/v4.2/db/schema.rb +3 -2
  20. data/spec/support/v4.2/sorbet_test_cases.rb +17 -0
  21. data/spec/support/v5.0/Gemfile.lock +5 -5
  22. data/spec/support/v5.0/app/models/wand.rb +2 -0
  23. data/spec/support/v5.0/app/models/wizard.rb +1 -1
  24. data/spec/support/v5.0/config/application.rb +1 -0
  25. data/spec/support/v5.0/config/initializers/sorbet_rails.rb +1 -1
  26. data/spec/support/v5.0/db/migrate/20190620000001_create_wizards.rb +1 -1
  27. data/spec/support/v5.0/db/migrate/20190620000004_add_more_column_types_to_wands.rb +1 -0
  28. data/spec/support/v5.0/db/schema.rb +1 -0
  29. data/spec/support/v5.0/sorbet_test_cases.rb +17 -0
  30. data/spec/support/v5.1/Gemfile.lock +5 -5
  31. data/spec/support/v5.1/app/models/wand.rb +2 -0
  32. data/spec/support/v5.1/app/models/wizard.rb +1 -1
  33. data/spec/support/v5.1/config/initializers/sorbet_rails.rb +1 -1
  34. data/spec/support/v5.1/db/migrate/20190620000001_create_wizards.rb +1 -1
  35. data/spec/support/v5.1/db/migrate/20190620000004_add_more_column_types_to_wands.rb +1 -0
  36. data/spec/support/v5.1/db/schema.rb +1 -0
  37. data/spec/support/v5.1/sorbet_test_cases.rb +17 -0
  38. data/spec/support/v5.2/Gemfile.lock +5 -5
  39. data/spec/support/v5.2/app/models/wand.rb +2 -0
  40. data/spec/support/v5.2/app/models/wizard.rb +1 -1
  41. data/spec/support/v5.2/config/initializers/sorbet_rails.rb +1 -1
  42. data/spec/support/v5.2/db/migrate/20190620000001_create_wizards.rb +1 -1
  43. data/spec/support/v5.2/db/migrate/20190620000004_add_more_column_types_to_wands.rb +1 -0
  44. data/spec/support/v5.2/db/schema.rb +1 -0
  45. data/spec/support/v5.2/sorbet_test_cases.rb +17 -0
  46. data/spec/support/v6.0/Gemfile.lock +5 -5
  47. data/spec/support/v6.0/app/models/wand.rb +2 -0
  48. data/spec/support/v6.0/app/models/wizard.rb +1 -1
  49. data/spec/support/v6.0/config/initializers/sorbet_rails.rb +1 -1
  50. data/spec/support/v6.0/config/initializers/wrap_parameters.rb +1 -1
  51. data/spec/support/v6.0/db/migrate/20190620000001_create_wizards.rb +1 -1
  52. data/spec/support/v6.0/db/migrate/20190620000004_add_more_column_types_to_wands.rb +1 -0
  53. data/spec/support/v6.0/db/schema.rb +1 -0
  54. data/spec/support/v6.0/sorbet_test_cases.rb +17 -0
  55. data/spec/test_data/v4.2/expected_potion.rbi +12 -3
  56. data/spec/test_data/v4.2/expected_spell_book.rbi +12 -3
  57. data/spec/test_data/v4.2/expected_squib.rbi +16 -7
  58. data/spec/test_data/v4.2/expected_srb_tc_output.txt +35 -27
  59. data/spec/test_data/v4.2/expected_wand.rbi +25 -7
  60. data/spec/test_data/v4.2/expected_wizard.rbi +16 -7
  61. data/spec/test_data/v4.2/expected_wizard_wo_spellbook.rbi +16 -7
  62. data/spec/test_data/v5.0/expected_helpers.rbi +5 -0
  63. data/spec/test_data/v5.0/expected_helpers_with_application_and_devise_helpers.rbi +5 -0
  64. data/spec/test_data/v5.0/expected_internal_metadata.rbi +16 -7
  65. data/spec/test_data/v5.0/expected_potion.rbi +12 -3
  66. data/spec/test_data/v5.0/expected_schema_migration.rbi +12 -3
  67. data/spec/test_data/v5.0/expected_spell_book.rbi +12 -3
  68. data/spec/test_data/v5.0/expected_squib.rbi +16 -7
  69. data/spec/test_data/v5.0/expected_wand.rbi +27 -9
  70. data/spec/test_data/v5.0/expected_wizard.rbi +16 -7
  71. data/spec/test_data/v5.0/expected_wizard_wo_spellbook.rbi +16 -7
  72. data/spec/test_data/v5.1/expected_helpers.rbi +5 -0
  73. data/spec/test_data/v5.1/expected_helpers_with_application_and_devise_helpers.rbi +5 -0
  74. data/spec/test_data/v5.1/expected_internal_metadata.rbi +16 -7
  75. data/spec/test_data/v5.1/expected_potion.rbi +12 -3
  76. data/spec/test_data/v5.1/expected_schema_migration.rbi +12 -3
  77. data/spec/test_data/v5.1/expected_spell_book.rbi +12 -3
  78. data/spec/test_data/v5.1/expected_squib.rbi +16 -7
  79. data/spec/test_data/v5.1/expected_wand.rbi +27 -9
  80. data/spec/test_data/v5.1/expected_wizard.rbi +16 -7
  81. data/spec/test_data/v5.1/expected_wizard_wo_spellbook.rbi +16 -7
  82. data/spec/test_data/v5.2/expected_attachment.rbi +12 -3
  83. data/spec/test_data/v5.2/expected_blob.rbi +12 -3
  84. data/spec/test_data/v5.2/expected_helpers.rbi +5 -0
  85. data/spec/test_data/v5.2/expected_helpers_with_application_and_devise_helpers.rbi +5 -0
  86. data/spec/test_data/v5.2/expected_internal_metadata.rbi +16 -7
  87. data/spec/test_data/v5.2/expected_potion.rbi +12 -3
  88. data/spec/test_data/v5.2/expected_schema_migration.rbi +12 -3
  89. data/spec/test_data/v5.2/expected_spell_book.rbi +12 -3
  90. data/spec/test_data/v5.2/expected_squib.rbi +16 -7
  91. data/spec/test_data/v5.2/expected_wand.rbi +27 -9
  92. data/spec/test_data/v5.2/expected_wizard.rbi +16 -7
  93. data/spec/test_data/v5.2/expected_wizard_wo_spellbook.rbi +16 -7
  94. data/spec/test_data/v6.0/expected_attachment.rbi +12 -3
  95. data/spec/test_data/v6.0/expected_blob.rbi +12 -3
  96. data/spec/test_data/v6.0/expected_helpers.rbi +5 -0
  97. data/spec/test_data/v6.0/expected_helpers_with_application_and_devise_helpers.rbi +5 -0
  98. data/spec/test_data/v6.0/expected_internal_metadata.rbi +16 -7
  99. data/spec/test_data/v6.0/expected_potion.rbi +12 -3
  100. data/spec/test_data/v6.0/expected_schema_migration.rbi +12 -3
  101. data/spec/test_data/v6.0/expected_spell_book.rbi +12 -3
  102. data/spec/test_data/v6.0/expected_squib.rbi +16 -7
  103. data/spec/test_data/v6.0/expected_wand.rbi +27 -9
  104. data/spec/test_data/v6.0/expected_wizard.rbi +16 -7
  105. data/spec/test_data/v6.0/expected_wizard_wo_spellbook.rbi +16 -7
  106. metadata +2 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f4aa01ecf2a262e021765ef53cdd6d5dbaaa93776c0d45ce991f9e70943eae84
4
- data.tar.gz: 3957eb0b707a6615466d102eb7fafc25ab21ec35a6a7b3413629ac2924cda36d
3
+ metadata.gz: 94d5e7839ac88952e888bdde9ac9ee2c838c7833732a9e5f3af80c2eaf29b62c
4
+ data.tar.gz: 74d8006e67f5283ba3f8dbdbefc347a49284c31307ad4cbc9a4e5a9986de80a6
5
5
  SHA512:
6
- metadata.gz: cf331590decda2ee306cecda5bd89b1c603b5cbbfb1000f8dfe13e2ca6277d13269e5916cf9337c855be390da9a09eb0b6dd5e36867425421adeb563f1ffafe4
7
- data.tar.gz: 61f5427e41cbee6b74311596b8aa54aa981ddf522692c151ca6db95b0cb5fc409e0ee476aab307b6baf0eb0576f38352c2e6c65be1d1d7bd7ccd28753bc3e5a8
6
+ metadata.gz: 734626fabb0cd9761597f102b4b626a09c4f6f15663abb776fcdadf044851e4f08cfb7da2e125c1109a115037a79192b16bc9f69f989a789b8c57d55835da8ee
7
+ data.tar.gz: 9d6dff417e3a1c160055b6880117a333efa0f3a3d6ff4ec8d0d9dce0b9298130857ed97a2e0e9e7d149019dd51b38a349b02302818fa57c47fec06eb8e28e6ab
data/README.md CHANGED
@@ -9,6 +9,8 @@ This gem adds a few Rake tasks to generate Ruby Interface (RBI) files for dynami
9
9
 
10
10
  `sorbet-rails` supports Rails 4.2 or later.
11
11
 
12
+ **Notice**: From v0.6, we'll stop supporting Rails 4.2. Fixes targeting Rails 4.2 specifically can be merged to v0.5.x branch.
13
+
12
14
  ## Initial Setup
13
15
 
14
16
  1. Follow the steps [here](https://sorbet.org/docs/adopting) to set up the latest version of Sorbet, up to being able to run `srb tc`.
@@ -48,6 +48,7 @@ module SorbetRails
48
48
  :active_record_attribute,
49
49
  :active_record_assoc,
50
50
  :active_record_finder_methods,
51
+ :active_record_factory_methods,
51
52
  :custom_finder_methods,
52
53
  :enumerable_collections,
53
54
  ]
@@ -31,6 +31,18 @@ class SorbetRails::HelperRbiFormatter
31
31
  end
32
32
  end
33
33
 
34
+ if ActionController::Helpers.method_defined?(:helpers)
35
+ # Adds the `helpers` method that provides access to all methods within the
36
+ # application's helpers.
37
+ # https://api.rubyonrails.org/classes/ActionController/Helpers/ClassMethods.html#method-i-helpers
38
+ @parlour.root.create_module('ActionController::Helpers') do |mod|
39
+ mod.create_method(
40
+ 'helpers',
41
+ return_type: "T.all(#{@helpers.join(', ')})"
42
+ )
43
+ end
44
+ end
45
+
34
46
  return @parlour.rbi
35
47
  end
36
48
  end
@@ -42,7 +42,7 @@ class SorbetRails::ModelPlugins::ActiveRecordAttribute < SorbetRails::ModelPlugi
42
42
  attribute_module_rbi.create_method(
43
43
  "#{column_name}=",
44
44
  parameters: [
45
- Parameter.new("value", type: column_type.to_s)
45
+ Parameter.new("value", type: value_type_for_attr_writer(column_type))
46
46
  ],
47
47
  return_type: nil,
48
48
  )
@@ -61,7 +61,11 @@ class SorbetRails::ModelPlugins::ActiveRecordAttribute < SorbetRails::ModelPlugi
61
61
  ActiveRecord::Base.connection.lookup_cast_type_from_column(column_def) :
62
62
  column_def.cast_type
63
63
 
64
- strict_type = active_record_type_to_sorbet_type(cast_type)
64
+ strict_type =
65
+ active_record_type_to_sorbet_type(
66
+ cast_type,
67
+ time_zone_aware: time_zone_aware_column?(column_def, cast_type),
68
+ )
65
69
 
66
70
  if column_def.respond_to?(:array?) && column_def.array?
67
71
  strict_type = "T::Array[#{strict_type}]"
@@ -72,25 +76,26 @@ class SorbetRails::ModelPlugins::ActiveRecordAttribute < SorbetRails::ModelPlugi
72
76
  sig do
73
77
  params(
74
78
  # in v4.2, datetime can be TimeZoneConverter
75
- klass: T.any(Object, ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter)
79
+ klass: T.any(Object, ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter),
80
+ time_zone_aware: T::Boolean,
76
81
  ).returns(T.any(String, Class))
77
82
  end
78
- def active_record_type_to_sorbet_type(klass)
83
+ def active_record_type_to_sorbet_type(klass, time_zone_aware: false)
79
84
  case klass
80
85
  when ActiveRecord::Type::Boolean
81
86
  "T::Boolean"
82
- when ActiveRecord::Type::DateTime
83
- DateTime
87
+ when ActiveRecord::Type::DateTime, ActiveRecord::Type::Time
88
+ time_zone_aware ? ActiveSupport::TimeWithZone : Time
84
89
  when ActiveRecord::Type::Date
85
90
  Date
86
91
  when ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
87
- klass.klass
92
+ # if type has already been decorated with TimeZoneConverter, it's probably
93
+ # a Rails 4.2 datetime column and can be considered a TimeWithZone
94
+ ActiveSupport::TimeWithZone
88
95
  when ActiveRecord::Type::Decimal
89
96
  BigDecimal
90
97
  when ActiveRecord::Type::Float
91
98
  Float
92
- when ActiveRecord::Type::Time
93
- Time
94
99
  when ActiveRecord::Type::BigInteger, ActiveRecord::Type::Integer, ActiveRecord::Type::DecimalWithoutScale, ActiveRecord::Type::UnsignedInteger
95
100
  Integer
96
101
  when ActiveRecord::Type::Binary, ActiveRecord::Type::String, ActiveRecord::Type::Text
@@ -107,4 +112,41 @@ class SorbetRails::ModelPlugins::ActiveRecordAttribute < SorbetRails::ModelPlugi
107
112
  end
108
113
  end
109
114
  end
115
+
116
+ # True if this column is "time zone aware", which means it'll be converted on
117
+ # access from its original class (e.g. `DateTime`) to something with better
118
+ # support for time zones (usually `ActiveSupport::TimeWithZone`)
119
+ sig do
120
+ params(column_def: T.untyped, cast_type: T.untyped).returns(T::Boolean)
121
+ end
122
+ def time_zone_aware_column?(column_def, cast_type)
123
+ # this private class method returns true if the attribute should be "time
124
+ # zone aware"; it takes into account various global and model-specific
125
+ # configuration options as described here:
126
+ # https://api.rubyonrails.org/classes/ActiveRecord/Timestamp.html
127
+ #
128
+ # although it's private, it's better this than trying to reimplement the "is
129
+ # this attribute tz aware?" logic ourselves
130
+ @model_class.send(
131
+ :create_time_zone_conversion_attribute?,
132
+ column_def.name,
133
+ cast_type
134
+ )
135
+ end
136
+
137
+ sig { params(column_type: Object).returns(String) }
138
+ def value_type_for_attr_writer(column_type)
139
+ assignable_time_types = [DateTime, Date, Time, ActiveSupport::TimeWithZone].map(&:to_s)
140
+
141
+ # it's safe - and convenient - to assign any "time like" object to a time zone
142
+ # aware attribute because Rails will cast it to a `ActiveSupport::TimeWithZone`
143
+ # (so rereading the attribute will always return the `TimeWithZone` type)
144
+ if column_type == ActiveSupport::TimeWithZone
145
+ "T.any(#{assignable_time_types.join(', ')})"
146
+ elsif column_type == "T.nilable(ActiveSupport::TimeWithZone)"
147
+ "T.nilable(T.any(#{assignable_time_types.join(', ')}))"
148
+ else
149
+ column_type.to_s
150
+ end
151
+ end
110
152
  end
@@ -0,0 +1,28 @@
1
+ # typed: strict
2
+ require ('sorbet-rails/model_plugins/base')
3
+ class SorbetRails::ModelPlugins::ActiveRecordFactoryMethods < SorbetRails::ModelPlugins::Base
4
+ # Create methods like `new`, `create`, `create!`, etc.
5
+ sig { override.params(root: Parlour::RbiGenerator::Namespace).void }
6
+ def generate(root)
7
+ model_rbi = root.create_class(self.model_class_name)
8
+
9
+ factory_methods = ['create', 'create!', 'new']
10
+
11
+ factory_methods.each do |factory_method|
12
+ add_factory_method(model_rbi, factory_method)
13
+ end
14
+ end
15
+
16
+ sig { params(model_rbi: Parlour::RbiGenerator::ClassNamespace, method_name: String).void }
17
+ def add_factory_method(model_rbi, method_name)
18
+ model_rbi.create_method(
19
+ method_name,
20
+ parameters: [
21
+ Parlour::RbiGenerator::Parameter.new("attributes", type: "T.untyped", default: 'nil'),
22
+ Parlour::RbiGenerator::Parameter.new("&block", type: "T.untyped")
23
+ ],
24
+ return_type: self.model_class_name,
25
+ class_method: true
26
+ )
27
+ end
28
+ end
@@ -36,6 +36,7 @@ class SorbetRails::ModelPlugins::EnumerableCollections < SorbetRails::ModelPlugi
36
36
  parameters: [
37
37
  Parameter.new("&block", type: "T.proc.params(e: #{self.model_class_name}).void")
38
38
  ],
39
+ return_type: "T::Array[#{self.model_class_name}]",
39
40
  implementation: true,
40
41
  )
41
42
  class_rbi.create_method(
@@ -7,6 +7,7 @@ require('sorbet-rails/model_plugins/active_record_named_scope')
7
7
  require('sorbet-rails/model_plugins/active_record_attribute')
8
8
  require('sorbet-rails/model_plugins/active_record_assoc')
9
9
  require('sorbet-rails/model_plugins/active_record_finder_methods')
10
+ require('sorbet-rails/model_plugins/active_record_factory_methods')
10
11
  require('sorbet-rails/model_plugins/custom_finder_methods')
11
12
  require('sorbet-rails/model_plugins/enumerable_collections')
12
13
  require('sorbet-rails/model_plugins/active_storage_methods')
@@ -54,6 +55,8 @@ module SorbetRails::ModelPlugins
54
55
  ActiveRecordAssoc
55
56
  when :active_record_finder_methods
56
57
  ActiveRecordFinderMethods
58
+ when :active_record_factory_methods
59
+ ActiveRecordFactoryMethods
57
60
  when :custom_finder_methods
58
61
  CustomFinderMethods
59
62
  when :enumerable_collections
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{sorbet-rails}
3
- s.version = "0.5.5.1"
3
+ s.version = "0.5.6"
4
4
  s.date = %q{2019-04-18}
5
5
  s.summary = %q{Set of tools to make Sorbet work with Rails seamlessly.}
6
6
  s.authors = ["Chan Zuckerberg Initiative"]
@@ -20,6 +20,15 @@ def add_routes
20
20
  route "get 'test/index' => 'test#index'"
21
21
  end
22
22
 
23
+ def add_environment
24
+ if ENV["RAILS_VERSION"] == "5.0"
25
+ # by default, Rails 5.0 treats only `datetime` as a time zone aware type;
26
+ # this config option brings it in line with all versions up to at least v6.0
27
+ # and makes our time-related attribute fixtures simpler to maintain
28
+ environment 'config.active_record.time_zone_aware_types = [:datetime, :time]'
29
+ end
30
+ end
31
+
23
32
  def create_initializers
24
33
  initializer "sorbet_rails.rb", <<~RUBY
25
34
  # typed: strict
@@ -99,6 +108,8 @@ def create_models
99
108
  class Wand < ApplicationRecord
100
109
  include Mythical
101
110
 
111
+ self.skip_time_zone_conversion_for_attributes = [:broken_at]
112
+
102
113
  enum core_type: {
103
114
  phoenix_feather: 0,
104
115
  dragon_heartstring: 1,
@@ -242,7 +253,7 @@ def create_migrations
242
253
  t.string :parent_email
243
254
  t.text :notes
244
255
 
245
- t.timestamps
256
+ t.timestamps null: false
246
257
  end
247
258
  end
248
259
  end
@@ -281,6 +292,7 @@ def create_migrations
281
292
  add_column :wands, :hardness, :decimal, null: false, precision: 10, scale: 10, default: 5
282
293
  add_column :wands, :reflectance, :decimal, null: false, precision: 10, scale: 0, default: 0.5
283
294
  add_column :wands, :broken, :boolean, null: false, default: false
295
+ add_column :wands, :broken_at, :datetime, null: true
284
296
  add_column :wands, :chosen_at_date, :date
285
297
  add_column :wands, :chosen_at_time, :time
286
298
  # JSON column type is only supported on 5.2 or higher
@@ -393,6 +405,7 @@ add_gems
393
405
  after_bundle do
394
406
  say "Creating application..."
395
407
  add_routes
408
+ add_environment
396
409
  create_initializers
397
410
  create_lib
398
411
  create_helpers
@@ -1,12 +1,29 @@
1
1
  # typed: false
2
2
  require 'sorbet-runtime'
3
3
 
4
+ wand = Wand.first!
4
5
  wizard = Wizard.first!
5
6
  T.assert_type!(wizard, Wizard)
6
7
 
7
8
  # -- model columns
8
9
  T.assert_type!(wizard.name, T.nilable(String))
9
10
 
11
+ # -- time/date columns
12
+ T.assert_type!(wizard.created_at, ActiveSupport::TimeWithZone)
13
+ T.assert_type!(wand.broken_at, T.nilable(Time))
14
+ T.assert_type!(wand.chosen_at_date, T.nilable(Date))
15
+
16
+ # assert that TZ aware attributes are casted to TimeWithZone after assigning
17
+ wizard.created_at = DateTime.now
18
+ T.assert_type!(wizard.created_at, ActiveSupport::TimeWithZone)
19
+
20
+ if ENV["RAILS_VERSION"] != "4.2"
21
+ # note: this is expected to fail in Rails 4.2, because in that version only
22
+ # `datetime` fields are made time zone aware (so this will have the type
23
+ # `T.nilable(Time)` instead)
24
+ T.assert_type!(wand.chosen_at_time, T.nilable(ActiveSupport::TimeWithZone))
25
+ end
26
+
10
27
  # -- model associations
11
28
  T.assert_type!(wizard.wand, T.nilable(Wand))
12
29
  T.assert_type!(T.must(wizard.wand).wizard, Wizard)
@@ -20,6 +20,10 @@ RSpec.describe 'sorbet' do
20
20
  wizard: harry,
21
21
  core_type: :phoenix_feather,
22
22
  wood_type: "holly",
23
+ chosen_at_date: Date.parse('2019-09-01'),
24
+ chosen_at_time: Time.parse('2019-09-01T09:00:00Z'),
25
+ broken: true,
26
+ broken_at: Time.parse('2019-09-05T15:30:00Z'),
23
27
  )
24
28
  end
25
29
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../../..
3
3
  specs:
4
- sorbet-rails (0.5.5)
4
+ sorbet-rails (0.5.6)
5
5
  parlour (~> 0.8.0)
6
6
 
7
7
  GEM
@@ -95,10 +95,10 @@ GEM
95
95
  thor (>= 0.18.1, < 2.0)
96
96
  rainbow (3.0.0)
97
97
  rake (12.3.3)
98
- sorbet (0.4.4755)
99
- sorbet-static (= 0.4.4755)
100
- sorbet-runtime (0.4.4755)
101
- sorbet-static (0.4.4755-x86_64-linux)
98
+ sorbet (0.4.4761)
99
+ sorbet-static (= 0.4.4761)
100
+ sorbet-runtime (0.4.4761)
101
+ sorbet-static (0.4.4761-universal-darwin-14)
102
102
  sprockets (3.7.2)
103
103
  concurrent-ruby (~> 1.0)
104
104
  rack (> 1, < 3)
@@ -2,6 +2,8 @@
2
2
  class Wand < ApplicationRecord
3
3
  include Mythical
4
4
 
5
+ self.skip_time_zone_conversion_for_attributes = [:broken_at]
6
+
5
7
  enum core_type: {
6
8
  phoenix_feather: 0,
7
9
  dragon_heartstring: 1,
@@ -1,4 +1,4 @@
1
- # typed: ignore
1
+ # typed: strict
2
2
  class Wizard < ApplicationRecord
3
3
  validates :name, length: { minimum: 5 }, presence: true
4
4
 
@@ -1,3 +1,3 @@
1
- # typed: strict
1
+ # typed: false
2
2
  require(Rails.root.join('lib/mythical_rbi_plugin'))
3
3
  SorbetRails::ModelRbiFormatter.register_plugin(MythicalRbiPlugin)
@@ -7,7 +7,7 @@ class CreateWizards < ActiveRecord::Migration
7
7
  t.string :parent_email
8
8
  t.text :notes
9
9
 
10
- t.timestamps
10
+ t.timestamps null: false
11
11
  end
12
12
  end
13
13
  end
@@ -5,6 +5,7 @@ class AddMoreColumnTypesToWands < ActiveRecord::Migration
5
5
  add_column :wands, :hardness, :decimal, null: false, precision: 10, scale: 10, default: 5
6
6
  add_column :wands, :reflectance, :decimal, null: false, precision: 10, scale: 0, default: 0.5
7
7
  add_column :wands, :broken, :boolean, null: false, default: false
8
+ add_column :wands, :broken_at, :datetime, null: true
8
9
  add_column :wands, :chosen_at_date, :date
9
10
  add_column :wands, :chosen_at_time, :time
10
11
  # JSON column type is only supported on 5.2 or higher
@@ -30,6 +30,7 @@ ActiveRecord::Schema.define(version: 20190620000007) do
30
30
  t.decimal "hardness", precision: 10, scale: 10, default: 5.0, null: false
31
31
  t.decimal "reflectance", precision: 10, default: 0, null: false
32
32
  t.boolean "broken", default: false, null: false
33
+ t.datetime "broken_at"
33
34
  t.date "chosen_at_date"
34
35
  t.time "chosen_at_time"
35
36
  end
@@ -39,8 +40,8 @@ ActiveRecord::Schema.define(version: 20190620000007) do
39
40
  t.integer "house"
40
41
  t.string "parent_email"
41
42
  t.text "notes"
42
- t.datetime "created_at"
43
- t.datetime "updated_at"
43
+ t.datetime "created_at", null: false
44
+ t.datetime "updated_at", null: false
44
45
  t.string "broom"
45
46
  t.string "type", default: "Wizard", null: false
46
47
  end
@@ -1,12 +1,29 @@
1
1
  # typed: false
2
2
  require 'sorbet-runtime'
3
3
 
4
+ wand = Wand.first!
4
5
  wizard = Wizard.first!
5
6
  T.assert_type!(wizard, Wizard)
6
7
 
7
8
  # -- model columns
8
9
  T.assert_type!(wizard.name, T.nilable(String))
9
10
 
11
+ # -- time/date columns
12
+ T.assert_type!(wizard.created_at, ActiveSupport::TimeWithZone)
13
+ T.assert_type!(wand.broken_at, T.nilable(Time))
14
+ T.assert_type!(wand.chosen_at_date, T.nilable(Date))
15
+
16
+ # assert that TZ aware attributes are casted to TimeWithZone after assigning
17
+ wizard.created_at = DateTime.now
18
+ T.assert_type!(wizard.created_at, ActiveSupport::TimeWithZone)
19
+
20
+ if ENV["RAILS_VERSION"] != "4.2"
21
+ # note: this is expected to fail in Rails 4.2, because in that version only
22
+ # `datetime` fields are made time zone aware (so this will have the type
23
+ # `T.nilable(Time)` instead)
24
+ T.assert_type!(wand.chosen_at_time, T.nilable(ActiveSupport::TimeWithZone))
25
+ end
26
+
10
27
  # -- model associations
11
28
  T.assert_type!(wizard.wand, T.nilable(Wand))
12
29
  T.assert_type!(T.must(wizard.wand).wizard, Wizard)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../../..
3
3
  specs:
4
- sorbet-rails (0.5.5)
4
+ sorbet-rails (0.5.6)
5
5
  parlour (~> 0.8.0)
6
6
 
7
7
  GEM
@@ -100,10 +100,10 @@ GEM
100
100
  thor (>= 0.18.1, < 2.0)
101
101
  rainbow (3.0.0)
102
102
  rake (12.3.3)
103
- sorbet (0.4.4755)
104
- sorbet-static (= 0.4.4755)
105
- sorbet-runtime (0.4.4755)
106
- sorbet-static (0.4.4755-universal-darwin-14)
103
+ sorbet (0.4.4761)
104
+ sorbet-static (= 0.4.4761)
105
+ sorbet-runtime (0.4.4761)
106
+ sorbet-static (0.4.4761-universal-darwin-14)
107
107
  sprockets (3.7.2)
108
108
  concurrent-ruby (~> 1.0)
109
109
  rack (> 1, < 3)
@@ -2,6 +2,8 @@
2
2
  class Wand < ApplicationRecord
3
3
  include Mythical
4
4
 
5
+ self.skip_time_zone_conversion_for_attributes = [:broken_at]
6
+
5
7
  enum core_type: {
6
8
  phoenix_feather: 0,
7
9
  dragon_heartstring: 1,