Skip to content

Commit

Permalink
%s/find(:\(first\|last\|all\), \([^()]*\))/scoped(\2).\1/gcI amongst …
Browse files Browse the repository at this point in the history
…other things
  • Loading branch information
jonleighton committed Apr 27, 2012
1 parent a57b784 commit df6f971
Show file tree
Hide file tree
Showing 30 changed files with 333 additions and 415 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/session_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def self.find_by_session_id(*args)
class << self; remove_possible_method :find_by_session_id; end

def self.find_by_session_id(session_id)
find :first, :conditions => {:session_id=>session_id}
where(session_id: session_id).first
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def test_calculations_work_with_reserved_words
end

def test_associations_work_with_reserved_words
assert_nothing_raised { Select.find(:all, :include => [:groups]) }
assert_nothing_raised { Select.scoped(:include => [:groups]).all }
end

#the following functions were added to DRY test cases
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class BindParameterTest < ActiveRecord::TestCase

def test_update_question_marks
str = "foo?bar"
x = Topic.find :first
x = Topic.first
x.title = str
x.content = str
x.save!
Expand All @@ -28,7 +28,7 @@ def test_create_question_marks

def test_update_null_bytes
str = "foo\0bar"
x = Topic.find :first
x = Topic.first
x.title = str
x.content = str
x.save!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def test_calculations_work_with_reserved_words
end

def test_associations_work_with_reserved_words
assert_nothing_raised { Select.find(:all, :include => [:groups]) }
assert_nothing_raised { Select.scoped(:include => [:groups]).all }
end

#the following functions were added to DRY test cases
Expand Down
6 changes: 3 additions & 3 deletions activerecord/test/cases/adapters/postgresql/hstore_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,21 @@ def test_parse7

def test_rewrite
@connection.execute "insert into hstores (tags) VALUES ('1=>2')"
x = Hstore.find :first
x = Hstore.first
x.tags = { '"a\'' => 'b' }
assert x.save!
end


def test_select
@connection.execute "insert into hstores (tags) VALUES ('1=>2')"
x = Hstore.find :first
x = Hstore.first
assert_equal({'1' => '2'}, x.tags)
end

def test_select_multikey
@connection.execute "insert into hstores (tags) VALUES ('1=>2,2=>3')"
x = Hstore.find :first
x = Hstore.first
assert_equal({'1' => '2', '2' => '3'}, x.tags)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ def test_natural_assignment_with_primary_key
def test_eager_loading_with_primary_key
Firm.create("name" => "Apple")
Client.create("name" => "Citibank", :firm_name => "Apple")
citibank_result = Client.find(:first, :conditions => {:name => "Citibank"}, :include => :firm_with_primary_key)
citibank_result = Client.scoped(:conditions => {:name => "Citibank"}, :include => :firm_with_primary_key).first
assert citibank_result.association_cache.key?(:firm_with_primary_key)
end

def test_eager_loading_with_primary_key_as_symbol
Firm.create("name" => "Apple")
Client.create("name" => "Citibank", :firm_name => "Apple")
citibank_result = Client.find(:first, :conditions => {:name => "Citibank"}, :include => :firm_with_primary_key_symbols)
citibank_result = Client.scoped(:conditions => {:name => "Citibank"}, :include => :firm_with_primary_key_symbols).first
assert citibank_result.association_cache.key?(:firm_with_primary_key_symbols)
end

Expand Down Expand Up @@ -334,7 +334,7 @@ def test_assignment_before_child_saved_with_primary_key
def test_new_record_with_foreign_key_but_no_object
c = Client.new("firm_id" => 1)
# sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
assert_equal Firm.find(:first, :order => "id"), c.firm_with_basic_id
assert_equal Firm.scoped(:order => "id").first, c.firm_with_basic_id
end

def test_setting_foreign_key_after_nil_target_loaded
Expand Down Expand Up @@ -396,7 +396,7 @@ def test_custom_counter_cache
def test_association_assignment_sticks
post = Post.first

author1, author2 = Author.find(:all, :limit => 2)
author1, author2 = Author.scoped(:limit => 2).all
assert_not_nil author1
assert_not_nil author2

Expand Down
28 changes: 14 additions & 14 deletions activerecord/test/cases/associations/cascaded_eager_loading_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
:categorizations, :people, :categories, :edges, :vertices

