module Subroutine::AssociationFields::ClassMethods

Public Instance Methods

association(field_name, opts = {}) click to toggle source
# 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 = {}) click to toggle source

Other options:

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

# 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