[WIP] Rails 5 / Arel 7 compatibility

Ransack hasn’t been working with Rails master targeting 5.0.0 and Arel
7 since last December when 3 months of heavy refactoring of Active
Record and Arel began.

Now that things have calmed down a bit (for the moment), let’s try to
bring Ransack up to speed with the new changes.
This commit is contained in:
jonatack 2015-03-21 16:15:06 +05:30
parent 4534bfb45e
commit 9e818cde53
2 changed files with 23 additions and 18 deletions

View File

@ -96,7 +96,7 @@ module Ransack
def join_sources def join_sources
base = base =
if ::ActiveRecord::VERSION::MAJOR >= 5 if ::ActiveRecord::VERSION::MAJOR >= 5
Arel::SelectManager.new(@object.engine) Arel::SelectManager.new(@object.table)
else else
Arel::SelectManager.new(@object.engine, @object.table) Arel::SelectManager.new(@object.engine, @object.table)
end end
@ -171,39 +171,37 @@ 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
build_join_dependency(relation) build_joins(relation)
end end
end end
# Checkout active_record/relation/query_methods.rb +build_joins+ for # Checkout active_record/relation/query_methods.rb +build_joins+ for
# reference. Lots of duplicated code maybe we can avoid it # reference. Lots of duplicated code maybe we can avoid it
def build_join_dependency(relation) def build_joins(relation)
buckets = relation.joins_values.group_by do |join| buckets = relation.joins_values.group_by do |join|
case join case join
when String when String
Constants::STRING_JOIN :string_join
when Hash, Symbol, Array when Hash, Symbol, Array
Constants::ASSOCIATION_JOIN :association_join
when JoinDependency, JoinDependency::JoinAssociation when ActiveRecord::Associations::JoinDependency
Constants::STASHED_JOIN :stashed_join
when Arel::Nodes::Join when Arel::Nodes::Join
Constants::JOIN_NODE :join_node
else else
raise 'unknown class: %s' % join.class.name raise 'unknown class: %s' % join.class.name
end end
end end
buckets.default = []
association_joins = buckets[Constants::ASSOCIATION_JOIN] || [] association_joins = buckets[:association_join]
stashed_association_joins = buckets[:stashed_join]
stashed_association_joins = buckets[Constants::STASHED_JOIN] || [] join_nodes = buckets[:join_node].uniq
string_joins = buckets[:string_join].map(&:strip).uniq
join_nodes = buckets[Constants::JOIN_NODE] || []
string_joins = (buckets[Constants::STRING_JOIN] || []).map(&:strip).uniq
join_list = join_list =
if ::ActiveRecord::VERSION::MAJOR >= 5 if ::ActiveRecord::VERSION::MAJOR >= 5
relation.send :custom_join_ast, relation.table.from, string_joins join_nodes +
convert_join_strings_to_ast(relation.table, string_joins)
else else
relation.send :custom_join_ast, relation.send :custom_join_ast,
relation.table.from(relation.table), string_joins relation.table.from(relation.table), string_joins
@ -224,6 +222,13 @@ module Ransack
end end
end end
def convert_join_strings_to_ast(table, joins)
joins
.flatten
.reject(&:blank?)
.map { |join| table.create_string_join(Arel.sql(join)) }
end
if ::ActiveRecord::VERSION::STRING >= Constants::RAILS_4_1 if ::ActiveRecord::VERSION::STRING >= Constants::RAILS_4_1
def build_or_find_association(name, parent = @base, klass = nil) def build_or_find_association(name, parent = @base, klass = nil)

View File

@ -37,7 +37,7 @@ module Ransack
end end
@default_table = Arel::Table.new( @default_table = Arel::Table.new(
@base.table_name, :as => @base.aliased_table_name, :engine => @engine @base.table_name, as: @base.aliased_table_name, type_caster: self
) )
@bind_pairs = Hash.new do |hash, key| @bind_pairs = Hash.new do |hash, key|
parent, attr_name = get_parent_and_attribute_name(key.to_s) parent, attr_name = get_parent_and_attribute_name(key.to_s)