PageRenderTime 131ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/activesupport/test/new_callback_inheritance_test.rb

https://bitbucket.org/nicksieger/rails
Ruby | 115 lines | 91 code | 24 blank | 0 comment | 10 complexity | f9d92c838c5d2b084cfe494102fe1514 MD5 | raw file
  1. require 'test/unit'
  2. $:.unshift "#{File.dirname(__FILE__)}/../lib"
  3. require 'active_support'
  4. class GrandParent
  5. include ActiveSupport::NewCallbacks
  6. attr_reader :log, :action_name
  7. def initialize(action_name)
  8. @action_name, @log = action_name, []
  9. end
  10. define_callbacks :dispatch
  11. dispatch_callback :before, :before1, :before2, :per_key => {:if => proc {|c| c.action_name == "index" || c.action_name == "update" }}
  12. dispatch_callback :after, :after1, :after2, :per_key => {:if => proc {|c| c.action_name == "update" || c.action_name == "delete" }}
  13. def before1
  14. @log << "before1"
  15. end
  16. def before2
  17. @log << "before2"
  18. end
  19. def after1
  20. @log << "after1"
  21. end
  22. def after2
  23. @log << "after2"
  24. end
  25. def dispatch
  26. _run_dispatch_callbacks(action_name) do
  27. @log << action_name
  28. end
  29. self
  30. end
  31. end
  32. class Parent < GrandParent
  33. skip_dispatch_callback :before, :before2, :per_key => {:unless => proc {|c| c.action_name == "update" }}
  34. skip_dispatch_callback :after, :after2, :per_key => {:unless => proc {|c| c.action_name == "delete" }}
  35. end
  36. class Child < GrandParent
  37. skip_dispatch_callback :before, :before2, :per_key => {:unless => proc {|c| c.action_name == "update" }}, :if => :state_open?
  38. def state_open?
  39. @state == :open
  40. end
  41. def initialize(action_name, state)
  42. super(action_name)
  43. @state = state
  44. end
  45. end
  46. class BasicCallbacksTest < Test::Unit::TestCase
  47. def setup
  48. @index = GrandParent.new("index").dispatch
  49. @update = GrandParent.new("update").dispatch
  50. @delete = GrandParent.new("delete").dispatch
  51. @unknown = GrandParent.new("unknown").dispatch
  52. end
  53. def test_basic_per_key1
  54. assert_equal %w(before1 before2 index), @index.log
  55. end
  56. def test_basic_per_key2
  57. assert_equal %w(before1 before2 update after2 after1), @update.log
  58. end
  59. def test_basic_per_key3
  60. assert_equal %w(delete after2 after1), @delete.log
  61. end
  62. end
  63. class InheritedCallbacksTest < Test::Unit::TestCase
  64. def setup
  65. @index = Parent.new("index").dispatch
  66. @update = Parent.new("update").dispatch
  67. @delete = Parent.new("delete").dispatch
  68. @unknown = Parent.new("unknown").dispatch
  69. end
  70. def test_inherited_excluded
  71. assert_equal %w(before1 index), @index.log
  72. end
  73. def test_inherited_not_excluded
  74. assert_equal %w(before1 before2 update after1), @update.log
  75. end
  76. def test_partially_excluded
  77. assert_equal %w(delete after2 after1), @delete.log
  78. end
  79. end
  80. class InheritedCallbacksTest2 < Test::Unit::TestCase
  81. def setup
  82. @update1 = Child.new("update", :open).dispatch
  83. @update2 = Child.new("update", :closed).dispatch
  84. end
  85. def test_crazy_mix_on
  86. assert_equal %w(before1 update after2 after1), @update1.log
  87. end
  88. def test_crazy_mix_off
  89. assert_equal %w(before1 before2 update after2 after1), @update2.log
  90. end
  91. end