Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongoid 4.0 support #407

Merged
merged 38 commits into from
Nov 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
6796c96
added pry
Zhomart Aug 1, 2014
e5b6014
ActiveRecord and Arel abstracted
Zhomart Aug 1, 2014
d16c8b0
mongoid initialized
Zhomart Aug 1, 2014
b3765cc
mongoid 4.0 added to Gemfile
Zhomart Aug 1, 2014
9a796f1
specs set up for mongoid
Zhomart Aug 1, 2014
7cb697a
adapter/mongoid little bit adapted
Zhomart Aug 1, 2014
718a05b
ransack made more abstract and added mongoid
Zhomart Aug 2, 2014
9e1204e
added mongoid/predicate_spec
Zhomart Aug 2, 2014
b3234d2
load DERIVED_PREDICATES only if it has been defined
Zhomart Aug 2, 2014
1799766
added mongoid search specs
Zhomart Aug 2, 2014
b85abef
ransack method for Mongoid::Document improved
Zhomart Aug 2, 2014
dace90e
sorting for mongoid works
Zhomart Aug 2, 2014
55d6b88
mongoid translate spec works
Zhomart Aug 2, 2014
2ef4210
added active_record style method Mongoid::Document.first(n)
Zhomart Aug 2, 2014
915205c
added mongoid/node specs and sham,machinist
Zhomart Aug 2, 2014
d2b0268
added some features to ransack/mongoid
Zhomart Aug 2, 2014
905f6a2
added adapters/mongoid/base specs
Zhomart Aug 2, 2014
b1f358a
mongoid/context spec added
Zhomart Aug 2, 2014
59920fe
by default loads ActiveRecord's CONSTANTS
Zhomart Aug 2, 2014
b657426
added more predication methods.
Zhomart Aug 3, 2014
9facab1
mongoid predications updated
Zhomart Aug 3, 2014
116884c
added mongoid/attrs/order_predications
Zhomart Aug 3, 2014
80c71f8
predications in, not_in added
Zhomart Aug 3, 2014
aff2151
added starts, ends
Zhomart Aug 3, 2014
a51158e
.travis changed
Zhomart Aug 4, 2014
f718ce5
if ENV['DB'] has mongodb then run rake mongoid else spec
Zhomart Aug 4, 2014
ee23c4a
predications fixed
Zhomart Aug 27, 2014
811903f
merged
Zhomart Sep 18, 2014
aeeaf59
merged travis.yml, Gemfile and Context
Zhomart Oct 6, 2014
1fbbbae
mongodb specs fixed for rails >= 4
Zhomart Oct 6, 2014
b81bf44
previous fix fixed correctly now
Zhomart Oct 6, 2014
12e1e4d
new AR codes moved to inside adapters/active_record
Zhomart Oct 31, 2014
903d588
Common constants of adapters moved to ransack/constants.rb
Zhomart Oct 31, 2014
bfae6b4
Merge branch 'master' into mongoid
Zhomart Nov 2, 2014
6041ec4
Added task rake mongoid_console
Zhomart Nov 2, 2014
378c7d5
Fixed mongoid search by id
Zhomart Nov 2, 2014
7088c9e
added specs: mongoid search by id
Zhomart Nov 2, 2014
e90ffd1
fixed bug after last merging with master.
Zhomart Nov 2, 2014
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
services: mongodb

language: ruby

sudo: false
Expand All @@ -8,9 +10,11 @@ rvm:
- 1.9

env:
- RAILS=master DB=mongodb
- RAILS=master DB=sqlite3
- RAILS=master DB=mysql
- RAILS=master DB=postgres
- RAILS=4-1-stable DB=mongodb
- RAILS=4-1-stable DB=sqlite3
- RAILS=4-1-stable DB=mysql
- RAILS=4-1-stable DB=postgres
Expand Down
12 changes: 9 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,35 @@ end

gem 'polyamorous', '~> 1.1'

gem 'pry'

# Provide timezone information on Windows
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw]

case rails
when /\// # A path
gem 'activesupport', path: "#{rails}/activesupport"
gem 'activerecord', path: "#{rails}/activerecord"
gem 'activerecord', path: "#{rails}/activerecord", require: false
gem 'actionpack', path: "#{rails}/actionpack"
when /^v/ # A tagged version
git 'git://github.com/rails/rails.git', :tag => rails do
gem 'activesupport'
gem 'activemodel'
gem 'activerecord'
gem 'activerecord', require: false
gem 'actionpack'
end
else
git 'git://github.com/rails/rails.git', :branch => rails do
gem 'activesupport'
gem 'activemodel'
gem 'activerecord'
gem 'activerecord', require: false
gem 'actionpack'
end
if rails == '3-0-stable'
gem 'mysql2', '< 0.3'
end
end

if ENV['DB'] =~ /mongodb/
gem 'mongoid', '~> 4.0.0', require: false
end
28 changes: 26 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@ require 'rspec/core/rake_task'
Bundler::GemHelper.install_tasks

