diff --git a/README.md b/README.md index 5b08f07e0..77c14e249 100644 --- a/README.md +++ b/README.md @@ -746,16 +746,10 @@ called on a `ransack` search returns a `Mongoid::Criteria` object: @people = @q.result.active.order_by(updated_at: -1).limit(10) ``` -_NOTE: Ransack currently works with either Active Record or Mongoid, but not +NOTE: Ransack currently works with either Active Record or Mongoid, but not both in the same application. If both are present, Ransack will default to -Active Record only. Here is the code containing the logic:_ - -```ruby - @current_adapters ||= { - :active_record => defined?(::ActiveRecord::Base), - :mongoid => defined?(::Mongoid) && !defined?(::ActiveRecord::Base) - } -``` +Active Record only. The logic is contained in +`Ransack::Adapters#instantiate_object_mapper` should you need to override it. ## Semantic Versioning diff --git a/lib/ransack.rb b/lib/ransack.rb index 947158147..180f76ab0 100644 --- a/lib/ransack.rb +++ b/lib/ransack.rb @@ -2,7 +2,7 @@ require 'ransack/configuration' require 'ransack/adapters' -Ransack::Adapters.require_constants +Ransack::Adapters.object_mapper.require_constants module Ransack extend Configuration @@ -31,6 +31,6 @@ class UntraversableAssociationError < StandardError; end; require 'action_controller' require 'ransack/translate' -Ransack::Adapters.require_adapter +Ransack::Adapters.object_mapper.require_adapter ActionController::Base.helper Ransack::Helpers::FormHelper diff --git a/lib/ransack/adapters.rb b/lib/ransack/adapters.rb index ee4418aa1..41cc2e6ec 100644 --- a/lib/ransack/adapters.rb +++ b/lib/ransack/adapters.rb @@ -1,63 +1,60 @@ module Ransack module Adapters - # TODO: Refactor to remove conditionals - - def self.current_adapters - @current_adapters ||= { - :active_record => defined?(::ActiveRecord::Base), - :mongoid => defined?(::Mongoid) && !defined?(::ActiveRecord::Base) - } + def self.object_mapper + @object_mapper ||= instantiate_object_mapper end - def self.require_constants - if current_adapters[:mongoid] - require 'ransack/adapters/mongoid/ransack/constants' + def self.instantiate_object_mapper + if defined?(::ActiveRecord::Base) + ActiveRecordAdapter.new + elsif defined?(::Mongoid) + MongoidAdapter.new end + end - if current_adapters[:active_record] + class ActiveRecordAdapter + def require_constants require 'ransack/adapters/active_record/ransack/constants' end - end - def self.require_adapter - if current_adapters[:active_record] + def require_adapter require 'ransack/adapters/active_record/ransack/translate' require 'ransack/adapters/active_record' end - if current_adapters[:mongoid] - require 'ransack/adapters/mongoid/ransack/translate' - require 'ransack/adapters/mongoid' + def require_context + require 'ransack/adapters/active_record/ransack/visitor' end - end - def self.require_context - if current_adapters[:active_record] - require 'ransack/adapters/active_record/ransack/visitor' + def require_nodes + require 'ransack/adapters/active_record/ransack/nodes/condition' end - if current_adapters[:mongoid] - require 'ransack/adapters/mongoid/ransack/visitor' + def require_search + require 'ransack/adapters/active_record/ransack/context' end end - def self.require_nodes - if current_adapters[:active_record] - require 'ransack/adapters/active_record/ransack/nodes/condition' + class MongoidAdapter + def require_constants + require 'ransack/adapters/mongoid/ransack/constants' end - if current_adapters[:mongoid] - require 'ransack/adapters/mongoid/ransack/nodes/condition' + def require_adapter + require 'ransack/adapters/mongoid/ransack/translate' + require 'ransack/adapters/mongoid' end - end - def self.require_search - if current_adapters[:active_record] - require 'ransack/adapters/active_record/ransack/context' + def require_context + require 'ransack/adapters/mongoid/ransack/visitor' + end + + def require_nodes + require 'ransack/adapters/mongoid/ransack/nodes/condition' end - if current_adapters[:mongoid] + def require_search require 'ransack/adapters/mongoid/ransack/context' end end diff --git a/lib/ransack/context.rb b/lib/ransack/context.rb index 5f012bfba..cc2efa9d0 100644 --- a/lib/ransack/context.rb +++ b/lib/ransack/context.rb @@ -1,5 +1,5 @@ require 'ransack/visitor' -Ransack::Adapters.require_context +Ransack::Adapters.object_mapper.require_context module Ransack class Context diff --git a/lib/ransack/nodes.rb b/lib/ransack/nodes.rb index 63946a70e..b4018c93e 100644 --- a/lib/ransack/nodes.rb +++ b/lib/ransack/nodes.rb @@ -3,6 +3,6 @@ require 'ransack/nodes/attribute' require 'ransack/nodes/value' require 'ransack/nodes/condition' -Ransack::Adapters.require_nodes +Ransack::Adapters.object_mapper.require_nodes require 'ransack/nodes/sort' require 'ransack/nodes/grouping'