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.



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

#associationObject (readonly)

Returns the value of attribute association.



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

def association
  @association
end

#modelObject (readonly)

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

#asObject



99
100
101
# File 'lib/rails_admin/adapters/active_record/association.rb', line 99

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

#association?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/rails_admin/adapters/active_record/association.rb', line 121

def association?
  true
end

#field_typeObject



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_ofObject



80
81
82
# File 'lib/rails_admin/adapters/active_record/association.rb', line 80

def foreign_inverse_of
  nil
end

#foreign_keyObject



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

def foreign_key
  if association.options[:query_constraints].present?
    association.options[:query_constraints].map(&:to_sym)
  elsif association.foreign_key.is_a?(Array)
    association.foreign_key.map(&:to_sym)
  else
    association.foreign_key.to_sym
  end
end

#foreign_key_nullable?Boolean

Returns:

  • (Boolean)


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

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



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

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

#inverse_ofObject



107
108
109
# File 'lib/rails_admin/adapters/active_record/association.rb', line 107

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

#key_accessorObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rails_admin/adapters/active_record/association.rb', line 84

def key_accessor
  case type
  when :has_many, :has_and_belongs_to_many
    :"#{name.to_s.singularize}_ids"
  when :has_one
    :"#{name}_id"
  else
    if foreign_key.is_a?(Array)
      :"#{name}_id"
    else
      foreign_key
    end
  end
end

#klassObject



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

#nameObject



14
15
16
# File 'lib/rails_admin/adapters/active_record/association.rb', line 14

def name
  association.name.to_sym
end

#nested_optionsObject



117
118
119
# File 'lib/rails_admin/adapters/active_record/association.rb', line 117

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

#polymorphic?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/rails_admin/adapters/active_record/association.rb', line 103

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

#pretty_nameObject



18
19
20
# File 'lib/rails_admin/adapters/active_record/association.rb', line 18

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

#primary_keyObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rails_admin/adapters/active_record/association.rb', line 42

def primary_key
  return nil if polymorphic?

  value =
    case type
    when :has_one
      association.klass.primary_key
    else
      association.association_primary_key
    end

  if value.is_a? Array
    :id
  else
    value.to_sym
  end
end

#read_only?Boolean

Returns:

  • (Boolean)


111
112
113
114
115
# File 'lib/rails_admin/adapters/active_record/association.rb', line 111

def read_only?
  (klass.all.instance_exec(&scope).readonly_value if scope.is_a?(Proc) && scope.arity == 0) ||
    association.nested? ||
    false
end

#typeObject



22
23
24
# File 'lib/rails_admin/adapters/active_record/association.rb', line 22

def type
  association.macro
end