Sha256: 13a7f1b727db54b1a65b10a324ac518533eb330ddcc5028c7523b7823f4a7673

Contents?: true

Size: 1.53 KB

Versions: 9

Compression:

Stored size: 1.53 KB

Contents

require 'rails_admin/adapters/active_record/abstract_object'
module RailsAdmin
  module Adapters
    module Mongoid
      class AbstractObject < RailsAdmin::Adapters::ActiveRecord::AbstractObject
        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
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
rails_admin-0.4.7 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-0.4.6 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-0.4.5 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-0.4.4 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-lbgraham-0.4.2 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-0.4.3 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-0.4.2 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-0.4.1 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-0.4.0 lib/rails_admin/adapters/mongoid/abstract_object.rb