Class: RailsAdmin::Adapters::Mongoid::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_admin/adapters/mongoid/association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(association, model) ⇒ Association

Returns a new instance of Association.



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

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/mongoid/association.rb', line 7

def association
  @association
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

Instance Method Details

#asObject



105
106
107
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 105

def as
  association.as.try :to_sym
end

#association?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 134

def association?
  true
end

#embeds?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 142

def embeds?
  %i[embeds_one embeds_many].include?(macro)
end

#field_typeObject



39
40
41
42
43
44
45
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 39

def field_type
  if polymorphic?
    :polymorphic_association
  else
    :"#{type}_association"
  end
end

#foreign_inverse_ofObject



86
87
88
89
90
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 86

def foreign_inverse_of
  return unless polymorphic? && %i[referenced_in belongs_to].include?(macro)

  inverse_of_field.try(:to_sym)
end

#foreign_keyObject



64
65
66
67
68
69
70
71
72
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 64

def foreign_key
  return if embeds?

  begin
    association.foreign_key.to_sym
  rescue StandardError
    nil
  end
end

#foreign_key_nullable?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 74

def foreign_key_nullable?
  return if foreign_key.nil?

  true
end

#foreign_typeObject



80
81
82
83
84
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 80

def foreign_type
  return unless polymorphic? && %i[referenced_in belongs_to].include?(macro)

  association.inverse_type.try(:to_sym) || :"#{name}_type"
end

#inverse_ofObject



113
114
115
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 113

def inverse_of
  association.inverse_of.try :to_sym
end

#key_accessorObject



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 92

def key_accessor
  case macro.to_sym
  when :has_many
    "#{name.to_s.singularize}_ids".to_sym
  when :has_one
    "#{name}_id".to_sym
  when :embedded_in, :embeds_one, :embeds_many
    nil
  else
    foreign_key
  end
end

#klassObject



47
48
49
50
51
52
53
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 47

def klass
  if polymorphic? && %i[referenced_in belongs_to].include?(macro)
    polymorphic_parents(:mongoid, association.inverse_class_name, name) || []
  else
    association.klass
  end
end

#macroObject



138
139
140
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 138

def macro
  association.try(:macro) || association.class.name.split('::').last.underscore.to_sym
end

#nameObject



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

def name
  association.name.to_sym
end

#nested_optionsObject



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 121

def nested_options
  nested = nested_attributes_options.try { |o| o[name] }
  if !nested && %i[embeds_one embeds_many].include?(macro.to_sym) && !cyclic?
    raise <<~MSG
      Embedded association without accepts_nested_attributes_for can't be handled by RailsAdmin,
      because embedded model doesn't have top-level access.
      Please add `accepts_nested_attributes_for :#{association.name}' line to `#{model}' model.
    MSG
  end

  nested
end

#polymorphic?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 109

def polymorphic?
  association.polymorphic? && %i[referenced_in belongs_to].include?(macro)
end

#pretty_nameObject



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

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

#primary_keyObject



55
56
57
58
59
60
61
62
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 55

def primary_key
  case type
  when :belongs_to, :has_and_belongs_to_many
    association.primary_key.to_sym
  else
    :_id
  end
end

#read_only?Boolean

Returns:

  • (Boolean)


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

def read_only?
  false
end

#typeObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 24

def type
  case macro.to_sym
  when :belongs_to, :referenced_in, :embedded_in
    :belongs_to
  when :has_one, :references_one, :embeds_one
    :has_one
  when :has_many, :references_many, :embeds_many
    :has_many
  when :has_and_belongs_to_many, :references_and_referenced_in_many
    :has_and_belongs_to_many
  else
    raise "Unknown association type: #{macro.inspect}"
  end
end