[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
base =
if ::ActiveRecord::VERSION::MAJOR >= 5
Arel::SelectManager.new(@object.engine)
Arel::SelectManager.new(@object.table)
else
Arel::SelectManager.new(@object.engine, @object.table)
end
@ -171,39 +171,37 @@ module Ransack
if relation.respond_to?(:join_dependency) # Squeel will enable this
relation.join_dependency
else
build_join_dependency(relation)
build_joins(relation)
end
end
# Checkout active_record/relation/query_methods.rb +build_joins+ for
# 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|
case join
when String
Constants::STRING_JOIN
:string_join
when Hash, Symbol, Array
Constants::ASSOCIATION_JOIN
when JoinDependency, JoinDependency::JoinAssociation
Constants::STASHED_JOIN
:association_join
when ActiveRecord::Associations::JoinDependency
:stashed_join
when Arel::Nodes::Join
Constants::JOIN_NODE
:join_node
else
raise 'unknown class: %s' % join.class.name
end
end
association_joins = buckets[Constants::ASSOCIATION_JOIN] || []
stashed_association_joins = buckets[Constants::STASHED_JOIN] || []
join_nodes = buckets[Constants::JOIN_NODE] || []
string_joins = (buckets[Constants::STRING_JOIN] || []).map(&:strip).uniq
buckets.default = []
association_joins = buckets[:association_join]
stashed_association_joins = buckets[:stashed_join]
join_nodes = buckets[:join_node].uniq
string_joins = buckets[:string_join].map(&:strip).uniq
join_list =
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
relation.send :custom_join_ast,
relation.table.from(relation.table), string_joins
@ -224,6 +222,13 @@ module Ransack
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
def build_or_find_association(name, parent = @base, klass = nil)

View File

@ -37,7 +37,7 @@ module Ransack
end
@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|
parent, attr_name = get_parent_and_attribute_name(key.to_s)