Sha256: 526fb58f86605392059f77a2035dca62758d2125535ce62cde8b7e4a853b29dd

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

$:.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')

require 'test/unit'
require 'ostruct'

require 'og/mixins/orderable'

require 'og'

$og = Og::Database.new(
	:adapter => 'psql',
	:database => 'test',
	:user => 'postgres',
	:password => 'navelrulez',
	:drop => 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.auto_manage_classes		

		a = Article.create('article')
		a.save

		c1 = Comment.new('1')
		a.add_comment(c1)
		c2 = Comment.new('2')
		a.add_comment(c2)
		c3 = Comment.new('3')
		a.add_comment(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

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
og-0.16.0 test/og/mixins/tc_orderable.rb