MOAR AR version checks for 4.1 and 4.0 support

This commit is contained in:
Washington Luiz 2014-04-09 14:25:54 -03:00
parent dbb0c2b064
commit e15a86cdd4
3 changed files with 71 additions and 43 deletions

View File

@ -5,11 +5,7 @@ gem 'rake'
rails = ENV['RAILS'] || 'master'
gem 'arel', '~> 5.0.0'
gem 'polyamorous',
github: 'activerecord-hackery/polyamorous',
branch: 'rails-4.1'
gem 'polyamorous', github: 'activerecord-hackery/polyamorous'
case rails
when /\// # A path

View File

@ -127,7 +127,7 @@ module Ransack
'string_join'
when Hash, Symbol, Array
'association_join'
when ::ActiveRecord::Associations::JoinDependency
when JoinDependency, JoinDependency::JoinAssociation
'stashed_join'
when Arel::Nodes::Join
'join_node'
@ -157,11 +157,17 @@ module Ransack
join_dependency.alias_tracker.aliases[join.left.name.downcase] = 1
end
if ::ActiveRecord::VERSION::STRING >= "4.1"
join_dependency
else
join_dependency.graft(*stashed_association_joins)
end
end
if ::ActiveRecord::VERSION::STRING >= "4.1"
def build_or_find_association(name, parent = @base, klass = nil)
list = if ActiveRecord::VERSION::STRING >= "4.1"
list = if ::ActiveRecord::VERSION::STRING >= "4.1"
@join_dependency.join_root.children.detect
else
@join_dependency.join_associations
@ -202,6 +208,26 @@ module Ransack
@associations_pot ||= {}
@associations_pot[assoc] = parent
end
else
def build_or_find_association(name, parent = @base, klass = nil)
found_association = @join_dependency.join_associations
.detect do |assoc|
assoc.reflection.name == name &&
assoc.parent == parent &&
(!klass || assoc.reflection.klass == klass)
end
unless found_association
@join_dependency.send(:build, Polyamorous::Join.new(
name, @join_type, klass), parent)
found_association = @join_dependency.join_associations.last
# Leverage the stashed association functionality in AR
@object = @object.joins(found_association)
end
found_association
end
end
end
end
end

View File

@ -36,8 +36,15 @@ module Ransack
@join_dependency = join_dependency(@object)
@join_type = options[:join_type] || Arel::OuterJoin
@search_key = options[:search_key] || Ransack.options[:search_key]
if ::ActiveRecord::VERSION::STRING >= "4.1"
@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
)
@ -141,6 +148,5 @@ module Ransack
def sortable_attributes(str = '')
traverse(str).ransortable_attributes(auth_object)
end
end
end