Skip to content

Commit

Permalink
Switching to using a :grouped_scope option on the generated grouped s…
Browse files Browse the repository at this point in the history
…cope association to trigger off in the has_many_association sql method chain.
  • Loading branch information
metaskills committed Sep 22, 2008
1 parent bdcc1b9 commit 882a208
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
16 changes: 10 additions & 6 deletions lib/grouped_scope/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,33 @@ def grouped_scopes
end

def grouped_scope(*args)
belongs_to_grouped_scope_grouping
create_belongs_to_grouped_scope_grouping
args.each do |association|
existing_assoc = reflect_on_association(association)
unless existing_assoc && existing_assoc.macro == :has_many
raise ArgumentError, "Cannot create a group scope for :#{association} because it is not a has_many " +
"association. Make sure to call grouped_scope after the has_many associations."
end
grouped_scope_method = :"grouped_scope_#{association}"
grouped_scopes[grouped_scope_method] = true
has_many grouped_scope_method, existing_assoc.options
grouped_scopes[association] = true
grouped_assoc = grouped_scope_for(association)
has_many grouped_assoc, existing_assoc.options
reflect_on_association(grouped_assoc).options[:grouped_scope] = true
end
include InstanceMethods
end

private

def belongs_to_grouped_scope_grouping
def grouped_scope_for(association)
:"grouped_scope_#{association}"
end

def create_belongs_to_grouped_scope_grouping
grouping_class_name = 'GroupedScope::Grouping'
existing_grouping = reflect_on_association(:grouping)
return false if existing_grouping && existing_grouping.macro == :belongs_to && existing_grouping.options[:class_name] == grouping_class_name
belongs_to :grouping, :foreign_key => 'group_id', :class_name => grouping_class_name
end


end
end
7 changes: 5 additions & 2 deletions lib/grouped_scope/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ def initialize(owner)
super
end

def respond_to?(method, include_private=false)
super || !proxy_class.grouped_scopes[method].blank?
end

protected

def method_missing(method, *args, &block)
group_method = "group_#{method}".to_sym
if proxy_class.grouped_scopes[method]
proxy_owner.send(group_method, *args, &block)
grouped_assoc = proxy_owner.class.grouped_scope_for(method)
proxy_owner.send(grouped_assoc, *args, &block)
else
super
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grouped_scope/has_many_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def self.included(klass)
end

def construct_sql_with_group_scope
if @owner.class.grouped_scopes[@reflection.name]
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
@counter_sql = @finder_sql
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 @@ -15,7 +15,7 @@ def setup
should 'add to inheritable attributes with new grouped_scope' do
assert_nil Employee.grouped_scopes[:foobars]
Employee.class_eval { has_many(:foobars) ; grouped_scope(:foobars) }
assert Employee.grouped_scopes[:grouped_scope_foobars]
assert Employee.grouped_scopes[:foobars]
end

end
Expand Down

0 comments on commit 882a208

Please sign in to comment.