Permalink
Browse files

Merge branch 'arel'

  • Loading branch information...
jeremy committed Oct 15, 2009
2 parents a9f9ae3 + 6eee1dd commit c09a5ffea7780a0f940f3f963c892f0ae1316802
Showing with 822 additions and 683 deletions.
  1. 0 .gitmodules
  2. +10 −6 actionpack/test/controller/caching_test.rb
  3. +10 −0 activerecord/Gemfile
  4. +1 −0 activerecord/activerecord.gemspec
  5. +14 −8 activerecord/lib/active_record.rb
  6. +146 −208 activerecord/lib/active_record/associations.rb
  7. +0 −9 activerecord/lib/active_record/associations/association_proxy.rb
  8. +2 −2 activerecord/lib/active_record/associations/belongs_to_association.rb
  9. +10 −12 activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
  10. +9 −9 activerecord/lib/active_record/associations/has_many_association.rb
  11. +1 −1 activerecord/lib/active_record/associations/has_many_through_association.rb
  12. +1 −1 activerecord/lib/active_record/associations/through_association_scope.rb
  13. +173 −143 activerecord/lib/active_record/base.rb
  14. +87 −116 activerecord/lib/active_record/calculations.rb
  15. +4 −31 activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
  16. +0 −6 activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
  17. +2 −13 activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
  18. +1 −14 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
  19. +5 −4 activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
  20. +15 −12 activerecord/lib/active_record/locking/optimistic.rb
  21. +28 −31 activerecord/lib/active_record/migration.rb
  22. +129 −0 activerecord/lib/active_record/relation.rb
  23. +0 −12 activerecord/test/cases/adapter_test.rb
  24. +1 −1 activerecord/test/cases/associations/cascaded_eager_loading_test.rb
  25. +0 −2 activerecord/test/cases/associations/eager_load_nested_include_test.rb
  26. +5 −5 activerecord/test/cases/associations/eager_test.rb
  27. +4 −4 activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
  28. +1 −1 activerecord/test/cases/associations/inner_join_association_test.rb
  29. +0 −19 activerecord/test/cases/associations_test.rb
  30. +1 −1 activerecord/test/cases/base_test.rb
  31. +1 −1 activerecord/test/cases/calculations_test.rb
  32. +2 −2 activerecord/test/cases/finder_test.rb
  33. +2 −2 activerecord/test/cases/inheritance_test.rb
  34. +3 −3 activerecord/test/cases/method_scoping_test.rb
  35. +4 −4 activerecord/test/cases/named_scope_test.rb
  36. +150 −0 activerecord/test/cases/relations_test.rb
