require File.join(File.dirname(__FILE__), '..', 'CONFIG.rb') require 'test/unit' require 'ostruct' require 'og' require 'glue/orderable' class Playlist prop :name, String has_many Track end class Track include Og::Orderable, :scope => :playlist belongs_to Playlist prop :name, String def initialize(name, playlist) @name = name self.playlist = playlist end end $og = Og.setup( :store => 'psql', # :store => :memory, :name => 'test', :user => 'postgres', :password => 'navelrulez', :destroy => true ) class TestCaseOgOrderable < Test::Unit::TestCase # :nodoc: all class Comment; end class Article property :title, :body, String has_many :comments, Comment, :list => true, :order => 'position DESC' def initialize(title = nil) @title = title end end class Comment property :body, String belongs_to :article, Article include Og::Orderable #, :scope => :article def initialize(body = nil) @body = body end end def test_all $og.manage_classes a = Article.create('article') a.save c1 = Comment.create('1') a.comments << c1 c2 = Comment.create('2') a.comments << c2 c3 = Comment.create('3') a.comments << c3 assert_equal 1, c1.position assert_equal 2, c2.position assert_equal 3, c3.position c3.move_higher c1.reload c2.reload c3.reload assert_equal 1, c1.position assert_equal 2, c3.position assert_equal 3, c2.position c2.move_to_top c1.reload c2.reload c3.reload assert_equal 1, c2.position assert_equal 2, c1.position assert_equal 3, c3.position c2.move_to_bottom c1.reload c2.reload c3.reload assert_equal 1, c1.position assert_equal 2, c3.position assert_equal 3, c2.position c3.delete! c1.reload c2.reload assert_equal 1, c1.position assert_equal 2, c2.position c2.delete! c1.reload assert_equal 1, c1.position c1.delete! end def track_list Track.find(:order => "position").collect { |t| t.name } end def test_orderable pl = Playlist.create %w{one two three four five six}.each do |n| Track.create(n, pl) end tr = Track.one(:condition => 'position = 1') tr.move_to(4) assert_equal(4, tr.position) assert_equal(%w{two three four one five six}, track_list() ) tr = Track.one(:condition => 'position = 5') tr.move_to(3) assert_equal(3, tr.position) assert_equal(%w{two three five four one six}, track_list() ) end end