Class: Statelogic::ActiveRecord::ClassMethods::StateScopeHelper
- Inherits:
-
Object
- Object
- Statelogic::ActiveRecord::ClassMethods::StateScopeHelper
- Defined in:
- lib/statelogic/activerecord.rb
Constant Summary collapse
- 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.
36 37 38 |
# File 'lib/statelogic/activerecord.rb', line 36 def initialize(cl, state, config) @class, @state, @config = cl, state, config end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/statelogic/activerecord.rb', line 51 def method_missing(method, *args, &block) if method.to_s =~ MACROS_PATTERN || @class.respond_to?("#{method}_callback_chain") = args.last args.push( = {}) unless .is_a?(Hash) [:if] = Array([:if]).unshift(:"#{@state}?") @class.send(method, *args, &block) else super end end |
Instance Method Details
#validates_transition_to(*states) ⇒ Object Also known as: transitions_to
40 41 42 43 44 45 46 47 |
# File 'lib/statelogic/activerecord.rb', line 40 def validates_transition_to(*states) attr = @config[:attribute] = states..update( :in => states, :if => [:"#{attr}_changed?", :"was_#{@state}?"] ) @class.validates_inclusion_of(attr, ) end |