Skip to content

Commit

Permalink
Moving to all in one SelfGrouping/Proxy class. First delete Group/Proxy.
Browse files Browse the repository at this point in the history
  • Loading branch information
metaskills committed Sep 22, 2008
1 parent 1dbcddb commit d28d587
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 86 deletions.
2 changes: 0 additions & 2 deletions lib/grouped_scope.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require 'grouped_scope/errors'
require 'grouped_scope/proxy'
require 'grouped_scope/grouping'
require 'grouped_scope/self_grouping'
require 'grouped_scope/group'
require 'grouped_scope/class_methods'
require 'grouped_scope/instance_methods'
require 'grouped_scope/has_many_association'
Expand Down
42 changes: 0 additions & 42 deletions lib/grouped_scope/group.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/grouped_scope/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module GroupedScope
module InstanceMethods

def group
@group ||= Group.new(self)
@group ||= SelfGroupping.new(self)
end


Expand Down
30 changes: 0 additions & 30 deletions lib/grouped_scope/proxy.rb

This file was deleted.

37 changes: 26 additions & 11 deletions lib/grouped_scope/self_grouping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ class SelfGroupping
include Enumerable

delegate :size, :first, :last, :[], :inspect, :to => :group
delegate :primary_key, :quote_value, :columns_hash, :to => :owner_class
delegate :primary_key, :quote_value, :columns_hash, :to => :proxy_class

attr_reader :owner
attr_reader :proxy_owner

def initialize(owner)
raise ArgumentError,'An ActiveRecord owner object must be specified.' if owner.blank? || !owner.respond_to?(:new_record?)
raise NoGroupIdError.new(owner) unless owner.class.column_names.include?('group_id')
@owner = owner
def initialize(proxy_owner)
raise NoGroupIdError.new(proxy_owner) unless proxy_owner.class.column_names.include?('group_id')
@proxy_owner = proxy_owner
end

def ids
Expand All @@ -30,6 +29,10 @@ def each
group.each { |member| yield member }
end

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


protected

Expand All @@ -38,25 +41,37 @@ def all_grouped?
end

def no_group?
owner.group_id.blank?
proxy_owner.group_id.blank?
end

def find_selves(options={})
owner.class.find :all, options
proxy_owner.class.find :all, options
end

def group_scope_options
return {} if all_grouped?
conditions = no_group? ? { primary_key => owner.id } : { :group_id => owner.group_id }
conditions = no_group? ? { primary_key => proxy_owner.id } : { :group_id => proxy_owner.group_id }
{ :conditions => conditions }
end

def group_id_scope_options
{ :select => primary_key }.merge(group_scope_options)
end

def owner_class
owner.class
def proxy_class
proxy_owner.class
end


private

def method_missing(method, *args, &block)
if proxy_class.grouped_scopes[method]
grouped_assoc = proxy_owner.class.grouped_scope_for(method)
proxy_owner.send(grouped_assoc, *args, &block)
else
super
end
end

end
Expand Down

0 comments on commit d28d587

Please sign in to comment.