Guest User

Untitled

a guest
Jul 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. # original idea is by @a_matsuda.
  2. # it { should filter(:update).by :require_session }
  3. # => should filter update by require_session
  4.  
  5. RSpec::Matchers.define :filter do |action_name|
  6. action_name = action_name.to_s
  7. match do
  8. callbacks = controller._process_action_callbacks.select { |c| c.kind == :before && @method_names.include?(c.filter.to_s)}
  9.  
  10. if callbacks.count != @method_names.count || callbacks.empty?
  11. false
  12. else
  13. callbacks.all? do |callback|
  14. callback.per_key[:if].all?{|c|eval(c)} && !callback.per_key[:unless].any?{|c|eval(c)}
  15. end
  16. end
  17. end
  18. def by(*method_names)
  19. @method_names = method_names.map(&:to_s)
  20. self
  21. end
  22. description do
  23. "filter #{action_name} by #{@method_names.join(', ')}"
  24. end
  25. failure_message_for_should do
  26. "not filter #{action_name} by #{@method_names.join(', ')}"
  27. end
  28. end
Add Comment
Please, Sign In to add comment