Guest User

Untitled

a guest
Jun 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. ## app/views/searches.show.html.erb
  2.  
  3. <% content_tag :ol, :class => 'results' do %>
  4. <% @search.results.each do |result| %>
  5. <%= content_tag :li, result.excerpt!.foo.bar %>
  6. <% end %>
  7. <% end %>
  8.  
  9. ## test/unit/search/excerption_chain_test.rb
  10. require 'test_helper'
  11.  
  12. class Search::ExcerptionChainTest < ActiveSupport::TestCase
  13. should("pass truthitest") { assert true }
  14.  
  15. class Foo
  16. attr_accessor :bar
  17. def initialize options
  18. self.bar = options.delete :bar
  19. end
  20. end
  21.  
  22. def setup
  23. @foo = Foo.new :bar => 'bar'
  24. @excerpter = stub()
  25.  
  26. @foo_chain = Search::ExcerptionChain.new @excerpter, @foo
  27. end
  28.  
  29. should("equal to the wrapped object") { assert_equal @foo_chain, @foo }
  30.  
  31. context "invoking sub-method #bar" do
  32. evaluate { @bar_chain = @foo_chain.bar }
  33.  
  34. should("use the same excerpter") { assert @foo_chain.excerpter.equal?(@bar_chain.excerpter) }
  35. should("use foo.bar as the object") { assert @foo.bar.equal?(@bar_chain.object) }
  36. end
  37.  
  38. should("run excerption and return the excerpted text when #to_s is invoked on the returned object", :before => proc do
  39. @excerpter.expects(:excerpts).with(@foo.bar).returns :excerpted_stuff
  40. end) do
  41. assert_equal :excerpted_stuff, @foo_chain.bar.to_s
  42. end
  43. end
  44.  
  45. ## app/models/search/excerption_chain.rb
  46.  
  47. class Search::ExcerptionChain
  48. instance_methods.each { |m| undef_method m unless m =~ /^__|^object_id$/ }
  49. attr_accessor :excerpter, :object
  50.  
  51. def initialize excerpter, object
  52. self.excerpter = excerpter
  53. self.object = object
  54. end
  55.  
  56. def method_missing method, *args
  57. Search::ExcerptionChain.new excerpter, object.send(method, *args)
  58. end
  59.  
  60. def to_s; excerpter.excerpts object.to_s end
  61. end
Add Comment
Please, Sign In to add comment