Skip to content

Commit

Permalink
Merge branch 'arel'
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed Oct 15, 2009
2 parents a9f9ae3 + 6eee1dd commit c09a5ff
Show file tree
Hide file tree
Showing 36 changed files with 822 additions and 683 deletions.
Empty file removed .gitmodules
Empty file.
16 changes: 10 additions & 6 deletions actionpack/test/controller/caching_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions activerecord/Gemfile
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions activerecord/activerecord.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
22 changes: 14 additions & 8 deletions activerecord/lib/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand 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'
Expand Down Expand Up @@ -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'
354 changes: 146 additions & 208 deletions activerecord/lib/active_record/associations.rb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit c09a5ff

Please sign in to comment.