pipa-statelogic 0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -38,10 +38,22 @@ http://pipa.lighthouseapp.com/projects/22857-statelogic
38
38
  end
39
39
  state 'suspended' do # you guess
40
40
  transitions_to 'unpaid', 'ready', 'redeemed'
41
+ validate do |order|
42
+ order.errors.add(:txref, :invalid) if order.txref && order.txref !~ /\AREF/
43
+ end
41
44
  end
42
45
  end
43
46
  ...
44
47
  end
45
48
 
49
+ order = Order.new
50
+ order.state = 'wtf'
51
+ order.txref = 'orly'
52
+ order.valid? # Please note that state transition checks are done during
53
+ # the validation step as well, and the error is added to
54
+ # the model's +errors+ collection on the state column and
55
+ # will appear on your form. Standard ActiveRecord's error
56
+ # message +:inclusion+ is used. Override it with Rails i18n
57
+ # for something more specific if it's to be displayed to user.
46
58
 
47
59
  Copyright (c) 2009 Igor Gunko, released under the MIT license
@@ -10,7 +10,8 @@ module Statelogic
10
10
  DEFAULT_OPTIONS = {:attribute => :state}.freeze
11
11
 
12
12
  class StateScopeHelper
13
- CALLBACKS = ::ActiveRecord::Callbacks::CALLBACKS.map(&:to_sym).to_set.freeze
13
+ CALLBACKS = (::ActiveRecord::Callbacks::CALLBACKS +
14
+ ::ActiveRecord::Validations::VALIDATIONS).map(&:to_sym).to_set.freeze
14
15
  MACROS_PATTERN = /\Avalidates_/.freeze
15
16
 
16
17
  def initialize(cl, state, config)
@@ -15,6 +15,9 @@ class Order < ActiveRecord::Base
15
15
  end
16
16
  state 'suspended' do
17
17
  transitions_to 'unpaid', 'ready', 'redeemed'
18
+ validate do |order|
19
+ order.errors.add(:txref, :invalid) if order.txref && order.txref !~ /\AREF/
20
+ end
18
21
  end
19
22
  end
20
23
  end
@@ -76,6 +79,8 @@ class OrderTest < ActiveSupport::TestCase
76
79
 
77
80
  should_allow_values_for :state, 'suspended', 'redeemed', 'ready', 'unpaid'
78
81
  should_not_allow_values_for :state, 'screwed_up', :message => default_error_message(:inclusion)
82
+ should_allow_values_for :txref, 'REF12345'
83
+ should_not_allow_values_for :txref, '12345'
79
84
  should_not_require_attributes :txref, :redeemed_at, :facility_id
80
85
  end
81
86
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pipa-statelogic
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.1"
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Gunko
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-06 00:00:00 -08:00
12
+ date: 2009-01-08 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency