Class: RailsAdmin::Adapters::Mongoid::Association
- Inherits:
-
Object
- Object
- RailsAdmin::Adapters::Mongoid::Association
- Defined in:
- lib/rails_admin/adapters/mongoid/association.rb
Instance Attribute Summary collapse
-
#association ⇒ Object
readonly
Returns the value of attribute association.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #as ⇒ Object
- #association? ⇒ Boolean
- #embeds? ⇒ Boolean
- #field_type ⇒ Object
- #foreign_inverse_of ⇒ Object
- #foreign_key ⇒ Object
- #foreign_key_nullable? ⇒ Boolean
- #foreign_type ⇒ Object
-
#initialize(association, model) ⇒ Association
constructor
A new instance of Association.
- #inverse_of ⇒ Object
- #key_accessor ⇒ Object
- #klass ⇒ Object
- #macro ⇒ Object
- #name ⇒ Object
- #nested_options ⇒ Object
- #polymorphic? ⇒ Boolean
- #pretty_name ⇒ Object
- #primary_key ⇒ Object
- #read_only? ⇒ Boolean
- #type ⇒ Object
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
#association ⇒ Object (readonly)
Returns the value of attribute association.
7 8 9 |
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 7 def association @association end |
#model ⇒ Object (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
#as ⇒ Object
105 106 107 |
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 105 def as association.as.try :to_sym end |
#association? ⇒ Boolean
134 135 136 |
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 134 def association? true end |
#embeds? ⇒ Boolean
142 143 144 |
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 142 def %i[embeds_one embeds_many].include?(macro) end |
#field_type ⇒ Object
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_of ⇒ Object
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_key ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 64 def foreign_key return if begin association.foreign_key.to_sym rescue StandardError nil end end |
#foreign_key_nullable? ⇒ 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_type ⇒ Object
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_of ⇒ Object
113 114 115 |
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 113 def inverse_of association.inverse_of.try :to_sym end |
#key_accessor ⇒ Object
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" when :has_one :"#{name}_id" when :embedded_in, :embeds_one, :embeds_many nil else foreign_key end end |
#klass ⇒ Object
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 |
#macro ⇒ Object
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 |
#name ⇒ Object
16 17 18 |
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 16 def name association.name.to_sym end |
#nested_options ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 121 def nested = .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
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_name ⇒ Object
20 21 22 |
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 20 def pretty_name name.to_s.tr('_', ' ').capitalize end |
#primary_key ⇒ Object
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
117 118 119 |
# File 'lib/rails_admin/adapters/mongoid/association.rb', line 117 def read_only? false end |
#type ⇒ Object
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 |