Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module FollowEventTransitionMatcher
- class FollowEventTransition
- def initialize(event, options)
- @event = event
- @from = options[:from].is_a?(Array) ? options[:from] : [options[:from]]
- @to = options[:to]
- end
- def matches?(actual)
- actual.aasm_states.each do |state|
- o = build_type(actual, :state => state.name.to_sym)
- if @from.to_a.include? state.name.to_sym
- o.send(@event.to_sym)
- if !o.send("#{@to}?".to_sym)
- return false
- end
- else
- begin
- o.send(@event.to_sym)
- return false
- rescue AASM::InvalidTransition
- end
- end
- end
- end
- def build_type(klass, opts = {})
- Factory.build(klass.to_s.downcase.to_sym, opts)
- end
- def failure_message_for_should
- "Expected #{@event} to only allow transitions from #{@from} to #{@to}"
- end
- def failure_message_for_should_not
- "WARN: follow event transition shouldn't be used with the should_not syntax."
- end
- end
- def follow_event_transition(event, opts = {})
- FollowEventTransition.new(event, opts)
- end
- end
Add Comment
Please, Sign In to add comment