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.


7
8
9
10
# File 'lib/rails_admin/adapters/mongoid/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/mongoid/association.rb', line 5

def association
  @association
end

#modelObject (readonly)

Returns the value of attribute model


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

def model
  @model
end

Instance Method Details

#asObject


67
68
69
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 67

def as
  association.as.try :to_sym
end

#association?Boolean

Returns:

  • (Boolean)

95
96
97
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 95

def association?
  true
end

#embeds?Boolean

Returns:

  • (Boolean)

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

def embeds?
  [:embeds_one, :embeds_many].include?(macro)
end

#foreign_inverse_ofObject


62
63
64
65
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 62

def foreign_inverse_of
  return unless polymorphic? && [:referenced_in, :belongs_to].include?(macro)
  inverse_of_field.try(:to_sym)
end

#foreign_keyObject


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

def foreign_key
  return if embeds?
  association.foreign_key.to_sym rescue nil
end

#foreign_key_nullable?Boolean

Returns:

  • (Boolean)

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

def foreign_key_nullable?
  return if foreign_key.nil?
  true
end

#foreign_typeObject


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

def foreign_type
  return unless polymorphic? && [:referenced_in, :belongs_to].include?(macro)
  association.inverse_type.try(:to_sym) || :"#{name}_type"
end

#inverse_ofObject


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

def inverse_of
  association.inverse_of.try :to_sym
end

#klassObject


35
36
37
38
39
40
41
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 35

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

#macroObject


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

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

#nameObject


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

def name
  association.name.to_sym
end

#nested_optionsObject


83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 83

def nested_options
  nested = nested_attributes_options.try { |o| o[name] }
  if !nested && [:embeds_one, :embeds_many].include?(macro.to_sym) && !cyclic?
    raise <<-MSG.gsub(/^\s+/, '')
    Embbeded 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)

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

def polymorphic?
  association.polymorphic? && [:referenced_in, :belongs_to].include?(macro)
end

#pretty_nameObject


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

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

#primary_keyObject


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

def primary_key
  association.primary_key.to_sym rescue :_id
end

#read_only?Boolean

Returns:

  • (Boolean)

79
80
81
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 79

def read_only?
  false
end

#typeObject


20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 20

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