Sha256: 8d390c559a7a0dc01fde4440a58a983daf202c67a049a179a529c6f77cdc2f12

Contents?: true

Size: 1.51 KB

Versions: 10

Compression:

Stored size: 1.51 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
                  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
                  unless persisted?
                    item.update_attribute('#{association.foreign_key}', id)
                  end
                  super item.id
                end
RUBY
            end
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
rails_admin-0.8.1 lib/rails_admin/adapters/mongoid/abstract_object.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/rails_admin-0.7.0/lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-0.7.0 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-0.6.8 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-0.6.7 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-0.6.6 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-0.6.5 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-0.6.4 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-0.6.3 lib/rails_admin/adapters/mongoid/abstract_object.rb
rails_admin-0.6.2 lib/rails_admin/adapters/mongoid/abstract_object.rb