forked from metaskills/grouped_scope
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Big move to a custom GroupedScope::AssociationReflection which delega…
…tes 99% to ungrouped reflection.
- Loading branch information
1 parent
a78b28d
commit cb9f875
Showing
6 changed files
with
105 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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 | ||
end | ||
|
||
def initialize(active_record,ungrouped_name) | ||
@active_record = active_record | ||
@ungrouped_name = ungrouped_name | ||
@name = :"grouped_scope_#{@ungrouped_name}" | ||
verify_ungrouped_reflection | ||
super(ungrouped_reflection.macro, @name, ungrouped_reflection.options.dup, @active_record) | ||
create_grouped_association | ||
end | ||
|
||
def ungrouped_reflection | ||
@active_record.reflections[@ungrouped_name] | ||
end | ||
|
||
|
||
private | ||
|
||
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 " + | ||
"or a has_and_belongs_to_many association. Make sure to call grouped_scope after " + | ||
"the has_many associations." | ||
end | ||
end | ||
|
||
def create_grouped_association | ||
active_record.send :has_many, name, options | ||
active_record.reflections[name] = self | ||
active_record.grouped_scopes[@ungrouped_name] = true | ||
options[:grouped_scope] = true | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require File.dirname(__FILE__) + '/../helper' | ||
|
||
class AssociationReflectionTest < GroupedScope::TestCase | ||
|
||
def setup | ||
setup_environment | ||
end | ||
|
||
context 'Raise and exception' do | ||
|
||
setup { @reflection_klass = GroupedScope::AssociationReflection } | ||
|
||
should 'when a association does not exist' do | ||
assert_raise(ArgumentError) { @reflection_klass.new(Employee,:foobars) } | ||
end | ||
|
||
should 'when the association is not a has_many or a has_and_belongs_to_many' do | ||
Employee.class_eval { belongs_to(:foo) } | ||
assert_raise(ArgumentError) { @reflection_klass.new(Employee,:foo) } | ||
end | ||
|
||
end | ||
|
||
context 'For #ungrouped_reflection' do | ||
|
||
should 'access ungrouped reflection' do | ||
assert_equal Employee.reflections[:reports], | ||
Employee.reflections[:grouped_scope_reports].ungrouped_reflection | ||
end | ||
|
||
should 'delegate core instance methods to #ungrouped_reflection' do | ||
[:class_name,:klass,:table_name,:quoted_table_name,:primary_key_name, | ||
:association_foreign_key,:counter_cache_column,:source_reflection].each do |m| | ||
assert_equal Employee.reflections[:reports].send(m), | ||
Employee.reflections[:grouped_scope_reports].send(m), | ||
"The method #{m.inspect} does not appear to be proxed to the ungrouped reflection." | ||
end | ||
end | ||
|
||
should 'not delegate to #ungrouped_reflection for #options and #name' do | ||
assert_not_equal Employee.reflections[:reports].name, Employee.reflections[:grouped_scope_reports].name | ||
assert_not_equal Employee.reflections[:reports].options, Employee.reflections[:grouped_scope_reports].options | ||
end | ||
|
||
end | ||
|
||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters