Skip to content

Commit

Permalink
Adding HasManyThroughAssociation support.
Browse files Browse the repository at this point in the history
  • Loading branch information
metaskills committed Sep 26, 2008
1 parent ab6c3bb commit 5bb4cc3
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ TODO
* Change these to account for :as option:
GroupedScope::HasManyAssociation
* Turn on some mocha options/warnings.

* Support finder sql

Running Tests
=============
Expand Down
4 changes: 1 addition & 3 deletions lib/grouped_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
require 'grouped_scope/association_reflection'
require 'grouped_scope/class_methods'
require 'grouped_scope/has_many_association'
require 'grouped_scope/has_many_through_association'

module GroupedScope

VERSION = '1.0.0'

end

ActiveRecord::Base.send :extend, GroupedScope::ClassMethods
ActiveRecord::Associations::HasManyAssociation.send :include, GroupedScope::HasManyAssociation

2 changes: 2 additions & 0 deletions lib/grouped_scope/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ def create_belongs_to_for_grouped_scope

end
end

ActiveRecord::Base.send :extend, GroupedScope::ClassMethods
16 changes: 13 additions & 3 deletions lib/grouped_scope/has_many_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,27 @@ def self.included(klass)
alias_method_chain :construct_sql, :group_scope
end
end

def construct_sql_with_group_scope
if @reflection.options[:grouped_scope]
@finder_sql = "#{@reflection.quoted_table_name}.#{@reflection.primary_key_name} IN (#{@owner.group.quoted_ids})"
@finder_sql << " AND (#{conditions})" if conditions
if @reflection.options[:as]
# TODO: Need to add case for polymorphic :as option.
# @finder_sql =
# "#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_id = #{@owner.quoted_id} AND " +
# "#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}"
# @finder_sql << " AND (#{conditions})" if conditions
else
@finder_sql = "#{@reflection.quoted_table_name}.#{@reflection.primary_key_name} IN (#{@owner.group.quoted_ids})"
@finder_sql << " AND (#{conditions})" if conditions
end
@counter_sql = @finder_sql
else
construct_sql_without_group_scope
end
end


end
end

ActiveRecord::Associations::HasManyAssociation.send :include, GroupedScope::HasManyAssociation
28 changes: 28 additions & 0 deletions lib/grouped_scope/has_many_through_association.rb
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
4 changes: 2 additions & 2 deletions test/grouped_scope/has_many_association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ def setup
end

should 'scope existing association to owner' do
assert_sql(/"reports".employee_id = #{@employee.id}/) do
assert_sql(/employee_id = #{@employee.id}/) do
@employee.reports(true)
end
end

should 'scope group association to group' do
assert_sql(/"reports".employee_id IN \(#{@employee.id}\)/) do
assert_sql(/employee_id IN \(#{@employee.id}\)/) do
@employee.group.reports(true)
end
end
Expand Down
49 changes: 49 additions & 0 deletions test/grouped_scope/has_many_through_association_test.rb
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

0 comments on commit 5bb4cc3

Please sign in to comment.