View
No changes.
@@ -149,7 +149,9 @@ def page_cached?(action)
class ActionCachingTestController < ActionController::Base
rescue_from(Exception) { head 500 }
- rescue_from(ActiveRecord::RecordNotFound) { head :not_found }
+ if defined? ActiveRecord
+ rescue_from(ActiveRecord::RecordNotFound) { head :not_found }
+ end
caches_action :index, :redirected, :forbidden, :if => Proc.new { |c| !c.request.format.json? }, :expires_in => 1.hour
caches_action :show, :cache_path => 'http://test.host/custom/show'
@@ -474,11 +476,13 @@ def test_file_extensions
assert_response :success
end
- def test_record_not_found_returns_404_for_multiple_requests
- get :record_not_found
- assert_response 404
- get :record_not_found
- assert_response 404
+ if defined? ActiveRecord
+ def test_record_not_found_returns_404_for_multiple_requests
+ get :record_not_found
+ assert_response 404
+ get :record_not_found
+ assert_response 404
+ end
end
def test_four_oh_four_returns_404_for_multiple_requests
View
@@ -0,0 +1,10 @@
+Gem.sources.each { |uri| source uri }
+sibling = "#{File.dirname(__FILE__)}/.."
+
+gem "activesupport", "3.0.pre", :vendored_at => "#{sibling}/activesupport"
+gem "activemodel", "3.0.pre", :vendored_at => "#{sibling}/activemodel"
+gem "arel", :git => "git://github.com/rails/arel.git", :branch => 'master'
+
+only :test do
+ gem "mocha"
+end
@@ -9,6 +9,7 @@ Gem::Specification.new do |s|
s.add_dependency('activesupport', '= 3.0.pre')
s.add_dependency('activemodel', '= 3.0.pre')
+ s.add_dependency('arel', '~> 0.1.0')
s.require_path = 'lib'
s.autorequire = 'active_record'
@@ -21,17 +21,21 @@
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++
-activesupport_path = "#{File.dirname(__FILE__)}/../../activesupport/lib"
-$:.unshift(activesupport_path) if File.directory?(activesupport_path)
-require 'active_support'
+bundled = "#{File.dirname(__FILE__)}/../vendor/gems/environment"
+if File.exist?("#{bundled}.rb")
+ require bundled
+else
+ activesupport_path = "#{File.dirname(__FILE__)}/../../activesupport/lib"
+ $:.unshift(activesupport_path) if File.directory?(activesupport_path)
-begin
- require 'active_model'
-rescue LoadError
- $:.unshift "#{File.dirname(__FILE__)}/../../activemodel/lib"
- require 'active_model'
+ activemodel_path = "#{File.dirname(__FILE__)}/../../activemodel/lib"
+ $:.unshift(activemodel_path) if File.directory?(activemodel_path)
end
+require 'active_support'
+require 'active_model'
+require 'arel'
+
module ActiveRecord
# TODO: Review explicit loads to see if they will automatically be handled by the initializer.
def self.load_all!
@@ -48,6 +52,7 @@ def self.load_all!
autoload :Associations, 'active_record/associations'
autoload :AttributeMethods, 'active_record/attribute_methods'
autoload :AutosaveAssociation, 'active_record/autosave_association'
+ autoload :Relation, 'active_record/relation'
autoload :Base, 'active_record/base'
autoload :Batches, 'active_record/batches'
autoload :Calculations, 'active_record/calculations'
@@ -92,4 +97,5 @@ module ConnectionAdapters
end
end
+Arel::Table.engine = Arel::Sql::Engine.new(ActiveRecord::Base)
I18n.load_path << File.dirname(__FILE__) + '/active_record/locale/en.yml'

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -156,15 +156,6 @@ def dependent?
@reflection.options[:dependent]
end
- # Returns a string with the IDs of +records+ joined with a comma, quoted
- # if needed. The result is ready to be inserted into a SQL IN clause.
- #
- # quoted_record_ids(records) # => "23,56,58,67"
- #
- def quoted_record_ids(records)
- records.map { |record| record.quoted_id }.join(',')
- end
-
def interpolate_sql(sql, record = nil)
@owner.send(:interpolate_sql, sql, record)
end
@@ -36,11 +36,11 @@ def replace(record)
loaded
record
end
-
+
def updated?
@updated
end
-
+
private
def find_target
find_method = if @reflection.options[:primary_key]
@@ -34,7 +34,7 @@ def construct_find_options!(options)
options[:readonly] = finding_with_ambiguous_select?(options[:select] || @reflection.options[:select])
options[:select] ||= (@reflection.options[:select] || '*')
end
-
+
def count_records
load_target.size
end
@@ -56,26 +56,23 @@ def insert_record(record, force = true, validate = true)
if @reflection.options[:insert_sql]
@owner.connection.insert(interpolate_sql(@reflection.options[:insert_sql], record))
else
+ relation = arel_table(@reflection.options[:join_table])
attributes = columns.inject({}) do |attrs, column|
case column.name.to_s
when @reflection.primary_key_name.to_s
- attrs[column.name] = owner_quoted_id
+ attrs[relation[column.name]] = owner_quoted_id
when @reflection.association_foreign_key.to_s
- attrs[column.name] = record.quoted_id
+ attrs[relation[column.name]] = record.quoted_id
else
if record.has_attribute?(column.name)
value = @owner.send(:quote_value, record[column.name], column)
- attrs[column.name] = value unless value.nil?
+ attrs[relation[column.name]] = value unless value.nil?
end
end
attrs
end
- sql =
- "INSERT INTO #{@owner.connection.quote_table_name @reflection.options[:join_table]} (#{@owner.send(:quoted_column_names, attributes).join(', ')}) " +
- "VALUES (#{attributes.values.join(', ')})"
-
- @owner.connection.insert(sql)
+ relation.insert(attributes)
end
return true
@@ -85,9 +82,10 @@ def delete_records(records)
if sql = @reflection.options[:delete_sql]
records.each { |record| @owner.connection.delete(interpolate_sql(sql, record)) }
else
- ids = quoted_record_ids(records)
- sql = "DELETE FROM #{@owner.connection.quote_table_name @reflection.options[:join_table]} WHERE #{@reflection.primary_key_name} = #{owner_quoted_id} AND #{@reflection.association_foreign_key} IN (#{ids})"
- @owner.connection.delete(sql)
+ relation = arel_table(@reflection.options[:join_table])
+ relation.conditions(relation[@reflection.primary_key_name].eq(@owner.id).
+ and(Arel::In.new(relation[@reflection.association_foreign_key], records.map(&:id)))
+ ).delete
end
end
@@ -40,11 +40,11 @@ def count_records
# we are certain the current target is an empty array. This is a
# documented side-effect of the method that may avoid an extra SELECT.
@target ||= [] and loaded if count == 0
-
+
if @reflection.options[:limit]
count = [ @reflection.options[:limit], count ].min
end
-
+
return count
end
@@ -69,11 +69,11 @@ def delete_records(records)
when :delete_all
@reflection.klass.delete(records.map { |record| record.id })
else
- ids = quoted_record_ids(records)
- @reflection.klass.update_all(
- "#{@reflection.primary_key_name} = NULL",
- "#{@reflection.primary_key_name} = #{owner_quoted_id} AND #{@reflection.klass.primary_key} IN (#{ids})"
- )
+ relation = arel_table(@reflection.table_name)
+ relation.conditions(relation[@reflection.primary_key_name].eq(@owner.id).
+ and(Arel::In.new(relation[@reflection.klass.primary_key], records.map(&:id)))
+ ).update(relation[@reflection.primary_key_name] => nil)
+
@owner.class.update_counters(@owner.id, cached_counter_attribute_name => -records.size) if has_cached_counter?
end
end
@@ -88,11 +88,11 @@ def construct_sql
@finder_sql = interpolate_sql(@reflection.options[:finder_sql])
when @reflection.options[:as]
- @finder_sql =
+ @finder_sql =
"#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_id = #{owner_quoted_id} AND " +
"#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}"
@finder_sql << " AND (#{conditions})" if conditions
-
+
else
@finder_sql = "#{@reflection.quoted_table_name}.#{@reflection.primary_key_name} = #{owner_quoted_id}"
@finder_sql << " AND (#{conditions})" if conditions
@@ -56,7 +56,7 @@ def construct_find_options!(options)
options[:joins] = construct_joins(options[:joins])
options[:include] = @reflection.source_reflection.options[:include] if options[:include].nil? && @reflection.source_reflection.options[:include]
end
-
+
def insert_record(record, force = true, validate = true)
if record.new_record?
if force
@@ -42,7 +42,7 @@ def construct_quoted_owner_attributes(reflection)
end
def construct_from
- @reflection.quoted_table_name
+ @reflection.table_name
end
def construct_select(custom_select = nil)
Oops, something went wrong.

0 comments on commit c09a5ff

Please sign in to comment.