File: machine_with_scopes_and_joins_test.rb

package info (click to toggle)
ruby-state-machines-activerecord 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 340 kB
  • sloc: ruby: 1,922; makefile: 5
file content (38 lines) | stat: -rw-r--r-- 1,278 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require_relative 'test_helper'

class MachineWithScopesAndJoinsTest < BaseTestCase
  def setup
    @company = new_model(:company)
    MachineWithScopesAndJoinsTest.const_set('Company', @company)

    @vehicle = new_model(:vehicle) do
      connection.add_column table_name, :company_id, :integer
      belongs_to :company, :class_name => 'MachineWithScopesAndJoinsTest::Company'
    end
    MachineWithScopesAndJoinsTest.const_set('Vehicle', @vehicle)

    @company_machine = StateMachines::Machine.new(@company, :initial => :active)
    @vehicle_machine = StateMachines::Machine.new(@vehicle, :initial => :parked)
    @vehicle_machine.state :idling

    @ford = @company.create
    @mustang = @vehicle.create(:company => @ford)
  end

  def test_should_find_records_in_with_scope
    assert_equal [@mustang], @vehicle.with_states(:parked).joins(:company).where("#{@company.table_name}.state = \"active\"")
  end

  def test_should_find_records_in_without_scope
    assert_equal [@mustang], @vehicle.without_states(:idling).joins(:company).where("#{@company.table_name}.state = \"active\"")
  end

  def teardown
    MachineWithScopesAndJoinsTest.class_eval do
      remove_const('Vehicle')
      remove_const('Company')
    end

    clear_active_support_dependencies
  end
end