Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Cleanup

* Use Ruby 1.9 hash syntax
* Remove usage of explicit self in cases that apply
  • Loading branch information...
commit 20339fd3c207377fcc496526a0fedbd66fe620bf 1 parent 2e57d22
Travis Anderson authored January 15, 2013 Hashrocket Workstation committed January 15, 2013
16  lib/active_model/transitions.rb
@@ -30,10 +30,10 @@ module Transitions
30 30
       validates_presence_of :state
31 31
       validate :state_inclusion
32 32
     end
33  
-    
  33
+
34 34
     # The optional options argument is passed to find when reloading so you may
35  
-    # do e.g. record.reload(:lock => true) to reload the same record with an
36  
-    # exclusive row lock. 
  35
+    # do e.g. record.reload(lock: true) to reload the same record with an
  36
+    # exclusive row lock.
37 37
     def reload(options = nil)
38 38
       super.tap do
39 39
         sm = self.class.get_state_machine
@@ -51,7 +51,7 @@ def write_state(state)
51 51
       write_state_without_persistence(prev_state)
52 52
       raise
53 53
     end
54  
-    
  54
+
55 55
     def write_state_without_persistence(state)
56 56
       ivar = self.class.get_state_machine.current_state_variable
57 57
       instance_variable_set(ivar, state)
@@ -59,16 +59,16 @@ def write_state_without_persistence(state)
59 59
     end
60 60
 
61 61
     def read_state
62  
-      self.state && self.state.to_sym
  62
+      state && state.to_sym
63 63
     end
64 64
 
65 65
     def set_initial_state
66  
-      self.state ||= self.class.get_state_machine.initial_state.to_s if self.has_attribute?(:state)
  66
+      self.state ||= self.class.get_state_machine.initial_state.to_s if has_attribute?(:state)
67 67
     end
68 68
 
69 69
     def state_inclusion
70  
-      unless self.class.get_state_machine.states.map{|s| s.name.to_s }.include?(self.state.to_s)
71  
-        self.errors.add(:state, :inclusion, :value => self.state)
  70
+      unless self.class.get_state_machine.states.detect{|s| s.name.to_s == state.to_s }
  71
+        self.errors.add(:state, :inclusion, value: state)
72 72
       end
73 73
     end
74 74
   end
18  lib/transitions/event.rb
@@ -44,7 +44,7 @@ def initialize(machine, name, options = {}, &block)
44 44
 
45 45
     def fire(obj, to_state = nil, *args)
46 46
       transitions = @transitions.select { |t| t.from == obj.current_state }
47  
-      raise InvalidTransition, error_message_for_invalid_transitions(obj, to_state) if transitions.size == 0
  47
+      raise InvalidTransition, error_message_for_invalid_transitions(obj, to_state) if transitions.empty?
48 48
 
49 49
       next_state = nil
50 50
       transitions.each do |transition|
@@ -71,7 +71,7 @@ def ==(event)
71 71
         name == event.name
72 72
       end
73 73
     end
74  
-    
  74
+
75 75
     # Has the timestamp option been specified for this event?
76 76
     def timestamp_defined?
77 77
       !@timestamp.nil?
@@ -83,12 +83,12 @@ def update(options = {}, &block)
83 83
       instance_eval(&block) if block
84 84
       self
85 85
     end
86  
-    
  86
+
87 87
     # update the timestamp attribute on obj
88 88
     def update_event_timestamp(obj, next_state)
89 89
       obj.send "#{timestamp_attribute_name(obj, next_state)}=", Time.now
90 90
     end
91  
-    
  91
+
92 92
     # Set the timestamp attribute.
93 93
     # @raise [ArgumentError] timestamp should be either a String, Symbol or true
94 94
     def timestamp=(value)
@@ -99,17 +99,17 @@ def timestamp=(value)
99 99
         raise ArgumentError, "timestamp must be either: true, a String or a Symbol"
100 100
       end
101 101
     end
102  
-    
  102
+
103 103
 
104 104
     private
105  
-    
  105
+
106 106
     # Returns the name of the timestamp attribute for this event
107 107
     # If the timestamp was simply true it returns the default_timestamp_name
108 108
     # otherwise, returns the user-specified timestamp name
109 109
     def timestamp_attribute_name(obj, next_state)
110 110
       timestamp == true ? default_timestamp_name(obj, next_state) : @timestamp
111 111
     end
112  
-    
  112
+
113 113
     # If @timestamp is true, try a default timestamp name
114 114
     def default_timestamp_name(obj, next_state)
115 115
       at_name = "%s_at" % next_state
@@ -118,14 +118,14 @@ def default_timestamp_name(obj, next_state)
118 118
       when obj.respond_to?(at_name) then at_name
119 119
       when obj.respond_to?(on_name) then on_name
120 120
       else
121  
-        raise NoMethodError, "Couldn't find a suitable timestamp field for event: #{@name}. 
  121
+        raise NoMethodError, "Couldn't find a suitable timestamp field for event: #{@name}.
122 122
           Please define #{at_name} or #{on_name} in #{obj.class}"
123 123
       end
124 124
     end
125 125
 
126 126
     def transitions(trans_opts)
127 127
       Array(trans_opts[:from]).each do |s|
128  
-        @transitions << StateTransition.new(trans_opts.merge({:from => s.to_sym}))
  128
+        @transitions << StateTransition.new(trans_opts.merge({from: s.to_sym}))
129 129
       end
130 130
     end
131 131
 
4  lib/transitions/machine.rb
@@ -85,7 +85,7 @@ def current_state_variable
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
@@ -102,7 +102,7 @@ def include_scopes
102 102
         if @klass.respond_to?(state_name)
103 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."
104 104
         end
105  
-        @klass.scope state_name, @klass.where(:state => state_name)
  105
+        @klass.scope state_name, @klass.where(state: state_name)
106 106
       end
107 107
     end
108 108
   end
2  test/active_record/test_active_record.rb
@@ -138,7 +138,7 @@ def setup
138 138
     @light.update_attribute(:state, 'green')
139 139
     assert @light.reload.green?, "reloaded state should come from database, not instance variable"
140 140
   end
141  
-  
  141
+
142 142
   test "calling non-bang event updates state attribute" do
143 143
     @light.reset!
144 144
     assert @light.red?

1 note on commit 20339fd

Matthew Conway

For great good :+1:

Please sign in to comment.
Something went wrong with that request. Please try again.