state_machines-activemodel 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f11ff8fca94c8ac107ae350fc15b495e60382aa2
4
- data.tar.gz: 3b4f9a5a1faef97c4e0c17056f4b9f1dbcfc5f4a
3
+ metadata.gz: b5e81550481dde36492aae79d59f69ee5f599927
4
+ data.tar.gz: 05248350d267f60ad3cb5ed43c89d00c2cd143d0
5
5
  SHA512:
6
- metadata.gz: f66365a5bb8cd800eb6e5aca5f8cd460d918a203da9ea0b1bf258a1bb01b78a2d4642c16af47e5d16519ade297f22984f443b5c2faa967fe487878efaecbac74
7
- data.tar.gz: e834da143cca6992e98b286f9f82203983b58dade11b58f6bd18bcc6e5a46d5bbee1f8987aff7a961f26be781866f165a52a4ca65be9792349bfeb4e34ecd947
6
+ metadata.gz: 16f73d32b72ebd5ca59bd45676281c87511f915a16968ecb6648ff3409745c49caa1fe25b46c212a742aca1cc3696ea8b0c7954029d27e7eca0af5b154e76fe8
7
+ data.tar.gz: 087afbda3cff8e922bd1e88d3858e73244ef779ada26903b5ed4f12d2f006fcafc48f4119b76c7c1e6413923c8f8cd6f5dbe33e239a19d8fde3c3c4fbc171741
data/.gitignore CHANGED
@@ -12,11 +12,10 @@ lib/bundler/man
12
12
  pkg
13
13
  rdoc
14
14
  spec/reports
15
- test/tmp
16
- test/version_tmp
17
15
  tmp
18
16
  *.bundle
19
17
  *.so
20
18
  *.o
21
19
  *.a
22
20
  mkmf.log
21
+ .idea/
data/.travis.yml CHANGED
@@ -6,14 +6,11 @@ script: bundle exec rake
6
6
  rvm:
7
7
  - 1.9.3
8
8
  - 2.0.0
9
- - 2.1.1
9
+ - 2.1.2
10
10
  - jruby-19mode
11
11
  - rbx-2
12
12
  gemfile:
13
13
  - gemfiles/active_model_3.2.gemfile
14
- - gemfiles/active_model_4.0_obs.gemfile
15
14
  - gemfiles/active_model_4.0.gemfile
16
- - gemfiles/active_model_4.1_obs.gemfile
17
15
  - gemfiles/active_model_4.1.gemfile
18
- - gemfiles/active_model_edge_obs.gemfile
19
16
  - gemfiles/active_model_edge.gemfile
data/Appraisals CHANGED
@@ -3,30 +3,15 @@ appraise "active_model_3.2" do
3
3
  gem "activemodel", "~> 3.2.0"
4
4
  end
5
5
 
6
- appraise "active_model_4.0_obs" do
7
- gem "activemodel", "~> 4.0.0"
8
- gem 'rails-observers'
9
- end
10
-
11
-
12
6
  appraise "active_model_4.0" do
13
7
  gem "activemodel", "~> 4.0.0"
14
8
  end
15
9
 
16
- appraise 'active_model_4.1_obs' do
17
- gem 'activemodel', '~> 4.1.0'
18
- gem 'rails-observers'
19
- end
20
10
 
21
11
  appraise 'active_model_4.1' do
22
12
  gem 'activemodel', '~> 4.1.0'
23
13
  end
24
14
 
25
- appraise 'active_model_edge_obs' do
26
- gem 'activemodel', github: 'rails/rails'
27
- gem 'rails-observers'
28
- end
29
-
30
15
  appraise 'active_model_edge' do
31
16
  gem 'activemodel', github: 'rails/rails'
