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.
Adding HasManyThroughAssociation support.
- Loading branch information
1 parent
ab6c3bb
commit 5bb4cc3
Showing
7 changed files
with
96 additions
and
9 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
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 |
---|---|---|
|
@@ -28,3 +28,5 @@ def create_belongs_to_for_grouped_scope | |
|
||
end | ||
end | ||
|
||
ActiveRecord::Base.send :extend, GroupedScope::ClassMethods |
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,28 @@ | ||
module GroupedScope | ||
module HasManyThroughAssociation | ||
|
||
def self.included(klass) | ||
klass.class_eval do | ||
alias_method_chain :construct_conditions, :group_scope | ||
end | ||
end | ||
|
||
def construct_conditions_with_group_scope | ||
conditions = construct_conditions_without_group_scope | ||
if @reflection.options[:grouped_scope] | ||
if as = @reflection.options[:as] | ||
# TODO: Need to add case for polymorphic :as option. | ||
else | ||
pattern = "#{@reflection.primary_key_name} = #{@owner.quoted_id}" | ||
replacement = "#{@reflection.primary_key_name} IN (#{@owner.group.quoted_ids})" | ||
conditions.sub!(pattern,replacement) | ||
end | ||
end | ||
conditions | ||
end | ||
|
||
|
||
end | ||
end | ||
|
||
ActiveRecord::Associations::HasManyThroughAssociation.send :include, GroupedScope::HasManyThroughAssociation |
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 HasManyThroughAssociationTest < GroupedScope::TestCase | ||
|
||
def setup | ||
setup_environment | ||
@e1 = Factory(:employee, :group_id => 1) | ||
@e1.departments << Department.hr << Department.finance | ||
@e2 = Factory(:employee, :group_id => 1) | ||
@e2.departments << Department.it | ||
@all_group_departments = [Department.hr, Department.it, Department.finance] | ||
end | ||
|
||
|
||
context 'For default association' do | ||
|
||
should 'scope to owner' do | ||
assert_sql(/employee_id = #{@e1.id}/) do | ||
@e1.departments(true) | ||
end | ||
end | ||
|
||
should 'scope count to owner' do | ||
assert_sql(/employee_id = #{@e1.id}/) do | ||
@e1.departments(true).count | ||
end | ||
end | ||
|
||
end | ||
|
||
context 'For grouped association' do | ||
|
||
should 'scope to group' do | ||
assert_sql(/employee_id IN \(#{@e1.id},#{@e2.id}\)/) do | ||
@e2.group.departments(true) | ||
end | ||
end | ||
|
||
should 'scope count to group' do | ||
assert_sql(/employee_id IN \(#{@e1.id},#{@e2.id}\)/) do | ||
@e1.group.departments(true).count | ||
end | ||
end | ||
|
||
end | ||
|
||
|
||
|
||
end |