adapter/mongoid little bit adapted

This commit is contained in:
Zhomart Mukhamejanov 2014-08-01 02:05:25 -07:00
parent 9a796f157e
commit 7cb697a98c
3 changed files with 48 additions and 41 deletions

View File

@ -1,7 +1,7 @@
require 'ransack/adapters/mongoid/base' require 'ransack/adapters/mongoid/base'
Mongoid::Document.extend Ransack::Adapters::Mongoid::Base Mongoid::Document.extend Ransack::Adapters::Mongoid::Base
case Mongoid::VERSION::STRING case Mongoid::VERSION
when /^3\.2\./ when /^3\.2\./
require 'ransack/adapters/mongoid/3.2/context' require 'ransack/adapters/mongoid/3.2/context'
else else

View File

@ -4,11 +4,13 @@ module Ransack
module Base module Base
def self.extended(base) def self.extended(base)
alias :search :ransack unless base.respond_to? :search base::ClassMethods.class_eval do
base.class_eval do def _ransackers
class_attribute :_ransackers @_ransackers ||= {}
self._ransackers ||= {}
end end
def _ransackers=(value)
@_ransackers = value
end end
def ransack(params = {}, options = {}) def ransack(params = {}, options = {})
@ -17,6 +19,8 @@ module Ransack
|k, v| v.blank? && v != false } : params, options) |k, v| v.blank? && v != false } : params, options)
end end
alias_method :search, :ransack
def ransacker(name, opts = {}, &block) def ransacker(name, opts = {}, &block)
self._ransackers = _ransackers.merge name.to_s => Ransacker self._ransackers = _ransackers.merge name.to_s => Ransacker
.new(self, name, opts, &block) .new(self, name, opts, &block)
@ -40,8 +44,10 @@ module Ransack
def ransackable_scopes(auth_object = nil) def ransackable_scopes(auth_object = nil)
[] []
end end
end # base::ClassMethods.class_eval
end end
end # Base
end end
end end
end end

View File

@ -7,8 +7,8 @@ module Ransack
class Context < ::Ransack::Context class Context < ::Ransack::Context
# Because the AR::Associations namespace is insane # Because the AR::Associations namespace is insane
JoinDependency = ::Mongoid::Associations::JoinDependency # JoinDependency = ::Mongoid::Associations::JoinDependency
JoinPart = JoinDependency::JoinPart # JoinPart = JoinDependency::JoinPart
def initialize(object, options = {}) def initialize(object, options = {})
super super
@ -112,6 +112,7 @@ module Ransack
if relation.respond_to?(:join_dependency) # Squeel will enable this if relation.respond_to?(:join_dependency) # Squeel will enable this
relation.join_dependency relation.join_dependency
else else
binding.pry
build_join_dependency(relation) build_join_dependency(relation)
end end
end end
@ -155,14 +156,14 @@ module Ransack
join_dependency.alias_tracker.aliases[join.left.name.downcase] = 1 join_dependency.alias_tracker.aliases[join.left.name.downcase] = 1
end end
if ::Mongoid::VERSION::STRING >= '4.1' if ::Mongoid::VERSION >= '4.1'
join_dependency join_dependency
else else
join_dependency.graft(*stashed_association_joins) join_dependency.graft(*stashed_association_joins)
end end
end end
if ::Mongoid::VERSION::STRING >= '4.1' if ::Mongoid::VERSION >= '4.1'
def build_or_find_association(name, parent = @base, klass = nil) def build_or_find_association(name, parent = @base, klass = nil)
found_association = @join_dependency.join_root.children found_association = @join_dependency.join_root.children