32
17
  end
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in state_machine2_activemodel.gemspec
4
- gemspec
4
+ gemspec
5
+
data/README.md CHANGED
@@ -1,3 +1,6 @@
1
+ [![Build Status](https://travis-ci.org/seuros/state_machines-activemodel.svg?branch=master)](https://travis-ci.org/seuros/state_machines-activemodel)
2
+ [![Code Climate](https://codeclimate.com/github/seuros/state_machines-activemodel.png)](https://codeclimate.com/github/seuros/state_machines-activemodel)
3
+
1
4
  # StateMachines ActiveModel Integration
2
5
 
3
6
  The ActiveModel integration is useful for both standalone usage and for providing
@@ -9,7 +12,7 @@ observers.
9
12
 
10
13
  Add this line to your application's Gemfile:
11
14
 
12
- gem 'state_machine2_activemodel'
15
+ gem 'state_machines-activemodel'
13
16
 
14
17
  And then execute:
15
18
 
@@ -17,7 +20,7 @@ And then execute:
17
20
 
18
21
  Or install it yourself as:
19
22
 
20
- $ gem install state_machine2_activemodel
23
+ $ gem install state_machines-activemodel
21
24
 
22
25
  ## Usage
23
26
 
@@ -1,17 +1,17 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- state_machines-activemodel (0.0.1)
4
+ state_machines-activemodel (0.0.2)
5
5
  activemodel (>= 3.2)
6
6
  state_machines
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- activemodel (3.2.17)
12
- activesupport (= 3.2.17)
11
+ activemodel (3.2.18)
12
+ activesupport (= 3.2.18)
13
13
  builder (~> 3.0.0)
14
- activesupport (3.2.17)
14
+ activesupport (3.2.18)
15
15
  i18n (~> 0.6, >= 0.6.4)
16
16
  multi_json (~> 1.0)
17
17
  appraisal (1.0.0)
@@ -21,7 +21,7 @@ GEM
21
21
  builder (3.0.4)
22
22
  diff-lcs (1.2.5)
23
23
  i18n (0.6.9)
24
- multi_json (1.9.3)
24
+ multi_json (1.10.0)
25
25
  rake (10.3.1)
26
26
  rspec (3.0.0.beta2)
27
27
  rspec-core (= 3.0.0.beta2)
@@ -35,7 +35,7 @@ GEM
35
35
  rspec-mocks (3.0.0.beta2)
36
36
  rspec-support (= 3.0.0.beta2)
37
37
  rspec-support (3.0.0.beta2)
38
- state_machines (0.0.1)
38
+ state_machines (0.0.2)
39
39
  thor (0.19.1)
40
40
 
41
41
  PLATFORMS
@@ -1,17 +1,17 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- state_machines-activemodel (0.0.1)
4
+ state_machines-activemodel (0.0.2)
5
5
  activemodel (>= 3.2)
6
6
  state_machines
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- activemodel (4.0.4)
12
- activesupport (= 4.0.4)
11
+ activemodel (4.0.5)
12
+ activesupport (= 4.0.5)
13
13
  builder (~> 3.1.0)
14
- activesupport (4.0.4)
14
+ activesupport (4.0.5)
15
15
  i18n (~> 0.6, >= 0.6.9)
16
16
  minitest (~> 4.2)
17
17
  multi_json (~> 1.3)
@@ -25,7 +25,7 @@ GEM
25
25
  diff-lcs (1.2.5)
26
26
  i18n (0.6.9)
27
27
  minitest (4.7.5)
28
- multi_json (1.9.3)
28
+ multi_json (1.10.0)
29
29
  rake (10.3.1)
30
30
  rspec (3.0.0.beta2)
31
31
  rspec-core (= 3.0.0.beta2)
@@ -39,7 +39,7 @@ GEM
39
39
  rspec-mocks (3.0.0.beta2)
40
40
  rspec-support (= 3.0.0.beta2)
41
41
  rspec-support (3.0.0.beta2)
42
- state_machines (0.0.1)
42
+ state_machines (0.0.2)
43
43
  thor (0.19.1)
44
44
  thread_safe (0.3.3)
45
45
  tzinfo (0.3.39)
@@ -1,17 +1,17 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- state_machines-activemodel (0.0.1)
4
+ state_machines-activemodel (0.0.2)
5
5
  activemodel (>= 3.2)
6
6
  state_machines
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- activemodel (4.1.0)
12
- activesupport (= 4.1.0)
11
+ activemodel (4.1.1)
12
+ activesupport (= 4.1.1)
13
13
  builder (~> 3.1)
14
- activesupport (4.1.0)
14
+ activesupport (4.1.1)
15
15
  i18n (~> 0.6, >= 0.6.9)
16
16
  json (~> 1.7, >= 1.7.7)
17
17
  minitest (~> 5.1)
@@ -39,7 +39,7 @@ GEM
39
39
  rspec-mocks (3.0.0.beta2)
40
40
  rspec-support (= 3.0.0.beta2)
41
41
  rspec-support (3.0.0.beta2)
42
- state_machines (0.0.1)
42
+ state_machines (0.0.2)
43
43
  thor (0.19.1)
44
44
  thread_safe (0.3.3)
45
45
  tzinfo (1.1.0)
@@ -1,6 +1,6 @@
1
1
  GIT
2
2
  remote: git://github.com/rails/rails.git
3
- revision: 89893a000188f977163dddc04ff37846169a35dc
3
+ revision: 595326647405282e9edc6754019945896b1f2fc5
4
4
  specs:
5
5
  activemodel (4.2.0.alpha)
6
6
  activesupport (= 4.2.0.alpha)
@@ -15,7 +15,7 @@ GIT
15
15
  PATH
16
16
  remote: ../
17
17
  specs:
18
- state_machines-activemodel (0.0.1)
18
+ state_machines-activemodel (0.0.2)
19
19
  activemodel (>= 3.2)
20
20
  state_machines
21
21
 
@@ -44,7 +44,7 @@ GEM
44
44
  rspec-mocks (3.0.0.beta2)
45
45
  rspec-support (= 3.0.0.beta2)
46
46
  rspec-support (3.0.0.beta2)
47
- state_machines (0.0.1)
47
+ state_machines (0.0.2)
48
48
  thor (0.19.1)
49
49
  thread_safe (0.3.3)
50
50
  tzinfo (1.1.0)
@@ -367,10 +367,10 @@ module StateMachines
367
367
 
368
368
  @defaults = {}
369
369
 
370
- # Classes that include ActiveModel::Observing or ActiveModel::Validations
370
+ # Classes that include ActiveModel::Validations
371
371
  # will automatically use the ActiveModel integration.
372
372
  def self.matching_ancestors
373
- %w(ActiveModel ActiveModel::Observing ActiveModel::Validations)
373
+ %w(ActiveModel ActiveModel::Validations)
374
374
  end
375
375
 
376
376
  # Adds a validation error to the given object
@@ -404,11 +404,6 @@ module StateMachines
404
404
  end
405
405
 
406
406
  protected
407
- # Whether observers are supported in the integration. Only true if
408
- # ActiveModel::Observer is available.
409
- def supports_observers?
410
- defined?(::ActiveModel::Observing) && owner_class <= ::ActiveModel::Observing
411
- end
412
407
 
413
408
  # Whether validations are supported in the integration. Only true if
414
409
  # the ActiveModel feature is enabled on the owner class.
@@ -462,7 +457,6 @@ module StateMachines
462
457
  # Build a list of ancestors for the given class to use when
463
458
  # determining which localization key to use for a particular string.
464
459
  def ancestors_for(klass)
465
- load_observer_extensions
466
460
  klass.lookup_ancestors
467
461
  end
468
462
 
@@ -470,10 +464,6 @@ module StateMachines
470
464
  def after_initialize
471
465
  super()
472
466
  load_locale
473
- if supports_observers?
474
- load_observer_extensions
475
- add_default_callbacks
476
- end
477
467
  end
478
468
 
479
469
  # Loads any locale files needed for translating validation errors
@@ -485,20 +475,6 @@ module StateMachines
485
475
  "#{File.dirname(__FILE__)}/active_model/locale.rb"
486
476
  end
487
477
 
488
- # Loads extensions to ActiveModel's Observers
489
- def load_observer_extensions
490
- require 'state_machines/integrations/active_model/observer'
491
- require 'state_machines/integrations/active_model/observer_update'
492
- end
493
-
494
- # Adds a set of default callbacks that utilize the Observer extensions
495
- def add_default_callbacks
496
- if supports_observers?
497
- callbacks[:before] << Callback.new(:before) { |object, transition| notify(:before, object, transition) }
498
- callbacks[:after] << Callback.new(:after) { |object, transition| notify(:after, object, transition) }
499
- callbacks[:failure] << Callback.new(:failure) { |object, transition| notify(:after_failure_to, object, transition) }
500
- end
501
- end
502
478
 
503
479
  # Skips defining reader/writer methods since this is done automatically
504
480
  def define_state_accessor
@@ -527,14 +503,7 @@ module StateMachines
527
503
  # initialization.
528
504
  def add_callback(type, options, &block)
529
505
  options[:terminator] = callback_terminator
530
-
531
- if supports_observers?
532
- @callbacks[type == :around ? :before : type].insert(-2, callback = Callback.new(type, options, &block))
533
- add_states(callback.known_states)
534
- callback
535
- else
536
- super
537
- end
506
+ super
538
507
  end
539
508
 
540
509
  # Configures new states with the built-in humanize scheme
@@ -551,43 +520,7 @@ module StateMachines
551
520
  end
552
521
  end
553
522
 
554
- # Notifies observers on the given object that a callback occurred
555
- # involving the given transition. This will attempt to call the
556
- # following methods on observers:
557
- # * <tt>#{type}_#{qualified_event}_from_#{from}_to_#{to}</tt>
558
- # * <tt>#{type}_#{qualified_event}_from_#{from}</tt>
559
- # * <tt>#{type}_#{qualified_event}_to_#{to}</tt>
560
- # * <tt>#{type}_#{qualified_event}</tt>
561
- # * <tt>#{type}_transition_#{machine_name}_from_#{from}_to_#{to}</tt>
562
- # * <tt>#{type}_transition_#{machine_name}_from_#{from}</tt>
563
- # * <tt>#{type}_transition_#{machine_name}_to_#{to}</tt>
564
- # * <tt>#{type}_transition_#{machine_name}</tt>
565
- # * <tt>#{type}_transition</tt>
566
- #
567
- # This will always return true regardless of the results of the
568
- # callbacks.
569
- def notify(type, object, transition)
570
- name = self.name
571
- event = transition.qualified_event
572
- from = transition.from_name || 'nil'
573
- to = transition.to_name || 'nil'
574
-
575
- # Machine-specific updates
576
- ["#{type}_#{event}", "#{type}_transition_#{name}"].each do |event_segment|
577
- ["_from_#{from}", nil].each do |from_segment|
578
- ["_to_#{to}", nil].each do |to_segment|
579
- object.class.changed if object.class.respond_to?(:changed)
580
- object.class.notify_observers('update_with_transition', ObserverUpdate.new([event_segment, from_segment, to_segment].join, object, transition))
581
- end
582
- end
583
- end
584
-
585
- # Generic updates
586
- object.class.changed if object.class.respond_to?(:changed)
587
- object.class.notify_observers('update_with_transition', ObserverUpdate.new("#{type}_transition", object, transition))
588
523
 
589
- true
590
- end
591
524
  end
592
525
  end
593
526
  end
@@ -1,7 +1,7 @@
1
1
  module StateMachines
2
2
  module Integrations
3
3
  module ActiveModel
4
- VERSION = '0.0.1'
4
+ VERSION = '0.0.2'
5
5
  end
6
6
  end
7
7
  end
@@ -636,4 +636,166 @@ describe StateMachines::Integrations::ActiveModel do
636
636
  assert record.valid?
637
637
  end
638
638
  end
639
+
640
+
641
+
642
+ context 'WithInternationalization' do
643
+ before(:each) do
644
+ I18n.backend = I18n::Backend::Simple.new
645
+
646
+ # Initialize the backend
647
+ I18n.backend.translate(:en, 'activemodel.errors.messages.invalid_transition', :event => 'ignite', :value => 'idling')
648
+
649
+ @model = new_model { include ActiveModel::Validations }
650
+ end
651
+
652
+ it 'should_use_defaults' do
653
+ I18n.backend.store_translations(:en, {
654
+ :activemodel => {:errors => {:messages => {:invalid_transition => 'cannot %{event}'}}}
655
+ })
656
+
657
+ machine = StateMachines::Machine.new(@model, :action => :save)
658
+ machine.state :parked, :idling
659
+ machine.event :ignite
660
+
661
+ record = @model.new(:state => 'idling')
662
+
663
+ machine.invalidate(record, :state, :invalid_transition, [[:event, 'ignite']])
664
+ assert_equal ['State cannot ignite'], record.errors.full_messages
665
+ end
666
+
667
+ it 'should_allow_customized_error_key' do
668
+ I18n.backend.store_translations(:en, {
669
+ :activemodel => {:errors => {:messages => {:bad_transition => 'cannot %{event}'}}}
670
+ })
671
+
672
+ machine = StateMachines::Machine.new(@model, :action => :save, :messages => {:invalid_transition => :bad_transition})
673
+ machine.state :parked, :idling
674
+
675
+ record = @model.new
676
+ record.state = 'idling'
677
+
678
+ machine.invalidate(record, :state, :invalid_transition, [[:event, 'ignite']])
679
+ assert_equal ['State cannot ignite'], record.errors.full_messages
680
+ end
681
+
682
+ it 'should_allow_customized_error_string' do
683
+ machine = StateMachines::Machine.new(@model, :action => :save, :messages => {:invalid_transition => 'cannot %{event}'})
684
+ machine.state :parked, :idling
685
+
686
+ record = @model.new(:state => 'idling')
687
+
688
+ machine.invalidate(record, :state, :invalid_transition, [[:event, 'ignite']])
689
+ assert_equal ['State cannot ignite'], record.errors.full_messages
690
+ end
691
+
692
+ it 'should_allow_customized_state_key_scoped_to_class_and_machine' do
693
+ I18n.backend.store_translations(:en, {
694
+ :activemodel => {:state_machines => {:'bar/foo' => {:state => {:states => {:parked => 'shutdown'}}}}}
695
+ })
696
+
697
+ machine = StateMachines::Machine.new(@model)
698
+ machine.state :parked
699
+
700
+ assert_equal 'shutdown', machine.state(:parked).human_name
701
+ end
702
+
703
+ it 'should_allow_customized_state_key_scoped_to_class' do
704
+ I18n.backend.store_translations(:en, {
705
+ :activemodel => {:state_machines => {:'bar/foo' => {:states => {:parked => 'shutdown'}}}}
706
+ })
707
+
708
+ machine = StateMachines::Machine.new(@model)
709
+ machine.state :parked
710
+
711
+ assert_equal 'shutdown', machine.state(:parked).human_name
712
+ end
713
+
714
+ it 'should_allow_customized_state_key_scoped_to_machine' do
715
+ I18n.backend.store_translations(:en, {
716
+ :activemodel => {:state_machines => {:state => {:states => {:parked => 'shutdown'}}}}
717
+ })
718
+
719
+ machine = StateMachines::Machine.new(@model)
720
+ machine.state :parked
721
+
722
+ assert_equal 'shutdown', machine.state(:parked).human_name
723
+ end
724
+
725
+ it 'should_allow_customized_state_key_unscoped' do
726
+ I18n.backend.store_translations(:en, {
727
+ :activemodel => {:state_machines => {:states => {:parked => 'shutdown'}}}
728
+ })
729
+
730
+ machine = StateMachines::Machine.new(@model)
731
+ machine.state :parked
732
+
733
+ assert_equal 'shutdown', machine.state(:parked).human_name
734
+ end
735
+
736
+ it 'should_support_nil_state_key' do
737
+ I18n.backend.store_translations(:en, {
738
+ :activemodel => {:state_machines => {:states => {:nil => 'empty'}}}
739
+ })
740
+
741
+ machine = StateMachines::Machine.new(@model)
742
+
743
+ assert_equal 'empty', machine.state(nil).human_name
744
+ end
745
+
746
+ it 'should_allow_customized_event_key_scoped_to_class_and_machine' do
747
+ I18n.backend.store_translations(:en, {
748
+ :activemodel => {:state_machines => {:'bar/foo' => {:state => {:events => {:park => 'stop'}}}}}
749
+ })
750
+
751
+ machine = StateMachines::Machine.new(@model)
752
+ machine.event :park
753
+
754
+ assert_equal 'stop', machine.event(:park).human_name
755
+ end
756
+
757
+ it 'should_allow_customized_event_key_scoped_to_class' do
758
+ I18n.backend.store_translations(:en, {
759
+ :activemodel => {:state_machines => {:'bar/foo' => {:events => {:park => 'stop'}}}}
760
+ })
761
+
762
+ machine = StateMachines::Machine.new(@model)
763
+ machine.event :park
764
+
765
+ assert_equal 'stop', machine.event(:park).human_name
766
+ end
767
+
768
+ it 'should_allow_customized_event_key_scoped_to_machine' do
769
+ I18n.backend.store_translations(:en, {
770
+ :activemodel => {:state_machines => {:state => {:events => {:park => 'stop'}}}}
771
+ })
772
+
773
+ machine = StateMachines::Machine.new(@model)
774
+ machine.event :park
775
+
776
+ assert_equal 'stop', machine.event(:park).human_name
777
+ end
778
+
779
+ it 'should_allow_customized_event_key_unscoped' do
780
+ I18n.backend.store_translations(:en, {
781
+ :activemodel => {:state_machines => {:events => {:park => 'stop'}}}
782
+ })
783
+
784
+ machine = StateMachines::Machine.new(@model)
785
+ machine.event :park
786
+
787
+ assert_equal 'stop', machine.event(:park).human_name
788
+ end
789
+
790
+ it 'should_only_add_locale_once_in_load_path' do
791
+ assert_equal 1, I18n.load_path.select { |path| path =~ %r{active_model/locale\.rb$} }.length
792
+
793
+ # Create another ActiveModel model that will triger the i18n feature
794
+ new_model
795
+
796
+ assert_equal 1, I18n.load_path.select { |path| path =~ %r{active_model/locale\.rb$} }.length
797
+ end
798
+
799
+ end
800
+
639
801
  end