Sha256: b4544c22d1c0bcd3ecca934a7339b362bd77c6e3895d2260c2810abcef732367

Contents?: true

Size: 1.75 KB

Versions: 4

Compression:

Stored size: 1.75 KB

Contents

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

require 'test/unit'
require 'ostruct'

require 'og'
require 'og/mixin/orderable'

$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

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
og-0.21.0 test/og/mixin/tc_orderable.rb
og-0.21.2 test/og/mixin/tc_orderable.rb
og-0.22.0 test/og/mixin/tc_orderable.rb
og-0.23.0 test/og/mixin/tc_orderable.rb