transitions 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +4 -0
  3. data/.travis.yml +3 -1
  4. data/CHANGELOG.md +4 -0
  5. data/Gemfile +7 -2
  6. data/README.md +1 -1
  7. data/Rakefile +4 -4
  8. data/lib/active_model/transitions.rb +8 -8
  9. data/lib/transitions/event.rb +15 -16
  10. data/lib/transitions/machine.rb +4 -5
  11. data/lib/transitions/presenter.rb +1 -1
  12. data/lib/transitions/state.rb +2 -1
  13. data/lib/transitions/state_transition.rb +2 -2
  14. data/lib/transitions/version.rb +1 -1
  15. data/lib/transitions.rb +10 -8
  16. data/test/active_record/test_active_record.rb +64 -66
  17. data/test/active_record/test_active_record_scopes.rb +7 -7
  18. data/test/active_record/test_active_record_timestamps.rb +27 -28
  19. data/test/active_record/test_custom_select.rb +3 -3
  20. data/test/event/test_event.rb +20 -20
  21. data/test/event/test_event_arguments.rb +3 -4
  22. data/test/event/test_event_being_fired.rb +4 -4
  23. data/test/event/test_event_checks.rb +5 -6
  24. data/test/helper.rb +1 -1
  25. data/test/machine/machine_template.rb +4 -4
  26. data/test/machine/test_available_states_listing.rb +1 -1
  27. data/test/machine/test_fire_event_machine.rb +5 -5
  28. data/test/machine/test_machine.rb +10 -10
  29. data/test/state/test_state.rb +16 -16
  30. data/test/state/test_state_predicate_method.rb +2 -2
  31. data/test/state_transition/test_state_transition.rb +11 -11
  32. data/test/state_transition/test_state_transition_event_failed_callback.rb +3 -3
  33. data/test/state_transition/test_state_transition_event_fired_callback.rb +3 -3
  34. data/test/state_transition/test_state_transition_guard_check.rb +14 -15
  35. data/test/state_transition/test_state_transition_on_transition_callback.rb +4 -4
  36. data/test/state_transition/test_state_transition_success_callback.rb +8 -8
  37. data/transitions.gemspec +16 -16
  38. metadata +4 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4f76fbe7e05b93bbec46f0faa6723169979ec375
4
- data.tar.gz: d968ddb9b44da30cb3b8f34bfa5f536e33d3a6a3
3
+ metadata.gz: 50aa94816f542aa2e3ddfb4ea2c3b158e1302226
4
+ data.tar.gz: e9f190796c29fc615582e3722c0d42fabaadb84b
5
5
  SHA512:
6
- metadata.gz: 39fecff9fec8636c7a99efbac2f32fed979717da83c552e9c73732582ffff3a7e987ef1ce9b1b4032abd6c3491c42f148fa477013c77e18da03e4230e393a3fa
7
- data.tar.gz: 7829b0d74c9cb917f4569285eaebb4ed83c4ba73405b3e42e7419f5c2a0d3ead24e4f42bf30ccf5a2a26a61e7524556b45ee290756d2fdbda13e585c2764226a
6
+ metadata.gz: 3428e39ec7f594f3e2988be6d99438b52fff0eed9b92e668c49a98d6107824c0aadd4ac4cbade09d609bfb3738a7f045f5223ae89a938d8e6982bc440935a888
7
+ data.tar.gz: 2878d1c45b68664d87bc1b036ef7d25cb4caaf01e8ebd9bdcc9e6130476c5d0aac0088ae7255a4ddb1591c27bb17fbce2938f0eab088afbd5ae153d1e61ef17e
data/.rubocop.yml ADDED
@@ -0,0 +1,4 @@
1
+ Metrics/LineLength:
2
+ Max: 120
3
+ Style/Documentation:
4
+ Enabled: false
data/.travis.yml CHANGED
@@ -1,7 +1,9 @@
1
1
  rvm:
2
- - 2.0.0
3
2
  - 1.9.3
3
+ - 2.0.0
4
+ - 2.1
4
5
  - jruby-19mode
6
+ - rbx-2
5
7
  gemfile:
6
8
  - gemfiles/rails_3_0.gemfile
7
9
  - gemfiles/rails_3_1.gemfile
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 0.2.1
2
+
3
+ * (ozydingo / Andrew Schwartz) Don't use ActiveModel#has_attribute? since this adds the attribute name to self.attribute_names
4
+
1
5
  # 0.2.0
