Sha256: 222c52a84ac8f2dd13eb0c134154cace411d20d9f88274544cc798571a34e9f6

Contents?: true

Size: 1.53 KB

Versions: 15

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|
            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
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
rails_admin-2.3.1 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-2.3.0 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-2.2.1 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-2.2.0 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-2.1.1 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-2.1.0 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-2.0.2 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-1.4.3 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-2.0.1 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-2.0.0 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-2.0.0.rc lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-2.0.0.beta lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-1.4.2 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-1.4.1 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-1.4.0 lib/rails_admin/adapters/mongoid/abstract_object.rb