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


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

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


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

def as
  association.as.try :to_sym
end

#association?Boolean


89
90
91
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 89

def association?
  true
end

#foreign_inverse_ofObject


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

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

#foreign_keyObject


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

def foreign_key
  return unless [:embeds_one, :embeds_many].exclude?(macro.to_sym)
  association.foreign_key.to_sym rescue nil
end

#foreign_typeObject


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

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

#inverse_ofObject


69
70
71
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 69

def inverse_of
  association.inverse_of.try :to_sym
end

#klassObject


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

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

#nameObject


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

def name
  association.name.to_sym
end

#nested_optionsObject


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 77

def nested_options
  nested = nested_attributes_options.try { |o| o[name] }
  if !nested && [:embeds_one, :embeds_many].include?(macro.to_sym) && !association.cyclic
    fail <<-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


65
66
67
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 65

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

#pretty_nameObject


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

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

#primary_keyObject


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

def primary_key
  :_id
end

#read_only?Boolean


73
74
75
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 73

def read_only?
  false
end

#typeObject


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

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
    fail("Unknown association type: #{macro.inspect}")
  end
end