Skip to content

Commit

Permalink
Merge pull request railsadminteam#2495 from dalpo/feature/fix-mongoid
Browse files Browse the repository at this point in the history
Fix parse BSON object value
  • Loading branch information
mshibuya committed Dec 5, 2015
2 parents 02a5e58 + fb9d14b commit 95ad1c2
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 29 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

## [Unreleased](https://github.com/sferik/rails_admin/tree/HEAD)

[Full Changelog](https://github.com/sferik/rails_admin/compare/v0.8.0...HEAD)
[Full Changelog](https://github.com/sferik/rails_admin/compare/v0.8.1...HEAD)

### Fixed
- Fixed Mongoid BSON object field ([#2495](https://github.com/sferik/rails_admin/issues/2495))


## [0.8.1](https://github.com/sferik/rails_admin/tree/v0.8.1) - 2015-11-24
Expand Down
21 changes: 9 additions & 12 deletions lib/rails_admin/adapters/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
require 'rails_admin/adapters/mongoid/abstract_object'
require 'rails_admin/adapters/mongoid/association'
require 'rails_admin/adapters/mongoid/property'
require 'rails_admin/adapters/mongoid/bson'

module RailsAdmin
module Adapters
module Mongoid
DISABLED_COLUMN_TYPES = ['Range', 'Moped::BSON::Binary', 'BSON::Binary', 'Mongoid::Geospatial::Point']
DISABLED_COLUMN_TYPES = %w(Range Moped::BSON::Binary BSON::Binary Mongoid::Geospatial::Point)

def parse_object_id(value)
Bson.parse_object_id(value)
end

def new(params = {})
AbstractObject.new(model.new(params))
Expand All @@ -17,10 +22,10 @@ def get(id)
AbstractObject.new(model.find(id))
rescue => e
raise e if %w(
BSON::InvalidObjectId
Mongoid::Errors::DocumentNotFound
Mongoid::Errors::InvalidFind
Moped::Errors::InvalidObjectId
BSON::InvalidObjectId
).exclude?(e.class.to_s)
end

Expand Down Expand Up @@ -117,11 +122,7 @@ def query_conditions(query, fields = config.list.fields.select(&:queryable?))
statements.concat make_condition_for_current_collection(field, conditions_per_collection)
end

if statements.any?
{'$or' => statements}
else
{}
end
statements.any? ? {'$or' => statements} : {}
end

# filters example => {"string_field"=>{"0055"=>{"o"=>"like", "v"=>"test_value"}}, ...}
Expand All @@ -144,11 +145,7 @@ def filter_conditions(filters, fields = config.list.fields.select(&:filterable?)
end
end

if statements.any?
{'$and' => statements}
else
{}
end
statements.any? ? {'$and' => statements} : {}
end

def parse_collection_name(column)
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
1 change: 1 addition & 0 deletions lib/rails_admin/adapters/mongoid/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Adapters
module Mongoid
class Association
attr_reader :association, :model

def initialize(association, model)
@association = association
@model = model
Expand Down
29 changes: 29 additions & 0 deletions lib/rails_admin/adapters/mongoid/bson.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'mongoid'

module RailsAdmin
module Adapters
module Mongoid
class Bson
OBJECT_ID = begin
if defined?(Moped::BSON)
Moped::BSON::ObjectId
elsif defined?(BSON::ObjectId)
BSON::ObjectId
end
end

class << self
def parse_object_id(value)
OBJECT_ID.from_string(value)
rescue => e
raise e if %w(
Moped::Errors::InvalidObjectId
BSON::ObjectId::Invalid
BSON::InvalidObjectId
).exclude?(e.class.to_s)
end
end
end
end
end
end
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::Bson::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::Bson::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 95ad1c2

Please sign in to comment.