Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## app/views/searches.show.html.erb
- <% content_tag :ol, :class => 'results' do %>
- <% @search.results.each do |result| %>
- <%= content_tag :li, result.excerpt!.foo.bar %>
- <% end %>
- <% end %>
- ## test/unit/search/excerption_chain_test.rb
- require 'test_helper'
- class Search::ExcerptionChainTest < ActiveSupport::TestCase
- should("pass truthitest") { assert true }
- class Foo
- attr_accessor :bar
- def initialize options
- self.bar = options.delete :bar
- end
- end
- def setup
- @foo = Foo.new :bar => 'bar'
- @excerpter = stub()
- @foo_chain = Search::ExcerptionChain.new @excerpter, @foo
- end
- should("equal to the wrapped object") { assert_equal @foo_chain, @foo }
- context "invoking sub-method #bar" do
- evaluate { @bar_chain = @foo_chain.bar }
- should("use the same excerpter") { assert @foo_chain.excerpter.equal?(@bar_chain.excerpter) }
- should("use foo.bar as the object") { assert @foo.bar.equal?(@bar_chain.object) }
- end
- should("run excerption and return the excerpted text when #to_s is invoked on the returned object", :before => proc do
- @excerpter.expects(:excerpts).with(@foo.bar).returns :excerpted_stuff
- end) do
- assert_equal :excerpted_stuff, @foo_chain.bar.to_s
- end
- end
- ## app/models/search/excerption_chain.rb
- class Search::ExcerptionChain
- instance_methods.each { |m| undef_method m unless m =~ /^__|^object_id$/ }
- attr_accessor :excerpter, :object
- def initialize excerpter, object
- self.excerpter = excerpter
- self.object = object
- end
- def method_missing method, *args
- Search::ExcerptionChain.new excerpter, object.send(method, *args)
- end
- def to_s; excerpter.excerpts object.to_s end
- end
Add Comment
Please, Sign In to add comment