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,50 +157,76 @@ module Ransack
join_dependency.alias_tracker.aliases[join.left.name.downcase] = 1
end
join_dependency
if ::ActiveRecord::VERSION::STRING >= "4.1"
join_dependency
else
join_dependency.graft(*stashed_association_joins)
end
end
def build_or_find_association(name, parent = @base, klass = nil)
list = if ActiveRecord::VERSION::STRING >= "4.1"
@join_dependency.join_root.children.detect
else
@join_dependency.join_associations
end
if ::ActiveRecord::VERSION::STRING >= "4.1"
found_association = list.detect do |assoc|
assoc.reflection.name == name &&
@associations_pot[assoc] == parent &&
(!klass || assoc.reflection.klass == klass)
end
def build_or_find_association(name, parent = @base, klass = nil)
list = if ::ActiveRecord::VERSION::STRING >= "4.1"
@join_dependency.join_root.children.detect
else
@join_dependency.join_associations
end
unless found_association
jd = JoinDependency.new(
parent.base_klass,
Polyamorous::Join.new(name, @join_type, klass),
[]
)
found_association = jd.join_root.children.last
associations found_association, parent
found_association = list.detect do |assoc|
assoc.reflection.name == name &&
@associations_pot[assoc] == parent &&
(!klass || assoc.reflection.klass == klass)
end
# TODO maybe we dont need to push associations here, we could loop
# through the @associations_pot instead
@join_dependency.join_root.children.push found_association
# Builds the arel nodes properly for this association
@join_dependency.send(
:construct_tables!, jd.join_root, found_association
unless found_association
jd = JoinDependency.new(
parent.base_klass,
Polyamorous::Join.new(name, @join_type, klass),
[]
)
found_association = jd.join_root.children.last
associations found_association, parent
# Leverage the stashed association functionality in AR
@object = @object.joins(jd)
# TODO maybe we dont need to push associations here, we could loop
# through the @associations_pot instead
@join_dependency.join_root.children.push found_association
# Builds the arel nodes properly for this association
@join_dependency.send(
:construct_tables!, jd.join_root, found_association
)
# Leverage the stashed association functionality in AR
@object = @object.joins(jd)
end
found_association
end
found_association
end
def associations(assoc, parent)
@associations_pot ||= {}
@associations_pot[assoc] = parent
end
else
def associations(assoc, parent)
@associations_pot ||= {}
@associations_pot[assoc] = parent
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

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]
@base = @join_dependency.join_root
@engine = @base.base_klass.arel_engine
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