Skip to content

Instantly share code, notes, and snippets.

@joshuaclayton
Created August 14, 2009 19:12
Show Gist options
  • Save joshuaclayton/168028 to your computer and use it in GitHub Desktop.
Save joshuaclayton/168028 to your computer and use it in GitHub Desktop.
class ListItemPresenter
delegate :id, :class, :errors, :to_param, :new_record?, :respond_to?, :is_a?,
:to => :@list_item
def initialize(options = {})
options.assert_valid_keys(:list_item, :customer, :source)
@list_item = options[:list_item]
@customer = options[:customer]
@source = parse_source(options[:source])
end
def available_templates
@customer.product_templates.by_customer_department(@list_item.product_list.customer_department)
end
def available_variants
return [] if @list_item.product_variant.blank?
@list_item.product_variant.product_template.variants
end
def product_template_id
return if @list_item.product_variant.blank?
@list_item.product_variant.product_template_id
end
def source(requested_source = nil)
requested_source ? parse_source(requested_source) : @source
end
def available_sources
[:template, :standard_product]
end
private
def method_missing(call, *args)
@list_item.send call, *args
end
def parse_source(requested_source = nil)
return self.available_sources.first if requested_source.blank?
if self.available_sources.include?(requested_source.to_sym)
requested_source.to_sym
else
self.available_sources.first
end
end
end
require 'test_helper'
class ListItemPresenterTest < ActiveSupport::TestCase
context "A list item presenter instance" do
setup do
@customer = Factory(:customer)
@product_list = Factory(:product_list, :customer => @customer)
@list_item = Factory(:list_item, :product_list => @product_list)
@list_item_presenter = ListItemPresenter.new(:list_item => @list_item, :customer => @customer)
end
should "know what templates are available to pick from" do
product_templates = stub
product_templates.expects(:by_customer_department).with(@product_list.customer_department).returns([1,2,3])
@customer.expects(:product_templates).returns(product_templates)
assert_equal [1,2,3], @list_item_presenter.available_templates
end
should "know product_template_id" do
@list_item.stubs(:product_variant).returns(nil)
assert_nil @list_item_presenter.product_template_id
product_variant = stub
product_variant.expects(:product_template_id).returns(6)
@list_item.stubs(:product_variant).returns(product_variant)
assert_equal 6, @list_item_presenter.product_template_id
end
should "know the available sources for the form" do
assert_equal [:template, :standard_product], @list_item_presenter.available_sources
end
context "calculating source" do
context "by parsing" do
setup do
# make parse_source public so we can test it cleanly
ListItemPresenter.send :public, :parse_source
@list_item_presenter.stubs(:available_sources).returns([:one, :two, :three])
end
should "return the first available source if no source is supplied" do
assert_equal :one, @list_item_presenter.parse_source
end
should "return a symbolized version of what's passed if present in available_sources" do
assert_equal :three, @list_item_presenter.parse_source("three")
end
should "return the first available source if source isn't included" do
assert_equal :one, @list_item_presenter.parse_source(123)
end
end
should "allow parsing of input when there is an argument to source" do
@list_item_presenter.expects(:parse_source).with("PASSED SOURCE").returns("PARSED")
assert_equal "PARSED", @list_item_presenter.source("PASSED SOURCE")
end
should "allow assignment of source during initialization" do
ListItemPresenter.any_instance.expects(:parse_source).with("MY SOURCE").returns("PARSED")
@list_item_presenter = ListItemPresenter.new(:list_item => @list_item, :customer => @customer, :source => "MY SOURCE")
assert_equal "PARSED", @list_item_presenter.source
end
end
should "use method_missing to pick up all other method calls" do
@list_item.expects(:another_method).returns("result")
assert_equal "result", @list_item_presenter.another_method
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment