Skip to content

Commit

Permalink
Different attempt for GroupedScope::AssociationReflection delegation …
Browse files Browse the repository at this point in the history
…to ungrouped_reflection.
  • Loading branch information
metaskills committed Sep 24, 2008
1 parent ba7de6c commit 20be575
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 52 deletions.
15 changes: 9 additions & 6 deletions lib/grouped_scope/association_reflection.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
module GroupedScope
class AssociationReflection < ActiveRecord::Reflection::AssociationReflection

(ActiveRecord::Reflection::MacroReflection.instance_methods +
ActiveRecord::Reflection::AssociationReflection.instance_methods).uniq.each do |m|
unless m =~ /^(__|nil\?|send)|^(object_id|options|name)$/
delegate m, :to => :ungrouped_reflection
end
((ActiveRecord::Reflection::AssociationReflection.instance_methods-Class.instance_methods) +
(ActiveRecord::Reflection::AssociationReflection.private_instance_methods-Class.private_instance_methods)).each do |m|
undef_method(m)
end
delegate :derive_class_name, :to => :ungrouped_reflection

attr_accessor :name, :options

def initialize(active_record,ungrouped_name)
@active_record = active_record
Expand All @@ -25,6 +24,10 @@ def ungrouped_reflection

private

def method_missing(method, *args, &block)
ungrouped_reflection.send(method, *args, &block)
end

def verify_ungrouped_reflection
if ungrouped_reflection.blank? || ungrouped_reflection.macro.to_s !~ /has_many|has_and_belongs_to_many/
raise ArgumentError, "Cannot create a group scope for :#{@ungrouped_name} because it is not a has_many " +
Expand Down
2 changes: 1 addition & 1 deletion test/grouped_scope/association_reflection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def setup
Employee.reflections[:grouped_scope_reports].ungrouped_reflection
end

should 'delegate core instance methods to #ungrouped_reflection' do
should 'delegate instance methods to #ungrouped_reflection' do
[:class_name,:klass,:table_name,:quoted_table_name,:primary_key_name,:active_record,
:association_foreign_key,:counter_cache_column,:source_reflection].each do |m|
assert_equal Employee.reflections[:reports].send(m),
Expand Down
2 changes: 1 addition & 1 deletion test/grouped_scope/class_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def setup

should 'create a has_many assoc named :grouped_scope_* using existing association as a suffix' do
grouped_reports_assoc = Employee.reflections[:grouped_scope_reports]
assert_instance_of ActiveRecord::Reflection::AssociationReflection, grouped_reports_assoc
assert_instance_of GroupedScope::AssociationReflection, grouped_reports_assoc
assert Factory(:employee).respond_to?(:grouped_scope_reports)
end

Expand Down
78 changes: 34 additions & 44 deletions test/grouped_scope/self_grouping_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,23 @@ def setup
assert_instance_of Array, @employee.group
end

should 'return #ids array' do
assert_equal [@employee.id], @employee.group.ids
end

should 'return #quoted_ids string for use in sql statments' do
assert_equal "#{@employee.id}", @employee.group.quoted_ids
end

should 'respond true to grouped associations' do
assert @employee.group.respond_to?(:reports)
end

should 'raise a GroupedScope::NoGroupIdError exception for objects with no group_id schema' do
assert !FooBar.column_names.include?('group_id')
assert_raise(GroupedScope::NoGroupIdError) { GroupedScope::SelfGroupping.new(FooBar.new) }
end

context 'for Array delegates' do

should 'respond to first/last' do
Expand All @@ -40,24 +53,7 @@ def setup

end






context 'Calling #group' do





context 'with no group_id schema' do

should 'raise a GroupedScope::NoGroupIdError exception' do
assert_raise(GroupedScope::NoGroupIdError) { FooBar.new.group }
end

end
context 'Created via calling #group' do

context 'with a NIL group_id' do

Expand All @@ -73,14 +69,6 @@ def setup
assert @employee.group.include?(@employee)
end

should 'return #ids array' do
assert_equal [@employee.id], @employee.group.ids
end

should 'return #quoted_ids string for use in sql statments' do
assert_equal "#{@employee.id}", @employee.group.quoted_ids
end

end

context 'with a set group_id' do
Expand All @@ -99,26 +87,28 @@ def setup

end

# context 'with different employees in different groups' do
#
# setup do
# @e1_g1 = Factory(:employee_with_reports, :group_id => 1)
# @e2_g1 = Factory(:employee, :group_id => 1)
# @e2_g2 = Factory(:employee_with_reports, :group_id => 2)
# @e2_g2 = Factory(:employee, :group_id => 2)
# end
#
# should 'have creaed factories in a sane manner' do
# # raise Employee.reflect_on_association(:reports).inspect
# # raise Employee.reflect_on_association(:grouped_scope_reports).inspect
# # assert_equal @e1_g1.reports.size, @e1_g1.group.reports.size
# end
#
# end

context 'with different groups' do

setup do
@e1_g1 = Factory(:employee_with_reports, :group_id => 1)
@e2_g1 = Factory(:employee, :group_id => 1)
@e2_g2 = Factory(:employee_with_reports, :group_id => 2)
@e2_g2 = Factory(:employee, :group_id => 2)
end

should 'have created factories in a sane manner' do
# raise Employee.reflections[:reports].inspect
# raise Employee.reflections[:grouped_scope_reports].inspect
# assert_equal @e1_g1.reports.size, @e1_g1.group.reports.size

@e1_g1.reports
# @e1_g1.group.reports
# @e1_g1.grouped_scope_reports
end

end

end

# Make sure legacy quoted_ids are quoted

end

0 comments on commit 20be575

Please sign in to comment.