Sha256: bb37b79b069a4e804c0604905a86cd7ef8e3c8b29528b4a16c8cbb31ffaa94a5

Contents?: true

Size: 1.42 KB

Versions: 22

Compression:

Stored size: 1.42 KB

Contents

require 'helper'

require 'neo/rails/presenter'

class PresenterTest < NeoRailsCase
  User = Struct.new(:name, :team)
  Team = Struct.new(:name)

  class BasePresenter
    include Neo::Rails::Presenter
  end

  class UserPresenter < BasePresenter
    def initialize(user)
      @user = user
    end

    def name
      @user.name
    end

    def team
      @team ||= TeamPresenter.new(@user.team)
    end

    def link
      view_context.link_to "/user/#{name}", name
    end
  end

  class TeamPresenter < BasePresenter
    def initialize(team)
      @team = team
    end

    def name
      @team.name
    end

    def link
      view_context.link_to "/team/#{name}", name
    end
  end

  class ViewContext
    def link_to(path, name)
      %{<a href="#{path}">#{name}</a>}
    end
  end

  before do
    Neo::Rails::Presenter.view_context = ViewContext.new
  end

  let(:team) { Team.new("A-Team") }
  let(:user) { User.new("Mr T.", team) }

  context "with user_presenter" do
    let(:user_presenter) { UserPresenter.new(user) }

    test "creates a presenter providing current view_context" do
      assert_instance_of ViewContext, user_presenter.view_context
    end

    test "shares view_context with sub presenters" do
      assert_same user_presenter.view_context, user_presenter.team.view_context
    end

    test "uses view_context's methods" do
      assert user_presenter.link
      assert user_presenter.team.link
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
neo-rails-0.4.1 test/presenter_test.rb
neo-rails-0.4.0 test/presenter_test.rb
neo-rails-0.3.2 test/presenter_test.rb
neo-rails-0.3.1 test/presenter_test.rb
neo-rails-0.3 test/presenter_test.rb
neo-rails-0.2.3.1 test/presenter_test.rb
neo-rails-0.2.3 test/presenter_test.rb
neo-rails-0.2.2 test/presenter_test.rb
neo-rails-0.2.1 test/presenter_test.rb
neo-rails-0.2.0 test/presenter_test.rb
neo-rails-0.1.3 test/presenter_test.rb
neo-rails-0.1.2 test/presenter_test.rb
neo-rails-0.1.1 test/presenter_test.rb
neo-rails-0.1.0 test/presenter_test.rb
neo-rails-0.0.8 test/presenter_test.rb
neo-rails-0.0.7 test/presenter_test.rb
neo-rails-0.0.6 test/presenter_test.rb
neo-rails-0.0.5 test/presenter_test.rb
neo-rails-0.0.4 test/presenter_test.rb
neo-rails-0.0.3 test/presenter_test.rb