Skip to content

Commit

Permalink
Making good progress on using new SelfGrouping proxy class. Added a f…
Browse files Browse the repository at this point in the history
…ew tests that cover the bases. Now on to enumerable.
  • Loading branch information
metaskills committed Sep 22, 2008
1 parent 117bd6c commit 223fa43
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
23 changes: 11 additions & 12 deletions lib/grouped_scope/self_grouping.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
module GroupedScope
class SelfGroupping

include Enumerable
attr_reader :proxy_owner

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

attr_reader :proxy_owner
[].methods.each do |m|
unless m =~ /(^__|^nil\?|^send|^object_id$|class|extend|^find$|count|sum|average|maximum|minimum|paginate|first|last|empty\?|respond_to\?)/
delegate m, :to => :group_proxy
end
end


def initialize(proxy_owner)
raise NoGroupIdError.new(proxy_owner) unless proxy_owner.class.column_names.include?('group_id')
Expand All @@ -21,27 +25,22 @@ def quoted_ids
ids.map { |id| quote_value(id,columns_hash[primary_key]) }.join(',')
end

def group
@group ||= find_selves(group_scope_options)
end

def each
group.each { |member| yield member }
end

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


protected

def group_proxy
@group_proxy ||= find_selves(group_scope_options)
end

def all_grouped?
false
end

def no_group?
# TODO [group_id] Abstractraction point.
proxy_owner.group_id.blank?
end

Expand Down
25 changes: 23 additions & 2 deletions test/grouped_scope/self_grouping_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,29 @@ def setup
setup_environment
end

should 'be true' do
assert true

context 'Calling #group' do

should 'return an array' do
assert_instance_of Array, Factory(:employee).group
end

context 'with a NIL group' do

setup do
@employee = Factory(:employee)
end

should 'return an array of one' do
assert_equal 1, @employee.group.size
end

should 'include self in group' do
assert @employee.group.include?(@employee)
end

end

end


Expand Down

0 comments on commit 223fa43

Please sign in to comment.