Skip to content

Commit

Permalink
Add named_scope train method calling with grouped scope. Also fixed U…
Browse files Browse the repository at this point in the history
…RGENT spelling error.
  • Loading branch information
metaskills committed Sep 25, 2008
1 parent d70de93 commit 76f75a2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
7 changes: 7 additions & 0 deletions test/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
e.reports { |employee| [employee.association(:report),employee.association(:report)] }
end

Factory.define :employee_with_urgent_reports, :class => 'Employee' do |e|
e.name { "Factory Employee ##{Factory.next(:id)}" }
e.email { Factory.next(:email) }
e.reports { |employee| [employee.association(:report), employee.association(:report,:title=>'URGENT'),
employee.association(:report), employee.association(:report,:body=>'This is URGENT.')] }
end


Factory.define :legacy_employee do |e|
e.name { "Legacy Factory Employee ##{Factory.next(:id)}" }
Expand Down
58 changes: 42 additions & 16 deletions test/grouped_scope/has_many_association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,66 @@ def setup
end
end

context 'calling association extensions' do

context 'training association extensions' do
setup do
@e1 = Factory(:employee_with_reports, :group_id => 1)
@e1 = Factory(:employee_with_urgent_reports, :group_id => 1)
@e2 = Factory(:employee, :group_id => 1)
@urget_report = Factory.build(:report, :title => 'URGET')
@e1.reports << @urget_report
@e1.save!
@urgent_reports = @e1.reports.select(&:urgent_title?)
end

should 'find urget report via normal ungrouped association' do
assert_equal [@urget_report], @e1.reports(true).urget
should 'find urgent report via normal ungrouped association' do
assert_same_elements @urgent_reports, @e1.reports(true).urgent
end

should 'find urget report via grouped reflection' do
assert_equal [@urget_report], @e2.group.reports.urget
should 'find urgent report via grouped reflection' do
assert_same_elements @urgent_reports, @e2.group.reports(true).urgent
end

should 'use extension sql along with group reflection' do
assert_sql(/'URGET'/,/"reports".employee_id IN/) do
@e2.group.reports.urget
should 'use assoc extension SQL along with group reflection' do
assert_sql(/'URGENT'/,/"reports".employee_id IN/) do
@e2.group.reports(true).urgent
end
end

end

context 'training named scopes' do

setup do
@e1 = Factory(:employee_with_urgent_reports, :group_id => 1)
@e2 = Factory(:employee, :group_id => 1)
@urgent_titles = @e1.reports.select(&:urgent_title?)
@urgent_bodys = @e1.reports.select(&:urgent_body?)
end

should 'find urgent reports via normal named scopes by normal owner' do
assert_same_elements @urgent_titles, @e1.reports(true).with_urgent_title
assert_same_elements @urgent_bodys, @e1.reports(true).with_urgent_body
end

should 'find urgent reports via group reflection by group member' do
assert_same_elements @urgent_titles, @e2.group.reports(true).with_urgent_title
assert_same_elements @urgent_bodys, @e2.group.reports(true).with_urgent_body
end

should 'use named scope SQL along with group reflection' do
assert_sql(/body LIKE '%URGENT%'/,/"title" = 'URGENT'/,/employee_id IN/) do
@e2.group.reports(true).with_urgent_title.with_urgent_body.inspect
end
end

end


end

context 'for a LegacyEmployee' do

setup do
@employee = Factory(:legacy_employee)
end

should 'scope existing association to owner' do
assert_sql(/"legacy_reports".email = '#{@employee.id}'/) do
@employee.reports(true)
Expand Down
8 changes: 6 additions & 2 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,19 @@ def setup_database(options)

class Employee < ActiveRecord::Base
has_many :reports do
def urget
all(:conditions => {:title => 'URGET'})
def urgent
all(:conditions => {:title => 'URGENT'})
end
end
grouped_scope :reports
end

class Report < ActiveRecord::Base
named_scope :with_urgent_title, :conditions => {:title => 'URGENT'}
named_scope :with_urgent_body, :conditions => "body LIKE '%URGENT%'"
belongs_to :employee
def urgent_title? ; self[:title] == 'URGENT' ; end
def urgent_body? ; self[:body] =~ /URGENT/ ; end
end

class LegacyEmployee < ActiveRecord::Base
Expand Down

0 comments on commit 76f75a2

Please sign in to comment.