def test_eager_association_loading_with_cascaded_two_levels
authors = Author.find(:all, :include=>{:posts=>:comments}, :order=>"authors.id")
authors = Author.scoped(:include=>{:posts=>:comments}, :order=>"authors.id").all
assert_equal 3, authors.size
assert_equal 5, authors[0].posts.size
assert_equal 3, authors[1].posts.size
assert_equal 10, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
end

def test_eager_association_loading_with_cascaded_two_levels_and_one_level
authors = Author.find(:all, :include=>[{:posts=>:comments}, :categorizations], :order=>"authors.id")
authors = Author.scoped(:include=>[{:posts=>:comments}, :categorizations], :order=>"authors.id").all
assert_equal 3, authors.size
assert_equal 5, authors[0].posts.size
assert_equal 3, authors[1].posts.size
Expand Down Expand Up @@ -84,37 +84,37 @@ def test_eager_association_loading_with_join_for_count
end

def test_eager_association_loading_with_cascaded_two_levels_with_two_has_many_associations
authors = Author.find(:all, :include=>{:posts=>[:comments, :categorizations]}, :order=>"authors.id")
authors = Author.scoped(:include=>{:posts=>[:comments, :categorizations]}, :order=>"authors.id").all
assert_equal 3, authors.size
assert_equal 5, authors[0].posts.size
assert_equal 3, authors[1].posts.size
assert_equal 10, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
end

def test_eager_association_loading_with_cascaded_two_levels_and_self_table_reference
authors = Author.find(:all, :include=>{:posts=>[:comments, :author]}, :order=>"authors.id")
authors = Author.scoped(:include=>{:posts=>[:comments, :author]}, :order=>"authors.id").all
assert_equal 3, authors.size
assert_equal 5, authors[0].posts.size
assert_equal authors(:david).name, authors[0].name
assert_equal [authors(:david).name], authors[0].posts.collect{|post| post.author.name}.uniq
end

def test_eager_association_loading_with_cascaded_two_levels_with_condition
authors = Author.find(:all, :include=>{:posts=>:comments}, :conditions=>"authors.id=1", :order=>"authors.id")
authors = Author.scoped(:include=>{:posts=>:comments}, :conditions=>"authors.id=1", :order=>"authors.id").all
assert_equal 1, authors.size
assert_equal 5, authors[0].posts.size
end

def test_eager_association_loading_with_cascaded_three_levels_by_ping_pong
firms = Firm.find(:all, :include=>{:account=>{:firm=>:account}}, :order=>"companies.id")
firms = Firm.scoped(:include=>{:account=>{:firm=>:account}}, :order=>"companies.id").all
assert_equal 2, firms.size
assert_equal firms.first.account, firms.first.account.firm.account
assert_equal companies(:first_firm).account, assert_no_queries { firms.first.account.firm.account }
assert_equal companies(:first_firm).account.firm.account, assert_no_queries { firms.first.account.firm.account }
end

def test_eager_association_loading_with_has_many_sti
topics = Topic.find(:all, :include => :replies, :order => 'topics.id')
topics = Topic.scoped(:include => :replies, :order => 'topics.id').all
first, second, = topics(:first).replies.size, topics(:second).replies.size
assert_no_queries do
assert_equal first, topics[0].replies.size
Expand All @@ -127,22 +127,22 @@ def test_eager_association_loading_with_has_many_sti_and_subclasses
silly.parent_id = 1
assert silly.save

topics = Topic.find(:all, :include => :replies, :order => ['topics.id', 'replies_topics.id'])
topics = Topic.scoped(:include => :replies, :order => ['topics.id', 'replies_topics.id']).all
assert_no_queries do
assert_equal 2, topics[0].replies.size
assert_equal 0, topics[1].replies.size
end
end

def test_eager_association_loading_with_belongs_to_sti
replies = Reply.find(:all, :include => :topic, :order => 'topics.id')
replies = Reply.scoped(:include => :topic, :order => 'topics.id').all
assert replies.include?(topics(:second))
assert !replies.include?(topics(:first))
assert_equal topics(:first), assert_no_queries { replies.first.topic }
end

