terrbear (owner)

Revisions

gist: 208506 Download_button fork
public
Public Clone URL: git://gist.github.com/208506.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
      module WriteState
        # Writes <tt>state</tt> to the state column and persists it to the database
        # using update_attribute (which bypasses validation)
        #
        # foo = Foo.find(1)
        # foo.aasm_current_state # => :opened
        # foo.close!
        # foo.aasm_current_state # => :closed
        # Foo.find(1).aasm_current_state # => :closed
        #
        # NOTE: intended to be called from an event
        def aasm_write_state(state)
          old_value = read_attribute(self.class.aasm_column)
          write_attribute(self.class.aasm_column, state.to_s)
 
          unless self.save(false)
            write_attribute(self.class.aasm_column, old_value)
            return false
          end
 
          true
        end
      end