Module: Subroutine::AssociationFields::ClassMethods

Defined in:
lib/subroutine/association_fields.rb

Instance Method Summary collapse

Instance Method Details

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



39
40
41
# File 'lib/subroutine/association_fields.rb', line 39

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



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

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
        silence_redefinition_of_method def #{config.foreign_type_method}
          #{config.inferred_foreign_type.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