RSpec::Core::RakeTask.new(:spec) do |rspec|
ENV['SPEC'] = 'spec/ransack/**/*_spec.rb'
rspec.rspec_opts = ['--backtrace']
end

task :default => :spec
RSpec::Core::RakeTask.new(:mongoid) do |rspec|
ENV['SPEC'] = 'spec/mongoid/**/*_spec.rb'
rspec.rspec_opts = ['--backtrace']
end

task :default do
if ENV['DB'] =~ /mongodb/
Rake::Task["mongoid"].invoke
else
Rake::Task["spec"].invoke
end
end

desc "Open an irb session with Ransack and the sample data used in specs"
task :console do
Expand All @@ -16,4 +28,16 @@ task :console do
require 'console'
ARGV.clear
IRB.start
end
end

desc "Open an irb session with Ransack, Mongoid and the sample data used in specs"
task :mongoid_console do
require 'irb'
require 'irb/completion'
require 'pry'
require 'mongoid'
require File.expand_path('../lib/ransack.rb', __FILE__)
require File.expand_path('../spec/mongoid/support/schema.rb', __FILE__)
ARGV.clear
Pry.start
end
9 changes: 9 additions & 0 deletions lib/ransack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

require 'ransack/configuration'

if defined?(::Mongoid)
require 'ransack/adapters/mongoid/ransack/constants'
else
require 'ransack/adapters/active_record/ransack/constants'
end

module Ransack
extend Configuration

Expand All @@ -19,9 +25,12 @@ class UntraversableAssociationError < StandardError; end;
end

require 'ransack/translate'
require 'ransack/adapters/active_record/ransack/translate' if defined?(::ActiveRecord::Base)
require 'ransack/adapters/mongoid/ransack/translate' if defined?(::Mongoid)
require 'ransack/search'
require 'ransack/ransacker'
require 'ransack/adapters/active_record' if defined?(::ActiveRecord::Base)
require 'ransack/adapters/mongoid' if defined?(::Mongoid)
require 'ransack/helpers'
require 'action_controller'

Expand Down
127 changes: 127 additions & 0 deletions lib/ransack/adapters/active_record/ransack/constants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
module Ransack
module Constants
DISTINCT = 'DISTINCT '.freeze
STRING_JOIN = 'string_join'.freeze
ASSOCIATION_JOIN = 'association_join'.freeze
STASHED_JOIN = 'stashed_join'.freeze
JOIN_NODE = 'join_node'.freeze

DERIVED_PREDICATES = [
['cont'.freeze, {
:arel_predicate => 'matches'.freeze,
:formatter => proc { |v| "%#{escape_wildcards(v)}%" }
}
],
['i_cont'.freeze, {
:arel_predicate => 'i_matches'.freeze,
:formatter => proc { |v| "%#{escape_wildcards(v)}%" }
}
],
['not_cont'.freeze, {
:arel_predicate => 'does_not_match'.freeze,
:formatter => proc { |v| "%#{escape_wildcards(v)}%" }
}
],
['i_not_cont'.freeze, {
:arel_predicate => 'i_does_not_match'.freeze,
:formatter => proc { |v| "%#{escape_wildcards(v)}%" }
}
],
['start'.freeze, {
:arel_predicate => 'matches'.freeze,
:formatter => proc { |v| "#{escape_wildcards(v)}%" }
}
],
['not_start'.freeze, {
:arel_predicate => 'does_not_match'.freeze,
:formatter => proc { |v| "#{escape_wildcards(v)}%" }
}
],
['end'.freeze, {
:arel_predicate => 'matches'.freeze,
:formatter => proc { |v| "%#{escape_wildcards(v)}" }
}
],
['not_end'.freeze, {
:arel_predicate => 'does_not_match'.freeze,
:formatter => proc { |v| "%#{escape_wildcards(v)}" }
}
],
['true'.freeze, {
:arel_predicate => proc { |v| v ? EQ : NOT_EQ },
:compounds => false,
:type => :boolean,
:validator => proc { |v| BOOLEAN_VALUES.include?(v) },
:formatter => proc { |v| true }
}
],
['not_true'.freeze, {
:arel_predicate => proc { |v| v ? NOT_EQ : EQ },
:compounds => false,
:type => :boolean,
:validator => proc { |v| BOOLEAN_VALUES.include?(v) },
:formatter => proc { |v| true }
}
],
['false'.freeze, {
:arel_predicate => proc { |v| v ? EQ : NOT_EQ },
:compounds => false,
:type => :boolean,
:validator => proc { |v| BOOLEAN_VALUES.include?(v) },
:formatter => proc { |v| false }
}
],
['not_false'.freeze, {
:arel_predicate => proc { |v| v ? NOT_EQ : EQ },
:compounds => false,
:type => :boolean,
:validator => proc { |v| BOOLEAN_VALUES.include?(v) },
:formatter => proc { |v| false }
}
],
['present'.freeze, {
:arel_predicate => proc { |v| v ? NOT_EQ_ALL : EQ_ANY },
:compounds => false,
:type => :boolean,
:validator => proc { |v| BOOLEAN_VALUES.include?(v) },
:formatter => proc { |v| [nil, Ransack::Constants::EMPTY] }
}
],
['blank'.freeze, {
:arel_predicate => proc { |v| v ? EQ_ANY : NOT_EQ_ALL },
:compounds => false,
:type => :boolean,
:validator => proc { |v| BOOLEAN_VALUES.include?(v) },
:formatter => proc { |v| [nil, Ransack::Constants::EMPTY] }
}
],
['null'.freeze, {
:arel_predicate => proc { |v| v ? EQ : NOT_EQ },
:compounds => false,
:type => :boolean,
:validator => proc { |v| BOOLEAN_VALUES.include?(v)},
:formatter => proc { |v| nil }
}
],
['not_null'.freeze, {
:arel_predicate => proc { |v| v ? NOT_EQ : EQ },
:compounds => false,
:type => :boolean,
:validator => proc { |v| BOOLEAN_VALUES.include?(v) },
:formatter => proc { |v| nil } }
]
].freeze

