Sha256: 5e7bc1cedc28c403897429f1be21f523f5a3f6adc6527d23ce3ff0ea1b6af0f6
Contents?: true
Size: 1.26 KB
Versions: 1
Compression:
Stored size: 1.26 KB
Contents
module StatePattern module ActiveRecord 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 = @attributes[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 end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
state_pattern-2.0.1 | ./lib/state_pattern/active_record.rb |