Class: Statelogic::ActiveRecord::ClassMethods::StateScopeHelper
- Inherits:
-
Object
- Object
- Statelogic::ActiveRecord::ClassMethods::StateScopeHelper
show all
- Defined in:
- lib/statelogic/activerecord.rb
Constant Summary
collapse
- MACROS_PATTERN =
/\Avalidates_/.freeze
Instance Method Summary
collapse
Constructor Details
#initialize(cl, state, config) ⇒ 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")
options = args.last
args.push(options = {}) unless options.is_a?(Hash)
options[:if] = Array(options[: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]
options = states..update(
:in => states,
:if => [:"#{attr}_changed?", :"was_#{@state}?"]
)
@class.validates_inclusion_of(attr, options)
end
|