Class: RailsAdmin::Adapters::ActiveRecord::Association

Inherits:
Object
  • Object
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


7
8
9
10
# File 'lib/rails_admin/adapters/active_record/association.rb', line 7

def initialize(association, model)
  @association = association
  @model = model
end

Instance Attribute Details

#associationObject (readonly)

Returns the value of attribute association


5
6
7
# File 'lib/rails_admin/adapters/active_record/association.rb', line 5

def association
  @association
end

#modelObject (readonly)

Returns the value of attribute model


5
6
7
# File 'lib/rails_admin/adapters/active_record/association.rb', line 5

def model
  @model
end

Instance Method Details

#asObject


48
49
50
# File 'lib/rails_admin/adapters/active_record/association.rb', line 48

def as
  options[:as].try :to_sym
end

#association?Boolean

Returns:

  • (Boolean)

70
71
72
# File 'lib/rails_admin/adapters/active_record/association.rb', line 70

def association?
  true
end

#foreign_inverse_ofObject


44
45
46
# File 'lib/rails_admin/adapters/active_record/association.rb', line 44

def foreign_inverse_of
  nil
end

#foreign_keyObject


36
37
38
# File 'lib/rails_admin/adapters/active_record/association.rb', line 36

def foreign_key
  association.foreign_key.to_sym
end

#foreign_typeObject


40
41
42
# File 'lib/rails_admin/adapters/active_record/association.rb', line 40

def foreign_type
  options[:foreign_type].try(:to_sym) || :"#{name}_type" if options[:polymorphic]
end

#inverse_ofObject


56
57
58
# File 'lib/rails_admin/adapters/active_record/association.rb', line 56

def inverse_of
  options[:inverse_of].try :to_sym
end

#klassObject


24
25
26
27
28
29
30
# File 'lib/rails_admin/adapters/active_record/association.rb', line 24

def klass
  if options[:polymorphic]
    polymorphic_parents(:active_record, model.name.to_s, name) || []
  else
    association.klass
  end
end

#nameObject


12
13
14
# File 'lib/rails_admin/adapters/active_record/association.rb', line 12

def name
  association.name.to_sym
end

#nested_optionsObject


66
67
68
# File 'lib/rails_admin/adapters/active_record/association.rb', line 66

def nested_options
  model.nested_attributes_options.try { |o| o[name.to_sym] }
end

#polymorphic?Boolean

Returns:

  • (Boolean)

52
53
54
# File 'lib/rails_admin/adapters/active_record/association.rb', line 52

def polymorphic?
  options[:polymorphic] || false
end

#pretty_nameObject


16
17
18
# File 'lib/rails_admin/adapters/active_record/association.rb', line 16

def pretty_name
  name.to_s.tr('_', ' ').capitalize
end

#primary_keyObject


32
33
34
# File 'lib/rails_admin/adapters/active_record/association.rb', line 32

def primary_key
  (options[:primary_key] || association.klass.primary_key).try(:to_sym) unless polymorphic?
end

#read_only?Boolean

Returns:

  • (Boolean)

60
61
62
63
64
# File 'lib/rails_admin/adapters/active_record/association.rb', line 60

def read_only?
  (klass.all.instance_eval(&scope).readonly_value if scope.is_a? Proc) ||
    association.nested? ||
    false
end

#typeObject


20
21
22
# File 'lib/rails_admin/adapters/active_record/association.rb', line 20

def type
  association.macro
end