Sha256: 24f1ab9cac32cf0d37bc411d07c7e2b21937aac8aa9477093b7d9becd44357f6

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

require "helper"

class CreateBears < ActiveRecord::Migration
  def self.up
    create_table(:bears, :force => true) do |t|
      t.string :state
    end
  end
end

class CreatePuppies < ActiveRecord::Migration
  def self.up
    create_table(:puppies, :force => true) do |t|
      t.string :state
    end
  end
end

class CreateBunnies < ActiveRecord::Migration
  def self.up
    create_table(:bunnies, :force => true) do |t|
      t.string :state
    end
  end
end

set_up_db CreateBunnies, CreatePuppies

class Bunny < ActiveRecord::Base
  include ActiveModel::Transitions

  state_machine :auto_scopes => true do
    state :hobbling
  end
end

class Puppy < ActiveRecord::Base
  include ActiveModel::Transitions

  state_machine do
    state :barking
  end
end

class TestScopes < Test::Unit::TestCase
  def setup
    set_up_db CreateBears, CreateBunnies, CreatePuppies
    @bunny = Bunny.create!
  end

  test "scopes exist" do
    assert_respond_to Bunny, :hobbling
  end

  test "scope returns correct object" do
    assert_equal Bunny.hobbling.first, @bunny
  end

  test 'scopes are only generated if we explicitly say so' do
    assert_not_respond_to Puppy, :barking
  end

  test 'scope generation raises an exception if we try to overwrite an existing method' do
    assert_raise(Transitions::InvalidMethodOverride) {
      class Bear < ActiveRecord::Base
        include ActiveModel::Transitions

        state_machine :auto_scopes => true do
          state :new
        end
      end
    }
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
transitions-0.1.3 test/active_record/test_active_record_scopes.rb
transitions-0.1.2 test/active_record/test_active_record_scopes.rb
transitions-0.1.1 test/active_record/test_active_record_scopes.rb