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.



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
39
# 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 <<-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
          if persisted?
            #{name}.substitute items
          else
            items.each do |item|
              item.update_attribute('#{association.foreign_key}', id)
            end
          end
        end
RUBY
    elsif [:has_one, :references_one].include? association.macro
      instance_eval <<-RUBY, __FILE__, __LINE__ + 1
        def #{name.to_s}_id=(item_id)
          item = (#{association.klass}.find(item_id) rescue nil)
          if persisted?
            self.#{name} = item
          else
            item.update_attribute('#{association.foreign_key}', id) if item
          end
        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

Instance Method Details

#destroyObject



41
42
43
44
# File 'lib/rails_admin/adapters/mongoid/abstract_object.rb', line 41

def destroy
  object.destroy
  object
end