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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#associationObject (readonly)

Returns the value of attribute association.



304
305
306
# File 'lib/rails_admin/adapters/mongoid.rb', line 304

def association
  @association
end

#modelObject (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

#asObject



363
364
365
# File 'lib/rails_admin/adapters/mongoid.rb', line 363

def as
  association.as.try :to_sym
end

#association?Boolean

Returns:

  • (Boolean)


391
392
393
# File 'lib/rails_admin/adapters/mongoid.rb', line 391

def association?
  true
end

#foreign_inverse_ofObject



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_keyObject



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_typeObject



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_ofObject



371
372
373
# File 'lib/rails_admin/adapters/mongoid.rb', line 371

def inverse_of
  association.inverse_of.try :to_sym
end

#klassObject



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

#nameObject



310
311
312
# File 'lib/rails_admin/adapters/mongoid.rb', line 310

def name
  association.name.to_sym
end

#nested_optionsObject



379
380
381
382
383
384
385
386
387
388
389
# File 'lib/rails_admin/adapters/mongoid.rb', line 379

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

Returns:

  • (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_nameObject



314
315
316
# File 'lib/rails_admin/adapters/mongoid.rb', line 314

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

#primary_keyObject



341
342
343
# File 'lib/rails_admin/adapters/mongoid.rb', line 341

def primary_key
  :_id
end

#read_only?Boolean

Returns:

  • (Boolean)


375
376
377
# File 'lib/rails_admin/adapters/mongoid.rb', line 375

def read_only?
  false
end

#typeObject



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