Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for rails 5.2 #859

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions lib/ransack/adapters/active_record/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class Context < ::Ransack::Context

def initialize(object, options = {})
super
@arel_visitor = @engine.connection.visitor
if ::ActiveRecord::VERSION::STRING < Constants::RAILS_5_2
@arel_visitor = @engine.connection.visitor
end
end

def relation_for(object)
Expand All @@ -25,7 +27,7 @@ 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
schema_cache = ::ActiveRecord::Base.connection.schema_cache
unless schema_cache.send(database_table_exists?, table)
raise "No table named #{table} exists."
end
Expand Down Expand Up @@ -135,7 +137,7 @@ def join_sources
end

def alias_tracker
@join_dependency.alias_tracker
@join_dependency.send(:alias_tracker)
end

def lock_association(association)
Expand All @@ -145,11 +147,11 @@ def lock_association(association)
if ::ActiveRecord::VERSION::STRING >= Constants::RAILS_4_1
def remove_association(association)
return if @lock_associations.include?(association)
@join_dependency.join_root.children.delete_if { |stashed|
@join_dependency.instance_variable_get(:@join_root).children.delete_if { |stashed|
stashed.eql?(association)
}
@object.joins_values.delete_if { |jd|
jd.join_root.children.map(&:object_id) == [association.object_id]
jd.instance_variable_get(:@join_root).children.map(&:object_id) == [association.object_id]
}
end
else
Expand Down Expand Up @@ -279,12 +281,14 @@ def build_joins(relation)
relation.table.from(relation.table), string_joins
end

alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(::ActiveRecord::Base.connection, relation.table.name, join_list)

join_dependency = JoinDependency.new(
relation.klass, association_joins, join_list
relation.klass, relation.table, association_joins, alias_tracker
)

join_nodes.each do |join|
join_dependency.alias_tracker.aliases[join.left.name.downcase] = 1
join_dependency.send(:alias_tracker).aliases[join.left.name.downcase] = 1
end

if ::ActiveRecord::VERSION::STRING >= Constants::RAILS_4_1
Expand All @@ -308,29 +312,32 @@ def build_or_find_association(name, parent = @base, klass = nil)
if ::ActiveRecord::VERSION::STRING >= Constants::RAILS_4_1

def find_association(name, parent = @base, klass = nil)
@join_dependency.join_root.children.detect do |assoc|
@join_dependency.instance_variable_get(:@join_root).children.detect do |assoc|
assoc.reflection.name == name &&
(@associations_pot.empty? || @associations_pot[assoc] == parent) &&
(!klass || assoc.reflection.klass == klass)
end
end

def build_association(name, parent = @base, klass = nil)
alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(::ActiveRecord::Base.connection, parent.table.name, [])

jd = JoinDependency.new(
parent.base_klass,
parent.base_klass.arel_table,
Polyamorous::Join.new(name, @join_type, klass),
[]
alias_tracker
)
found_association = jd.join_root.children.last
found_association = jd.instance_variable_get(:@join_root).children.last
@associations_pot[found_association] = parent

# 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
@join_dependency.instance_variable_get(:@join_root).children.push found_association

# Builds the arel nodes properly for this association
@join_dependency.send(
:construct_tables!, jd.join_root, found_association
:construct_tables!, jd.instance_variable_get(:@join_root), found_association
)

# Leverage the stashed association functionality in AR
Expand All @@ -340,7 +347,7 @@ def build_association(name, parent = @base, klass = nil)
end

def extract_joins(association)
parent = @join_dependency.join_root
parent = @join_dependency.instance_variable_get(:@join_root)
reflection = association.reflection
join_constraints = if ::ActiveRecord::VERSION::STRING < Constants::RAILS_5_1
association.join_constraints(
Expand Down
15 changes: 12 additions & 3 deletions lib/ransack/adapters/active_record/ransack/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,26 @@ def initialize(object, options = {})
@associations_pot = {}
@lock_associations = []

if ::ActiveRecord::VERSION::STRING >= Constants::RAILS_4_1
if ::ActiveRecord::VERSION::STRING >= Constants::RAILS_5_2
@base = @join_dependency.instance_variable_get(:@join_root)
elsif ::ActiveRecord::VERSION::STRING >= Constants::RAILS_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, type_caster: self
if ::ActiveRecord::VERSION::STRING >= Constants::RAILS_5_2
@default_table = Arel::Table.new(
@base.table_name, as: @base.base_klass.table_name, type_caster: self
)
else
@default_table = Arel::Table.new(
@base.table_name, as: @base.aliased_table_name, type_caster: self
)
end

@bind_pairs = Hash.new do |hash, key|
parent, attr_name = get_parent_and_attribute_name(key)
if parent && attr_name
Expand Down
1 change: 1 addition & 0 deletions lib/ransack/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module Constants

RAILS_4_1 = '4.1'.freeze
RAILS_5_1 = '5.1'.freeze
RAILS_5_2 = '5.2'.freeze

RANSACK_SLASH_SEARCHES = 'ransack/searches'.freeze
RANSACK_SLASH_SEARCHES_SLASH_SEARCH = 'ransack/searches/search'.freeze
Expand Down
2 changes: 1 addition & 1 deletion lib/ransack/helpers/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module ActionView::Helpers::Tags
# https://github.com/rails/rails/commit/c1a118a
class Base
private
def value(object)
def value
object.send @method_name if object # use send instead of public_send
end
end
Expand Down
2 changes: 1 addition & 1 deletion ransack.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |s|
s.rubyforge_project = "ransack"

s.add_dependency 'actionpack', '>= 3.0'
s.add_dependency 'activerecord', '>= 3.0'
s.add_dependency 'activerecord', '>= 5.2.0.beta2'
s.add_dependency 'activesupport', '>= 3.0'
s.add_dependency 'i18n'
s.add_dependency 'polyamorous', '~> 1.3'
Expand Down