module StatePattern::ActiveRecord
Public Class Methods
after_initialize()
click to toggle source
# File lib/state_pattern/active_record.rb, line 14 def self.after_initialize; end
included(base)
click to toggle source
# File lib/state_pattern/active_record.rb, line 3 def self.included(base) base.class_eval do include StatePattern after_initialize :set_state_from_db def set_state_from_db set_state(state_string_as_class || self.class.initial_state_class) end #enable after_initialize callback if ::ActiveRecord::VERSION::STRING >= "3.0.0" def self.after_initialize; end else def after_initialize; end end def set_state_with_active_record_attribute(state_class) set_state_without_active_record_attribute(state_class) write_attribute(self.class.state_attribute, @current_state_instance.class.name) end alias_method_chain :set_state, :active_record_attribute def state=(new_state_string) set_state(state_string_as_class(new_state_string)) end def state_string_as_class(state_string = read_attribute(self.class.state_attribute)) state_string.camelize.constantize unless state_string.nil? end def self.state_attribute @state_attribute ||= "state" end def self.set_state_attribute(state_attr) @state_attribute = state_attr.to_s end end end
set_state_attribute(state_attr)
click to toggle source
# File lib/state_pattern/active_record.rb, line 37 def self.set_state_attribute(state_attr) @state_attribute = state_attr.to_s end
state_attribute()
click to toggle source
# File lib/state_pattern/active_record.rb, line 33 def self.state_attribute @state_attribute ||= "state" end
Public Instance Methods
after_initialize()
click to toggle source
# File lib/state_pattern/active_record.rb, line 16 def after_initialize; end
set_state_from_db()
click to toggle source
# File lib/state_pattern/active_record.rb, line 8 def set_state_from_db set_state(state_string_as_class || self.class.initial_state_class) end
set_state_with_active_record_attribute(state_class)
click to toggle source
# File lib/state_pattern/active_record.rb, line 19 def set_state_with_active_record_attribute(state_class) set_state_without_active_record_attribute(state_class) write_attribute(self.class.state_attribute, @current_state_instance.class.name) end
state=(new_state_string)
click to toggle source
# File lib/state_pattern/active_record.rb, line 25 def state=(new_state_string) set_state(state_string_as_class(new_state_string)) end
state_string_as_class(state_string = read_attribute(self.class.state_attribute))
click to toggle source
# File lib/state_pattern/active_record.rb, line 29 def state_string_as_class(state_string = read_attribute(self.class.state_attribute)) state_string.camelize.constantize unless state_string.nil? end