Skip to content

Commit

Permalink
collection helper keeps paginated info
Browse files Browse the repository at this point in the history
  • Loading branch information
ahoward committed Apr 9, 2012
1 parent 5fbf1b9 commit 188a1cd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
5 changes: 5 additions & 0 deletions lib/dao/conducer.rb
Expand Up @@ -28,6 +28,11 @@ def subclasses
defined?(@@subclasses) ? @@subclasses : (@@subclasses = [])
end

def collection_for(results, &block)
block ||= proc{|model| new(model)}
results.dup.map!(&block)
end

def name(*args)
return send('name=', args.first) unless args.empty?
@name ||= super
Expand Down
4 changes: 4 additions & 0 deletions lib/dao/conducer/controller_support.rb
Expand Up @@ -38,6 +38,10 @@ def action
to_s
end

def ==(other)
super(other.to_s)
end

Synonyms = {
'new' => 'create',
'create' => 'new',
Expand Down
42 changes: 32 additions & 10 deletions test/conducer_test.rb
Expand Up @@ -377,7 +377,10 @@ def _update_attributes(attributes = {})
end


##
#
context :teh_mount do
#
testing 'that mounted objects can be declared at the class level' do
conducer_class =
new_conducer_class do
Expand All @@ -393,6 +396,7 @@ def _update_attributes(attributes = {})
assert{ c.mounted.first._value.nil? }
end

#
testing 'that mounted objects replace their location in attributes' do
conducer_class =
new_conducer_class do
Expand All @@ -410,6 +414,7 @@ def _update_attributes(attributes = {})
assert{ test(?f, upload._value) }
end

#
testing 'that the default save uses the mounted _value and _clears it' do
conducer_class =
new_conducer_class do
Expand Down Expand Up @@ -439,18 +444,29 @@ def _update_attributes(attributes = {})
end
end

=begin
context 'collections' do
testing 'that subclasses have their own collection subclass' do
c = assert{ new_foo_conducer_class }
assert{ c::Collection }
assert{ c.collection.new.is_a?(Array) }
assert{ c.collection_class.ancestors.include?(Dao::Conducer::Collection) }
assert{ c.collection.new.is_a?(Array) }
##
#
context :collections do
testing 'can be created from page-y blessed arrays' do
paginated = Paginated[Post.new, Post.new, Post.new]
paginated.limit = 42
paginated.offset = 42.0
paginated.total_count = 420

conducer_class = new_conducer_class

collection = assert{ conducer_class.collection_for(paginated) }
assert{ collection.limit == 42 }
assert{ collection.offset == 42.0 }
assert{ collection.total_count == 420 }

user = User.new
collection = assert{ conducer_class.collection_for(paginated){|model| conducer_class.for(:show, user, model)} }
assert{ collection.all?{|conducer| conducer.action == :show} }
assert{ collection.all?{|conducer| conducer.models.first==user} }
assert{ collection.all?{|conducer| conducer.models.last.is_a?(Post)} }
end
end
=end


protected
def new_foo_conducer_class(&block)
Expand Down Expand Up @@ -602,6 +618,12 @@ def destroy
end
end

class Paginated < ::Array
attr_accessor :limit
attr_accessor :offset
attr_accessor :total_count
end

class User < Model
end

Expand Down

0 comments on commit 188a1cd

Please sign in to comment.