2
6
 
3
7
  * (troessner) Fix missing explicit returns in our callback handling
data/Gemfile CHANGED
@@ -3,5 +3,10 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  gem 'activerecord', '~> 3.2.14'
6
- gem 'activerecord-jdbcsqlite3-adapter', :platforms => :jruby
7
- gem 'sqlite3', :platforms => :ruby
6
+ gem 'activerecord-jdbcsqlite3-adapter', platforms: :jruby
7
+
8
+ platforms :ruby do
9
+ gem 'sqlite3'
10
+ gem 'pry'
11
+ gem 'byebug'
12
+ end
data/README.md CHANGED
@@ -215,7 +215,7 @@ can use `success`. This will be called after the `save!` is complete (if you
215
215
  use the `state_name!` method) and should be used for any methods that require
216
216
  that the object be persisted.
217
217
  ```ruby
218
- event :discontinue, :success => :notfiy_admin do
218
+ event :discontinue, :success => :notify_admin do
219
219
  transitions :to => :discontinued, :from => [:available, :out_of_stock]
220
220
  end
221
221
  ```
data/Rakefile CHANGED
@@ -1,14 +1,14 @@
1
- require "bundler"
1
+ require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
3
  Bundler.setup
4
4
 
5
5
  require 'appraisal'
6
6
 
7
- require "rake/testtask"
7
+ require 'rake/testtask'
8
8
  Rake::TestTask.new(:test) do |test|
9
9
  test.libs = %w(lib test)
10
- test.pattern = "test/**/test_*.rb"
10
+ test.pattern = 'test/**/test_*.rb'
11
11
  test.verbose = true
12
12
  end
13
13
 
14
- task :default => :test
14
+ task default: :test
@@ -6,8 +6,8 @@ module ActiveModel
6
6
  class ::Transitions::Machine
7
7
  unless method_defined?(:new_transitions_initialize) || method_defined?(:new_transitions_update)
8
8
  attr_reader :attribute_name
9
- alias :old_transitions_initialize :initialize
10
- alias :old_transitions_update :update
9
+ alias_method :old_transitions_initialize, :initialize
10
+ alias_method :old_transitions_update, :update
11
11
 
12
12
  def new_transitions_initialize(*args, &block)
13
13
  @attribute_name = :state
@@ -19,8 +19,8 @@ module ActiveModel
19
19
  old_transitions_update(options, &block)
20
20
  end
21
21
 
22
- alias :initialize :new_transitions_initialize
23
- alias :update :new_transitions_update
22
+ alias_method :initialize, :new_transitions_initialize
23
+ alias_method :update, :new_transitions_update
24
24
  end
25
25
  end
26
26
  include ::Transitions
@@ -66,7 +66,7 @@ module ActiveModel
66
66
 
67
67
  def set_initial_state
68
68
  # In case we use a query with a custom select that excludes our state attribute name we need to skip the initialization below.
69
- if self.has_attribute?(transitions_state_column_name) && state_not_set?
69
+ if self.attribute_names.include?(transitions_state_column_name.to_s) && state_not_set?
70
70
  self[transitions_state_column_name] = self.class.get_state_machine.initial_state.to_s
71
71
  self.class.get_state_machine.state_index[self[transitions_state_column_name].to_sym].call_action(:enter, self)
72
72
  end
@@ -74,13 +74,13 @@ module ActiveModel
74
74
 
75
75
  def state_presence
76
76
  unless self[transitions_state_column_name].present?
77
- self.errors.add(transitions_state_column_name, :presence)
77
+ errors.add(transitions_state_column_name, :presence)
78
78
  end
79
79
  end
80
80
 
81
81
  def state_inclusion
82
- unless self.class.get_state_machine.states.map{|s| s.name.to_s }.include?(self[transitions_state_column_name].to_s)
83
- self.errors.add(transitions_state_column_name, :inclusion, :value => self[transitions_state_column_name])
82
+ unless self.class.get_state_machine.states.map { |s| s.name.to_s }.include?(self[transitions_state_column_name].to_s)
83
+ errors.add(transitions_state_column_name, :inclusion, value: self[transitions_state_column_name])
84
84
  end
85
85
  end
86
86
 
@@ -13,15 +13,15 @@ module Transitions
13
13
  machine.fire_event(name, self, false, *args)
14
14
  end
15
15
 
