Class: Statelogic::ActiveRecord::ClassMethods::StateScopeHelper
- Inherits:
-
Object
- Object
- Statelogic::ActiveRecord::ClassMethods::StateScopeHelper
- Defined in:
- lib/statelogic/activerecord.rb
Constant Summary collapse
- CALLBACKS =
::ActiveRecord::Callbacks::CALLBACKS.map(&:to_sym).to_set.freeze
- MACROS_PATTERN =
/\Avalidates_/.freeze
Instance Method Summary collapse
-
#initialize(cl, state, config) ⇒ StateScopeHelper
constructor
A new instance of StateScopeHelper.
- #method_missing(method, *args, &block) ⇒ Object
- #validates_transition_to(*states) ⇒ Object (also: #transitions_to)
Constructor Details
#initialize(cl, state, config) ⇒ StateScopeHelper
Returns a new instance of StateScopeHelper.
16 17 18 |
# File 'lib/statelogic/activerecord.rb', line 16 def initialize(cl, state, config) @class, @state, @config, @conditions = cl, state, config, state.map {|x| :"#{x}?"} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/statelogic/activerecord.rb', line 31 def method_missing(method, *args, &block) if CALLBACKS.include?(method) || method.to_s =~ MACROS_PATTERN = args.last args.push( = {}) unless .is_a?(Hash) [:if] = Array([:if]).unshift(@conditions) @class.send(method, *args, &block) else super end end |
Instance Method Details
#validates_transition_to(*states) ⇒ Object Also known as: transitions_to
20 21 22 23 24 25 26 27 |
# File 'lib/statelogic/activerecord.rb', line 20 def validates_transition_to(*states) attr = @config[:attribute] = states..update( :in => states, :if => [:"#{attr}_changed?", :"was_#{@state}?"] ) @class.validates_inclusion_of(attr, ) end |