File: machine_with_initialized_aliased_attribute_test.rb

package info (click to toggle)
ruby-state-machines-activemodel 0.101.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 320 kB
  • sloc: ruby: 1,222; makefile: 6; sh: 4
file content (44 lines) | stat: -rw-r--r-- 1,209 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
39
40
41
42
43
44
# frozen_string_literal: true

require 'test_helper'

class MachineWithInitializedAliasedAttributeTest < BaseTestCase
  def test_should_match_original_attribute_value_with_attribute_methods
    model = new_model do
      include ActiveModel::AttributeMethods

      alias_attribute :custom_status, :state
    end

    machine = StateMachines::Machine.new(model, initial: :parked, integration: :active_model)
    machine.other_states(:started)

    record = model.new(custom_status: 'started')

    refute record.state?(:parked)
    assert record.state?(:started)
  end

  def test_should_not_match_original_attribute_value_without_attribute_methods
    model = new_plain_model do
      include ActiveModel::Model

      attr_accessor :state

      def self.alias_attribute(new_name, old_name)
        alias_method new_name, old_name
        alias_method "#{new_name}=", "#{old_name}="
      end

      alias_attribute :custom_status, :state
    end

    machine = StateMachines::Machine.new(model, initial: :parked, integration: :active_model)
    machine.other_states(:started)

    record = model.new(custom_status: 'started')

    assert record.state?(:parked)
    refute record.state?(:started)
  end
end