16
- machine.klass.send(:define_method, "can_#{name.to_s}?") do |*args|
16
+ machine.klass.send(:define_method, "can_#{name}?") do |*_args|
17
17
  machine.events_for(current_state).include?(name.to_sym)
18
18
  end
19
19
 
20
- machine.klass.send(:define_method, "can_execute_#{name.to_s}?") do |*args|
20
+ machine.klass.send(:define_method, "can_execute_#{name}?") do |*args|
21
21
  event = name.to_sym
22
22
 
23
- send("can_#{name.to_s}?", *args) &&
24
- machine.events[event].can_execute_transition_from_state?(current_state, self, *args)
23
+ send("can_#{name}?", *args) &&
24
+ machine.events[event].can_execute_transition_from_state?(current_state, self, *args)
25
25
  end
26
26
  end
27
27
  update(options, &block)
@@ -29,7 +29,7 @@ module Transitions
29
29
 
30
30
  def fire(obj, to_state = nil, *args)
31
31
  transitions = @transitions.select { |t| t.from == obj.current_state }
32
- raise InvalidTransition, error_message_for_invalid_transitions(obj) if transitions.size == 0
32
+ fail InvalidTransition, error_message_for_invalid_transitions(obj) if transitions.size == 0
33
33
 
34
34
  next_state = nil
35
35
  transitions.each do |transition|
@@ -60,7 +60,7 @@ module Transitions
60
60
  name == event.name
61
61
  end
62
62
  end
63
-
63
+
64
64
  # Has the timestamp option been specified for this event?
65
65
  def timestamp_defined?
66
66
  !@timestamp.nil?
@@ -72,12 +72,12 @@ module Transitions
72
72
  instance_eval(&block) if block
73
73
  self
74
74
  end
75
-
75
+
76
76
  # update the timestamp attribute on obj
77
77
  def update_event_timestamp(obj, next_state)
78
78
  obj.send "#{timestamp_attribute_name(obj, next_state)}=", Time.now
79
79
  end
80
-
80
+
81
81
  # Set the timestamp attribute.
82
82
  # @raise [ArgumentError] timestamp should be either a String, Symbol or true
83
83
  def timestamp=(value)
@@ -85,36 +85,35 @@ module Transitions
85
85
  when String, Symbol, TrueClass
86
86
  @timestamp = value
87
87
  else
88
- raise ArgumentError, "timestamp must be either: true, a String or a Symbol"
88
+ fail ArgumentError, 'timestamp must be either: true, a String or a Symbol'
89
89
  end
90
90
  end
91
-
92
91
 
93
92
  private
94
-
93
+
95
94
  # Returns the name of the timestamp attribute for this event
96
95
  # If the timestamp was simply true it returns the default_timestamp_name
97
96
  # otherwise, returns the user-specified timestamp name
98
97
  def timestamp_attribute_name(obj, next_state)
99
98
  timestamp == true ? default_timestamp_name(obj, next_state) : @timestamp
100
99
  end
101
-
100
+
102
101
  # If @timestamp is true, try a default timestamp name
103
102
  def default_timestamp_name(obj, next_state)
104
- at_name = "%s_at" % next_state
105
- on_name = "%s_on" % next_state
103
+ at_name = '%s_at' % next_state
104
+ on_name = '%s_on' % next_state
106
105
  case
107
106
  when obj.respond_to?(at_name) then at_name
108
107
  when obj.respond_to?(on_name) then on_name
109
108
  else
110
- raise NoMethodError, "Couldn't find a suitable timestamp field for event: #{@name}.
109
+ fail NoMethodError, "Couldn't find a suitable timestamp field for event: #{@name}.
111
110
  Please define #{at_name} or #{on_name} in #{obj.class}"
112
111
  end
113
112
  end
114
113
 
115
114
  def transitions(trans_opts)
116
115
  Array(trans_opts[:from]).each do |s|
117
- @transitions << StateTransition.new(trans_opts.merge({:from => s.to_sym}))
116
+ @transitions << StateTransition.new(trans_opts.merge(from: s.to_sym))
118
117
  end
119
118
  end
120
119
 
@@ -45,7 +45,7 @@ module Transitions
45
45
 
46
46
  def events_for(state)
47
47
  events = @events.values.select { |event| event.transitions_from_state?(state) }
48
- events.map! { |event| event.name }
48
+ events.map!(&:name)
49
49
  end
50
50
 
51
51
  def current_state_variable
