diff --git a/lib/rails_admin/adapters/mongoid.rb b/lib/rails_admin/adapters/mongoid.rb index be5adc16e2..3bd4857be6 100644 --- a/lib/rails_admin/adapters/mongoid.rb +++ b/lib/rails_admin/adapters/mongoid.rb @@ -8,6 +8,19 @@ module RailsAdmin module Adapters module Mongoid DISABLED_COLUMN_TYPES = ['Range', 'Moped::BSON::Binary', 'BSON::Binary', 'Mongoid::Geospatial::Point'] + OBJECT_ID ||= begin + if defined?(Moped::BSON) + Moped::BSON::ObjectId + elsif defined?(BSON::ObjectId) + BSON::ObjectId + end + end + + def parse_object_id(value) + OBJECT_ID.from_string(value) + rescue BSON::ObjectId::Invalid, BSON::InvalidObjectId, Moped::Errors::InvalidObjectId + nil + end def new(params = {}) AbstractObject.new(model.new(params)) diff --git a/lib/rails_admin/adapters/mongoid/abstract_object.rb b/lib/rails_admin/adapters/mongoid/abstract_object.rb index 886a91fddf..b1dd1dbe36 100644 --- a/lib/rails_admin/adapters/mongoid/abstract_object.rb +++ b/lib/rails_admin/adapters/mongoid/abstract_object.rb @@ -1,4 +1,5 @@ require 'rails_admin/adapters/active_record/abstract_object' + module RailsAdmin module Adapters module Mongoid diff --git a/lib/rails_admin/config/fields/types/bson_object_id.rb b/lib/rails_admin/config/fields/types/bson_object_id.rb index 97e54b6ac9..59f3077ee0 100644 --- a/lib/rails_admin/config/fields/types/bson_object_id.rb +++ b/lib/rails_admin/config/fields/types/bson_object_id.rb @@ -8,14 +8,6 @@ class BsonObjectId < RailsAdmin::Config::Fields::Types::String # Register field type for the type loader RailsAdmin::Config::Fields::Types.register(self) - OBJECT_ID ||= begin - if defined?(Moped::BSON) - Moped::BSON::ObjectId - elsif defined?(BSON::ObjectId) - BSON::ObjectId - end - end - register_instance_option :label do label = ((@label ||= {})[::I18n.locale] ||= abstract_model.model.human_attribute_name name) label = 'Id' if label == '' @@ -35,13 +27,7 @@ def generic_help end def parse_value(value) - value.present? ? OBJECT_ID.from_string(value) : nil - rescue BSON::ObjectId::Invalid - nil - rescue => e - unless ['BSON::InvalidObjectId', 'Moped::Errors::InvalidObjectId'].include? e.class.to_s - raise e - end + value.present? ? abstract_model.parse_object_id(value) : nil end def parse_input(params) diff --git a/spec/dummy_app/app/mongoid/field_test.rb b/spec/dummy_app/app/mongoid/field_test.rb index 6b22d39809..ec23cf6764 100644 --- a/spec/dummy_app/app/mongoid/field_test.rb +++ b/spec/dummy_app/app/mongoid/field_test.rb @@ -12,7 +12,7 @@ class FieldTest field :array_field, type: Array field :big_decimal_field, type: BigDecimal field :boolean_field, type: Boolean - field :bson_object_id_field, type: RailsAdmin::Config::Fields::Types::BsonObjectId::OBJECT_ID + field :bson_object_id_field, type: RailsAdmin::Adapters::Mongoid::OBJECT_ID field :bson_binary_field, type: BSON::Binary field :date_field, type: Date field :datetime_field, type: DateTime diff --git a/spec/rails_admin/config/fields/types/bson_object_id_spec.rb b/spec/rails_admin/config/fields/types/bson_object_id_spec.rb index 24ed287ce5..1d895e1931 100644 --- a/spec/rails_admin/config/fields/types/bson_object_id_spec.rb +++ b/spec/rails_admin/config/fields/types/bson_object_id_spec.rb @@ -2,4 +2,25 @@ describe RailsAdmin::Config::Fields::Types::BsonObjectId do it_behaves_like 'a generic field type', :string_field, :bson_object_id + + describe '#parse_value' do + let(:bson) { RailsAdmin::Adapters::Mongoid::OBJECT_ID.new } + let(:field) do + RailsAdmin.config(FieldTest).fields.detect do |f| + f.name == :bson_object_id_field + end + end + + before :each do + RailsAdmin.config do |config| + config.model FieldTest do + field :bson_object_id_field, :bson_object_id + end + end + end + + it 'parse valid bson_object_id', mongoid: true do + expect(field.parse_value(bson.to_s)).to eq bson + end + end end