Skip to content

Commit

Permalink
Initial implementation of has_many association sorting [#26 state:res…
Browse files Browse the repository at this point in the history
…olved]
  • Loading branch information
yrashk committed Apr 30, 2008
1 parent d6ffeca commit 67e1af5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/strokedb/document/associations.rb
Expand Up @@ -9,20 +9,23 @@ def view.map(uuid, doc)
expected_meta = self[:expected_meta]
expected_nsurl = self[:expected_nsurl]
conditions = self[:conditions]

sort_by = self[:sort_by]

if doc.meta.name == expected_meta && doc.meta.nsurl == expected_nsurl
if (reference_slotname_value = doc[reference_slotname]) &&
(conditions.nil? ||
(conditions &&
(conditions.keys.select {|k| doc[k] == conditions[k]}.size == conditions.size)))
begin
key = [reference_slotname_value, doc]
key = [key[0],doc.send(sort_by),key[1]] if sort_by
through.each {|t| doc = doc.send(t) }
rescue SlotNotFoundError
return nil unless doc
else
[
[
[reference_slotname_value, doc],
key,
doc
]
]
Expand Down Expand Up @@ -70,7 +73,9 @@ def has_many(slotname, opts={}, &block)
nsurl = opts['nsurl'] || (name.modulize.empty? ? Module.nsurl : meta.modulize.constantize.nsurl)
extend_with = opts['extend'] || block
conditions = opts['conditions']

sort_by = opts['sort_by']
reverse = opts['reverse'] || false

@meta_initialization_procs << Proc.new do
case extend_with
when Proc
Expand All @@ -96,7 +101,8 @@ def has_many(slotname, opts={}, &block)
# end

view = View.define!("#{name.modulize.empty? ? Module.nsurl : name.modulize.constantize.nsurl}##{name.demodulize.tableize.singularize}_has_many_#{slotname}",
{ :reference_slotname => reference_slotname, :through => through, :expected_meta => meta, :expected_nsurl => nsurl, :extend_with => extend_with, :conditions => conditions }, &AssociationViewImplementation)
{ :reference_slotname => reference_slotname, :through => through, :expected_meta => meta, :expected_nsurl => nsurl, :extend_with => extend_with,
:conditions => conditions, :sort_by => sort_by, :reverse => reverse }, &AssociationViewImplementation)

@args.last.reverse_merge!({"has_many_#{slotname}" => view})
define_method(slotname) do
Expand All @@ -113,7 +119,7 @@ def initialize_associations
define_method(:_has_many_association) do |slotname|
slot_has_many = meta["has_many_#{slotname}"]
result = LazyArray.new.load_with do |lazy_array|
slot_has_many.find(:key => self)
slot_has_many.find(:key => self, :reverse => slot_has_many[:reverse])
end
if extend_with = slot_has_many[:extend_with]
result.extend(extend_with.constantize)
Expand Down
49 changes: 49 additions & 0 deletions spec/lib/strokedb/document/associations_spec.rb
Expand Up @@ -162,9 +162,58 @@
song.should_not be_new
playlist.songs.should == [song]
end



end



describe "Playlist.has_many :songs association with sort slot defined" do

before(:each) do
setup_default_store
setup_index
Object.send!(:remove_const,'Playlist') if defined?(Playlist)
Object.send!(:remove_const,'Song') if defined?(Song)
Playlist = Meta.new do
has_many :songs, :sort_by => :created_at
end
Song = Meta.new
end

it "having songs with created_at should be able sort by it" do
playlist = Playlist.create!
song2 = Song.create!(:playlist => playlist, :created_at => Time.now)
song1 = Song.create!(:playlist => playlist, :created_at => Time.now - 100)
playlist.songs.should == [song1, song2]
end

end

describe "Playlist.has_many :songs association with sort slot defined and reverse order" do

before(:each) do
setup_default_store
setup_index
Object.send!(:remove_const,'Playlist') if defined?(Playlist)
Object.send!(:remove_const,'Song') if defined?(Song)
Playlist = Meta.new do
has_many :songs, :sort_by => :created_at, :reverse => true
end
Song = Meta.new
end

it "having songs with created_at should be able sort by it" do
playlist = Playlist.create!
song2 = Song.create!(:playlist => playlist, :created_at => Time.now)
song1 = Song.create!(:playlist => playlist, :created_at => Time.now - 100)
playlist.songs.should == [song2, song1]
end

end


describe "Namespace::Playlist.has_many :songs association" do

before(:each) do
Expand Down

0 comments on commit 67e1af5

Please sign in to comment.