Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Commit

Permalink
Fix Rails 4.2 spec error.
Browse files Browse the repository at this point in the history
  • Loading branch information
bigxiang committed Feb 5, 2015
1 parent 05c9071 commit 6ab44c6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
32 changes: 26 additions & 6 deletions lib/squeel/adapters/active_record/4.2/relation_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,31 @@ def reverse_order!
self
end

def build_arel
arel = Arel::SelectManager.new(table.engine, table)

build_joins(arel, joins_values.flatten) unless joins_values.empty?

collapse_wheres(arel, where_visit((where_values - ['']).uniq))

arel.having(*having_visit(having_values.uniq.reject{|h| h.blank?})) unless having_values.empty?

arel.take(connection.sanitize_limit(limit_value)) if limit_value
arel.skip(offset_value.to_i) if offset_value

arel.group(*group_visit(group_values.uniq.reject{|g| g.blank?})) unless group_values.empty?

build_order(arel)

build_select(arel, select_visit(select_values.uniq))

arel.distinct(distinct_value)
arel.from(build_from) if from_value
arel.lock(lock_value) if lock_value

arel
end

def build_join_dependency(manager, joins)
buckets = joins.group_by do |join|
case join
Expand Down Expand Up @@ -88,13 +113,8 @@ def where_values_hash_with_squeel(relation_table_name = table_name)

def expand_attrs_from_hash(opts)
opts = ::ActiveRecord::PredicateBuilder.resolve_column_aliases(klass, opts)

bind_args = [opts]
# Active Record 4.1 compatibility
# (for commits before 08579e4078454c6058f1289b58bf5bfa26661376 - https://github.com/rails/rails/commit/08579e4078454c6058f1289b58bf5bfa26661376)
bind_args << bind_values.length if method(:create_binds).arity > 1
tmp_opts, bind_values = create_binds(*bind_args)

tmp_opts, bind_values = create_binds(opts)
self.bind_values += bind_values

attributes = @klass.send(:expand_hash_conditions_for_aggregates, tmp_opts)
Expand Down
14 changes: 12 additions & 2 deletions spec/squeel/adapters/active_record/relation_extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,12 @@ module ActiveRecord
})

arel = relation.build_arel
arel.to_sql.should match /#{Q}parents_people_2#{Q}.#{Q}name#{Q} = 'bob'/

if activerecord_version_at_least('4.2.0')
arel.to_sql.should match /#{Q}parents_people_2#{Q}.#{Q}name#{Q} = ?/
else
arel.to_sql.should match /#{Q}parents_people_2#{Q}.#{Q}name#{Q} = 'bob'/
end
end

it 'combines multiple conditions of the same type against the same column with AND' do
Expand Down Expand Up @@ -174,7 +179,12 @@ module ActiveRecord
})

arel = relation.build_arel
arel.to_sql.should match /HAVING #{Q}parents_people_2#{Q}.#{Q}name#{Q} = 'joe'/

if activerecord_version_at_least('4.2.0')
arel.to_sql.should match /HAVING #{Q}parents_people_2#{Q}.#{Q}name#{Q} = ?/
else
arel.to_sql.should match /HAVING #{Q}parents_people_2#{Q}.#{Q}name#{Q} = 'joe'/
end
end

it 'maps orders inside a hash to their appropriate association table' do
Expand Down

0 comments on commit 6ab44c6

Please sign in to comment.