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.
15 16 17 |
# File 'lib/statelogic/activerecord.rb', line 15 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
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/statelogic/activerecord.rb', line 30 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
19 20 21 22 23 24 25 26 |
# File 'lib/statelogic/activerecord.rb', line 19 def validates_transition_to(*states) attr = @config[:attribute] = states..update( :in => states, :if => [:"#{attr}_changed?", :"was_#{@state}?"] ) @class.validates_inclusion_of(attr, ) end |