module Stateflow
  module Persistence
    module ActiveRecord
      extend ActiveSupport::Concern

      included do
        before_validation(:ensure_initial_state, :on => :create)
      end

      module ClassMethods
        def add_scope(state)
          scope state.name, where("#{machine.state_column}".to_sym => state.name.to_s)
        end
      end

      def load_from_persistence
        send machine.state_column.to_sym
      end

      def save_to_persistence(new_state, options = {})
        send("#{machine.state_column}=".to_sym, new_state)
        save if options[:save]
      end

      def ensure_initial_state
        send("#{machine.state_column.to_s}=", current_state.name.to_s) if send(machine.state_column.to_s).blank?
      end
    end
  end
end

module Stateflow
  module Persistence
    module ActiveRecord
      extend ActiveSupport::Concern

      included do
        before_validation(:ensure_initial_state, :on => :create)
      end

      module ClassMethods
        def add_scope(state)
          scope state.name, proc { where("#{machine.state_column}".to_sym => state.name.to_s) }
        end
      end

      def load_from_persistence
        send machine.state_column.to_sym
      end

      def save_to_persistence(new_state, options = {})
        send("#{machine.state_column}=".to_sym, new_state)
        save if options[:save]
      end

      def ensure_initial_state
        send("#{machine.state_column.to_s}=", current_state.name.to_s) if send(machine.state_column.to_s).blank?
      end
    end
  end
end

Fix ActiveRecord deprecation warning from old-school scope usage.
Fix ActiveRecord deprecation warning from old-school scope usage.

Removes annoying deprecation warnings from Rails output:

```
DEPRECATION WARNING: Using #scope without passing a callable object is deprecated. For example `scope :red, where(color: 'red')` should be changed to `scope :red, -> { where(color: 'red') }`. There are numerous gotchas in the former usage and it makes the implementation more complicated and buggy. (If you prefer, you can just define a class method named `self.red`.). (called from add_scope at /Users/jnh/.rvm/gems/ruby-2.0.0-p247/bundler/gems/stateflow-dbc47eed5c77/lib/stateflow/persistence/active_record.rb:12)
```