@@ -53,7 +53,7 @@ module Transitions
53
53
  :@current_state
54
54
  end
55
55
 
56
- private
56
+ private
57
57
 
58
58
  def handle_state_exit_callback(record)
59
59
  state_index[record.current_state].call_action(:exit, record)
@@ -85,7 +85,7 @@ module Transitions
85
85
 
86
86
  def state(name, options = {})
87
87
  unless @state_index.key? name # Just ignore duplicates
88
- state = State.new(name, :machine => self)
88
+ state = State.new(name, machine: self)
89
89
  state.update options
90
90
  @state_index[name] = state
91
91
  @states << state
@@ -100,11 +100,10 @@ module Transitions
100
100
  @states.each do |state|
101
101
  state_name = state.name.to_s
102
102
  if @klass.respond_to?(state_name)
103
- raise InvalidMethodOverride, "Transitions: Can not define scope `#{state_name}` because there is already an equally named method defined - either rename the existing method or the state."
103
+ fail InvalidMethodOverride, "Transitions: Can not define scope `#{state_name}` because there is already an equally named method defined - either rename the existing method or the state."
104
104
  end
105
105
  @klass.scope state_name, -> { @klass.where(@klass.state_machine.attribute_name => state_name) }
106
106
  end
107
107
  end
108
108
  end
109
109
  end
110
-
@@ -1,7 +1,7 @@
1
1
  module Transitions
2
2
  module Presenter
3
3
  def available_states
4
- @state_machine.states.map(&:name).sort_by {|x| x.to_s}
4
+ @state_machine.states.map(&:name).sort_by(&:to_s)
5
5
  end
6
6
 
7
7
  def available_events
@@ -43,10 +43,11 @@ module Transitions
43
43
  end
44
44
 
45
45
  private
46
+
46
47
  def define_state_query_method(machine)
47
48
  method_name, state_name = "#{@name}?", @name # Instance vars are out of scope when calling define_method below, so we use local variables.
48
49
  if machine.klass.method_defined?(method_name.to_sym)
49
- raise InvalidMethodOverride, "Transitions: Can not define method `#{method_name}` because it is already defined - either rename the existing method or the state."
50
+ fail InvalidMethodOverride, "Transitions: Can not define method `#{method_name}` because it is already defined - either rename the existing method or the state."
50
51
  end
51
52
  machine.klass.send :define_method, method_name do
52
53
  current_state.to_s == state_name.to_s
@@ -25,7 +25,7 @@ module Transitions
25
25
  end
26
26
  else
27
27
  # TODO We probably should check for this in the constructor and not that late.
28
- raise ArgumentError, "You can only pass a Symbol, a String, a Proc or an Array to 'on_transition' - got #{@on_transition.class}." unless @on_transition.nil?
28
+ fail ArgumentError, "You can only pass a Symbol, a String, a Proc or an Array to 'on_transition' - got #{@on_transition.class}." unless @on_transition.nil?
29
29
  end
30
30
  end
31
31
 
@@ -37,7 +37,7 @@ module Transitions
37
37
  @from == value
38
38
  end
39
39
 
40
- private
40
+ private
41
41
 
42
42
  def perform_guard(obj, guard, *args)
43
43
  if guard.respond_to?(:call)
@@ -1,3 +1,3 @@
1
1
  module Transitions
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
data/lib/transitions.rb CHANGED
@@ -1,9 +1,9 @@
1
- require "transitions/event"
2
- require "transitions/machine"
3
- require "transitions/presenter"
4
- require "transitions/state"
5
- require "transitions/state_transition"
6
- require "transitions/version"
1
+ require 'transitions/event'
2
+ require 'transitions/machine'
3
+ require 'transitions/presenter'
4
+ require 'transitions/state'
5
+ require 'transitions/state_transition'
6
+ require 'transitions/version'
7
7
 
8
8
  module Transitions
9
9
  class InvalidTransition < StandardError; end
@@ -26,10 +26,12 @@ module Transitions
26
26
  block ? @state_machine.update(options, &block) : @state_machine
27
27
  end
28
28
 
29
- def get_state_machine; @state_machine; end
29
+ def get_state_machine
30
+ @state_machine
31
+ end
30
32
 
31
33
  def available_states
32
- @state_machine.states.map(&:name).sort_by {|x| x.to_s}
34
+ @state_machine.states.map(&:name).sort_by(&:to_s)
33
35
  end
34
36
 
35
37
  def available_events