Revisions

gist: 168028 Download_button fork
public
Public Clone URL: git://gist.github.com/168028.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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