Add Rails 4 compatibility to master
This commit is contained in:
parent
fc5fbcf8b4
commit
5a522b4f4a
6
Gemfile
6
Gemfile
|
@ -1,11 +1,11 @@
|
||||||
source "http://rubygems.org"
|
source "https://rubygems.org"
|
||||||
gemspec
|
gemspec
|
||||||
|
|
||||||
gem 'rake'
|
gem 'rake'
|
||||||
|
|
||||||
rails = ENV['RAILS'] || '3-2-stable'
|
rails = ENV['RAILS'] || '4-0-stable'
|
||||||
|
|
||||||
gem 'arel', '3.0.2'
|
gem 'arel'
|
||||||
|
|
||||||
case rails
|
case rails
|
||||||
when /\// # A path
|
when /\// # A path
|
||||||
|
|
|
@ -7,6 +7,8 @@ when /^3\.0\./
|
||||||
require 'ransack/adapters/active_record/3.0/context'
|
require 'ransack/adapters/active_record/3.0/context'
|
||||||
when /^3\.1\./
|
when /^3\.1\./
|
||||||
require 'ransack/adapters/active_record/3.1/context'
|
require 'ransack/adapters/active_record/3.1/context'
|
||||||
|
when /^3\.2\./
|
||||||
|
require 'ransack/adapters/active_record/3.2/context'
|
||||||
else
|
else
|
||||||
require 'ransack/adapters/active_record/context'
|
require 'ransack/adapters/active_record/context'
|
||||||
end
|
end
|
|
@ -11,11 +11,17 @@ module Ransack
|
||||||
JoinDependency = ::ActiveRecord::Associations::ClassMethods::JoinDependency
|
JoinDependency = ::ActiveRecord::Associations::ClassMethods::JoinDependency
|
||||||
JoinBase = JoinDependency::JoinBase
|
JoinBase = JoinDependency::JoinBase
|
||||||
|
|
||||||
|
# Redefine a few things for ActiveRecord 3.0.
|
||||||
|
|
||||||
def initialize(object, options = {})
|
def initialize(object, options = {})
|
||||||
super
|
super
|
||||||
@arel_visitor = Arel::Visitors.visitor_for @engine
|
@arel_visitor = Arel::Visitors.visitor_for @engine
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def relation_for(object)
|
||||||
|
object.scoped
|
||||||
|
end
|
||||||
|
|
||||||
def evaluate(search, opts = {})
|
def evaluate(search, opts = {})
|
||||||
viz = Visitor.new
|
viz = Visitor.new
|
||||||
relation = @object.where(viz.accept(search.base))
|
relation = @object.where(viz.accept(search.base))
|
||||||
|
@ -48,18 +54,6 @@ module Ransack
|
||||||
parent.table
|
parent.table
|
||||||
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
|
|
||||||
obj.active_record
|
|
||||||
else
|
|
||||||
raise ArgumentError, "Don't know how to klassify #{obj}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def type_for(attr)
|
def type_for(attr)
|
||||||
return nil unless attr && attr.valid?
|
return nil unless attr && attr.valid?
|
||||||
klassify(attr.parent).columns_hash[attr.arel_attribute.name.to_s].type
|
klassify(attr.parent).columns_hash[attr.arel_attribute.name.to_s].type
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
require 'ransack/context'
|
require 'ransack/context'
|
||||||
|
require 'ransack/adapters/active_record/compat'
|
||||||
require 'polyamorous'
|
require 'polyamorous'
|
||||||
|
|
||||||
module Ransack
|
module Ransack
|
||||||
|
@ -9,11 +10,17 @@ module Ransack
|
||||||
JoinDependency = ::ActiveRecord::Associations::JoinDependency
|
JoinDependency = ::ActiveRecord::Associations::JoinDependency
|
||||||
JoinPart = JoinDependency::JoinPart
|
JoinPart = JoinDependency::JoinPart
|
||||||
|
|
||||||
|
# Redefine a few things for ActiveRecord 3.1.
|
||||||
|
|
||||||
def initialize(object, options = {})
|
def initialize(object, options = {})
|
||||||
super
|
super
|
||||||
@arel_visitor = Arel::Visitors.visitor_for @engine
|
@arel_visitor = Arel::Visitors.visitor_for @engine
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def relation_for(object)
|
||||||
|
object.scoped
|
||||||
|
end
|
||||||
|
|
||||||
def evaluate(search, opts = {})
|
def evaluate(search, opts = {})
|
||||||
viz = Visitor.new
|
viz = Visitor.new
|
||||||
relation = @object.where(viz.accept(search.base))
|
relation = @object.where(viz.accept(search.base))
|
||||||
|
@ -46,18 +53,6 @@ module Ransack
|
||||||
parent.table
|
parent.table
|
||||||
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
|
|
||||||
obj.active_record
|
|
||||||
else
|
|
||||||
raise ArgumentError, "Don't know how to klassify #{obj}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def type_for(attr)
|
def type_for(attr)
|
||||||
return nil unless attr && attr.valid?
|
return nil unless attr && attr.valid?
|
||||||
name = attr.arel_attribute.name.to_s
|
name = attr.arel_attribute.name.to_s
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
require 'ransack/context'
|
||||||
|
require 'ransack/adapters/active_record/3.1/context'
|
||||||
|
require 'ransack/adapters/active_record/compat'
|
||||||
|
require 'polyamorous'
|
||||||
|
|
||||||
|
module Ransack
|
||||||
|
module Adapters
|
||||||
|
module ActiveRecord
|
||||||
|
class Context < ::Ransack::Context
|
||||||
|
|
||||||
|
# Redefine a few things for ActiveRecord 3.2.
|
||||||
|
|
||||||
|
def initialize(object, options = {})
|
||||||
|
super
|
||||||
|
@arel_visitor = @engine.connection.visitor
|
||||||
|
end
|
||||||
|
|
||||||
|
def relation_for(object)
|
||||||
|
object.scoped
|
||||||
|
end
|
||||||
|
|
||||||
|
def type_for(attr)
|
||||||
|
return nil unless attr && attr.valid?
|
||||||
|
name = attr.arel_attribute.name.to_s
|
||||||
|
table = attr.arel_attribute.relation.table_name
|
||||||
|
|
||||||
|
schema_cache = @engine.connection.schema_cache
|
||||||
|
raise "No table named #{table} exists" unless schema_cache.table_exists?(table)
|
||||||
|
schema_cache.columns_hash[table][name].type
|
||||||
|
end
|
||||||
|
|
||||||
|
def evaluate(search, opts = {})
|
||||||
|
viz = Visitor.new
|
||||||
|
relation = @object.where(viz.accept(search.base))
|
||||||
|
if search.sorts.any?
|
||||||
|
relation = relation.except(:order).reorder(viz.accept(search.sorts))
|
||||||
|
end
|
||||||
|
opts[:distinct] ? relation.uniq : relation
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,5 +1,5 @@
|
||||||
require 'ransack/context'
|
require 'ransack/context'
|
||||||
require 'ransack/adapters/active_record/3.1/context'
|
require 'ransack/adapters/active_record/3.2/context'
|
||||||
require 'ransack/adapters/active_record/compat'
|
require 'ransack/adapters/active_record/compat'
|
||||||
require 'polyamorous'
|
require 'polyamorous'
|
||||||
|
|
||||||
|
@ -8,13 +8,15 @@ module Ransack
|
||||||
module ActiveRecord
|
module ActiveRecord
|
||||||
class Context < ::Ransack::Context
|
class Context < ::Ransack::Context
|
||||||
|
|
||||||
# Redefine a few things that have changed with 3.2.
|
|
||||||
|
|
||||||
def initialize(object, options = {})
|
def initialize(object, options = {})
|
||||||
super
|
super
|
||||||
@arel_visitor = @engine.connection.visitor
|
@arel_visitor = @engine.connection.visitor
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def relation_for(object)
|
||||||
|
object.all
|
||||||
|
end
|
||||||
|
|
||||||
def type_for(attr)
|
def type_for(attr)
|
||||||
return nil unless attr && attr.valid?
|
return nil unless attr && attr.valid?
|
||||||
name = attr.arel_attribute.name.to_s
|
name = attr.arel_attribute.name.to_s
|
||||||
|
@ -22,7 +24,7 @@ module Ransack
|
||||||
|
|
||||||
schema_cache = @engine.connection.schema_cache
|
schema_cache = @engine.connection.schema_cache
|
||||||
raise "No table named #{table} exists" unless schema_cache.table_exists?(table)
|
raise "No table named #{table} exists" unless schema_cache.table_exists?(table)
|
||||||
schema_cache.columns_hash[table][name].type
|
schema_cache.columns_hash(table)[name].type
|
||||||
end
|
end
|
||||||
|
|
||||||
def evaluate(search, opts = {})
|
def evaluate(search, opts = {})
|
||||||
|
@ -31,7 +33,7 @@ module Ransack
|
||||||
if search.sorts.any?
|
if search.sorts.any?
|
||||||
relation = relation.except(:order).reorder(viz.accept(search.sorts))
|
relation = relation.except(:order).reorder(viz.accept(search.sorts))
|
||||||
end
|
end
|
||||||
opts[:distinct] ? relation.uniq : relation
|
opts[:distinct] ? relation.distinct : relation
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -28,7 +28,7 @@ module Ransack
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(object, options = {})
|
def initialize(object, options = {})
|
||||||
@object = object.scoped
|
@object = relation_for(object)
|
||||||
@klass = @object.klass
|
@klass = @object.klass
|
||||||
@join_dependency = join_dependency(@object)
|
@join_dependency = join_dependency(@object)
|
||||||
@join_type = options[:join_type] || Arel::OuterJoin
|
@join_type = options[:join_type] || Arel::OuterJoin
|
||||||
|
@ -44,6 +44,20 @@ module Ransack
|
||||||
end
|
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}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Convert a string representing a chain of associations and an attribute
|
# Convert a string representing a chain of associations and an attribute
|
||||||
# into the attribute itself
|
# into the attribute itself
|
||||||
def contextualize(str)
|
def contextualize(str)
|
||||||
|
|
|
@ -23,7 +23,7 @@ module Ransack
|
||||||
attribute_names = attributes_str.split(/_and_|_or_/)
|
attribute_names = attributes_str.split(/_and_|_or_/)
|
||||||
combinator = attributes_str.match(/_and_/) ? :and : :or
|
combinator = attributes_str.match(/_and_/) ? :and : :or
|
||||||
defaults = base_ancestors.map do |klass|
|
defaults = base_ancestors.map do |klass|
|
||||||
:"ransack.attributes.#{klass.model_name.underscore}.#{original_name}"
|
:"ransack.attributes.#{klass.model_name.singular}.#{original_name}"
|
||||||
end
|
end
|
||||||
|
|
||||||
translated_names = attribute_names.map do |attr|
|
translated_names = attribute_names.map do |attr|
|
||||||
|
@ -50,7 +50,7 @@ module Ransack
|
||||||
raise ArgumentError, "A context is required to translate associations"
|
raise ArgumentError, "A context is required to translate associations"
|
||||||
end
|
end
|
||||||
|
|
||||||
defaults = key.blank? ? [:"#{context.klass.i18n_scope}.models.#{context.klass.model_name.underscore}"] : [:"ransack.associations.#{context.klass.model_name.underscore}.#{key}"]
|
defaults = key.blank? ? [:"#{context.klass.i18n_scope}.models.#{context.klass.model_name.singular}"] : [:"ransack.associations.#{context.klass.model_name.singular}.#{key}"]
|
||||||
defaults << context.traverse(key).model_name.human
|
defaults << context.traverse(key).model_name.human
|
||||||
options = {:count => 1, :default => defaults}
|
options = {:count => 1, :default => defaults}
|
||||||
I18n.translate(defaults.shift, options)
|
I18n.translate(defaults.shift, options)
|
||||||
|
@ -65,20 +65,20 @@ module Ransack
|
||||||
interpolations = {}
|
interpolations = {}
|
||||||
interpolations[:attr_fallback_name] = I18n.translate(
|
interpolations[:attr_fallback_name] = I18n.translate(
|
||||||
(associated_class ?
|
(associated_class ?
|
||||||
:"ransack.attributes.#{associated_class.model_name.underscore}.#{attr_name}" :
|
:"ransack.attributes.#{associated_class.model_name.singular}.#{attr_name}" :
|
||||||
:"ransack.attributes.#{context.klass.model_name.underscore}.#{attr_name}"
|
:"ransack.attributes.#{context.klass.model_name.singular}.#{attr_name}"
|
||||||
),
|
),
|
||||||
:default => [
|
:default => [
|
||||||
(associated_class ?
|
(associated_class ?
|
||||||
:"#{associated_class.i18n_scope}.attributes.#{associated_class.model_name.underscore}.#{attr_name}" :
|
:"#{associated_class.i18n_scope}.attributes.#{associated_class.model_name.singular}.#{attr_name}" :
|
||||||
:"#{context.klass.i18n_scope}.attributes.#{context.klass.model_name.underscore}.#{attr_name}"
|
:"#{context.klass.i18n_scope}.attributes.#{context.klass.model_name.singular}.#{attr_name}"
|
||||||
),
|
),
|
||||||
:".attributes.#{attr_name}",
|
:".attributes.#{attr_name}",
|
||||||
attr_name.humanize
|
attr_name.humanize
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
defaults = [
|
defaults = [
|
||||||
:"ransack.attributes.#{context.klass.model_name.underscore}.#{name}"
|
:"ransack.attributes.#{context.klass.model_name.singular}.#{name}"
|
||||||
]
|
]
|
||||||
if include_associations && associated_class
|
if include_associations && associated_class
|
||||||
defaults << '%{association_name} %{attr_fallback_name}'
|
defaults << '%{association_name} %{attr_fallback_name}'
|
||||||
|
|
|
@ -14,8 +14,8 @@ Gem::Specification.new do |s|
|
||||||
|
|
||||||
s.rubyforge_project = "ransack"
|
s.rubyforge_project = "ransack"
|
||||||
|
|
||||||
s.add_dependency 'activerecord', '~> 3.0'
|
s.add_dependency 'activerecord', '>= 3.0'
|
||||||
s.add_dependency 'actionpack', '~> 3.0'
|
s.add_dependency 'actionpack', '>= 3.0'
|
||||||
s.add_dependency 'polyamorous', '~> 0.6.0'
|
s.add_dependency 'polyamorous', '~> 0.6.0'
|
||||||
s.add_development_dependency 'rspec', '~> 2.8.0'
|
s.add_development_dependency 'rspec', '~> 2.8.0'
|
||||||
s.add_development_dependency 'machinist', '~> 1.0.6'
|
s.add_development_dependency 'machinist', '~> 1.0.6'
|
||||||
|
|
|
@ -6,6 +6,12 @@ module Ransack
|
||||||
describe Context do
|
describe Context do
|
||||||
subject { Context.new(Person) }
|
subject { Context.new(Person) }
|
||||||
|
|
||||||
|
describe '#relation_for' do
|
||||||
|
it 'returns relation for given object' do
|
||||||
|
subject.object.should be_an ::ActiveRecord::Relation
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe '#evaluate' do
|
describe '#evaluate' do
|
||||||
it 'evaluates search obects' do
|
it 'evaluates search obects' do
|
||||||
search = Search.new(Person, :name_eq => 'Joe Blow')
|
search = Search.new(Person, :name_eq => 'Joe Blow')
|
||||||
|
|
Loading…
Reference in New Issue