Class: Orthoses::ActiveStorage::Attached::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/orthoses/active_storage/attached/model.rb

Overview

<= 6.0

def has_one_attached(name, dependent: :purge_later)

>= 6.1

def has_one_attached(name, dependent: :purge_later, service: nil, strict_loading: false)

Instance Method Summary collapse

Constructor Details

#initialize(loader) ⇒ Model

Returns a new instance of Model.



11
12
13
# File 'lib/orthoses/active_storage/attached/model.rb', line 11

def initialize(loader)
  @loader = loader
end

Instance Method Details

#callObject



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
# File 'lib/orthoses/active_storage/attached/model.rb', line 15

def call
  store = @loader.call

  ::ActiveRecord::Base.descendants.each do |base|
    next if base.abstract_class?
    next if base.reflect_on_all_attachments.empty?

    base_name = Utils.module_name(base) or next
    base.reflect_on_all_attachments.each do |reflection|
      type =
        case reflection
        when ::ActiveStorage::Reflection::HasOneAttachedReflection
          "ActiveStorage::Attached::One"
        when ::ActiveStorage::Reflection::HasManyAttachedReflection
          "ActiveStorage::Attached::Many"
        else
          "untyped"
        end
      name = reflection.name

      store[base_name].tap do |content|
        content << "def #{name}: () -> #{type}"
        content << "def #{name}=: (untyped attachable) -> untyped"
      end
    end
  end

  store
end