Class: ActiveFlagPlugin

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

Overview

typed: strict

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

#active_flag_keys(model_class) ⇒ Object


43
44
45
# File 'lib/sorbet-rails/gem_plugins/active_flag_plugin.rb', line 43

def active_flag_keys(model_class)
  T.unsafe(model_class).active_flags.keys
end

#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
# File 'lib/sorbet-rails/gem_plugins/active_flag_plugin.rb', line 4

def generate(root)
  # method added here: https://github.com/kenn/active_flag/blob/master/lib/active_flag.rb#L13
  return unless model_class.respond_to?(:active_flags)

  module_name = self.model_module_name("GeneratedActiveFlagMethods")
  module_rbi = root.create_module(module_name)

  model_class_rbi = root.create_class(self.model_class_name)
  model_class_rbi.create_include(module_name)

  module_rbi.create_method(
    "active_flags",
    return_type: "T::Hash[Symbol, ActiveFlag::Definition]",
    class_method: true
  )

  active_flag_keys(model_class).each do |flag|
    module_rbi.create_method(
      flag.to_s,
      return_type: "::ActiveFlag::Value"
    )

    module_rbi.create_method(
      "#{flag}=",
      parameters: [
        Parameter.new("value", type: "T::Array[Symbol]")
      ],
      return_type: nil
    )

    module_rbi.create_method(
      flag.to_s.pluralize,
      return_type: "::ActiveFlag::Definition",
      class_method: true
    )
  end
end