Class: RailsAdmin::Adapters::Mongoid::Association
- Inherits:
-
Object
- Object
- RailsAdmin::Adapters::Mongoid::Association
- Defined in:
- lib/rails_admin/adapters/mongoid.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
- #foreign_inverse_of ⇒ Object
- #foreign_key ⇒ Object
- #foreign_type ⇒ Object
-
#initialize(association, model) ⇒ Association
constructor
A new instance of Association.
- #inverse_of ⇒ Object
- #klass ⇒ 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.
305 306 307 308 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 305 def initialize(association, model) @association = association @model = model end |
Instance Attribute Details
#association ⇒ Object (readonly)
Returns the value of attribute association.
304 305 306 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 304 def association @association end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
304 305 306 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 304 def model @model end |
Instance Method Details
#as ⇒ Object
363 364 365 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 363 def as association.as.try :to_sym end |
#association? ⇒ Boolean
391 392 393 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 391 def association? true end |
#foreign_inverse_of ⇒ Object
357 358 359 360 361 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 357 def foreign_inverse_of if polymorphic? && [:referenced_in, :belongs_to].include?(macro) inverse_of_field.try(:to_sym) end end |
#foreign_key ⇒ Object
345 346 347 348 349 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 345 def foreign_key if [:embeds_one, :embeds_many].exclude?(macro.to_sym) association.foreign_key.to_sym rescue nil end end |
#foreign_type ⇒ Object
351 352 353 354 355 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 351 def foreign_type if polymorphic? && [:referenced_in, :belongs_to].include?(macro) association.inverse_type.try(:to_sym) || :"#{name}_type" end end |
#inverse_of ⇒ Object
371 372 373 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 371 def inverse_of association.inverse_of.try :to_sym end |
#klass ⇒ Object
333 334 335 336 337 338 339 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 333 def klass if polymorphic? && [:referenced_in, :belongs_to].include?(macro) polymorphic_parents(:mongoid, model.name, name) || [] else association.klass end end |
#name ⇒ Object
310 311 312 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 310 def name association.name.to_sym end |
#nested_options ⇒ Object
379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 379 def nested = .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
367 368 369 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 367 def polymorphic? association.polymorphic? && [:referenced_in, :belongs_to].include?(macro) end |
#pretty_name ⇒ Object
314 315 316 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 314 def pretty_name name.to_s.tr('_', ' ').capitalize end |
#primary_key ⇒ Object
341 342 343 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 341 def primary_key :_id end |
#read_only? ⇒ Boolean
375 376 377 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 375 def read_only? false end |
#type ⇒ Object
318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/rails_admin/adapters/mongoid.rb', line 318 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 |