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

Returns a new instance of AbstractObject.



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 7

def initialize(object)
  super
  object.associations.each do |name, association|
    association = Association.new(association, object.class)
    if [:has_many, :references_many].include? association.macro
      instance_eval <<-RUBY, __FILE__, __LINE__ + 1
        def #{name.to_s.singularize}_ids
          #{name}.map{|item| item.id }
        end

        def #{name.to_s.singularize}_ids=(item_ids)
          __items__ = Array.wrap(item_ids).map{|item_id| #{name}.klass.find(item_id) rescue nil }.compact
          unless persisted?
            __items__.each do |item|
              item.update_attribute('#{association.foreign_key}', id)
            end
          end
          super __items__.map(&:id)
        end
RUBY
    elsif [:has_one, :references_one].include? association.macro
      instance_eval <<-RUBY, __FILE__, __LINE__ + 1
        def #{name}_id=(item_id)
          item = (#{association.klass}.find(item_id) rescue nil)
          return unless item
          item.update_attribute('#{association.foreign_key}', id) unless persisted?
          super item.id
        end
RUBY
    end
  end
end

Dynamic Method Handling

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