Class: RailsAdmin::Adapters::ActiveRecord::Association
- Inherits:
-
Object
- Object
- RailsAdmin::Adapters::ActiveRecord::Association
show all
- Defined in:
- lib/rails_admin/adapters/active_record/association.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(association, model) ⇒ Association
Returns a new instance of Association.
9
10
11
12
|
# File 'lib/rails_admin/adapters/active_record/association.rb', line 9
def initialize(association, model)
@association = association
@model = model
end
|
Instance Attribute Details
#association ⇒ Object
Returns the value of attribute association.
7
8
9
|
# File 'lib/rails_admin/adapters/active_record/association.rb', line 7
def association
@association
end
|
#model ⇒ Object
Returns the value of attribute model.
7
8
9
|
# File 'lib/rails_admin/adapters/active_record/association.rb', line 7
def model
@model
end
|
Instance Method Details
#as ⇒ Object
82
83
84
|
# File 'lib/rails_admin/adapters/active_record/association.rb', line 82
def as
options[:as].try :to_sym
end
|
#association? ⇒ Boolean
104
105
106
|
# File 'lib/rails_admin/adapters/active_record/association.rb', line 104
def association?
true
end
|
#field_type ⇒ Object
26
27
28
29
30
31
32
|
# File 'lib/rails_admin/adapters/active_record/association.rb', line 26
def field_type
if polymorphic?
:polymorphic_association
else
:"#{association.macro}_association"
end
end
|
#foreign_inverse_of ⇒ Object
67
68
69
|
# File 'lib/rails_admin/adapters/active_record/association.rb', line 67
def foreign_inverse_of
nil
end
|
#foreign_key ⇒ Object
53
54
55
|
# File 'lib/rails_admin/adapters/active_record/association.rb', line 53
def foreign_key
association.foreign_key.to_sym
end
|
#foreign_key_nullable? ⇒ Boolean
57
58
59
60
61
|
# File 'lib/rails_admin/adapters/active_record/association.rb', line 57
def foreign_key_nullable?
return true if foreign_key.nil? || type != :has_many
(column = klass.columns_hash[foreign_key.to_s]).nil? || column.null
end
|
#foreign_type ⇒ Object
63
64
65
|
# File 'lib/rails_admin/adapters/active_record/association.rb', line 63
def foreign_type
options[:foreign_type].try(:to_sym) || :"#{name}_type" if options[:polymorphic]
end
|
#inverse_of ⇒ Object
90
91
92
|
# File 'lib/rails_admin/adapters/active_record/association.rb', line 90
def inverse_of
options[:inverse_of].try :to_sym
end
|
#key_accessor ⇒ Object
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/rails_admin/adapters/active_record/association.rb', line 71
def key_accessor
case type
when :has_many, :has_and_belongs_to_many
"#{name.to_s.singularize}_ids".to_sym
when :has_one
"#{name}_id".to_sym
else
foreign_key
end
end
|
#klass ⇒ Object
34
35
36
37
38
39
40
|
# File 'lib/rails_admin/adapters/active_record/association.rb', line 34
def klass
if options[:polymorphic]
polymorphic_parents(:active_record, association.active_record.name.to_s, name) || []
else
association.klass
end
end
|
#name ⇒ Object
14
15
16
|
# File 'lib/rails_admin/adapters/active_record/association.rb', line 14
def name
association.name.to_sym
end
|
#nested_options ⇒ Object
100
101
102
|
# File 'lib/rails_admin/adapters/active_record/association.rb', line 100
def nested_options
model.nested_attributes_options.try { |o| o[name.to_sym] }
end
|
#polymorphic? ⇒ Boolean
86
87
88
|
# File 'lib/rails_admin/adapters/active_record/association.rb', line 86
def polymorphic?
options[:polymorphic] || false
end
|
#pretty_name ⇒ Object
18
19
20
|
# File 'lib/rails_admin/adapters/active_record/association.rb', line 18
def pretty_name
name.to_s.tr('_', ' ').capitalize
end
|
#primary_key ⇒ Object
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/rails_admin/adapters/active_record/association.rb', line 42
def primary_key
return nil if polymorphic?
case type
when :has_one
association.klass.primary_key
else
association.association_primary_key
end.try(:to_sym)
end
|
#read_only? ⇒ Boolean
94
95
96
97
98
|
# File 'lib/rails_admin/adapters/active_record/association.rb', line 94
def read_only?
(klass.all.instance_exec(&scope).readonly_value if scope.is_a?(Proc) && scope.arity == 0) ||
association.nested? ||
false
end
|
#type ⇒ Object
22
23
24
|
# File 'lib/rails_admin/adapters/active_record/association.rb', line 22
def type
association.macro
end
|