class Subroutine::AssociationFields::Configuration

Public Instance Methods

as() click to toggle source
# File lib/subroutine/association_fields/configuration.rb, line 33
def as
  config[:as] || field_name
end
behavior() click to toggle source
# File lib/subroutine/association_fields/configuration.rb, line 69
def behavior
  :association
end
build_foreign_key_field() click to toggle source
# File lib/subroutine/association_fields/configuration.rb, line 57
def build_foreign_key_field
  build_child_field(foreign_key_method, type: :integer)
end
build_foreign_type_field() click to toggle source
# File lib/subroutine/association_fields/configuration.rb, line 61
def build_foreign_type_field
  build_child_field(foreign_type_method, type: :string)
end
class_name() click to toggle source
# File lib/subroutine/association_fields/configuration.rb, line 37
def class_name
  config[:class_name]&.to_s
end
foreign_key() click to toggle source
# File lib/subroutine/association_fields/configuration.rb, line 45
def foreign_key
  config[:foreign_key]
end
foreign_key_method() click to toggle source
# File lib/subroutine/association_fields/configuration.rb, line 49
def foreign_key_method
  (foreign_key || "#{field_name}_id").to_sym
end
foreign_type_method() click to toggle source
# File lib/subroutine/association_fields/configuration.rb, line 53
def foreign_type_method
  foreign_key_method.to_s.gsub(/_id$/, "_type").to_sym
end
inferred_class_name() click to toggle source
# File lib/subroutine/association_fields/configuration.rb, line 41
def inferred_class_name
  class_name || as.to_s.camelize
end
polymorphic?() click to toggle source
# File lib/subroutine/association_fields/configuration.rb, line 29
def polymorphic?
  !!config[:polymorphic]
end
required_modules() click to toggle source
# File lib/subroutine/association_fields/configuration.rb, line 18
def required_modules
  super + [::Subroutine::AssociationFields]
end
unscoped?() click to toggle source
# File lib/subroutine/association_fields/configuration.rb, line 65
def unscoped?
  !!config[:unscoped]
end
validate!() click to toggle source
# File lib/subroutine/association_fields/configuration.rb, line 10
def validate!
  super

  if as && foreign_key
    raise ArgumentError, ":as and :foreign_key options should not be provided together to an association invocation"
  end
end

Protected Instance Methods

build_child_field(name, opts = {}) click to toggle source
# File lib/subroutine/association_fields/configuration.rb, line 75
def build_child_field(name, opts = {})
  ComponentConfiguration.new(name, inheritable_options.merge(opts).merge(association_name: as))
end