Skip to content

Commit

Permalink
Fix: parse BSON object value
Browse files Browse the repository at this point in the history
  • Loading branch information
dalpo committed Dec 3, 2015
1 parent ee93a12 commit 8cc1cba
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
13 changes: 13 additions & 0 deletions lib/rails_admin/adapters/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions lib/rails_admin/adapters/mongoid/abstract_object.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'rails_admin/adapters/active_record/abstract_object'

module RailsAdmin
module Adapters
module Mongoid
Expand Down
16 changes: 1 addition & 15 deletions lib/rails_admin/config/fields/types/bson_object_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 == ''
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy_app/app/mongoid/field_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions spec/rails_admin/config/fields/types/bson_object_id_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8cc1cba

Please sign in to comment.