Sha256: 4f26611f509088106ef59ff0694328fbef62c41cc16358ff3cf59a52528f45ee

Contents?: true

Size: 1.65 KB

Versions: 4

Compression:

Stored size: 1.65 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 :status # Explicitly use another state column to ensure that this whole enchilada is working with other state column names than the default ones.
    end
  end
end

set_up_db CreateBunnies, CreatePuppies

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

  state_machine :attribute_name => :status, :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) do
      Class.new(ActiveRecord::Base) do
        include ActiveModel::Transitions

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
transitions-0.1.11 test/active_record/test_active_record_scopes.rb
transitions-0.1.10 test/active_record/test_active_record_scopes.rb
transitions-0.1.9 test/active_record/test_active_record_scopes.rb
transitions-0.1.8 test/active_record/test_active_record_scopes.rb