Sha256: 867861e441c582d75ae93c13f63373323cee9f4016308f2bf3fba8da8c1fe74e

Contents?: true

Size: 1011 Bytes

Versions: 8

Compression:

Stored size: 1011 Bytes

Contents

# frozen_string_literal: true

module RailsAdmin
  module Adapters
    module Mongoid
      module ObjectExtension
        def self.extended(object)
          object.associations.each do |name, association|
            association = Association.new(association, object.class)
            case association.macro
            when :has_many
              unless association.autosave?
                object.singleton_class.after_create do
                  send(name).each(&:save)
                end
              end
            when :has_one
              unless association.autosave?
                object.singleton_class.after_create do
                  send(name)&.save
                end
              end
              object.instance_eval <<-RUBY, __FILE__, __LINE__ + 1
                def #{name}_id=(item_id)
                  self.#{name} = (#{association.klass}.find(item_id) rescue nil)
                end
              RUBY
            end
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rails_admin-3.1.4 lib/rails_admin/adapters/mongoid/object_extension.rb
rails_admin-3.1.3 lib/rails_admin/adapters/mongoid/object_extension.rb
rails_admin-3.1.2 lib/rails_admin/adapters/mongoid/object_extension.rb
rails_admin-3.1.1 lib/rails_admin/adapters/mongoid/object_extension.rb
rails_admin-3.1.0 lib/rails_admin/adapters/mongoid/object_extension.rb
rails_admin-3.1.0.rc2 lib/rails_admin/adapters/mongoid/object_extension.rb
rails_admin-3.1.0.rc lib/rails_admin/adapters/mongoid/object_extension.rb
rails_admin-3.1.0.beta lib/rails_admin/adapters/mongoid/object_extension.rb