Class: Subroutine::AssociationFields::Configuration

Inherits:
Fields::Configuration show all
Defined in:
lib/subroutine/association_fields/configuration.rb

Constant Summary

Constants inherited from Fields::Configuration

Fields::Configuration::NO_GROUPS, Fields::Configuration::PROTECTED_GROUP_IDENTIFIERS

Instance Attribute Summary

Attributes inherited from Fields::Configuration

#field_name

Instance Method Summary collapse

Methods inherited from Fields::Configuration

#bypass_indifferent_assignment?, #field_reader?, #field_writer?, from, #get_default, #groups, #has_default?, #in_group?, #inheritable_options, #initialize, #inspect, #mass_assignable?, #merge, #sanitize_options

Constructor Details

This class inherits a constructor from Subroutine::Fields::Configuration

Instance Method Details

#asObject



37
38
39
# File 'lib/subroutine/association_fields/configuration.rb', line 37

def as
  config[:as] || field_name
end

#behaviorObject



78
79
80
# File 'lib/subroutine/association_fields/configuration.rb', line 78

def behavior
  :association
end

#build_foreign_key_fieldObject



66
67
68
# File 'lib/subroutine/association_fields/configuration.rb', line 66

def build_foreign_key_field
  build_child_field(foreign_key_method, type: :foreign_key, foreign_key_type: -> { determine_foreign_key_type })
end

#build_foreign_type_fieldObject



70
71
72
# File 'lib/subroutine/association_fields/configuration.rb', line 70

def build_foreign_type_field
  build_child_field(foreign_type_method, type: :string)
end

#find_byObject



62
63
64
# File 'lib/subroutine/association_fields/configuration.rb', line 62

def find_by
  (config[:find_by] || :id).to_sym
end

#foreign_keyObject



50
51
52
# File 'lib/subroutine/association_fields/configuration.rb', line 50

def foreign_key
  config[:foreign_key]
end

#foreign_key_methodObject



54
55
56
# File 'lib/subroutine/association_fields/configuration.rb', line 54

def foreign_key_method
  (foreign_key || "#{field_name}_id").to_sym
end

#foreign_typeObject Also known as: class_name



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

def foreign_type
  (config[:foreign_type] || config[:class_name])&.to_s
end

#foreign_type_methodObject



58
59
60
# File 'lib/subroutine/association_fields/configuration.rb', line 58

def foreign_type_method
  foreign_key_method.to_s.gsub(/_id$/, "_type").to_sym
end

#inferred_foreign_typeObject



46
47
48
# File 'lib/subroutine/association_fields/configuration.rb', line 46

def inferred_foreign_type
  foreign_type || as.to_s.camelize
end

#polymorphic?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/subroutine/association_fields/configuration.rb', line 29

def polymorphic?
  !!config[:polymorphic]
end

#raise_on_miss?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/subroutine/association_fields/configuration.rb', line 33

def raise_on_miss?
  config[:raise_on_miss] != false
end


22
23
24
25
26
27
# File 'lib/subroutine/association_fields/configuration.rb', line 22

def related_field_names
  out = super
  out << foreign_key_method
  out << foreign_type_method if polymorphic?
  out
end

#required_modulesObject



18
19
20
# File 'lib/subroutine/association_fields/configuration.rb', line 18

def required_modules
  super + [::Subroutine::AssociationFields]
end

#unscoped?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/subroutine/association_fields/configuration.rb', line 74

def unscoped?
  !!config[:unscoped]
end

#validate!Object



10
11
12
13
14
15
16
# File 'lib/subroutine/association_fields/configuration.rb', line 10

def validate!
  super

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