diff --git a/TODO b/TODO index a238f2b..be1df1e 100644 --- a/TODO +++ b/TODO @@ -13,6 +13,21 @@ Notes: + >> User.reflections[:boxes] + => #:columns, :readonly=>false, :order=>"columns.position, boxes.position", :extend=>[UserBoxesAssociationExtension]}, @active_record=User(14 columns), @plural_name="boxes", @collection=true> + >> User.reflections[:boxes].source_reflection + => #"position", :extend=>[]}, @active_record=Column(3 columns), @plural_name="boxes", @collection=true> + >> User.reflections[:boxes].through_reflection + => #"position", :extend=>[]}, @active_record=User(14 columns), @plural_name="columns", @collection=true, @class_name="Column", @klass=Column(3 columns)> + >> User.reflections[:boxes].source_reflection_names + => [:box, :boxes] + >> User.reflections[:boxes].source_options + => {:order=>"position", :extend=>[]} + >> User.reflections[:boxes].through_options + => {:order=>"position", :extend=>[]} + >> User.reflections[:boxes].class_name + => "Box" + >> User.reflections[:columns].primary_key_column => # @@ -26,8 +41,6 @@ => User(14 columns) - - equalities = wheres.grep(Arel::Nodes::Equality) >> ActiveRecord::Associations::AssociationScope.new(User.first.association(:columns)).scope.where_values @@ -80,7 +93,7 @@ * Kill &block stuff. - +* Make association conditions use pure SQL. Avoid 100's of IDs. diff --git a/lib/grouped_scope/arish/associations/association_scope.rb b/lib/grouped_scope/arish/associations/association_scope.rb index 22055b4..67feeac 100644 --- a/lib/grouped_scope/arish/associations/association_scope.rb +++ b/lib/grouped_scope/arish/associations/association_scope.rb @@ -55,9 +55,7 @@ def add_constraints(scope) scope = scope.where(interpolate(condition)) end else - # GroupedScope changed this line. - # constraint = table[key].eq(foreign_table[foreign_key]) - constraint = table[key].in(owner.group.ids) + constraint = table[key].eq(foreign_table[foreign_key]) if reflection.type type = chain[i + 1].klass.base_class.name diff --git a/lib/grouped_scope/arish/associations/builder/grouped_collection_association.rb b/lib/grouped_scope/arish/associations/builder/grouped_collection_association.rb index 64fe2e7..08f2d04 100644 --- a/lib/grouped_scope/arish/associations/builder/grouped_collection_association.rb +++ b/lib/grouped_scope/arish/associations/builder/grouped_collection_association.rb @@ -38,6 +38,9 @@ def find_ungrouped_reflection(model, name) def ungrouped_reflection_options(ungrouped_reflection) ungrouped_reflection.options.dup.tap do |options| options[:class_name] = ungrouped_reflection.class_name + if ungrouped_reflection.source_reflection && options[:source].blank? + options[:source] = ungrouped_reflection.source_reflection.name + end end end diff --git a/test/grouped_scope/has_many_through_test.rb b/test/grouped_scope/has_many_through_test.rb index 0270cb6..21c9647 100644 --- a/test/grouped_scope/has_many_through_test.rb +++ b/test/grouped_scope/has_many_through_test.rb @@ -29,21 +29,20 @@ class GroupedScope::HasManyThroughTest < GroupedScope::TestCase describe 'For grouped association' do it 'scope to group' do - raise @e2.group.departments(true).inspect assert_sql(/"employee_id" IN \(#{@e1.id}, #{@e2.id}\)/) do @e2.group.departments(true) end end - # it 'scope count to group' do - # assert_sql(/employee_id IN \(#{@e1.id},#{@e2.id}\)/) do - # @e1.group.departments(true).count - # end - # end - # - # it 'have a group count equal to sum of seperate owner counts' do - # assert_equal @e1.departments(true).count + @e2.departments(true).count, @e2.group.departments(true).count - # end + it 'scope count to group' do + assert_sql(/"employee_id" IN \(#{@e1.id}, #{@e2.id}\)/) do + @e1.group.departments(true).count + end + end + + it 'have a group count equal to sum of separate owner counts' do + assert_equal @e1.departments(true).count + @e2.departments(true).count, @e2.group.departments(true).count + end end