require File.join(File.dirname(__FILE__), '..', 'og', 'CONFIG.rb') require 'rubygems' require 'facets' require 'test/unit' require 'ostruct' require 'og' require 'glue/orderable' class TestCaseOgOrderable < Test::Unit::TestCase # :nodoc: all include Glue class Article attr_accessor :title, :body, String has_many :comments, Comment, :list => true, :order => 'position DESC' def initialize(title = nil) @title = title end end class Comment attr_accessor :body, String belongs_to :article # We have to set the parent before inserting the comment # for the scope to work! is Orderable #, :scope => :article def initialize(body = nil) @body = body end end class Playlist attr_accessor :name, String has_many :tracks end class Track attr_accessor :name, String is Orderable, :scope => :playlist belongs_to :playlist def initialize(name, playlist) @name = name self.playlist = playlist end end $og1.manage_classes(Article, Comment, Playlist, Track) def test_all a = Article.create('article') 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