Module: Subroutine::AssociationFields::ClassMethods

Defined in:
lib/subroutine/association_fields.rb

Instance Method Summary collapse

Instance Method Details

#association(field_name, opts = {}) ⇒ Object



41
42
43
# File 'lib/subroutine/association_fields.rb', line 41

def association(field_name, opts = {})
  field(field_name, opts.merge(type: :association))
end

#field_with_association(field_name, options = {}) ⇒ Object

Other options:

  • unscoped => set true if the record should be looked up via Type.unscoped



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/subroutine/association_fields.rb', line 67

def field_with_association(field_name, options = {})
  if options[:type]&.to_sym == :association
    config = ::Subroutine::AssociationFields::Configuration.new(field_name, options)

    if config.polymorphic?
      field config.foreign_type_method, config.build_foreign_type_field
    else
      class_eval <<-EV, __FILE__, __LINE__ + 1
        try(:silence_redefinition_of_method, :#{config.foreign_type_method})
        def #{config.foreign_type_method}
          #{config.inferred_class_name.inspect}
        end
      EV
    end

    field config.foreign_key_method, config.build_foreign_key_field

    field_without_association(config.as, config)
  else
    field_without_association(field_name, options)
  end
end