Class: RailsAdmin::Adapters::Mongoid::AbstractObject

Inherits:
ActiveRecord::AbstractObject show all
Defined in:
lib/rails_admin/adapters/mongoid/abstract_object.rb

Instance Attribute Summary

Attributes inherited from ActiveRecord::AbstractObject

#object

Instance Method Summary collapse

Methods inherited from ActiveRecord::AbstractObject

#method_missing, #save, #set_attributes

Constructor Details

#initialize(object) ⇒ AbstractObject


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rails_admin/adapters/mongoid/abstract_object.rb', line 6

def initialize(object)
  super
  object.associations.each do |name, association|
    if [:has_many, :references_many].include? association.macro
      instance_eval "def \#{name.to_s.singularize}_ids\n\#{name}.map{|item| item.id }\nend\n\ndef \#{name.to_s.singularize}_ids=(item_ids)\n__items__ = Array.wrap(item_ids).map{|item_id| \#{name}.klass.find(item_id) rescue nil }.compact\nunless persisted?\n__items__.each do |item|\nitem.update_attribute('\#{association.foreign_key}', id)\nend\nend\nsuper __items__.map(&:id)\nend\n", __FILE__, __LINE__ + 1
    elsif [:has_one, :references_one].include? association.macro
      instance_eval "def \#{name}_id=(item_id)\nitem = (\#{association.klass}.find(item_id) rescue nil)\nreturn unless item\nunless persisted?\nitem.update_attribute('\#{association.foreign_key}', id)\nend\nsuper item.id\nend\n", __FILE__, __LINE__ + 1
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RailsAdmin::Adapters::ActiveRecord::AbstractObject