Class: Subroutine::AssociationFields::Configuration
Constant Summary
Fields::Configuration::NO_GROUPS, Fields::Configuration::PROTECTED_GROUP_IDENTIFIERS
Instance Attribute Summary
#field_name
Instance Method Summary
collapse
#bypass_indifferent_assignment?, #field_reader?, #field_writer?, from, #get_default, #groups, #has_default?, #in_group?, #inheritable_options, #initialize, #inspect, #mass_assignable?, #merge, #sanitize_options
Instance Method Details
#as ⇒ Object
37
38
39
|
# File 'lib/subroutine/association_fields/configuration.rb', line 37
def as
config[:as] || field_name
end
|
#behavior ⇒ Object
78
79
80
|
# File 'lib/subroutine/association_fields/configuration.rb', line 78
def behavior
:association
end
|
#build_foreign_key_field ⇒ Object
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_field ⇒ Object
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_by ⇒ Object
62
63
64
|
# File 'lib/subroutine/association_fields/configuration.rb', line 62
def find_by
(config[:find_by] || :id).to_sym
end
|
#foreign_key ⇒ Object
50
51
52
|
# File 'lib/subroutine/association_fields/configuration.rb', line 50
def foreign_key
config[:foreign_key]
end
|
#foreign_key_method ⇒ Object
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_type ⇒ Object
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_method ⇒ Object
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_type ⇒ Object
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
29
30
31
|
# File 'lib/subroutine/association_fields/configuration.rb', line 29
def polymorphic?
!!config[:polymorphic]
end
|
#raise_on_miss? ⇒ 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_modules ⇒ Object
18
19
20
|
# File 'lib/subroutine/association_fields/configuration.rb', line 18
def required_modules
super + [::Subroutine::AssociationFields]
end
|
#unscoped? ⇒ 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
|