Skip to content

Commit

Permalink
Merge pull request #17 from appfolio/Rails42PatchFixes
Browse files Browse the repository at this point in the history
fix Rails 4.2 patches to better match the code in Rails 4.2
  • Loading branch information
jonkessler committed Aug 2, 2015
2 parents 85dec6b + 433f645 commit 1e339a1
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions lib/store_base_sti_class_for_4_2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ def build_scope
scope = klass.unscoped

values = reflection_scope.values
reflection_binds = reflection_scope.bind_values
preload_values = preload_scope.values
preload_binds = preload_scope.bind_values

scope.where_values = Array(values[:where]) + Array(preload_values[:where])
scope.references_values = Array(values[:references]) + Array(preload_values[:references])
scope.bind_values = (reflection_binds + preload_binds)

select_method = scope.respond_to?(:select!) ? :select! : :_select!
scope.send select_method, preload_values[:select] || values[:select] || table[Arel.star]
scope._select! preload_values[:select] || values[:select] || table[Arel.star]
scope.includes! preload_values[:includes] || values[:includes]
scope.joins! preload_values[:joins] || values[:joins]
scope.order! preload_values[:order] || values[:order]

if preload_values.key? :order
scope.order! preload_values[:order]
else
if values.key? :order
scope.order! values[:order]
end
if preload_values[:readonly] || values[:readonly]
scope.readonly!
end

if options[:as]
Expand Down Expand Up @@ -107,10 +107,11 @@ def through_scope
unless reflection_scope.where_values.empty?
scope.includes_values = Array(reflection_scope.values[:includes] || options[:source])
scope.where_values = reflection_scope.values[:where]
scope.bind_values = reflection_scope.bind_values
end

scope.references! reflection_scope.values[:references]
scope.order! reflection_scope.values[:order] if scope.eager_loading?
scope = scope.order reflection_scope.values[:order] if scope.eager_loading?
end

scope
Expand Down Expand Up @@ -141,13 +142,7 @@ def self.get_bind_values(owner, chain)
end
binds
end
class BindSubstitution
def bind_value(scope, column, value, alias_tracker)
substitute = alias_tracker.connection.substitute_at(column)
scope.bind_values += [[column, @block.call(value)]]
substitute
end
end

def next_chain_scope(scope, table, reflection, tracker, assoc_klass, foreign_table, next_reflection)
join_keys = reflection.join_keys(assoc_klass)
key = join_keys.key
Expand Down Expand Up @@ -176,6 +171,7 @@ def next_chain_scope(scope, table, reflection, tracker, assoc_klass, foreign_tab

scope = scope.joins(join(foreign_table, constraint))
end

def last_chain_scope(scope, table, reflection, owner, tracker, assoc_klass)
join_keys = reflection.join_keys(assoc_klass)
key = join_keys.key
Expand Down

0 comments on commit 1e339a1

Please sign in to comment.