module_function
# replace % \ to \% \\
def escape_wildcards(unescaped)
case ActiveRecord::Base.connection.adapter_name
when "Mysql2".freeze, "PostgreSQL".freeze
# Necessary for PostgreSQL and MySQL
unescaped.to_s.gsub(/([\\|\%|.])/, '\\\\\\1')
else
unescaped
end
end
end
end
64 changes: 64 additions & 0 deletions lib/ransack/adapters/active_record/ransack/context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'ransack/visitor'

module Ransack
class Context
attr_reader :arel_visitor

class << self

def for_class(klass, options = {})
if klass < ActiveRecord::Base
Adapters::ActiveRecord::Context.new(klass, options)
end
end

def for_object(object, options = {})
case object
when ActiveRecord::Relation
Adapters::ActiveRecord::Context.new(object.klass, options)
end
end

end # << self

def initialize(object, options = {})
@object = relation_for(object)
@klass = @object.klass
@join_dependency = join_dependency(@object)
@join_type = options[:join_type] || Polyamorous::OuterJoin
@search_key = options[:search_key] || Ransack.options[:search_key]

if ::ActiveRecord::VERSION::STRING >= "4.1".freeze
@base = @join_dependency.join_root
@engine = @base.base_klass.arel_engine
else
@base = @join_dependency.join_base
@engine = @base.arel_engine
end

@default_table = Arel::Table.new(
@base.table_name, :as => @base.aliased_table_name, :engine => @engine
)
@bind_pairs = Hash.new do |hash, key|
parent, attr_name = get_parent_and_attribute_name(key.to_s)
if parent && attr_name
hash[key] = [parent, attr_name]
end
end
end

def klassify(obj)
if Class === obj && ::ActiveRecord::Base > obj
obj
elsif obj.respond_to? :klass
obj.klass
elsif obj.respond_to? :active_record # Rails 3
obj.active_record
elsif obj.respond_to? :base_klass # Rails 4
obj.base_klass
else
raise ArgumentError, "Don't know how to klassify #{obj.inspect}"
end
end
end
end
27 changes: 27 additions & 0 deletions lib/ransack/adapters/active_record/ransack/nodes/condition.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Ransack
module Nodes
class Condition

def arel_predicate
predicates = attributes.map do |attr|
attr.attr.send(
arel_predicate_for_attribute(attr),
formatted_values_for_attribute(attr)
)
end

if predicates.size > 1
case combinator
when Ransack::Constants::AND
Arel::Nodes::Grouping.new(Arel::Nodes::And.new(predicates))
when Ransack::Constants::OR
predicates.inject(&:or)
end
else
predicates.first
end
end

end # Condition
end
end
12 changes: 12 additions & 0 deletions lib/ransack/adapters/active_record/ransack/translate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Ransack
module Translate

def self.i18n_key(klass)
if ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR == 0
klass.model_name.i18n_key.to_s.tr('.'.freeze, '/'.freeze)
else
klass.model_name.i18n_key.to_s
end
end
end
end
24 changes: 24 additions & 0 deletions lib/ransack/adapters/active_record/ransack/visitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Ransack
class Visitor
def visit_and(object)
nodes = object.values.map { |o| accept(o) }.compact
return nil unless nodes.size > 0

if nodes.size > 1
Arel::Nodes::Grouping.new(Arel::Nodes::And.new(nodes))
else
nodes.first
end
end

def quoted?(object)
case object
when Arel::Nodes::SqlLiteral, Bignum, Fixnum
false
else
true
end
end

end
end
13 changes: 13 additions & 0 deletions lib/ransack/adapters/mongoid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'ransack/adapters/mongoid/base'
::Mongoid::Document.send :include, Ransack::Adapters::Mongoid::Base

require 'ransack/adapters/mongoid/attributes/attribute'
require 'ransack/adapters/mongoid/table'
require 'ransack/adapters/mongoid/inquiry_hash'

case ::Mongoid::VERSION
when /^3\.2\./
require 'ransack/adapters/mongoid/3.2/context'
else
require 'ransack/adapters/mongoid/context'
end
Empty file.
Loading