Class: ShrinePlugin

Inherits:
SorbetRails::ModelPlugins::Base show all
Defined in:
lib/sorbet-rails/gem_plugins/shrine_plugin.rb

Overview

typed: false

Constant Summary

Constants inherited from SorbetRails::ModelPlugins::Base

SorbetRails::ModelPlugins::Base::Parameter

Instance Attribute Summary

Attributes inherited from SorbetRails::ModelPlugins::Base

#available_classes, #model_class

Instance Method Summary collapse

Methods inherited from SorbetRails::ModelPlugins::Base

#initialize, #serialization_coder_for_column

Methods included from SorbetRails::ModelUtils

#add_relation_query_method, #exists_class_method?, #exists_instance_method?, #habtm_class?, #model_assoc_proxy_class_name, #model_assoc_relation_class_name, #model_class_name, #model_module_name, #model_query_methods_returning_assoc_relation_module_name, #model_query_methods_returning_relation_module_name, #model_relation_class_name, #model_relation_type_alias, #model_relation_type_class_name

Methods included from SorbetRails::ModelColumnUtils

#active_record_type_to_sorbet_type, #attribute_has_unconditional_presence_validation?, #model_class, #nilable_column?, #time_zone_aware_column?, #type_for_column_def

Constructor Details

This class inherits a constructor from SorbetRails::ModelPlugins::Base

Instance Method Details

#generate(root) ⇒ Object



4
5
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sorbet-rails/gem_plugins/shrine_plugin.rb', line 4

def generate(root)
  model_rbi = root.create_class(model_class_name)
  processed_attachments = Set.new
  @model_class.included_modules.each do |included_module|
    # based on https://github.com/shrinerb/shrine/blob/master/lib/shrine/attachment.rb
    if included_module.is_a?(Shrine::Attachment)
      attachment_name = included_module.attachment_name.to_s
      next if processed_attachments.include?(attachment_name)
      processed_attachments.add(attachment_name)

      # TODO: detect nilability based on column nilability
      model_rbi.create_method(
        attachment_name,
        return_type: "T.nilable(#{included_module.shrine_class}::UploadedFile)",
      )
      model_rbi.create_method(
        "#{attachment_name}=",
        parameters: [
          ::Parlour::RbiGenerator::Parameter.new(
            "file",
            type:
              "T.any(String, T::Hash[T.untyped, T.untyped],
              #{included_module.shrine_class}::UploadedFile)".squish!,
          ),
        ],
      )
      model_rbi.create_method(
        "#{attachment_name}_attacher",
        return_type: "T.nilable(#{included_module.shrine_class}::Attacher)",
      )

      # remote_url plugin
      # https://github.com/shrinerb/shrine/blob/2b7fad37dbaa955a7d3b82e9e05eaacb3506b4ba/
      # ../lib/shrine/plugins/remote_url.rb
      if included_module.is_a?(Shrine::Plugins::RemoteUrl::AttachmentMethods)
        model_rbi.create_method(
          "#{attachment_name}_remote_url",
          return_type: "T.nilable(String)",
        )
        model_rbi.create_method(
          "#{attachment_name}_remote_url=",
          parameters: [
            ::Parlour::RbiGenerator::Parameter.new(
              "url",
              type: "T.untyped" # TODO likely T.nilable(String)
            ),
          ],
        )
      end

      #--
      attachment = Object.const_get("#{included_module.shrine_class}::Attachment")
      attachment_rbi = root.create_class(
        attachment.name,
        superclass: 'Shrine::Attachment',
      )
      attachment_rbi.create_method(
        "initialize",
        parameters: [
          ::Parlour::RbiGenerator::Parameter.new('name', type: 'T.any(String, Symbol)'),
          ::Parlour::RbiGenerator::Parameter.new('**option', type: 'T.untyped'),
        ]
      )
      #--
      attacher = Object.const_get("#{included_module.shrine_class}::Attacher")
      attacher_rbi = root.create_class(
        attacher.name,
        superclass: 'Shrine::Attacher',
      )
      #--
      uploaded_file = Object.const_get("#{included_module.shrine_class}::UploadedFile")
      uploaded_file_rbi = root.create_class(
        uploaded_file.name,
        superclass: 'Shrine::UploadedFile',
      )
    end
  end
end