Skip to content

Commit

Permalink
Split ContentPage into ContentPagePresenter and ContentPresenter
Browse files Browse the repository at this point in the history
  • Loading branch information
jadehopepunk committed Feb 22, 2012
1 parent 6643c6d commit 498d45e
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 70 deletions.
6 changes: 3 additions & 3 deletions core/app/views/refinery/_content_page.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
allowed_to_use_fallback = !local_assigns[:show_empty_sections] && !local_assigns[:remove_automatic_sections]

if content_page
if !content_page.is_a?(Refinery::Pages::ContentPage)
raise ArgumentError.new("Sections passed to the content page partial must be Refinery::Pages::SectionPresenter objects inside a Refinery::Pages::ContentPage")
if !content_page.is_a?(Refinery::Pages::ContentPagePresenter)
raise ArgumentError.new("Sections passed to the content page partial must be Refinery::Pages::SectionPresenter objects inside a Refinery::Pages::ContentPagePresenter")
end
else
content_page = Refinery::Pages::ContentPage.build_for_page(@page, page_title)
content_page = Refinery::Pages::ContentPagePresenter.build_for_page(@page, page_title)
end

content_page.hide_sections(sections_to_hide)
Expand Down
33 changes: 33 additions & 0 deletions pages/lib/refinery/pages/content_page_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Refinery
module Pages
class ContentPagePresenter < ContentPresenter
def self.build_for_page(page, page_title)
self.new(page, page_title)
end

def initialize(page, page_title)
super()
add_default_title_section(page_title) if page_title.present?
add_page_parts(page.parts) if page
add_default_post_page_sections
end

private

def add_default_title_section(title)
add_section SectionPresenter.new(:id => :body_content_title, :fallback_html => title, :title => true)
end

def add_default_post_page_sections
add_section_if_missing(:id => :body_content_left)
add_section_if_missing(:id => :body_content_right)
end

def add_page_parts(parts)
parts.each do |part|
add_section SectionPresenter.from_page_part(part)
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
module Refinery
module Pages
class ContentPage
def self.build_for_page(page, page_title)
sections = self.new
sections.add_default_title_section(page_title) if page_title.present?
sections.add_page_parts(page.parts) if page
sections.add_default_post_page_sections
sections
end

class ContentPresenter
def initialize(initial_sections = [])
@sections = initial_sections
end
Expand All @@ -17,25 +9,10 @@ def blank_section_css_classes(allowed_to_use_fallback)
@sections.reject {|section| section.has_content?(allowed_to_use_fallback)}.map(&:not_present_css_class)
end

def add_default_title_section(title)
add_section SectionPresenter.new(:id => :body_content_title, :fallback_html => title, :title => true)
end

def add_default_post_page_sections
add_section_if_missing(:id => :body_content_left)
add_section_if_missing(:id => :body_content_right)
end

def hide_sections(ids_to_hide)
@sections.select {|section| ids_to_hide.include?(section.id)}.each(&:hide)
end

def add_page_parts(parts)
parts.each do |part|
add_section SectionPresenter.from_page_part(part)
end
end

def fetch_template_overrides
@sections.each do |section|
if section.id.present?
Expand Down
43 changes: 43 additions & 0 deletions pages/spec/lib/pages/content_page_presenter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "spec_helper"

module Refinery
module Pages
describe ContentPagePresenter do
let(:part) { double(PagePart, :body => 'part_body', :title => 'A Wonderful Page Part') }
let(:part2) { double(PagePart, :body => 'part_body2', :title => 'Another Wonderful Page Part') }
let(:title) { 'This Great Page' }

describe "when building for page" do
let(:page_with_one_part) { double(Page, :parts => [part]) }

it "adds page title section before page parts" do
content = ContentPagePresenter.build_for_page(page_with_one_part, title)
content.get_section(0).fallback_html.should == title
end