def test_eager_association_loading_with_multiple_stis_and_order
author = Author.find(:first, :include => { :posts => [ :special_comments , :very_special_comment ] }, :order => ['authors.name', 'comments.body', 'very_special_comments_posts.body'], :conditions => 'posts.id = 4')
author = Author.scoped(:include => { :posts => [ :special_comments , :very_special_comment ] }, :order => ['authors.name', 'comments.body', 'very_special_comments_posts.body'], :conditions => 'posts.id = 4').first
assert_equal authors(:david), author
assert_no_queries do
author.posts.first.special_comments
Expand All @@ -151,7 +151,7 @@ def test_eager_association_loading_with_multiple_stis_and_order
end

def test_eager_association_loading_of_stis_with_multiple_references
authors = Author.find(:all, :include => { :posts => { :special_comments => { :post => [ :special_comments, :very_special_comment ] } } }, :order => 'comments.body, very_special_comments_posts.body', :conditions => 'posts.id = 4')
authors = Author.scoped(:include => { :posts => { :special_comments => { :post => [ :special_comments, :very_special_comment ] } } }, :order => 'comments.body, very_special_comments_posts.body', :conditions => 'posts.id = 4').all
assert_equal [authors(:david)], authors
assert_no_queries do
authors.first.posts.first.special_comments.first.post.special_comments
Expand All @@ -160,20 +160,20 @@ def test_eager_association_loading_of_stis_with_multiple_references
end

def test_eager_association_loading_where_first_level_returns_nil
authors = Author.find(:all, :include => {:post_about_thinking => :comments}, :order => 'authors.id DESC')
authors = Author.scoped(:include => {:post_about_thinking => :comments}, :order => 'authors.id DESC').all
assert_equal [authors(:bob), authors(:mary), authors(:david)], authors
assert_no_queries do
authors[2].post_about_thinking.comments.first
end
end

def test_eager_association_loading_with_recursive_cascading_four_levels_has_many_through
source = Vertex.find(:first, :include=>{:sinks=>{:sinks=>{:sinks=>:sinks}}}, :order => 'vertices.id')
source = Vertex.scoped(:include=>{:sinks=>{:sinks=>{:sinks=>:sinks}}}, :order => 'vertices.id').first
assert_equal vertices(:vertex_4), assert_no_queries { source.sinks.first.sinks.first.sinks.first }
end

def test_eager_association_loading_with_recursive_cascading_four_levels_has_and_belongs_to_many
sink = Vertex.find(:first, :include=>{:sources=>{:sources=>{:sources=>:sources}}}, :order => 'vertices.id DESC')
sink = Vertex.scoped(:include=>{:sources=>{:sources=>{:sources=>:sources}}}, :order => 'vertices.id DESC').first
assert_equal vertices(:vertex_1), assert_no_queries { sink.sources.first.sources.first.sources.first.sources.first }
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def generate_test_object_graphs

def test_include_query
res = 0
res = ShapeExpression.find :all, :include => [ :shape, { :paint => :non_poly } ]
res = ShapeExpression.scoped(:include => [ :shape, { :paint => :non_poly } ]).all
assert_equal NUM_SHAPE_EXPRESSIONS, res.size
assert_queries(0) do
res.each do |se|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,43 +103,43 @@ def teardown
def test_eager_no_extra_singularization_belongs_to
return unless @have_tables
assert_nothing_raised do
Virus.find(:all, :include => :octopus)
Virus.scoped(:include => :octopus).all
end
end

def test_eager_no_extra_singularization_has_one
return unless @have_tables
assert_nothing_raised do
Octopus.find(:all, :include => :virus)
Octopus.scoped(:include => :virus).all
end
end

def test_eager_no_extra_singularization_has_many
return unless @have_tables
assert_nothing_raised do
Bus.find(:all, :include => :passes)
Bus.scoped(:include => :passes).all
end
end

def test_eager_no_extra_singularization_has_and_belongs_to_many
return unless @have_tables
assert_nothing_raised do
Crisis.find(:all, :include => :messes)
Mess.find(:all, :include => :crises)
Crisis.scoped(:include => :messes).all
Mess.scoped(:include => :crises).all
end
end

def test_eager_no_extra_singularization_has_many_through_belongs_to
return unless @have_tables
assert_nothing_raised do
Crisis.find(:all, :include => :successes)
Crisis.scoped(:include => :successes).all
end
end

def test_eager_no_extra_singularization_has_many_through_has_many
return unless @have_tables
assert_nothing_raised do
Crisis.find(:all, :include => :compresses)
Crisis.scoped(:include => :compresses).all
end
end
end
Loading

0 comments on commit df6f971

Please sign in to comment.