Sha256: c33fb5c5830a1d2d4fd4f0d8b77d1f159e84114d554b9f428467b2a38712dc7c

Contents?: true

Size: 1.78 KB

Versions: 6

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module Orthoses
  module ActiveRecord
    class BelongsTo
      def initialize(loader)
        @loader = loader
      end

      def call
        @loader.call.tap do |store|
          ::ActiveRecord::Base.descendants.each do |base|
            next if base.abstract_class?
            base_name = Orthoses::Utils.module_name(base) || next

            lines = base.reflect_on_all_associations(:belongs_to).flat_map do |ref|
              # FIXME: Can I get list of class for polymorphic?
              type = if ref.polymorphic?
                'untyped'
              else
                Orthoses::ActiveRecord.reflection_klass_name(ref) or next
              end
              opt = "#{type}?"

              [
                "def #{ref.name}: () -> #{opt}",
                "def #{ref.name}=: (#{opt}) -> #{opt}",
                "def reload_#{ref.name}: () -> #{opt}",
              ].tap do |ary|
                if !ref.polymorphic?
                  ary << "def build_#{ref.name}: (?untyped attributes) ?{ (#{type}) -> void } -> #{type}"
                  ary << "def create_#{ref.name}: (?untyped attributes) ?{ (#{type}) -> void } -> #{type}"
                  ary << "def create_#{ref.name}!: (?untyped attributes) ?{ (#{type}) -> void } -> #{type}"
                end
              end
            end

            generated_association_methods = "#{base_name}::GeneratedAssociationMethods"

            store[generated_association_methods].tap do |content|
              content.header = "module #{generated_association_methods}"
              content.concat(lines)
            end

            sig = "include #{generated_association_methods}"
            store[base_name] << sig if !store[base_name].body.include?(sig)
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
orthoses-rails-1.7.0 lib/orthoses/active_record/belongs_to.rb
orthoses-rails-1.6.0 lib/orthoses/active_record/belongs_to.rb
orthoses-rails-1.5.0 lib/orthoses/active_record/belongs_to.rb
orthoses-rails-1.4.0 lib/orthoses/active_record/belongs_to.rb
orthoses-rails-1.3.0 lib/orthoses/active_record/belongs_to.rb
orthoses-rails-1.2.0 lib/orthoses/active_record/belongs_to.rb