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


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

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

#association?Boolean

Returns:

  • (Boolean)

75
76
77
# File 'lib/rails_admin/adapters/active_record/association.rb', line 75

def association?
  true
end

#foreign_inverse_ofObject


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

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_key_nullable?Boolean

Returns:

  • (Boolean)

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

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_typeObject


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

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

#inverse_ofObject


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

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


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

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

#polymorphic?Boolean

Returns:

  • (Boolean)

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

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)

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

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