Skip to content

Commit

Permalink
Update page part section presenter to use page part slug as id
Browse files Browse the repository at this point in the history
Add migration to update all page parts slug and title

Fixes #3010
Resolves #3011
  • Loading branch information
Brice Sanchez authored and parndt committed Jul 23, 2015
1 parent b996a76 commit 3b3e59a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
Expand Up @@ -6,13 +6,7 @@ class PagePartSectionPresenter < SectionPresenter
def initialize(page_part)
super()
self.fallback_html = page_part.body.html_safe if page_part.body
self.id = convert_title_to_id(page_part.title) if page_part.title
end

private

def convert_title_to_id(title)
title.to_s.gsub(/\ /, '').underscore.to_sym
self.id = page_part.slug.to_sym if page_part.slug
end
end
end
Expand Down
@@ -0,0 +1,13 @@
class UpdateSlugAndTitleInRefineryPageParts < ActiveRecord::Migration
def change
begin
::Refinery::PagePart.all.each do |pp|
pp.title ||= pp.slug
pp.slug = pp.slug.downcase.gsub(" ", "_")
pp.save!
end
rescue NameError
warn "Refinery::PagePart was not defined!"
end
end
end
Expand Up @@ -3,8 +3,8 @@
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(:part) { double(PagePart, :body => 'part_body', :slug => 'a_wonderful_page_part', :title => 'A Wonderful Page Part') }
let(:part2) { double(PagePart, :body => 'part_body2', :slug => 'a_wonderful_page_part', :title => 'Another Wonderful Page Part') }
let(:title) { 'This Great Page' }

describe "when building for page" do
Expand Down
Expand Up @@ -4,29 +4,29 @@ module Refinery
module Pages
describe PagePartSectionPresenter do
it "can be built from a page part" do
part = double(PagePart, :body => 'part_body', :title => 'A Wonderful Page Part')
part = double(PagePart, :body => 'part_body', :slug => 'a_wonderful_page_part', :title => 'A Wonderful Page Part')
section = PagePartSectionPresenter.new(part)
expect(section.fallback_html).to eq('part_body')
expect(section.id).to eq(:a_wonderful_page_part)
end

it "marks the body as html safe" do
part = double(PagePart, :body => '<p>part_body</p>', :title => nil)
part = double(PagePart, :body => '<p>part_body</p>', :slug => nil, :title => nil)
section = PagePartSectionPresenter.new(part)
expect(section.fallback_html).to be_html_safe
expect(section.wrapped_html).to xml_eq(%q{<section><div class="inner"><p>part_body</p></div></section>})
end

it "handles a nil page body" do
part = double(PagePart, :body => nil, :title => nil)
part = double(PagePart, :body => nil, :slug => nil, :title => nil)
section = PagePartSectionPresenter.new(part)
expect(section.fallback_html).to be_nil
expect(section.wrapped_html).to be_nil
expect(section.has_content?).to be_falsey
end

it "has no id if title is nil" do
part = double(PagePart, :body => 'foobar', :title => nil)
part = double(PagePart, :body => 'foobar', :slug => nil, :title => nil)
section = PagePartSectionPresenter.new(part)
expect(section.id).to be_nil
end
Expand Down

0 comments on commit 3b3e59a

Please sign in to comment.