Permalink
Browse files

ActiveRecord 3.0.3 support

  • Loading branch information...
cfis committed Nov 20, 2010
1 parent 69d43f9 commit b7c0ace512c4e95f501d9a2ff93c8980fb9e8010
View
@@ -1,4 +1,6 @@
== 3.0.1 2010-11-19
+* Support ActiveRecord 3.0.3 and Arel 2+
+* Require ActiveRecord 3.0.3 as minimum dependency
* Fix double quoting issue with table names - thanks to Kevin Motschiedler for a patch.
* Fix SQLiteAdapter class inheritance issue - thanks to Brandon Hauff for a patch.
View
@@ -40,7 +40,7 @@ spec = Gem::Specification.new do |s|
# Dependencies
s.required_ruby_version = '>= 1.8.7'
- s.add_dependency('activerecord', '>= 3.0.1')
+ s.add_dependency('activerecord', '>= 3.0.3')
s.add_development_dependency "rspec"
end
@@ -22,7 +22,6 @@ def construct_sql
construct_counter_sql
end
- # Deletes the records according to the <tt>:dependent</tt> option.
def delete_records(records)
case @reflection.options[:dependent]
when :destroy
@@ -32,9 +31,10 @@ def delete_records(records)
else
relation = Arel::Table.new(@reflection.table_name)
# CPK
- # relation.where(relation[@reflection.primary_key_name].eq(@owner.id).
- # and(Arel::Predicates::In.new(relation[@reflection.klass.primary_key], records.map(&:id)))
- # ).update(relation[@reflection.primary_key_name] => nil)
+ #relation.where(relation[@reflection.primary_key_name].eq(@owner.id).
+ # and(relation[@reflection.klass.primary_key].in(records.map { |r| r.id }))
+ #).update(relation[@reflection.primary_key_name] => nil)
+
id_predicate = nil
owner_key_values = @reflection.cpk_primary_key.zip([@owner.id].flatten)
owner_key_values.each do |key, value|
@@ -57,8 +57,9 @@ def delete_records(records)
relation = relation.where(id_predicate.and(record_predicates))
+ nullify_relation = Arel::Table.new(@reflection.table_name)
nullify = @reflection.cpk_primary_key.inject(Hash.new) do |hash, key|
- hash[relation[key]] = nil
+ hash[nullify_relation[key]] = nil
hash
end
@@ -1,27 +1,29 @@
-module ActiveRecord
- module Calculations
- def execute_simple_calculation(operation, column_name, distinct)
- # CPK changes
- if column_name.kind_of?(Array)
- columns = column_name.map do |primary_key_column|
- table[primary_key_column].to_sql
- end
- projection = "DISTINCT #{columns.join(',')}"
- subquery = "(#{table.project(projection).to_sql}) AS subquery"
- relation = Arel::Table.new(subquery).project(Arel::SqlLiteral.new('*').count)
- type_cast_calculated_value(@klass.connection.select_value(relation.to_sql),
- column_for(column_name.first), operation)
- else
- column = if @klass.column_names.include?(column_name.to_s)
- Arel::Attribute.new(@klass.unscoped, column_name)
- else
- Arel::SqlLiteral.new(column_name == :all ? "*" : column_name.to_s)
- end
+# TODO - This code doesn't work with ActiveRecord 3.0.3...
- # Postgresql doesn't like ORDER BY when there are no GROUP BY
- relation = except(:order).select(operation == 'count' ? column.count(distinct) : column.send(operation))
- type_cast_calculated_value(@klass.connection.select_value(relation.to_sql), column_for(column_name), operation)
- end
- end
- end
-end
+#module ActiveRecord
+# module Calculations
+# def execute_simple_calculation(operation, column_name, distinct)
+# # CPK changes
+# if column_name.kind_of?(Array)
+# columns = column_name.map do |primary_key_column|
+# table[primary_key_column].to_sql
+# end
+# projection = "DISTINCT #{columns.join(',')}"
+# subquery = "(#{table.project(projection).to_sql}) AS subquery"
+# relation = Arel::Table.new(subquery).project(Arel::SqlLiteral.new('*').count)
+# type_cast_calculated_value(@klass.connection.select_value(relation.to_sql),
+# column_for(column_name.first), operation)
+# else
+# column = if @klass.column_names.include?(column_name.to_s)
+# Arel::Attribute.new(@klass.unscoped, column_name)
+# else
+# Arel::SqlLiteral.new(column_name == :all ? "*" : column_name.to_s)
+# end
+#
+# # Postgresql doesn't like ORDER BY when there are no GROUP BY
+# relation = except(:order).select(operation == 'count' ? column.count(distinct) : column.send(operation))
+# type_cast_calculated_value(@klass.connection.select_value(relation.to_sql), column_for(column_name), operation)
+# end
+# end
+# end
+#end
@@ -7,10 +7,10 @@ def construct_limited_ids_condition(relation)
# CPK
# values = @klass.connection.distinct("#{@klass.connection.quote_table_name @klass.table_name}.#{@klass.primary_key}", orders)
-
keys = @klass.primary_keys.map do |key|
"#{@klass.connection.quote_table_name @klass.table_name}.#{key}"
end
+
values = @klass.connection.distinct(keys.join(', '), orders)
ids_array = relation.select(values).collect {|row| row[@klass.primary_key]}
@@ -25,10 +25,18 @@ def construct_limited_ids_condition(relation)
and_expressions = [self.primary_keys, id_set].transpose.map do |key, id|
table[key].eq(id)
end
- Arel::Predicates::All.new(*and_expressions)
+
+ # Merge all the ands together
+ first = and_expressions.shift
+ Arel::Nodes::Grouping.new(and_expressions.inject(first) do |memo, expr|
+ Arel::Nodes::And.new(memo, expr)
+ end)
end
- Arel::Predicates::Any.new(*or_expressions).to_sql
+ first = or_expressions.shift
+ Arel::Nodes::Grouping.new(or_expressions.inject(first) do |memo, expr|
+ Arel::Nodes::Or.new(memo, expr)
+ end)
end
def exists?(id = nil)
@@ -2,8 +2,7 @@ module CompositePrimaryKeys
module VERSION #:nodoc:
MAJOR = 3
MINOR = 0
- TINY = 1
- PATCH = 0
- STRING = [MAJOR, MINOR, TINY, PATCH].join('.')
+ TINY = 3
+ STRING = [MAJOR, MINOR, TINY].join('.')
end
end

0 comments on commit b7c0ace

Please sign in to comment.