Skip to content

Instantly share code, notes, and snippets.

@bloudermilk
Last active December 12, 2015 04:18
Show Gist options
  • Save bloudermilk/4713137 to your computer and use it in GitHub Desktop.
Save bloudermilk/4713137 to your computer and use it in GitHub Desktop.
PORVO: Plain old Ruby view objects (draft)
method PageHeaderHelper
  def page_header(title, subtitle = nil, &contents)
    PageHeader.new(self, title, subtitle, &contents)
  end

  class PageHeader
    attr_reader :context, :title, :subtitle

    def initialize(context, title, subtitle = nil)
      @context = context
      @title = title
      @subtitle = subtitle
    end

    def to_s
      context.content_tag(:div, class: "page-header") do
        context.content_tag(:h1) do
          context.h(title) + subtitle_html
        end
      end
    end

    def subtitle_html
      context.content_tag(:small, subtitle) if subtitle
    end
  end
end

Now, your view looks like this:

<%= page_header "Posts", "by Date" %>

And your test looks like this:

require "spec_helper"

describe PageHeaderHelper do
  describe "#page_header" do
    it "instantiates a PageHeader with the current view context and given arguments"
  end

  describe PageHeaderHelper::PageHeader do
    describe "#subtitle_html" do
      it "has some specific behaviour that is helpful to test independantly"
    end
  end
end
@bf4
Copy link

bf4 commented Feb 19, 2013

  • method --> module

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment