Skip to content

Commit

Permalink
Refactor: split Bson parsing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dalpo committed Dec 4, 2015
1 parent 00e0263 commit fb9d14b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 24 deletions.
28 changes: 6 additions & 22 deletions lib/rails_admin/adapters/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,15 @@
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']
OBJECT_ID ||= begin
if defined?(Moped::BSON)
Moped::BSON::ObjectId
elsif defined?(BSON::ObjectId)
BSON::ObjectId
end
end
DISABLED_COLUMN_TYPES = %w(Range Moped::BSON::Binary BSON::Binary Mongoid::Geospatial::Point)

def parse_object_id(value)
OBJECT_ID.from_string(value)
rescue BSON::ObjectId::Invalid, BSON::InvalidObjectId, Moped::Errors::InvalidObjectId
nil
Bson.parse_object_id(value)
end

def new(params = {})
Expand All @@ -30,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 @@ -130,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 @@ -157,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/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
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::Adapters::Mongoid::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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
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(:bson) { RailsAdmin::Adapters::Mongoid::Bson::OBJECT_ID.new }
let(:field) do
RailsAdmin.config(FieldTest).fields.detect do |f|
f.name == :bson_object_id_field
Expand Down

0 comments on commit fb9d14b

Please sign in to comment.