it "adds a section for each page part" do
page = double(Page, :parts => [part, part2])
content = ContentPagePresenter.build_for_page(page, title)
content.get_section(1).fallback_html.should == 'part_body'
content.get_section(2).fallback_html.should == 'part_body2'
end

it "adds body content left and right after page parts" do
content = ContentPagePresenter.build_for_page(page_with_one_part, title)
content.get_section(2).id.should == :body_content_left
content.get_section(3).id.should == :body_content_right
end

it "doesnt add page parts if page is nil" do
content = ContentPagePresenter.build_for_page(nil, title)
content.get_section(1).id.should == :body_content_left
end

it "doesnt add title if it is blank" do
content = ContentPagePresenter.build_for_page(nil, '')
content.get_section(0).id.should == :body_content_left
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,24 @@

module Refinery
module Pages
describe ContentPage do
let(:part) { double(PagePart, :body => 'part_body', :title => 'A Wonderful Page Part') }
let(:part2) { double(PagePart, :body => 'part_body2', :title => 'Another Wonderful Page Part') }
let(:title) { 'This Great Page' }
describe ContentPresenter do
let(:section1) { double(SectionPresenter, :id => 'foo') }
let(:section2) { double(SectionPresenter, :id => 'bar') }

describe "when building for page" do
let(:page_with_one_part) { double(Page, :parts => [part]) }

it "adds page title section before page parts" do
content = ContentPage.build_for_page(page_with_one_part, title)
content.get_section(0).fallback_html.should == title
end

it "adds a section for each page part" do
page = double(Page, :parts => [part, part2])
content = ContentPage.build_for_page(page, title)
content.get_section(1).fallback_html.should == 'part_body'
content.get_section(2).fallback_html.should == 'part_body2'
end

it "adds body content left and right after page parts" do
content = ContentPage.build_for_page(page_with_one_part, title)
content.get_section(2).id.should == :body_content_left
content.get_section(3).id.should == :body_content_right
end

it "doesnt add page parts if page is nil" do
content = ContentPage.build_for_page(nil, title)
content.get_section(1).id.should == :body_content_left
end

it "doesnt add title if it is blank" do
content = ContentPage.build_for_page(nil, '')
content.get_section(0).id.should == :body_content_left
end
end

describe "when building css classes for blank sections" do
let(:section) { double(SectionPresenter, :not_present_css_class => 'no_section1') }

it "includes css class for any section which doesnt have content" do
section.stub(:has_content?).with(true).and_return(false)
content = ContentPage.new
content = ContentPresenter.new
content.add_section section

content.blank_section_css_classes(true).should == ['no_section1']
end

it "doesnt include sections which have content" do
section.stub(:has_content?).with(true).and_return(true)
content = ContentPage.new
content = ContentPresenter.new
content.add_section section

content.blank_section_css_classes(true).should be_empty
Expand All @@ -63,7 +28,7 @@ module Pages

describe "when hiding sections" do
before do
@content = ContentPage.new
@content = ContentPresenter.new
@content.add_section section1
@content.add_section section2
end
Expand All @@ -76,7 +41,7 @@ module Pages

describe "when fetching template overrides" do
before do
@content = ContentPage.new
@content = ContentPresenter.new
end

it "yields a section with an id and stores the result in its override html" do
Expand All @@ -103,20 +68,20 @@ module Pages

describe "when rendering as html" do
it "is blank if it has no sections" do
content = ContentPage.new
content = ContentPresenter.new
content.to_html.should be_blank
end

it "returns sections joined by a newline" do
section1.stub(:wrapped_html).and_return('foo')
section2.stub(:wrapped_html).and_return('bar')
content = ContentPage.new([section1, section2])
content = ContentPresenter.new([section1, section2])
content.to_html.should == "foo\nbar"
end

it "passes allowed_to_use_fallback option on to sections" do
section1.should_receive(:wrapped_html).with(false).and_return('foo')
content = ContentPage.new([section1])
content = ContentPresenter.new([section1])
content.to_html(false)
end
end
Expand Down

0 comments on commit 498d45e

Please sign in to comment.